nv84_fence.c 7.5 KB

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