nv84_fence.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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->sequence);
  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->sequence);
  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_vma_del(priv->bo, &fctx->vma_gart);
  109. nouveau_bo_vma_del(priv->bo, &fctx->vma);
  110. nouveau_fence_context_del(&fctx->base);
  111. chan->fence = NULL;
  112. kfree(fctx);
  113. }
  114. int
  115. nv84_fence_context_new(struct nouveau_channel *chan)
  116. {
  117. struct nouveau_cli *cli = (void *)nvif_client(&chan->device->base);
  118. struct nv84_fence_priv *priv = chan->drm->fence;
  119. struct nv84_fence_chan *fctx;
  120. int ret, i;
  121. fctx = chan->fence = kzalloc(sizeof(*fctx), GFP_KERNEL);
  122. if (!fctx)
  123. return -ENOMEM;
  124. nouveau_fence_context_new(&fctx->base);
  125. fctx->base.emit = nv84_fence_emit;
  126. fctx->base.sync = nv84_fence_sync;
  127. fctx->base.read = nv84_fence_read;
  128. fctx->base.emit32 = nv84_fence_emit32;
  129. fctx->base.sync32 = nv84_fence_sync32;
  130. ret = nouveau_bo_vma_add(priv->bo, cli->vm, &fctx->vma);
  131. if (ret == 0) {
  132. ret = nouveau_bo_vma_add(priv->bo_gart, cli->vm,
  133. &fctx->vma_gart);
  134. }
  135. /* map display semaphore buffers into channel's vm */
  136. for (i = 0; !ret && i < chan->drm->dev->mode_config.num_crtc; i++) {
  137. struct nouveau_bo *bo = nv50_display_crtc_sema(chan->drm->dev, i);
  138. ret = nouveau_bo_vma_add(bo, cli->vm, &fctx->dispc_vma[i]);
  139. }
  140. nouveau_bo_wr32(priv->bo, chan->chid * 16/4, 0x00000000);
  141. if (ret)
  142. nv84_fence_context_del(chan);
  143. return ret;
  144. }
  145. static bool
  146. nv84_fence_suspend(struct nouveau_drm *drm)
  147. {
  148. struct nouveau_fifo *pfifo = nvkm_fifo(&drm->device);
  149. struct nv84_fence_priv *priv = drm->fence;
  150. int i;
  151. priv->suspend = vmalloc((pfifo->max + 1) * sizeof(u32));
  152. if (priv->suspend) {
  153. for (i = 0; i <= pfifo->max; 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 nouveau_fifo *pfifo = nvkm_fifo(&drm->device);
  162. struct nv84_fence_priv *priv = drm->fence;
  163. int i;
  164. if (priv->suspend) {
  165. for (i = 0; i <= pfifo->max; i++)
  166. nouveau_bo_wr32(priv->bo, i*4, priv->suspend[i]);
  167. vfree(priv->suspend);
  168. priv->suspend = NULL;
  169. }
  170. }
  171. static void
  172. nv84_fence_destroy(struct nouveau_drm *drm)
  173. {
  174. struct nv84_fence_priv *priv = drm->fence;
  175. nouveau_bo_unmap(priv->bo_gart);
  176. if (priv->bo_gart)
  177. nouveau_bo_unpin(priv->bo_gart);
  178. nouveau_bo_ref(NULL, &priv->bo_gart);
  179. nouveau_bo_unmap(priv->bo);
  180. if (priv->bo)
  181. nouveau_bo_unpin(priv->bo);
  182. nouveau_bo_ref(NULL, &priv->bo);
  183. drm->fence = NULL;
  184. kfree(priv);
  185. }
  186. int
  187. nv84_fence_create(struct nouveau_drm *drm)
  188. {
  189. struct nouveau_fifo *pfifo = nvkm_fifo(&drm->device);
  190. struct nv84_fence_priv *priv;
  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. init_waitqueue_head(&priv->base.waiting);
  201. priv->base.uevent = true;
  202. ret = nouveau_bo_new(drm->dev, 16 * (pfifo->max + 1), 0,
  203. TTM_PL_FLAG_VRAM, 0, 0, NULL, &priv->bo);
  204. if (ret == 0) {
  205. ret = nouveau_bo_pin(priv->bo, TTM_PL_FLAG_VRAM);
  206. if (ret == 0) {
  207. ret = nouveau_bo_map(priv->bo);
  208. if (ret)
  209. nouveau_bo_unpin(priv->bo);
  210. }
  211. if (ret)
  212. nouveau_bo_ref(NULL, &priv->bo);
  213. }
  214. if (ret == 0)
  215. ret = nouveau_bo_new(drm->dev, 16 * (pfifo->max + 1), 0,
  216. TTM_PL_FLAG_TT, 0, 0, NULL,
  217. &priv->bo_gart);
  218. if (ret == 0) {
  219. ret = nouveau_bo_pin(priv->bo_gart, TTM_PL_FLAG_TT);
  220. if (ret == 0) {
  221. ret = nouveau_bo_map(priv->bo_gart);
  222. if (ret)
  223. nouveau_bo_unpin(priv->bo_gart);
  224. }
  225. if (ret)
  226. nouveau_bo_ref(NULL, &priv->bo_gart);
  227. }
  228. if (ret)
  229. nv84_fence_destroy(drm);
  230. return ret;
  231. }