nv84_fence.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. nouveau_bo_vma_del(priv->bo, &fctx->vma_gart);
  98. nouveau_bo_vma_del(priv->bo, &fctx->vma);
  99. nouveau_fence_context_del(&fctx->base);
  100. chan->fence = NULL;
  101. nouveau_fence_context_free(&fctx->base);
  102. }
  103. int
  104. nv84_fence_context_new(struct nouveau_channel *chan)
  105. {
  106. struct nouveau_cli *cli = (void *)chan->user.client;
  107. struct nv84_fence_priv *priv = chan->drm->fence;
  108. struct nv84_fence_chan *fctx;
  109. int ret;
  110. fctx = chan->fence = kzalloc(sizeof(*fctx), GFP_KERNEL);
  111. if (!fctx)
  112. return -ENOMEM;
  113. nouveau_fence_context_new(chan, &fctx->base);
  114. fctx->base.emit = nv84_fence_emit;
  115. fctx->base.sync = nv84_fence_sync;
  116. fctx->base.read = nv84_fence_read;
  117. fctx->base.emit32 = nv84_fence_emit32;
  118. fctx->base.sync32 = nv84_fence_sync32;
  119. fctx->base.sequence = nv84_fence_read(chan);
  120. ret = nouveau_bo_vma_add(priv->bo, cli->vm, &fctx->vma);
  121. if (ret == 0) {
  122. ret = nouveau_bo_vma_add(priv->bo_gart, cli->vm,
  123. &fctx->vma_gart);
  124. }
  125. if (ret)
  126. nv84_fence_context_del(chan);
  127. return ret;
  128. }
  129. static bool
  130. nv84_fence_suspend(struct nouveau_drm *drm)
  131. {
  132. struct nv84_fence_priv *priv = drm->fence;
  133. int i;
  134. priv->suspend = vmalloc(priv->base.contexts * sizeof(u32));
  135. if (priv->suspend) {
  136. for (i = 0; i < priv->base.contexts; i++)
  137. priv->suspend[i] = nouveau_bo_rd32(priv->bo, i*4);
  138. }
  139. return priv->suspend != NULL;
  140. }
  141. static void
  142. nv84_fence_resume(struct nouveau_drm *drm)
  143. {
  144. struct nv84_fence_priv *priv = drm->fence;
  145. int i;
  146. if (priv->suspend) {
  147. for (i = 0; i < priv->base.contexts; i++)
  148. nouveau_bo_wr32(priv->bo, i*4, priv->suspend[i]);
  149. vfree(priv->suspend);
  150. priv->suspend = NULL;
  151. }
  152. }
  153. static void
  154. nv84_fence_destroy(struct nouveau_drm *drm)
  155. {
  156. struct nv84_fence_priv *priv = drm->fence;
  157. nouveau_bo_unmap(priv->bo_gart);
  158. if (priv->bo_gart)
  159. nouveau_bo_unpin(priv->bo_gart);
  160. nouveau_bo_ref(NULL, &priv->bo_gart);
  161. nouveau_bo_unmap(priv->bo);
  162. if (priv->bo)
  163. nouveau_bo_unpin(priv->bo);
  164. nouveau_bo_ref(NULL, &priv->bo);
  165. drm->fence = NULL;
  166. kfree(priv);
  167. }
  168. int
  169. nv84_fence_create(struct nouveau_drm *drm)
  170. {
  171. struct nvkm_fifo *fifo = nvxx_fifo(&drm->device);
  172. struct nv84_fence_priv *priv;
  173. u32 domain;
  174. int ret;
  175. priv = drm->fence = kzalloc(sizeof(*priv), GFP_KERNEL);
  176. if (!priv)
  177. return -ENOMEM;
  178. priv->base.dtor = nv84_fence_destroy;
  179. priv->base.suspend = nv84_fence_suspend;
  180. priv->base.resume = nv84_fence_resume;
  181. priv->base.context_new = nv84_fence_context_new;
  182. priv->base.context_del = nv84_fence_context_del;
  183. priv->base.contexts = fifo->nr;
  184. priv->base.context_base = dma_fence_context_alloc(priv->base.contexts);
  185. priv->base.uevent = true;
  186. /* Use VRAM if there is any ; otherwise fallback to system memory */
  187. domain = drm->device.info.ram_size != 0 ? TTM_PL_FLAG_VRAM :
  188. /*
  189. * fences created in sysmem must be non-cached or we
  190. * will lose CPU/GPU coherency!
  191. */
  192. TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED;
  193. ret = nouveau_bo_new(drm->dev, 16 * priv->base.contexts, 0, domain, 0,
  194. 0, NULL, NULL, &priv->bo);
  195. if (ret == 0) {
  196. ret = nouveau_bo_pin(priv->bo, domain, false);
  197. if (ret == 0) {
  198. ret = nouveau_bo_map(priv->bo);
  199. if (ret)
  200. nouveau_bo_unpin(priv->bo);
  201. }
  202. if (ret)
  203. nouveau_bo_ref(NULL, &priv->bo);
  204. }
  205. if (ret == 0)
  206. ret = nouveau_bo_new(drm->dev, 16 * priv->base.contexts, 0,
  207. TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED, 0,
  208. 0, NULL, NULL, &priv->bo_gart);
  209. if (ret == 0) {
  210. ret = nouveau_bo_pin(priv->bo_gart, TTM_PL_FLAG_TT, false);
  211. if (ret == 0) {
  212. ret = nouveau_bo_map(priv->bo_gart);
  213. if (ret)
  214. nouveau_bo_unpin(priv->bo_gart);
  215. }
  216. if (ret)
  217. nouveau_bo_ref(NULL, &priv->bo_gart);
  218. }
  219. if (ret)
  220. nv84_fence_destroy(drm);
  221. return ret;
  222. }