nouveau_dma.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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_pre_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. int
  51. nouveau_dma_init(struct nouveau_channel *chan)
  52. {
  53. struct drm_device *dev = chan->dev;
  54. struct drm_nouveau_private *dev_priv = dev->dev_private;
  55. int ret, i;
  56. if (dev_priv->card_type >= NV_C0) {
  57. ret = nouveau_gpuobj_gr_new(chan, 0x9039, 0x9039);
  58. if (ret)
  59. return ret;
  60. ret = RING_SPACE(chan, 2);
  61. if (ret)
  62. return ret;
  63. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0000, 1);
  64. OUT_RING (chan, 0x00009039);
  65. FIRE_RING (chan);
  66. return 0;
  67. }
  68. /* Create NV_MEMORY_TO_MEMORY_FORMAT for buffer moves */
  69. ret = nouveau_gpuobj_gr_new(chan, NvM2MF, dev_priv->card_type < NV_50 ?
  70. 0x0039 : 0x5039);
  71. if (ret)
  72. return ret;
  73. /* NV_MEMORY_TO_MEMORY_FORMAT requires a notifier object */
  74. ret = nouveau_notifier_alloc(chan, NvNotify0, 32, 0xfe0, 0x1000,
  75. &chan->m2mf_ntfy);
  76. if (ret)
  77. return ret;
  78. /* Insert NOPS for NOUVEAU_DMA_SKIPS */
  79. ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
  80. if (ret)
  81. return ret;
  82. for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
  83. OUT_RING(chan, 0);
  84. /* Initialise NV_MEMORY_TO_MEMORY_FORMAT */
  85. ret = RING_SPACE(chan, 6);
  86. if (ret)
  87. return ret;
  88. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NAME, 1);
  89. OUT_RING (chan, NvM2MF);
  90. BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_NOTIFY, 3);
  91. OUT_RING (chan, NvNotify0);
  92. OUT_RING (chan, chan->vram_handle);
  93. OUT_RING (chan, chan->gart_handle);
  94. /* Sit back and pray the channel works.. */
  95. FIRE_RING(chan);
  96. return 0;
  97. }
  98. void
  99. OUT_RINGp(struct nouveau_channel *chan, const void *data, unsigned nr_dwords)
  100. {
  101. bool is_iomem;
  102. u32 *mem = ttm_kmap_obj_virtual(&chan->pushbuf_bo->kmap, &is_iomem);
  103. mem = &mem[chan->dma.cur];
  104. if (is_iomem)
  105. memcpy_toio((void __force __iomem *)mem, data, nr_dwords * 4);
  106. else
  107. memcpy(mem, data, nr_dwords * 4);
  108. chan->dma.cur += nr_dwords;
  109. }
  110. /* Fetch and adjust GPU GET pointer
  111. *
  112. * Returns:
  113. * value >= 0, the adjusted GET pointer
  114. * -EINVAL if GET pointer currently outside main push buffer
  115. * -EBUSY if timeout exceeded
  116. */
  117. static inline int
  118. READ_GET(struct nouveau_channel *chan, uint64_t *prev_get, int *timeout)
  119. {
  120. uint64_t val;
  121. val = nvchan_rd32(chan, chan->user_get);
  122. if (chan->user_get_hi)
  123. val |= (uint64_t)nvchan_rd32(chan, chan->user_get_hi) << 32;
  124. /* reset counter as long as GET is still advancing, this is
  125. * to avoid misdetecting a GPU lockup if the GPU happens to
  126. * just be processing an operation that takes a long time
  127. */
  128. if (val != *prev_get) {
  129. *prev_get = val;
  130. *timeout = 0;
  131. }
  132. if ((++*timeout & 0xff) == 0) {
  133. DRM_UDELAY(1);
  134. if (*timeout > 100000)
  135. return -EBUSY;
  136. }
  137. if (val < chan->pushbuf_base ||
  138. val > chan->pushbuf_base + (chan->dma.max << 2))
  139. return -EINVAL;
  140. return (val - chan->pushbuf_base) >> 2;
  141. }
  142. void
  143. nv50_dma_push(struct nouveau_channel *chan, struct nouveau_bo *bo,
  144. int delta, int length)
  145. {
  146. struct nouveau_bo *pb = chan->pushbuf_bo;
  147. struct nouveau_vma *vma;
  148. int ip = (chan->dma.ib_put * 2) + chan->dma.ib_base;
  149. u64 offset;
  150. vma = nouveau_bo_vma_find(bo, chan->vm);
  151. BUG_ON(!vma);
  152. offset = vma->offset + delta;
  153. BUG_ON(chan->dma.ib_free < 1);
  154. nouveau_bo_wr32(pb, ip++, lower_32_bits(offset));
  155. nouveau_bo_wr32(pb, ip++, upper_32_bits(offset) | length << 8);
  156. chan->dma.ib_put = (chan->dma.ib_put + 1) & chan->dma.ib_max;
  157. DRM_MEMORYBARRIER();
  158. /* Flush writes. */
  159. nouveau_bo_rd32(pb, 0);
  160. nvchan_wr32(chan, 0x8c, chan->dma.ib_put);
  161. chan->dma.ib_free--;
  162. }
  163. static int
  164. nv50_dma_push_wait(struct nouveau_channel *chan, int count)
  165. {
  166. uint32_t cnt = 0, prev_get = 0;
  167. while (chan->dma.ib_free < count) {
  168. uint32_t get = nvchan_rd32(chan, 0x88);
  169. if (get != prev_get) {
  170. prev_get = get;
  171. cnt = 0;
  172. }
  173. if ((++cnt & 0xff) == 0) {
  174. DRM_UDELAY(1);
  175. if (cnt > 100000)
  176. return -EBUSY;
  177. }
  178. chan->dma.ib_free = get - chan->dma.ib_put;
  179. if (chan->dma.ib_free <= 0)
  180. chan->dma.ib_free += chan->dma.ib_max;
  181. }
  182. return 0;
  183. }
  184. static int
  185. nv50_dma_wait(struct nouveau_channel *chan, int slots, int count)
  186. {
  187. uint64_t prev_get = 0;
  188. int ret, cnt = 0;
  189. ret = nv50_dma_push_wait(chan, slots + 1);
  190. if (unlikely(ret))
  191. return ret;
  192. while (chan->dma.free < count) {
  193. int get = READ_GET(chan, &prev_get, &cnt);
  194. if (unlikely(get < 0)) {
  195. if (get == -EINVAL)
  196. continue;
  197. return get;
  198. }
  199. if (get <= chan->dma.cur) {
  200. chan->dma.free = chan->dma.max - chan->dma.cur;
  201. if (chan->dma.free >= count)
  202. break;
  203. FIRE_RING(chan);
  204. do {
  205. get = READ_GET(chan, &prev_get, &cnt);
  206. if (unlikely(get < 0)) {
  207. if (get == -EINVAL)
  208. continue;
  209. return get;
  210. }
  211. } while (get == 0);
  212. chan->dma.cur = 0;
  213. chan->dma.put = 0;
  214. }
  215. chan->dma.free = get - chan->dma.cur - 1;
  216. }
  217. return 0;
  218. }
  219. int
  220. nouveau_dma_wait(struct nouveau_channel *chan, int slots, int size)
  221. {
  222. uint64_t prev_get = 0;
  223. int cnt = 0, get;
  224. if (chan->dma.ib_max)
  225. return nv50_dma_wait(chan, slots, size);
  226. while (chan->dma.free < size) {
  227. get = READ_GET(chan, &prev_get, &cnt);
  228. if (unlikely(get == -EBUSY))
  229. return -EBUSY;
  230. /* loop until we have a usable GET pointer. the value
  231. * we read from the GPU may be outside the main ring if
  232. * PFIFO is processing a buffer called from the main ring,
  233. * discard these values until something sensible is seen.
  234. *
  235. * the other case we discard GET is while the GPU is fetching
  236. * from the SKIPS area, so the code below doesn't have to deal
  237. * with some fun corner cases.
  238. */
  239. if (unlikely(get == -EINVAL) || get < NOUVEAU_DMA_SKIPS)
  240. continue;
  241. if (get <= chan->dma.cur) {
  242. /* engine is fetching behind us, or is completely
  243. * idle (GET == PUT) so we have free space up until
  244. * the end of the push buffer
  245. *
  246. * we can only hit that path once per call due to
  247. * looping back to the beginning of the push buffer,
  248. * we'll hit the fetching-ahead-of-us path from that
  249. * point on.
  250. *
  251. * the *one* exception to that rule is if we read
  252. * GET==PUT, in which case the below conditional will
  253. * always succeed and break us out of the wait loop.
  254. */
  255. chan->dma.free = chan->dma.max - chan->dma.cur;
  256. if (chan->dma.free >= size)
  257. break;
  258. /* not enough space left at the end of the push buffer,
  259. * instruct the GPU to jump back to the start right
  260. * after processing the currently pending commands.
  261. */
  262. OUT_RING(chan, chan->pushbuf_base | 0x20000000);
  263. /* wait for GET to depart from the skips area.
  264. * prevents writing GET==PUT and causing a race
  265. * condition that causes us to think the GPU is
  266. * idle when it's not.
  267. */
  268. do {
  269. get = READ_GET(chan, &prev_get, &cnt);
  270. if (unlikely(get == -EBUSY))
  271. return -EBUSY;
  272. if (unlikely(get == -EINVAL))
  273. continue;
  274. } while (get <= NOUVEAU_DMA_SKIPS);
  275. WRITE_PUT(NOUVEAU_DMA_SKIPS);
  276. /* we're now submitting commands at the start of
  277. * the push buffer.
  278. */
  279. chan->dma.cur =
  280. chan->dma.put = NOUVEAU_DMA_SKIPS;
  281. }
  282. /* engine fetching ahead of us, we have space up until the
  283. * current GET pointer. the "- 1" is to ensure there's
  284. * space left to emit a jump back to the beginning of the
  285. * push buffer if we require it. we can never get GET == PUT
  286. * here, so this is safe.
  287. */
  288. chan->dma.free = get - chan->dma.cur - 1;
  289. }
  290. return 0;
  291. }