nouveau_dma.c 6.9 KB

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