nv84_fence.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "nouveau_drv.h"
  25. #include "nouveau_dma.h"
  26. #include "nouveau_fence.h"
  27. #include "nv50_display.h"
  28. static int
  29. nv84_fence_emit32(struct nouveau_channel *chan, u64 virtual, u32 sequence)
  30. {
  31. int ret = RING_SPACE(chan, 8);
  32. if (ret == 0) {
  33. BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
  34. OUT_RING (chan, chan->vram.handle);
  35. BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 5);
  36. OUT_RING (chan, upper_32_bits(virtual));
  37. OUT_RING (chan, lower_32_bits(virtual));
  38. OUT_RING (chan, sequence);
  39. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
  40. OUT_RING (chan, 0x00000000);
  41. FIRE_RING (chan);
  42. }
  43. return ret;
  44. }
  45. static int
  46. nv84_fence_sync32(struct nouveau_channel *chan, u64 virtual, u32 sequence)
  47. {
  48. int ret = RING_SPACE(chan, 7);
  49. if (ret == 0) {
  50. BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
  51. OUT_RING (chan, chan->vram.handle);
  52. BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  53. OUT_RING (chan, upper_32_bits(virtual));
  54. OUT_RING (chan, lower_32_bits(virtual));
  55. OUT_RING (chan, sequence);
  56. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_GEQUAL);
  57. FIRE_RING (chan);
  58. }
  59. return ret;
  60. }
  61. static int
  62. nv84_fence_emit(struct nouveau_fence *fence)
  63. {
  64. struct nouveau_channel *chan = fence->channel;
  65. struct nv84_fence_chan *fctx = chan->fence;
  66. u64 addr = chan->chid * 16;
  67. if (fence->sysmem)
  68. addr += fctx->vma_gart.offset;
  69. else
  70. addr += fctx->vma.offset;
  71. return fctx->base.emit32(chan, addr, fence->base.seqno);
  72. }
  73. static int
  74. nv84_fence_sync(struct nouveau_fence *fence,
  75. struct nouveau_channel *prev, struct nouveau_channel *chan)
  76. {
  77. struct nv84_fence_chan *fctx = chan->fence;
  78. u64 addr = prev->chid * 16;
  79. if (fence->sysmem)
  80. addr += fctx->vma_gart.offset;
  81. else
  82. addr += fctx->vma.offset;
  83. return fctx->base.sync32(chan, addr, fence->base.seqno);
  84. }
  85. static u32
  86. nv84_fence_read(struct nouveau_channel *chan)
  87. {
  88. struct nv84_fence_priv *priv = chan->drm->fence;
  89. return nouveau_bo_rd32(priv->bo, chan->chid * 16/4);
  90. }
  91. static void
  92. nv84_fence_context_del(struct nouveau_channel *chan)
  93. {
  94. struct nv84_fence_priv *priv = chan->drm->fence;
  95. struct nv84_fence_chan *fctx = chan->fence;
  96. nouveau_bo_wr32(priv->bo, chan->chid * 16 / 4, fctx->base.sequence);
  97. mutex_lock(&priv->mutex);
  98. nouveau_bo_vma_del(priv->bo, &fctx->vma_gart);
  99. nouveau_bo_vma_del(priv->bo, &fctx->vma);
  100. mutex_unlock(&priv->mutex);
  101. nouveau_fence_context_del(&fctx->base);
  102. chan->fence = NULL;
  103. nouveau_fence_context_free(&fctx->base);
  104. }
  105. int
  106. nv84_fence_context_new(struct nouveau_channel *chan)
  107. {
  108. struct nouveau_cli *cli = (void *)chan->user.client;
  109. struct nv84_fence_priv *priv = chan->drm->fence;
  110. struct nv84_fence_chan *fctx;
  111. int ret;
  112. fctx = chan->fence = kzalloc(sizeof(*fctx), GFP_KERNEL);
  113. if (!fctx)
  114. return -ENOMEM;
  115. nouveau_fence_context_new(chan, &fctx->base);
  116. fctx->base.emit = nv84_fence_emit;
  117. fctx->base.sync = nv84_fence_sync;
  118. fctx->base.read = nv84_fence_read;
  119. fctx->base.emit32 = nv84_fence_emit32;
  120. fctx->base.sync32 = nv84_fence_sync32;
  121. fctx->base.sequence = nv84_fence_read(chan);
  122. mutex_lock(&priv->mutex);
  123. ret = nouveau_bo_vma_add(priv->bo, cli->vm, &fctx->vma);
  124. if (ret == 0) {
  125. ret = nouveau_bo_vma_add(priv->bo_gart, cli->vm,
  126. &fctx->vma_gart);
  127. }
  128. mutex_unlock(&priv->mutex);
  129. if (ret)
  130. nv84_fence_context_del(chan);
  131. return ret;
  132. }
  133. static bool
  134. nv84_fence_suspend(struct nouveau_drm *drm)
  135. {
  136. struct nv84_fence_priv *priv = drm->fence;
  137. int i;
  138. priv->suspend = vmalloc(priv->base.contexts * sizeof(u32));
  139. if (priv->suspend) {
  140. for (i = 0; i < priv->base.contexts; i++)
  141. priv->suspend[i] = nouveau_bo_rd32(priv->bo, i*4);
  142. }
  143. return priv->suspend != NULL;
  144. }
  145. static void
  146. nv84_fence_resume(struct nouveau_drm *drm)
  147. {
  148. struct nv84_fence_priv *priv = drm->fence;
  149. int i;
  150. if (priv->suspend) {
  151. for (i = 0; i < priv->base.contexts; i++)
  152. nouveau_bo_wr32(priv->bo, i*4, priv->suspend[i]);
  153. vfree(priv->suspend);
  154. priv->suspend = NULL;
  155. }
  156. }
  157. static void
  158. nv84_fence_destroy(struct nouveau_drm *drm)
  159. {
  160. struct nv84_fence_priv *priv = drm->fence;
  161. nouveau_bo_unmap(priv->bo_gart);
  162. if (priv->bo_gart)
  163. nouveau_bo_unpin(priv->bo_gart);
  164. nouveau_bo_ref(NULL, &priv->bo_gart);
  165. nouveau_bo_unmap(priv->bo);
  166. if (priv->bo)
  167. nouveau_bo_unpin(priv->bo);
  168. nouveau_bo_ref(NULL, &priv->bo);
  169. drm->fence = NULL;
  170. kfree(priv);
  171. }
  172. int
  173. nv84_fence_create(struct nouveau_drm *drm)
  174. {
  175. struct nvkm_fifo *fifo = nvxx_fifo(&drm->client.device);
  176. struct nv84_fence_priv *priv;
  177. u32 domain;
  178. int ret;
  179. priv = drm->fence = kzalloc(sizeof(*priv), GFP_KERNEL);
  180. if (!priv)
  181. return -ENOMEM;
  182. priv->base.dtor = nv84_fence_destroy;
  183. priv->base.suspend = nv84_fence_suspend;
  184. priv->base.resume = nv84_fence_resume;
  185. priv->base.context_new = nv84_fence_context_new;
  186. priv->base.context_del = nv84_fence_context_del;
  187. priv->base.contexts = fifo->nr;
  188. priv->base.context_base = dma_fence_context_alloc(priv->base.contexts);
  189. priv->base.uevent = true;
  190. mutex_init(&priv->mutex);
  191. /* Use VRAM if there is any ; otherwise fallback to system memory */
  192. domain = drm->client.device.info.ram_size != 0 ? TTM_PL_FLAG_VRAM :
  193. /*
  194. * fences created in sysmem must be non-cached or we
  195. * will lose CPU/GPU coherency!
  196. */
  197. TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED;
  198. ret = nouveau_bo_new(&drm->client, 16 * priv->base.contexts, 0,
  199. domain, 0, 0, NULL, NULL, &priv->bo);
  200. if (ret == 0) {
  201. ret = nouveau_bo_pin(priv->bo, domain, false);
  202. if (ret == 0) {
  203. ret = nouveau_bo_map(priv->bo);
  204. if (ret)
  205. nouveau_bo_unpin(priv->bo);
  206. }
  207. if (ret)
  208. nouveau_bo_ref(NULL, &priv->bo);
  209. }
  210. if (ret == 0)
  211. ret = nouveau_bo_new(&drm->client, 16 * priv->base.contexts, 0,
  212. TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED, 0,
  213. 0, NULL, NULL, &priv->bo_gart);
  214. if (ret == 0) {
  215. ret = nouveau_bo_pin(priv->bo_gart, TTM_PL_FLAG_TT, false);
  216. if (ret == 0) {
  217. ret = nouveau_bo_map(priv->bo_gart);
  218. if (ret)
  219. nouveau_bo_unpin(priv->bo_gart);
  220. }
  221. if (ret)
  222. nouveau_bo_ref(NULL, &priv->bo_gart);
  223. }
  224. if (ret)
  225. nv84_fence_destroy(drm);
  226. return ret;
  227. }