nouveau_dma.c 9.1 KB

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