nouveau_dma.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "nouveau_drv.h"
  28. #include "nouveau_dma.h"
  29. #include "nouveau_ramht.h"
  30. void
  31. nouveau_dma_init(struct nouveau_channel *chan)
  32. {
  33. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  34. struct nouveau_bo *pushbuf = chan->pushbuf_bo;
  35. if (dev_priv->card_type >= NV_50) {
  36. const int ib_size = pushbuf->bo.mem.size / 2;
  37. chan->dma.ib_base = (pushbuf->bo.mem.size - ib_size) >> 2;
  38. chan->dma.ib_max = (ib_size / 8) - 1;
  39. chan->dma.ib_put = 0;
  40. chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
  41. chan->dma.max = (pushbuf->bo.mem.size - ib_size) >> 2;
  42. } else {
  43. chan->dma.max = (pushbuf->bo.mem.size >> 2) - 2;
  44. }
  45. chan->dma.put = 0;
  46. chan->dma.cur = chan->dma.put;
  47. chan->dma.free = chan->dma.max - chan->dma.cur;
  48. }
  49. void
  50. OUT_RINGp(struct nouveau_channel *chan, const void *data, unsigned nr_dwords)
  51. {
  52. bool is_iomem;
  53. u32 *mem = ttm_kmap_obj_virtual(&chan->pushbuf_bo->kmap, &is_iomem);
  54. mem = &mem[chan->dma.cur];
  55. if (is_iomem)
  56. memcpy_toio((void __force __iomem *)mem, data, nr_dwords * 4);
  57. else
  58. memcpy(mem, data, nr_dwords * 4);
  59. chan->dma.cur += nr_dwords;
  60. }
  61. /* Fetch and adjust GPU GET pointer
  62. *
  63. * Returns:
  64. * value >= 0, the adjusted GET pointer
  65. * -EINVAL if GET pointer currently outside main push buffer
  66. * -EBUSY if timeout exceeded
  67. */
  68. static inline int
  69. READ_GET(struct nouveau_channel *chan, uint64_t *prev_get, int *timeout)
  70. {
  71. uint64_t val;
  72. val = nvchan_rd32(chan, chan->user_get);
  73. if (chan->user_get_hi)
  74. val |= (uint64_t)nvchan_rd32(chan, chan->user_get_hi) << 32;
  75. /* reset counter as long as GET is still advancing, this is
  76. * to avoid misdetecting a GPU lockup if the GPU happens to
  77. * just be processing an operation that takes a long time
  78. */
  79. if (val != *prev_get) {
  80. *prev_get = val;
  81. *timeout = 0;
  82. }
  83. if ((++*timeout & 0xff) == 0) {
  84. DRM_UDELAY(1);
  85. if (*timeout > 100000)
  86. return -EBUSY;
  87. }
  88. if (val < chan->pushbuf_base ||
  89. val > chan->pushbuf_base + (chan->dma.max << 2))
  90. return -EINVAL;
  91. return (val - chan->pushbuf_base) >> 2;
  92. }
  93. void
  94. nv50_dma_push(struct nouveau_channel *chan, struct nouveau_bo *bo,
  95. int delta, int length)
  96. {
  97. struct nouveau_bo *pb = chan->pushbuf_bo;
  98. struct nouveau_vma *vma;
  99. int ip = (chan->dma.ib_put * 2) + chan->dma.ib_base;
  100. u64 offset;
  101. vma = nouveau_bo_vma_find(bo, chan->vm);
  102. BUG_ON(!vma);
  103. offset = vma->offset + delta;
  104. BUG_ON(chan->dma.ib_free < 1);
  105. nouveau_bo_wr32(pb, ip++, lower_32_bits(offset));
  106. nouveau_bo_wr32(pb, ip++, upper_32_bits(offset) | length << 8);
  107. chan->dma.ib_put = (chan->dma.ib_put + 1) & chan->dma.ib_max;
  108. DRM_MEMORYBARRIER();
  109. /* Flush writes. */
  110. nouveau_bo_rd32(pb, 0);
  111. nvchan_wr32(chan, 0x8c, chan->dma.ib_put);
  112. chan->dma.ib_free--;
  113. }
  114. static int
  115. nv50_dma_push_wait(struct nouveau_channel *chan, int count)
  116. {
  117. uint32_t cnt = 0, prev_get = 0;
  118. while (chan->dma.ib_free < count) {
  119. uint32_t get = nvchan_rd32(chan, 0x88);
  120. if (get != prev_get) {
  121. prev_get = get;
  122. cnt = 0;
  123. }
  124. if ((++cnt & 0xff) == 0) {
  125. DRM_UDELAY(1);
  126. if (cnt > 100000)
  127. return -EBUSY;
  128. }
  129. chan->dma.ib_free = get - chan->dma.ib_put;
  130. if (chan->dma.ib_free <= 0)
  131. chan->dma.ib_free += chan->dma.ib_max;
  132. }
  133. return 0;
  134. }
  135. static int
  136. nv50_dma_wait(struct nouveau_channel *chan, int slots, int count)
  137. {
  138. uint64_t prev_get = 0;
  139. int ret, cnt = 0;
  140. ret = nv50_dma_push_wait(chan, slots + 1);
  141. if (unlikely(ret))
  142. return ret;
  143. while (chan->dma.free < count) {
  144. int get = READ_GET(chan, &prev_get, &cnt);
  145. if (unlikely(get < 0)) {
  146. if (get == -EINVAL)
  147. continue;
  148. return get;
  149. }
  150. if (get <= chan->dma.cur) {
  151. chan->dma.free = chan->dma.max - chan->dma.cur;
  152. if (chan->dma.free >= count)
  153. break;
  154. FIRE_RING(chan);
  155. do {
  156. get = READ_GET(chan, &prev_get, &cnt);
  157. if (unlikely(get < 0)) {
  158. if (get == -EINVAL)
  159. continue;
  160. return get;
  161. }
  162. } while (get == 0);
  163. chan->dma.cur = 0;
  164. chan->dma.put = 0;
  165. }
  166. chan->dma.free = get - chan->dma.cur - 1;
  167. }
  168. return 0;
  169. }
  170. int
  171. nouveau_dma_wait(struct nouveau_channel *chan, int slots, int size)
  172. {
  173. uint64_t prev_get = 0;
  174. int cnt = 0, get;
  175. if (chan->dma.ib_max)
  176. return nv50_dma_wait(chan, slots, size);
  177. while (chan->dma.free < size) {
  178. get = READ_GET(chan, &prev_get, &cnt);
  179. if (unlikely(get == -EBUSY))
  180. return -EBUSY;
  181. /* loop until we have a usable GET pointer. the value
  182. * we read from the GPU may be outside the main ring if
  183. * PFIFO is processing a buffer called from the main ring,
  184. * discard these values until something sensible is seen.
  185. *
  186. * the other case we discard GET is while the GPU is fetching
  187. * from the SKIPS area, so the code below doesn't have to deal
  188. * with some fun corner cases.
  189. */
  190. if (unlikely(get == -EINVAL) || get < NOUVEAU_DMA_SKIPS)
  191. continue;
  192. if (get <= chan->dma.cur) {
  193. /* engine is fetching behind us, or is completely
  194. * idle (GET == PUT) so we have free space up until
  195. * the end of the push buffer
  196. *
  197. * we can only hit that path once per call due to
  198. * looping back to the beginning of the push buffer,
  199. * we'll hit the fetching-ahead-of-us path from that
  200. * point on.
  201. *
  202. * the *one* exception to that rule is if we read
  203. * GET==PUT, in which case the below conditional will
  204. * always succeed and break us out of the wait loop.
  205. */
  206. chan->dma.free = chan->dma.max - chan->dma.cur;
  207. if (chan->dma.free >= size)
  208. break;
  209. /* not enough space left at the end of the push buffer,
  210. * instruct the GPU to jump back to the start right
  211. * after processing the currently pending commands.
  212. */
  213. OUT_RING(chan, chan->pushbuf_base | 0x20000000);
  214. /* wait for GET to depart from the skips area.
  215. * prevents writing GET==PUT and causing a race
  216. * condition that causes us to think the GPU is
  217. * idle when it's not.
  218. */
  219. do {
  220. get = READ_GET(chan, &prev_get, &cnt);
  221. if (unlikely(get == -EBUSY))
  222. return -EBUSY;
  223. if (unlikely(get == -EINVAL))
  224. continue;
  225. } while (get <= NOUVEAU_DMA_SKIPS);
  226. WRITE_PUT(NOUVEAU_DMA_SKIPS);
  227. /* we're now submitting commands at the start of
  228. * the push buffer.
  229. */
  230. chan->dma.cur =
  231. chan->dma.put = NOUVEAU_DMA_SKIPS;
  232. }
  233. /* engine fetching ahead of us, we have space up until the
  234. * current GET pointer. the "- 1" is to ensure there's
  235. * space left to emit a jump back to the beginning of the
  236. * push buffer if we require it. we can never get GET == PUT
  237. * here, so this is safe.
  238. */
  239. chan->dma.free = get - chan->dma.cur - 1;
  240. }
  241. return 0;
  242. }