nouveau_dma.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (C) 2007 Ben Skeggs.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include "drmP.h"
  27. #include "drm.h"
  28. #include "nouveau_drv.h"
  29. #include "nouveau_dma.h"
  30. #include "nouveau_ramht.h"
  31. void
  32. nouveau_dma_init(struct nouveau_channel *chan)
  33. {
  34. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  35. struct nouveau_bo *pushbuf = chan->pushbuf_bo;
  36. if (dev_priv->card_type >= NV_50) {
  37. const int ib_size = pushbuf->bo.mem.size / 2;
  38. chan->dma.ib_base = (pushbuf->bo.mem.size - ib_size) >> 2;
  39. chan->dma.ib_max = (ib_size / 8) - 1;
  40. chan->dma.ib_put = 0;
  41. chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
  42. chan->dma.max = (pushbuf->bo.mem.size - ib_size) >> 2;
  43. } else {
  44. chan->dma.max = (pushbuf->bo.mem.size >> 2) - 2;
  45. }
  46. chan->dma.put = 0;
  47. chan->dma.cur = chan->dma.put;
  48. chan->dma.free = chan->dma.max - chan->dma.cur;
  49. }
  50. void
  51. OUT_RINGp(struct nouveau_channel *chan, const void *data, unsigned nr_dwords)
  52. {
  53. bool is_iomem;
  54. u32 *mem = ttm_kmap_obj_virtual(&chan->pushbuf_bo->kmap, &is_iomem);
  55. mem = &mem[chan->dma.cur];
  56. if (is_iomem)
  57. memcpy_toio((void __force __iomem *)mem, data, nr_dwords * 4);
  58. else
  59. memcpy(mem, data, nr_dwords * 4);
  60. chan->dma.cur += nr_dwords;
  61. }
  62. /* Fetch and adjust GPU GET pointer
  63. *
  64. * Returns:
  65. * value >= 0, the adjusted GET pointer
  66. * -EINVAL if GET pointer currently outside main push buffer
  67. * -EBUSY if timeout exceeded
  68. */
  69. static inline int
  70. READ_GET(struct nouveau_channel *chan, uint64_t *prev_get, int *timeout)
  71. {
  72. uint64_t val;
  73. val = nvchan_rd32(chan, chan->user_get);
  74. if (chan->user_get_hi)
  75. val |= (uint64_t)nvchan_rd32(chan, chan->user_get_hi) << 32;
  76. /* reset counter as long as GET is still advancing, this is
  77. * to avoid misdetecting a GPU lockup if the GPU happens to
  78. * just be processing an operation that takes a long time
  79. */
  80. if (val != *prev_get) {
  81. *prev_get = val;
  82. *timeout = 0;
  83. }
  84. if ((++*timeout & 0xff) == 0) {
  85. DRM_UDELAY(1);
  86. if (*timeout > 100000)
  87. return -EBUSY;
  88. }
  89. if (val < chan->pushbuf_base ||
  90. val > chan->pushbuf_base + (chan->dma.max << 2))
  91. return -EINVAL;
  92. return (val - chan->pushbuf_base) >> 2;
  93. }
  94. void
  95. nv50_dma_push(struct nouveau_channel *chan, struct nouveau_bo *bo,
  96. int delta, int length)
  97. {
  98. struct nouveau_bo *pb = chan->pushbuf_bo;
  99. struct nouveau_vma *vma;
  100. int ip = (chan->dma.ib_put * 2) + chan->dma.ib_base;
  101. u64 offset;
  102. vma = nouveau_bo_vma_find(bo, chan->vm);
  103. BUG_ON(!vma);
  104. offset = vma->offset + delta;
  105. BUG_ON(chan->dma.ib_free < 1);
  106. nouveau_bo_wr32(pb, ip++, lower_32_bits(offset));
  107. nouveau_bo_wr32(pb, ip++, upper_32_bits(offset) | length << 8);
  108. chan->dma.ib_put = (chan->dma.ib_put + 1) & chan->dma.ib_max;
  109. DRM_MEMORYBARRIER();
  110. /* Flush writes. */
  111. nouveau_bo_rd32(pb, 0);
  112. nvchan_wr32(chan, 0x8c, chan->dma.ib_put);
  113. chan->dma.ib_free--;
  114. }
  115. static int
  116. nv50_dma_push_wait(struct nouveau_channel *chan, int count)
  117. {
  118. uint32_t cnt = 0, prev_get = 0;
  119. while (chan->dma.ib_free < count) {
  120. uint32_t get = nvchan_rd32(chan, 0x88);
  121. if (get != prev_get) {
  122. prev_get = get;
  123. cnt = 0;
  124. }
  125. if ((++cnt & 0xff) == 0) {
  126. DRM_UDELAY(1);
  127. if (cnt > 100000)
  128. return -EBUSY;
  129. }
  130. chan->dma.ib_free = get - chan->dma.ib_put;
  131. if (chan->dma.ib_free <= 0)
  132. chan->dma.ib_free += chan->dma.ib_max;
  133. }
  134. return 0;
  135. }
  136. static int
  137. nv50_dma_wait(struct nouveau_channel *chan, int slots, int count)
  138. {
  139. uint64_t prev_get = 0;
  140. int ret, cnt = 0;
  141. ret = nv50_dma_push_wait(chan, slots + 1);
  142. if (unlikely(ret))
  143. return ret;
  144. while (chan->dma.free < count) {
  145. int get = READ_GET(chan, &prev_get, &cnt);
  146. if (unlikely(get < 0)) {
  147. if (get == -EINVAL)
  148. continue;
  149. return get;
  150. }
  151. if (get <= chan->dma.cur) {
  152. chan->dma.free = chan->dma.max - chan->dma.cur;
  153. if (chan->dma.free >= count)
  154. break;
  155. FIRE_RING(chan);
  156. do {
  157. get = READ_GET(chan, &prev_get, &cnt);
  158. if (unlikely(get < 0)) {
  159. if (get == -EINVAL)
  160. continue;
  161. return get;
  162. }
  163. } while (get == 0);
  164. chan->dma.cur = 0;
  165. chan->dma.put = 0;
  166. }
  167. chan->dma.free = get - chan->dma.cur - 1;
  168. }
  169. return 0;
  170. }
  171. int
  172. nouveau_dma_wait(struct nouveau_channel *chan, int slots, int size)
  173. {
  174. uint64_t prev_get = 0;
  175. int cnt = 0, get;
  176. if (chan->dma.ib_max)
  177. return nv50_dma_wait(chan, slots, size);
  178. while (chan->dma.free < size) {
  179. get = READ_GET(chan, &prev_get, &cnt);
  180. if (unlikely(get == -EBUSY))
  181. return -EBUSY;
  182. /* loop until we have a usable GET pointer. the value
  183. * we read from the GPU may be outside the main ring if
  184. * PFIFO is processing a buffer called from the main ring,
  185. * discard these values until something sensible is seen.
  186. *
  187. * the other case we discard GET is while the GPU is fetching
  188. * from the SKIPS area, so the code below doesn't have to deal
  189. * with some fun corner cases.
  190. */
  191. if (unlikely(get == -EINVAL) || get < NOUVEAU_DMA_SKIPS)
  192. continue;
  193. if (get <= chan->dma.cur) {
  194. /* engine is fetching behind us, or is completely
  195. * idle (GET == PUT) so we have free space up until
  196. * the end of the push buffer
  197. *
  198. * we can only hit that path once per call due to
  199. * looping back to the beginning of the push buffer,
  200. * we'll hit the fetching-ahead-of-us path from that
  201. * point on.
  202. *
  203. * the *one* exception to that rule is if we read
  204. * GET==PUT, in which case the below conditional will
  205. * always succeed and break us out of the wait loop.
  206. */
  207. chan->dma.free = chan->dma.max - chan->dma.cur;
  208. if (chan->dma.free >= size)
  209. break;
  210. /* not enough space left at the end of the push buffer,
  211. * instruct the GPU to jump back to the start right
  212. * after processing the currently pending commands.
  213. */
  214. OUT_RING(chan, chan->pushbuf_base | 0x20000000);
  215. /* wait for GET to depart from the skips area.
  216. * prevents writing GET==PUT and causing a race
  217. * condition that causes us to think the GPU is
  218. * idle when it's not.
  219. */
  220. do {
  221. get = READ_GET(chan, &prev_get, &cnt);
  222. if (unlikely(get == -EBUSY))
  223. return -EBUSY;
  224. if (unlikely(get == -EINVAL))
  225. continue;
  226. } while (get <= NOUVEAU_DMA_SKIPS);
  227. WRITE_PUT(NOUVEAU_DMA_SKIPS);
  228. /* we're now submitting commands at the start of
  229. * the push buffer.
  230. */
  231. chan->dma.cur =
  232. chan->dma.put = NOUVEAU_DMA_SKIPS;
  233. }
  234. /* engine fetching ahead of us, we have space up until the
  235. * current GET pointer. the "- 1" is to ensure there's
  236. * space left to emit a jump back to the beginning of the
  237. * push buffer if we require it. we can never get GET == PUT
  238. * here, so this is safe.
  239. */
  240. chan->dma.free = get - chan->dma.cur - 1;
  241. }
  242. return 0;
  243. }