nvc0_fence.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_dma.h"
  27. #include "nouveau_fifo.h"
  28. #include "nouveau_ramht.h"
  29. #include "nouveau_fence.h"
  30. struct nvc0_fence_priv {
  31. struct nouveau_fence_priv base;
  32. struct nouveau_bo *bo;
  33. u32 *suspend;
  34. };
  35. struct nvc0_fence_chan {
  36. struct nouveau_fence_chan base;
  37. struct nouveau_vma vma;
  38. };
  39. static int
  40. nvc0_fence_emit(struct nouveau_fence *fence)
  41. {
  42. struct nouveau_channel *chan = fence->channel;
  43. struct nvc0_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE];
  44. u64 addr = fctx->vma.offset + chan->id * 16;
  45. int ret;
  46. ret = RING_SPACE(chan, 5);
  47. if (ret == 0) {
  48. BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  49. OUT_RING (chan, upper_32_bits(addr));
  50. OUT_RING (chan, lower_32_bits(addr));
  51. OUT_RING (chan, fence->sequence);
  52. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
  53. FIRE_RING (chan);
  54. }
  55. return ret;
  56. }
  57. static int
  58. nvc0_fence_sync(struct nouveau_fence *fence,
  59. struct nouveau_channel *prev, struct nouveau_channel *chan)
  60. {
  61. struct nvc0_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE];
  62. u64 addr = fctx->vma.offset + prev->id * 16;
  63. int ret;
  64. ret = RING_SPACE(chan, 5);
  65. if (ret == 0) {
  66. BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
  67. OUT_RING (chan, upper_32_bits(addr));
  68. OUT_RING (chan, lower_32_bits(addr));
  69. OUT_RING (chan, fence->sequence);
  70. OUT_RING (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_GEQUAL |
  71. NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
  72. FIRE_RING (chan);
  73. }
  74. return ret;
  75. }
  76. static u32
  77. nvc0_fence_read(struct nouveau_channel *chan)
  78. {
  79. struct nvc0_fence_priv *priv = nv_engine(chan->dev, NVOBJ_ENGINE_FENCE);
  80. return nouveau_bo_rd32(priv->bo, chan->id * 16/4);
  81. }
  82. static void
  83. nvc0_fence_context_del(struct nouveau_channel *chan, int engine)
  84. {
  85. struct nvc0_fence_priv *priv = nv_engine(chan->dev, engine);
  86. struct nvc0_fence_chan *fctx = chan->engctx[engine];
  87. nouveau_bo_vma_del(priv->bo, &fctx->vma);
  88. nouveau_fence_context_del(&fctx->base);
  89. chan->engctx[engine] = NULL;
  90. kfree(fctx);
  91. }
  92. static int
  93. nvc0_fence_context_new(struct nouveau_channel *chan, int engine)
  94. {
  95. struct nvc0_fence_priv *priv = nv_engine(chan->dev, engine);
  96. struct nvc0_fence_chan *fctx;
  97. int ret;
  98. fctx = chan->engctx[engine] = kzalloc(sizeof(*fctx), GFP_KERNEL);
  99. if (!fctx)
  100. return -ENOMEM;
  101. nouveau_fence_context_new(&fctx->base);
  102. ret = nouveau_bo_vma_add(priv->bo, chan->vm, &fctx->vma);
  103. if (ret)
  104. nvc0_fence_context_del(chan, engine);
  105. nouveau_bo_wr32(priv->bo, chan->id * 16/4, 0x00000000);
  106. return ret;
  107. }
  108. static int
  109. nvc0_fence_fini(struct drm_device *dev, int engine, bool suspend)
  110. {
  111. struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  112. struct nvc0_fence_priv *priv = nv_engine(dev, engine);
  113. int i;
  114. if (suspend) {
  115. priv->suspend = vmalloc(pfifo->channels * sizeof(u32));
  116. if (!priv->suspend)
  117. return -ENOMEM;
  118. for (i = 0; i < pfifo->channels; i++)
  119. priv->suspend[i] = nouveau_bo_rd32(priv->bo, i);
  120. }
  121. return 0;
  122. }
  123. static int
  124. nvc0_fence_init(struct drm_device *dev, int engine)
  125. {
  126. struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  127. struct nvc0_fence_priv *priv = nv_engine(dev, engine);
  128. int i;
  129. if (priv->suspend) {
  130. for (i = 0; i < pfifo->channels; i++)
  131. nouveau_bo_wr32(priv->bo, i, priv->suspend[i]);
  132. vfree(priv->suspend);
  133. priv->suspend = NULL;
  134. }
  135. return 0;
  136. }
  137. static void
  138. nvc0_fence_destroy(struct drm_device *dev, int engine)
  139. {
  140. struct drm_nouveau_private *dev_priv = dev->dev_private;
  141. struct nvc0_fence_priv *priv = nv_engine(dev, engine);
  142. nouveau_bo_unmap(priv->bo);
  143. nouveau_bo_ref(NULL, &priv->bo);
  144. dev_priv->eng[engine] = NULL;
  145. kfree(priv);
  146. }
  147. int
  148. nvc0_fence_create(struct drm_device *dev)
  149. {
  150. struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  151. struct drm_nouveau_private *dev_priv = dev->dev_private;
  152. struct nvc0_fence_priv *priv;
  153. int ret;
  154. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  155. if (!priv)
  156. return -ENOMEM;
  157. priv->base.engine.destroy = nvc0_fence_destroy;
  158. priv->base.engine.init = nvc0_fence_init;
  159. priv->base.engine.fini = nvc0_fence_fini;
  160. priv->base.engine.context_new = nvc0_fence_context_new;
  161. priv->base.engine.context_del = nvc0_fence_context_del;
  162. priv->base.emit = nvc0_fence_emit;
  163. priv->base.sync = nvc0_fence_sync;
  164. priv->base.read = nvc0_fence_read;
  165. dev_priv->eng[NVOBJ_ENGINE_FENCE] = &priv->base.engine;
  166. ret = nouveau_bo_new(dev, 16 * pfifo->channels, 0, TTM_PL_FLAG_VRAM,
  167. 0, 0, NULL, &priv->bo);
  168. if (ret == 0) {
  169. ret = nouveau_bo_pin(priv->bo, TTM_PL_FLAG_VRAM);
  170. if (ret == 0)
  171. ret = nouveau_bo_map(priv->bo);
  172. if (ret)
  173. nouveau_bo_ref(NULL, &priv->bo);
  174. }
  175. if (ret)
  176. nvc0_fence_destroy(dev, NVOBJ_ENGINE_FENCE);
  177. return ret;
  178. }