nouveau_dma.c 6.9 KB

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