nv84_fence.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <core/object.h>
  25. #include <core/client.h>
  26. #include <core/class.h>
  27. #include <engine/fifo.h>
  28. #include "nouveau_drm.h"
  29. #include "nouveau_dma.h"
  30. #include "nouveau_fence.h"
  31. #include "nv50_display.h"
  32. u64
  33. nv84_fence_crtc(struct nouveau_channel *chan, int crtc)
  34. {
  35. struct nv84_fence_chan *fctx = chan->fence;
  36. return fctx->dispc_vma[crtc].offset;
  37. }
  38. static int
  39. nv84_fence_emit32(struct nouveau_channel *chan, u64 virtual, u32 sequence)
  40. {
  41. int ret = RING_SPACE(chan, 8);
  42. if (ret == 0) {
  43. BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
  44. OUT_RING (chan, chan->vram);
  45. BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 5);
  46. OUT_RING (chan, upper_32_bits(virtual));
  47. OUT_RING (chan, lower_32_bits(virtual));
  48. OUT_RING (chan, sequence);
  49. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
  50. OUT_RING (chan, 0x00000000);
  51. FIRE_RING (chan);
  52. }
  53. return ret;
  54. }
  55. static int
  56. nv84_fence_sync32(struct nouveau_channel *chan, u64 virtual, u32 sequence)
  57. {
  58. int ret = RING_SPACE(chan, 7);
  59. if (ret == 0) {
  60. BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
  61. OUT_RING (chan, chan->vram);
  62. BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  63. OUT_RING (chan, upper_32_bits(virtual));
  64. OUT_RING (chan, lower_32_bits(virtual));
  65. OUT_RING (chan, sequence);
  66. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_GEQUAL);
  67. FIRE_RING (chan);
  68. }
  69. return ret;
  70. }
  71. static int
  72. nv84_fence_emit(struct nouveau_fence *fence)
  73. {
  74. struct nouveau_channel *chan = fence->channel;
  75. struct nv84_fence_chan *fctx = chan->fence;
  76. struct nouveau_fifo_chan *fifo = (void *)chan->object;
  77. u64 addr = fifo->chid * 16;
  78. if (fence->sysmem)
  79. addr += fctx->vma_gart.offset;
  80. else
  81. addr += fctx->vma.offset;
  82. return fctx->base.emit32(chan, addr, fence->sequence);
  83. }
  84. static int
  85. nv84_fence_sync(struct nouveau_fence *fence,
  86. struct nouveau_channel *prev, struct nouveau_channel *chan)
  87. {
  88. struct nv84_fence_chan *fctx = chan->fence;
  89. struct nouveau_fifo_chan *fifo = (void *)prev->object;
  90. u64 addr = fifo->chid * 16;
  91. if (fence->sysmem)
  92. addr += fctx->vma_gart.offset;
  93. else
  94. addr += fctx->vma.offset;
  95. return fctx->base.sync32(chan, addr, fence->sequence);
  96. }
  97. static u32
  98. nv84_fence_read(struct nouveau_channel *chan)
  99. {
  100. struct nouveau_fifo_chan *fifo = (void *)chan->object;
  101. struct nv84_fence_priv *priv = chan->drm->fence;
  102. return nouveau_bo_rd32(priv->bo, fifo->chid * 16/4);
  103. }
  104. static void
  105. nv84_fence_context_del(struct nouveau_channel *chan)
  106. {
  107. struct drm_device *dev = chan->drm->dev;
  108. struct nv84_fence_priv *priv = chan->drm->fence;
  109. struct nv84_fence_chan *fctx = chan->fence;
  110. int i;
  111. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  112. struct nouveau_bo *bo = nv50_display_crtc_sema(dev, i);
  113. nouveau_bo_vma_del(bo, &fctx->dispc_vma[i]);
  114. }
  115. nouveau_bo_vma_del(priv->bo, &fctx->vma_gart);
  116. nouveau_bo_vma_del(priv->bo, &fctx->vma);
  117. nouveau_fence_context_del(&fctx->base);
  118. chan->fence = NULL;
  119. kfree(fctx);
  120. }
  121. int
  122. nv84_fence_context_new(struct nouveau_channel *chan)
  123. {
  124. struct nouveau_fifo_chan *fifo = (void *)chan->object;
  125. struct nouveau_client *client = nouveau_client(fifo);
  126. struct nv84_fence_priv *priv = chan->drm->fence;
  127. struct nv84_fence_chan *fctx;
  128. int ret, i;
  129. fctx = chan->fence = kzalloc(sizeof(*fctx), GFP_KERNEL);
  130. if (!fctx)
  131. return -ENOMEM;
  132. nouveau_fence_context_new(&fctx->base);
  133. fctx->base.emit = nv84_fence_emit;
  134. fctx->base.sync = nv84_fence_sync;
  135. fctx->base.read = nv84_fence_read;
  136. fctx->base.emit32 = nv84_fence_emit32;
  137. fctx->base.sync32 = nv84_fence_sync32;
  138. ret = nouveau_bo_vma_add(priv->bo, client->vm, &fctx->vma);
  139. if (ret == 0) {
  140. ret = nouveau_bo_vma_add(priv->bo_gart, client->vm,
  141. &fctx->vma_gart);
  142. }
  143. /* map display semaphore buffers into channel's vm */
  144. for (i = 0; !ret && i < chan->drm->dev->mode_config.num_crtc; i++) {
  145. struct nouveau_bo *bo = nv50_display_crtc_sema(chan->drm->dev, i);
  146. ret = nouveau_bo_vma_add(bo, client->vm, &fctx->dispc_vma[i]);
  147. }
  148. nouveau_bo_wr32(priv->bo, fifo->chid * 16/4, 0x00000000);
  149. if (ret)
  150. nv84_fence_context_del(chan);
  151. return ret;
  152. }
  153. static bool
  154. nv84_fence_suspend(struct nouveau_drm *drm)
  155. {
  156. struct nouveau_fifo *pfifo = nouveau_fifo(drm->device);
  157. struct nv84_fence_priv *priv = drm->fence;
  158. int i;
  159. priv->suspend = vmalloc((pfifo->max + 1) * sizeof(u32));
  160. if (priv->suspend) {
  161. for (i = 0; i <= pfifo->max; i++)
  162. priv->suspend[i] = nouveau_bo_rd32(priv->bo, i*4);
  163. }
  164. return priv->suspend != NULL;
  165. }
  166. static void
  167. nv84_fence_resume(struct nouveau_drm *drm)
  168. {
  169. struct nouveau_fifo *pfifo = nouveau_fifo(drm->device);
  170. struct nv84_fence_priv *priv = drm->fence;
  171. int i;
  172. if (priv->suspend) {
  173. for (i = 0; i <= pfifo->max; i++)
  174. nouveau_bo_wr32(priv->bo, i*4, priv->suspend[i]);
  175. vfree(priv->suspend);
  176. priv->suspend = NULL;
  177. }
  178. }
  179. static void
  180. nv84_fence_destroy(struct nouveau_drm *drm)
  181. {
  182. struct nv84_fence_priv *priv = drm->fence;
  183. nouveau_bo_unmap(priv->bo_gart);
  184. if (priv->bo_gart)
  185. nouveau_bo_unpin(priv->bo_gart);
  186. nouveau_bo_ref(NULL, &priv->bo_gart);
  187. nouveau_bo_unmap(priv->bo);
  188. if (priv->bo)
  189. nouveau_bo_unpin(priv->bo);
  190. nouveau_bo_ref(NULL, &priv->bo);
  191. drm->fence = NULL;
  192. kfree(priv);
  193. }
  194. int
  195. nv84_fence_create(struct nouveau_drm *drm)
  196. {
  197. struct nouveau_fifo *pfifo = nouveau_fifo(drm->device);
  198. struct nv84_fence_priv *priv;
  199. int ret;
  200. priv = drm->fence = kzalloc(sizeof(*priv), GFP_KERNEL);
  201. if (!priv)
  202. return -ENOMEM;
  203. priv->base.dtor = nv84_fence_destroy;
  204. priv->base.suspend = nv84_fence_suspend;
  205. priv->base.resume = nv84_fence_resume;
  206. priv->base.context_new = nv84_fence_context_new;
  207. priv->base.context_del = nv84_fence_context_del;
  208. init_waitqueue_head(&priv->base.waiting);
  209. priv->base.uevent = true;
  210. ret = nouveau_bo_new(drm->dev, 16 * (pfifo->max + 1), 0,
  211. TTM_PL_FLAG_VRAM, 0, 0, NULL, &priv->bo);
  212. if (ret == 0) {
  213. ret = nouveau_bo_pin(priv->bo, TTM_PL_FLAG_VRAM);
  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 * (pfifo->max + 1), 0,
  224. TTM_PL_FLAG_TT, 0, 0, NULL,
  225. &priv->bo_gart);
  226. if (ret == 0) {
  227. ret = nouveau_bo_pin(priv->bo_gart, TTM_PL_FLAG_TT);
  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. }