nvc0_fence.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. struct nvc0_fence_priv {
  33. struct nouveau_fence_priv base;
  34. struct nouveau_bo *bo;
  35. u32 *suspend;
  36. };
  37. struct nvc0_fence_chan {
  38. struct nouveau_fence_chan base;
  39. struct nouveau_vma vma;
  40. struct nouveau_vma dispc_vma[4];
  41. };
  42. u64
  43. nvc0_fence_crtc(struct nouveau_channel *chan, int crtc)
  44. {
  45. struct nvc0_fence_chan *fctx = chan->fence;
  46. return fctx->dispc_vma[crtc].offset;
  47. }
  48. static int
  49. nvc0_fence_emit(struct nouveau_fence *fence)
  50. {
  51. struct nouveau_channel *chan = fence->channel;
  52. struct nvc0_fence_chan *fctx = chan->fence;
  53. struct nouveau_fifo_chan *fifo = (void *)chan->object;
  54. u64 addr = fctx->vma.offset + fifo->chid * 16;
  55. int ret;
  56. ret = RING_SPACE(chan, 5);
  57. if (ret == 0) {
  58. BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  59. OUT_RING (chan, upper_32_bits(addr));
  60. OUT_RING (chan, lower_32_bits(addr));
  61. OUT_RING (chan, fence->sequence);
  62. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
  63. FIRE_RING (chan);
  64. }
  65. return ret;
  66. }
  67. static int
  68. nvc0_fence_sync(struct nouveau_fence *fence,
  69. struct nouveau_channel *prev, struct nouveau_channel *chan)
  70. {
  71. struct nvc0_fence_chan *fctx = chan->fence;
  72. struct nouveau_fifo_chan *fifo = (void *)prev->object;
  73. u64 addr = fctx->vma.offset + fifo->chid * 16;
  74. int ret;
  75. ret = RING_SPACE(chan, 5);
  76. if (ret == 0) {
  77. BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  78. OUT_RING (chan, upper_32_bits(addr));
  79. OUT_RING (chan, lower_32_bits(addr));
  80. OUT_RING (chan, fence->sequence);
  81. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_GEQUAL |
  82. NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
  83. FIRE_RING (chan);
  84. }
  85. return ret;
  86. }
  87. static u32
  88. nvc0_fence_read(struct nouveau_channel *chan)
  89. {
  90. struct nouveau_fifo_chan *fifo = (void *)chan->object;
  91. struct nvc0_fence_priv *priv = chan->drm->fence;
  92. return nouveau_bo_rd32(priv->bo, fifo->chid * 16/4);
  93. }
  94. static void
  95. nvc0_fence_context_del(struct nouveau_channel *chan)
  96. {
  97. struct drm_device *dev = chan->drm->dev;
  98. struct nvc0_fence_priv *priv = chan->drm->fence;
  99. struct nvc0_fence_chan *fctx = chan->fence;
  100. int i;
  101. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  102. struct nouveau_bo *bo = nv50_display_crtc_sema(dev, i);
  103. nouveau_bo_vma_del(bo, &fctx->dispc_vma[i]);
  104. }
  105. nouveau_bo_vma_del(priv->bo, &fctx->vma);
  106. nouveau_fence_context_del(&fctx->base);
  107. chan->fence = NULL;
  108. kfree(fctx);
  109. }
  110. static int
  111. nvc0_fence_context_new(struct nouveau_channel *chan)
  112. {
  113. struct nouveau_fifo_chan *fifo = (void *)chan->object;
  114. struct nouveau_client *client = nouveau_client(fifo);
  115. struct nvc0_fence_priv *priv = chan->drm->fence;
  116. struct nvc0_fence_chan *fctx;
  117. int ret, i;
  118. fctx = chan->fence = kzalloc(sizeof(*fctx), GFP_KERNEL);
  119. if (!fctx)
  120. return -ENOMEM;
  121. nouveau_fence_context_new(&fctx->base);
  122. ret = nouveau_bo_vma_add(priv->bo, client->vm, &fctx->vma);
  123. if (ret)
  124. nvc0_fence_context_del(chan);
  125. /* map display semaphore buffers into channel's vm */
  126. for (i = 0; !ret && i < chan->drm->dev->mode_config.num_crtc; i++) {
  127. struct nouveau_bo *bo = nv50_display_crtc_sema(chan->drm->dev, i);
  128. ret = nouveau_bo_vma_add(bo, client->vm, &fctx->dispc_vma[i]);
  129. }
  130. nouveau_bo_wr32(priv->bo, fifo->chid * 16/4, 0x00000000);
  131. return ret;
  132. }
  133. static bool
  134. nvc0_fence_suspend(struct nouveau_drm *drm)
  135. {
  136. struct nouveau_fifo *pfifo = nouveau_fifo(drm->device);
  137. struct nvc0_fence_priv *priv = drm->fence;
  138. int i;
  139. priv->suspend = vmalloc((pfifo->max + 1) * sizeof(u32));
  140. if (priv->suspend) {
  141. for (i = 0; i <= pfifo->max; i++)
  142. priv->suspend[i] = nouveau_bo_rd32(priv->bo, i);
  143. }
  144. return priv->suspend != NULL;
  145. }
  146. static void
  147. nvc0_fence_resume(struct nouveau_drm *drm)
  148. {
  149. struct nouveau_fifo *pfifo = nouveau_fifo(drm->device);
  150. struct nvc0_fence_priv *priv = drm->fence;
  151. int i;
  152. if (priv->suspend) {
  153. for (i = 0; i <= pfifo->max; i++)
  154. nouveau_bo_wr32(priv->bo, i, priv->suspend[i]);
  155. vfree(priv->suspend);
  156. priv->suspend = NULL;
  157. }
  158. }
  159. static void
  160. nvc0_fence_destroy(struct nouveau_drm *drm)
  161. {
  162. struct nvc0_fence_priv *priv = drm->fence;
  163. nouveau_bo_unmap(priv->bo);
  164. if (priv->bo)
  165. nouveau_bo_unpin(priv->bo);
  166. nouveau_bo_ref(NULL, &priv->bo);
  167. drm->fence = NULL;
  168. kfree(priv);
  169. }
  170. int
  171. nvc0_fence_create(struct nouveau_drm *drm)
  172. {
  173. struct nouveau_fifo *pfifo = nouveau_fifo(drm->device);
  174. struct nvc0_fence_priv *priv;
  175. int ret;
  176. priv = drm->fence = kzalloc(sizeof(*priv), GFP_KERNEL);
  177. if (!priv)
  178. return -ENOMEM;
  179. priv->base.dtor = nvc0_fence_destroy;
  180. priv->base.suspend = nvc0_fence_suspend;
  181. priv->base.resume = nvc0_fence_resume;
  182. priv->base.context_new = nvc0_fence_context_new;
  183. priv->base.context_del = nvc0_fence_context_del;
  184. priv->base.emit = nvc0_fence_emit;
  185. priv->base.sync = nvc0_fence_sync;
  186. priv->base.read = nvc0_fence_read;
  187. ret = nouveau_bo_new(drm->dev, 16 * (pfifo->max + 1), 0,
  188. TTM_PL_FLAG_VRAM, 0, 0, NULL, &priv->bo);
  189. if (ret == 0) {
  190. ret = nouveau_bo_pin(priv->bo, TTM_PL_FLAG_VRAM);
  191. if (ret == 0) {
  192. ret = nouveau_bo_map(priv->bo);
  193. if (ret)
  194. nouveau_bo_unpin(priv->bo);
  195. }
  196. if (ret)
  197. nouveau_bo_ref(NULL, &priv->bo);
  198. }
  199. if (ret)
  200. nvc0_fence_destroy(drm);
  201. return ret;
  202. }