nv84_fence.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_drv.h"
  25. #include "nouveau_dma.h"
  26. #include "nouveau_fence.h"
  27. #include "nouveau_vmm.h"
  28. #include "nv50_display.h"
  29. #include <nvif/push206e.h>
  30. #include <nvhw/class/cl826f.h>
  31. static int
  32. nv84_fence_emit32(struct nouveau_channel *chan, u64 virtual, u32 sequence)
  33. {
  34. struct nvif_push *push = chan->chan.push;
  35. int ret = PUSH_WAIT(push, 8);
  36. if (ret == 0) {
  37. PUSH_MTHD(push, NV826F, SET_CONTEXT_DMA_SEMAPHORE, chan->vram.handle);
  38. PUSH_MTHD(push, NV826F, SEMAPHOREA,
  39. NVVAL(NV826F, SEMAPHOREA, OFFSET_UPPER, upper_32_bits(virtual)),
  40. SEMAPHOREB, lower_32_bits(virtual),
  41. SEMAPHOREC, sequence,
  42. SEMAPHORED,
  43. NVDEF(NV826F, SEMAPHORED, OPERATION, RELEASE),
  44. NON_STALLED_INTERRUPT, 0);
  45. PUSH_KICK(push);
  46. }
  47. return ret;
  48. }
  49. static int
  50. nv84_fence_sync32(struct nouveau_channel *chan, u64 virtual, u32 sequence)
  51. {
  52. struct nvif_push *push = chan->chan.push;
  53. int ret = PUSH_WAIT(push, 7);
  54. if (ret == 0) {
  55. PUSH_MTHD(push, NV826F, SET_CONTEXT_DMA_SEMAPHORE, chan->vram.handle);
  56. PUSH_MTHD(push, NV826F, SEMAPHOREA,
  57. NVVAL(NV826F, SEMAPHOREA, OFFSET_UPPER, upper_32_bits(virtual)),
  58. SEMAPHOREB, lower_32_bits(virtual),
  59. SEMAPHOREC, sequence,
  60. SEMAPHORED,
  61. NVDEF(NV826F, SEMAPHORED, OPERATION, ACQ_GEQ));
  62. PUSH_KICK(push);
  63. }
  64. return ret;
  65. }
  66. static int
  67. nv84_fence_emit(struct nouveau_fence *fence)
  68. {
  69. struct nouveau_channel *chan = fence->channel;
  70. struct nv84_fence_chan *fctx = chan->fence;
  71. u64 addr = fctx->vma->addr + chan->chid * 16;
  72. return fctx->base.emit32(chan, addr, fence->base.seqno);
  73. }
  74. static int
  75. nv84_fence_sync(struct nouveau_fence *fence,
  76. struct nouveau_channel *prev, struct nouveau_channel *chan)
  77. {
  78. struct nv84_fence_chan *fctx = chan->fence;
  79. u64 addr = fctx->vma->addr + prev->chid * 16;
  80. return fctx->base.sync32(chan, addr, fence->base.seqno);
  81. }
  82. static u32
  83. nv84_fence_read(struct nouveau_channel *chan)
  84. {
  85. struct nv84_fence_priv *priv = chan->drm->fence;
  86. return nouveau_bo_rd32(priv->bo, chan->chid * 16/4);
  87. }
  88. static void
  89. nv84_fence_context_del(struct nouveau_channel *chan)
  90. {
  91. struct nv84_fence_priv *priv = chan->drm->fence;
  92. struct nv84_fence_chan *fctx = chan->fence;
  93. nouveau_bo_wr32(priv->bo, chan->chid * 16 / 4, fctx->base.sequence);
  94. mutex_lock(&priv->mutex);
  95. nouveau_vma_del(&fctx->vma);
  96. mutex_unlock(&priv->mutex);
  97. nouveau_fence_context_del(&fctx->base);
  98. chan->fence = NULL;
  99. nouveau_fence_context_free(&fctx->base);
  100. }
  101. int
  102. nv84_fence_context_new(struct nouveau_channel *chan)
  103. {
  104. struct nv84_fence_priv *priv = chan->drm->fence;
  105. struct nv84_fence_chan *fctx;
  106. int ret;
  107. fctx = chan->fence = kzalloc(sizeof(*fctx), GFP_KERNEL);
  108. if (!fctx)
  109. return -ENOMEM;
  110. nouveau_fence_context_new(chan, &fctx->base);
  111. fctx->base.emit = nv84_fence_emit;
  112. fctx->base.sync = nv84_fence_sync;
  113. fctx->base.read = nv84_fence_read;
  114. fctx->base.emit32 = nv84_fence_emit32;
  115. fctx->base.sync32 = nv84_fence_sync32;
  116. fctx->base.sequence = nv84_fence_read(chan);
  117. mutex_lock(&priv->mutex);
  118. ret = nouveau_vma_new(priv->bo, chan->vmm, &fctx->vma);
  119. mutex_unlock(&priv->mutex);
  120. if (ret)
  121. nv84_fence_context_del(chan);
  122. return ret;
  123. }
  124. static bool
  125. nv84_fence_suspend(struct nouveau_drm *drm)
  126. {
  127. struct nv84_fence_priv *priv = drm->fence;
  128. int i;
  129. priv->suspend = vmalloc(array_size(sizeof(u32), drm->chan.nr));
  130. if (priv->suspend) {
  131. for (i = 0; i < drm->chan.nr; i++)
  132. priv->suspend[i] = nouveau_bo_rd32(priv->bo, i*4);
  133. }
  134. return priv->suspend != NULL;
  135. }
  136. static void
  137. nv84_fence_resume(struct nouveau_drm *drm)
  138. {
  139. struct nv84_fence_priv *priv = drm->fence;
  140. int i;
  141. if (priv->suspend) {
  142. for (i = 0; i < drm->chan.nr; i++)
  143. nouveau_bo_wr32(priv->bo, i*4, priv->suspend[i]);
  144. vfree(priv->suspend);
  145. priv->suspend = NULL;
  146. }
  147. }
  148. static void
  149. nv84_fence_destroy(struct nouveau_drm *drm)
  150. {
  151. struct nv84_fence_priv *priv = drm->fence;
  152. nouveau_bo_unmap(priv->bo);
  153. if (priv->bo)
  154. nouveau_bo_unpin(priv->bo);
  155. nouveau_bo_ref(NULL, &priv->bo);
  156. drm->fence = NULL;
  157. kfree(priv);
  158. }
  159. int
  160. nv84_fence_create(struct nouveau_drm *drm)
  161. {
  162. struct nv84_fence_priv *priv;
  163. u32 domain;
  164. int ret;
  165. priv = drm->fence = kzalloc(sizeof(*priv), GFP_KERNEL);
  166. if (!priv)
  167. return -ENOMEM;
  168. priv->base.dtor = nv84_fence_destroy;
  169. priv->base.suspend = nv84_fence_suspend;
  170. priv->base.resume = nv84_fence_resume;
  171. priv->base.context_new = nv84_fence_context_new;
  172. priv->base.context_del = nv84_fence_context_del;
  173. priv->base.uevent = drm->client.device.info.family < NV_DEVICE_INFO_V0_AMPERE;
  174. mutex_init(&priv->mutex);
  175. /* Use VRAM if there is any ; otherwise fallback to system memory */
  176. domain = drm->client.device.info.ram_size != 0 ?
  177. NOUVEAU_GEM_DOMAIN_VRAM :
  178. /*
  179. * fences created in sysmem must be non-cached or we
  180. * will lose CPU/GPU coherency!
  181. */
  182. NOUVEAU_GEM_DOMAIN_GART | NOUVEAU_GEM_DOMAIN_COHERENT;
  183. ret = nouveau_bo_new(&drm->client, 16 * drm->chan.nr, 0,
  184. domain, 0, 0, NULL, NULL, &priv->bo);
  185. if (ret == 0) {
  186. ret = nouveau_bo_pin(priv->bo, domain, false);
  187. if (ret == 0) {
  188. ret = nouveau_bo_map(priv->bo);
  189. if (ret)
  190. nouveau_bo_unpin(priv->bo);
  191. }
  192. if (ret)
  193. nouveau_bo_ref(NULL, &priv->bo);
  194. }
  195. if (ret)
  196. nv84_fence_destroy(drm);
  197. return ret;
  198. }