nv50_fifo.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2012 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 "nouveau_drv.h"
  28. #include "nouveau_fifo.h"
  29. #include "nouveau_ramht.h"
  30. #include "nouveau_vm.h"
  31. struct nv50_fifo_priv {
  32. struct nouveau_fifo_priv base;
  33. struct nouveau_gpuobj *playlist[2];
  34. int cur_playlist;
  35. };
  36. struct nv50_fifo_chan {
  37. struct nouveau_fifo_chan base;
  38. };
  39. void
  40. nv50_fifo_playlist_update(struct drm_device *dev)
  41. {
  42. struct nv50_fifo_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  43. struct drm_nouveau_private *dev_priv = dev->dev_private;
  44. struct nouveau_gpuobj *cur;
  45. int i, p;
  46. cur = priv->playlist[priv->cur_playlist];
  47. priv->cur_playlist = !priv->cur_playlist;
  48. for (i = 0, p = 0; i < priv->base.channels; i++) {
  49. if (nv_rd32(dev, 0x002600 + (i * 4)) & 0x80000000)
  50. nv_wo32(cur, p++ * 4, i);
  51. }
  52. dev_priv->engine.instmem.flush(dev);
  53. nv_wr32(dev, 0x0032f4, cur->vinst >> 12);
  54. nv_wr32(dev, 0x0032ec, p);
  55. nv_wr32(dev, 0x002500, 0x00000101);
  56. }
  57. static int
  58. nv50_fifo_context_new(struct nouveau_channel *chan, int engine)
  59. {
  60. struct nv50_fifo_priv *priv = nv_engine(chan->dev, engine);
  61. struct nv50_fifo_chan *fctx;
  62. struct drm_device *dev = chan->dev;
  63. struct drm_nouveau_private *dev_priv = dev->dev_private;
  64. u64 ib_offset = chan->pushbuf_base + chan->dma.ib_base * 4;
  65. u64 instance = chan->ramin->vinst >> 12;
  66. unsigned long flags;
  67. int ret = 0, i;
  68. fctx = chan->engctx[engine] = kzalloc(sizeof(*fctx), GFP_KERNEL);
  69. if (!fctx)
  70. return -ENOMEM;
  71. atomic_inc(&chan->vm->engref[engine]);
  72. chan->user = ioremap(pci_resource_start(dev->pdev, 0) +
  73. NV50_USER(chan->id), PAGE_SIZE);
  74. if (!chan->user) {
  75. ret = -ENOMEM;
  76. goto error;
  77. }
  78. for (i = 0; i < 0x100; i += 4)
  79. nv_wo32(chan->ramin, i, 0x00000000);
  80. nv_wo32(chan->ramin, 0x3c, 0x403f6078);
  81. nv_wo32(chan->ramin, 0x40, 0x00000000);
  82. nv_wo32(chan->ramin, 0x44, 0x01003fff);
  83. nv_wo32(chan->ramin, 0x48, chan->pushbuf->cinst >> 4);
  84. nv_wo32(chan->ramin, 0x50, lower_32_bits(ib_offset));
  85. nv_wo32(chan->ramin, 0x54, upper_32_bits(ib_offset) |
  86. drm_order(chan->dma.ib_max + 1) << 16);
  87. nv_wo32(chan->ramin, 0x60, 0x7fffffff);
  88. nv_wo32(chan->ramin, 0x78, 0x00000000);
  89. nv_wo32(chan->ramin, 0x7c, 0x30000001);
  90. nv_wo32(chan->ramin, 0x80, ((chan->ramht->bits - 9) << 27) |
  91. (4 << 24) /* SEARCH_FULL */ |
  92. (chan->ramht->gpuobj->cinst >> 4));
  93. dev_priv->engine.instmem.flush(dev);
  94. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  95. nv_wr32(dev, 0x002600 + (chan->id * 4), 0x80000000 | instance);
  96. nv50_fifo_playlist_update(dev);
  97. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  98. error:
  99. if (ret)
  100. priv->base.base.context_del(chan, engine);
  101. return ret;
  102. }
  103. static bool
  104. nv50_fifo_kickoff(struct nouveau_channel *chan)
  105. {
  106. struct drm_device *dev = chan->dev;
  107. bool done = true;
  108. u32 me;
  109. /* HW bug workaround:
  110. *
  111. * PFIFO will hang forever if the connected engines don't report
  112. * that they've processed the context switch request.
  113. *
  114. * In order for the kickoff to work, we need to ensure all the
  115. * connected engines are in a state where they can answer.
  116. *
  117. * Newer chipsets don't seem to suffer from this issue, and well,
  118. * there's also a "ignore these engines" bitmask reg we can use
  119. * if we hit the issue there..
  120. */
  121. /* PME: make sure engine is enabled */
  122. me = nv_mask(dev, 0x00b860, 0x00000001, 0x00000001);
  123. /* do the kickoff... */
  124. nv_wr32(dev, 0x0032fc, chan->ramin->vinst >> 12);
  125. if (!nv_wait_ne(dev, 0x0032fc, 0xffffffff, 0xffffffff)) {
  126. NV_INFO(dev, "PFIFO: channel %d unload timeout\n", chan->id);
  127. done = false;
  128. }
  129. /* restore any engine states we changed, and exit */
  130. nv_wr32(dev, 0x00b860, me);
  131. return done;
  132. }
  133. static void
  134. nv50_fifo_context_del(struct nouveau_channel *chan, int engine)
  135. {
  136. struct nv50_fifo_chan *fctx = chan->engctx[engine];
  137. struct drm_device *dev = chan->dev;
  138. struct drm_nouveau_private *dev_priv = dev->dev_private;
  139. unsigned long flags;
  140. /* remove channel from playlist, will context switch if active */
  141. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  142. nv_mask(dev, 0x002600 + (chan->id * 4), 0x80000000, 0x00000000);
  143. nv50_fifo_playlist_update(dev);
  144. /* tell any engines on this channel to unload their contexts */
  145. nv50_fifo_kickoff(chan);
  146. nv_wr32(dev, 0x002600 + (chan->id * 4), 0x00000000);
  147. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  148. /* clean up */
  149. if (chan->user) {
  150. iounmap(chan->user);
  151. chan->user = NULL;
  152. }
  153. atomic_dec(&chan->vm->engref[engine]);
  154. chan->engctx[engine] = NULL;
  155. kfree(fctx);
  156. }
  157. static int
  158. nv50_fifo_init(struct drm_device *dev, int engine)
  159. {
  160. struct drm_nouveau_private *dev_priv = dev->dev_private;
  161. u32 instance;
  162. int i;
  163. nv_mask(dev, 0x000200, 0x00000100, 0x00000000);
  164. nv_mask(dev, 0x000200, 0x00000100, 0x00000100);
  165. nv_wr32(dev, 0x00250c, 0x6f3cfc34);
  166. nv_wr32(dev, 0x002044, 0x01003fff);
  167. nv_wr32(dev, 0x002100, 0xffffffff);
  168. nv_wr32(dev, 0x002140, 0xffffffff);
  169. for (i = 0; i < 128; i++) {
  170. struct nouveau_channel *chan = dev_priv->channels.ptr[i];
  171. if (chan && chan->engctx[engine])
  172. instance = 0x80000000 | chan->ramin->vinst >> 12;
  173. else
  174. instance = 0x00000000;
  175. nv_wr32(dev, 0x002600 + (i * 4), instance);
  176. }
  177. nv50_fifo_playlist_update(dev);
  178. nv_wr32(dev, 0x003200, 1);
  179. nv_wr32(dev, 0x003250, 1);
  180. nv_wr32(dev, 0x002500, 1);
  181. return 0;
  182. }
  183. static int
  184. nv50_fifo_fini(struct drm_device *dev, int engine, bool suspend)
  185. {
  186. struct drm_nouveau_private *dev_priv = dev->dev_private;
  187. struct nv50_fifo_priv *priv = nv_engine(dev, engine);
  188. int i;
  189. /* set playlist length to zero, fifo will unload context */
  190. nv_wr32(dev, 0x0032ec, 0);
  191. /* tell all connected engines to unload their contexts */
  192. for (i = 0; i < priv->base.channels; i++) {
  193. struct nouveau_channel *chan = dev_priv->channels.ptr[i];
  194. if (chan && !nv50_fifo_kickoff(chan))
  195. return -EBUSY;
  196. }
  197. nv_wr32(dev, 0x002140, 0);
  198. return 0;
  199. }
  200. void
  201. nv50_fifo_tlb_flush(struct drm_device *dev, int engine)
  202. {
  203. nv50_vm_flush_engine(dev, 5);
  204. }
  205. void
  206. nv50_fifo_destroy(struct drm_device *dev, int engine)
  207. {
  208. struct drm_nouveau_private *dev_priv = dev->dev_private;
  209. struct nv50_fifo_priv *priv = nv_engine(dev, engine);
  210. nouveau_irq_unregister(dev, 8);
  211. nouveau_gpuobj_ref(NULL, &priv->playlist[0]);
  212. nouveau_gpuobj_ref(NULL, &priv->playlist[1]);
  213. dev_priv->eng[engine] = NULL;
  214. kfree(priv);
  215. }
  216. int
  217. nv50_fifo_create(struct drm_device *dev)
  218. {
  219. struct drm_nouveau_private *dev_priv = dev->dev_private;
  220. struct nv50_fifo_priv *priv;
  221. int ret;
  222. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  223. if (!priv)
  224. return -ENOMEM;
  225. priv->base.base.destroy = nv50_fifo_destroy;
  226. priv->base.base.init = nv50_fifo_init;
  227. priv->base.base.fini = nv50_fifo_fini;
  228. priv->base.base.context_new = nv50_fifo_context_new;
  229. priv->base.base.context_del = nv50_fifo_context_del;
  230. priv->base.base.tlb_flush = nv50_fifo_tlb_flush;
  231. priv->base.channels = 127;
  232. dev_priv->eng[NVOBJ_ENGINE_FIFO] = &priv->base.base;
  233. ret = nouveau_gpuobj_new(dev, NULL, priv->base.channels * 4, 0x1000,
  234. NVOBJ_FLAG_ZERO_ALLOC, &priv->playlist[0]);
  235. if (ret)
  236. goto error;
  237. ret = nouveau_gpuobj_new(dev, NULL, priv->base.channels * 4, 0x1000,
  238. NVOBJ_FLAG_ZERO_ALLOC, &priv->playlist[1]);
  239. if (ret)
  240. goto error;
  241. nouveau_irq_register(dev, 8, nv04_fifo_isr);
  242. error:
  243. if (ret)
  244. priv->base.base.destroy(dev, NVOBJ_ENGINE_FIFO);
  245. return ret;
  246. }