nvc0_fence.c 6.8 KB

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