nv84_fence.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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_drm.h"
  25. #include "nouveau_dma.h"
  26. #include "nouveau_fence.h"
  27. #include "nv50_display.h"
  28. u64
  29. nv84_fence_crtc(struct nouveau_channel *chan, int crtc)
  30. {
  31. struct nv84_fence_chan *fctx = chan->fence;
  32. return fctx->dispc_vma[crtc].offset;
  33. }
  34. static int
  35. nv84_fence_emit32(struct nouveau_channel *chan, u64 virtual, u32 sequence)
  36. {
  37. int ret = RING_SPACE(chan, 8);
  38. if (ret == 0) {
  39. BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
  40. OUT_RING (chan, chan->vram.handle);
  41. BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 5);
  42. OUT_RING (chan, upper_32_bits(virtual));
  43. OUT_RING (chan, lower_32_bits(virtual));
  44. OUT_RING (chan, sequence);
  45. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
  46. OUT_RING (chan, 0x00000000);
  47. FIRE_RING (chan);
  48. }
  49. return ret;
  50. }
  51. static int
  52. nv84_fence_sync32(struct nouveau_channel *chan, u64 virtual, u32 sequence)
  53. {
  54. int ret = RING_SPACE(chan, 7);
  55. if (ret == 0) {
  56. BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
  57. OUT_RING (chan, chan->vram.handle);
  58. BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  59. OUT_RING (chan, upper_32_bits(virtual));
  60. OUT_RING (chan, lower_32_bits(virtual));
  61. OUT_RING (chan, sequence);
  62. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_GEQUAL);
  63. FIRE_RING (chan);
  64. }
  65. return ret;
  66. }
  67. static int
  68. nv84_fence_emit(struct nouveau_fence *fence)
  69. {
  70. struct nouveau_channel *chan = fence->channel;
  71. struct nv84_fence_chan *fctx = chan->fence;
  72. u64 addr = chan->chid * 16;
  73. if (fence->sysmem)
  74. addr += fctx->vma_gart.offset;
  75. else
  76. addr += fctx->vma.offset;
  77. return fctx->base.emit32(chan, addr, fence->base.seqno);
  78. }
  79. static int
  80. nv84_fence_sync(struct nouveau_fence *fence,
  81. struct nouveau_channel *prev, struct nouveau_channel *chan)
  82. {
  83. struct nv84_fence_chan *fctx = chan->fence;
  84. u64 addr = prev->chid * 16;
  85. if (fence->sysmem)
  86. addr += fctx->vma_gart.offset;
  87. else
  88. addr += fctx->vma.offset;
  89. return fctx->base.sync32(chan, addr, fence->base.seqno);
  90. }
  91. static u32
  92. nv84_fence_read(struct nouveau_channel *chan)
  93. {
  94. struct nv84_fence_priv *priv = chan->drm->fence;
  95. return nouveau_bo_rd32(priv->bo, chan->chid * 16/4);
  96. }
  97. static void
  98. nv84_fence_context_del(struct nouveau_channel *chan)
  99. {
  100. struct drm_device *dev = chan->drm->dev;
  101. struct nv84_fence_priv *priv = chan->drm->fence;
  102. struct nv84_fence_chan *fctx = chan->fence;
  103. int i;
  104. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  105. struct nouveau_bo *bo = nv50_display_crtc_sema(dev, i);
  106. nouveau_bo_vma_del(bo, &fctx->dispc_vma[i]);
  107. }
  108. nouveau_bo_wr32(priv->bo, chan->chid * 16 / 4, fctx->base.sequence);
  109. mutex_lock(&priv->mutex);
  110. nouveau_bo_vma_del(priv->bo, &fctx->vma_gart);
  111. nouveau_bo_vma_del(priv->bo, &fctx->vma);
  112. mutex_unlock(&priv->mutex);
  113. nouveau_fence_context_del(&fctx->base);
  114. chan->fence = NULL;
  115. nouveau_fence_context_free(&fctx->base);
  116. }
  117. int
  118. nv84_fence_context_new(struct nouveau_channel *chan)
  119. {
  120. struct nouveau_cli *cli = (void *)chan->user.client;
  121. struct nv84_fence_priv *priv = chan->drm->fence;
  122. struct nv84_fence_chan *fctx;
  123. int ret, i;
  124. fctx = chan->fence = kzalloc(sizeof(*fctx), GFP_KERNEL);
  125. if (!fctx)
  126. return -ENOMEM;
  127. nouveau_fence_context_new(chan, &fctx->base);
  128. fctx->base.emit = nv84_fence_emit;
  129. fctx->base.sync = nv84_fence_sync;
  130. fctx->base.read = nv84_fence_read;
  131. fctx->base.emit32 = nv84_fence_emit32;
  132. fctx->base.sync32 = nv84_fence_sync32;
  133. fctx->base.sequence = nv84_fence_read(chan);
  134. mutex_lock(&priv->mutex);
  135. ret = nouveau_bo_vma_add(priv->bo, cli->vm, &fctx->vma);
  136. if (ret == 0) {
  137. ret = nouveau_bo_vma_add(priv->bo_gart, cli->vm,
  138. &fctx->vma_gart);
  139. }
  140. mutex_unlock(&priv->mutex);
  141. /* map display semaphore buffers into channel's vm */
  142. for (i = 0; !ret && i < chan->drm->dev->mode_config.num_crtc; i++) {
  143. struct nouveau_bo *bo = nv50_display_crtc_sema(chan->drm->dev, i);
  144. ret = nouveau_bo_vma_add(bo, cli->vm, &fctx->dispc_vma[i]);
  145. }
  146. if (ret)
  147. nv84_fence_context_del(chan);
  148. return ret;
  149. }
  150. static bool
  151. nv84_fence_suspend(struct nouveau_drm *drm)
  152. {
  153. struct nv84_fence_priv *priv = drm->fence;
  154. int i;
  155. priv->suspend = vmalloc(priv->base.contexts * sizeof(u32));
  156. if (priv->suspend) {
  157. for (i = 0; i < priv->base.contexts; i++)
  158. priv->suspend[i] = nouveau_bo_rd32(priv->bo, i*4);
  159. }
  160. return priv->suspend != NULL;
  161. }
  162. static void
  163. nv84_fence_resume(struct nouveau_drm *drm)
  164. {
  165. struct nv84_fence_priv *priv = drm->fence;
  166. int i;
  167. if (priv->suspend) {
  168. for (i = 0; i < priv->base.contexts; i++)
  169. nouveau_bo_wr32(priv->bo, i*4, priv->suspend[i]);
  170. vfree(priv->suspend);
  171. priv->suspend = NULL;
  172. }
  173. }
  174. static void
  175. nv84_fence_destroy(struct nouveau_drm *drm)
  176. {
  177. struct nv84_fence_priv *priv = drm->fence;
  178. nouveau_bo_unmap(priv->bo_gart);
  179. if (priv->bo_gart)
  180. nouveau_bo_unpin(priv->bo_gart);
  181. nouveau_bo_ref(NULL, &priv->bo_gart);
  182. nouveau_bo_unmap(priv->bo);
  183. if (priv->bo)
  184. nouveau_bo_unpin(priv->bo);
  185. nouveau_bo_ref(NULL, &priv->bo);
  186. drm->fence = NULL;
  187. kfree(priv);
  188. }
  189. int
  190. nv84_fence_create(struct nouveau_drm *drm)
  191. {
  192. struct nvkm_fifo *fifo = nvxx_fifo(&drm->device);
  193. struct nv84_fence_priv *priv;
  194. u32 domain;
  195. int ret;
  196. priv = drm->fence = kzalloc(sizeof(*priv), GFP_KERNEL);
  197. if (!priv)
  198. return -ENOMEM;
  199. priv->base.dtor = nv84_fence_destroy;
  200. priv->base.suspend = nv84_fence_suspend;
  201. priv->base.resume = nv84_fence_resume;
  202. priv->base.context_new = nv84_fence_context_new;
  203. priv->base.context_del = nv84_fence_context_del;
  204. priv->base.contexts = fifo->nr;
  205. priv->base.context_base = fence_context_alloc(priv->base.contexts);
  206. priv->base.uevent = true;
  207. mutex_init(&priv->mutex);
  208. /* Use VRAM if there is any ; otherwise fallback to system memory */
  209. domain = drm->device.info.ram_size != 0 ? TTM_PL_FLAG_VRAM :
  210. /*
  211. * fences created in sysmem must be non-cached or we
  212. * will lose CPU/GPU coherency!
  213. */
  214. TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED;
  215. ret = nouveau_bo_new(drm->dev, 16 * priv->base.contexts, 0, domain, 0,
  216. 0, NULL, NULL, &priv->bo);
  217. if (ret == 0) {
  218. ret = nouveau_bo_pin(priv->bo, domain, false);
  219. if (ret == 0) {
  220. ret = nouveau_bo_map(priv->bo);
  221. if (ret)
  222. nouveau_bo_unpin(priv->bo);
  223. }
  224. if (ret)
  225. nouveau_bo_ref(NULL, &priv->bo);
  226. }
  227. if (ret == 0)
  228. ret = nouveau_bo_new(drm->dev, 16 * priv->base.contexts, 0,
  229. TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED, 0,
  230. 0, NULL, NULL, &priv->bo_gart);
  231. if (ret == 0) {
  232. ret = nouveau_bo_pin(priv->bo_gart, TTM_PL_FLAG_TT, false);
  233. if (ret == 0) {
  234. ret = nouveau_bo_map(priv->bo_gart);
  235. if (ret)
  236. nouveau_bo_unpin(priv->bo_gart);
  237. }
  238. if (ret)
  239. nouveau_bo_ref(NULL, &priv->bo_gart);
  240. }
  241. if (ret)
  242. nv84_fence_destroy(drm);
  243. return ret;
  244. }