nouveau_dma.c 6.9 KB

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