nouveau_channel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright 2005-2006 Stephane Marchesin
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #include "drmP.h"
  25. #include "drm.h"
  26. #include "nouveau_drv.h"
  27. #include <nouveau_drm.h>
  28. #include "nouveau_dma.h"
  29. #include <engine/fifo.h>
  30. #include <core/ramht.h>
  31. #include "nouveau_fence.h"
  32. #include "nouveau_software.h"
  33. MODULE_PARM_DESC(vram_pushbuf, "Force DMA push buffers to be in VRAM");
  34. int nouveau_vram_pushbuf;
  35. module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
  36. static int
  37. nouveau_channel_pushbuf_init(struct nouveau_channel *chan)
  38. {
  39. u32 mem = nouveau_vram_pushbuf ? TTM_PL_FLAG_VRAM : TTM_PL_FLAG_TT;
  40. struct drm_device *dev = chan->dev;
  41. struct drm_nouveau_private *dev_priv = dev->dev_private;
  42. int ret;
  43. /* allocate buffer object */
  44. ret = nouveau_bo_new(dev, 65536, 0, mem, 0, 0, NULL, &chan->pushbuf_bo);
  45. if (ret)
  46. goto out;
  47. ret = nouveau_bo_pin(chan->pushbuf_bo, mem);
  48. if (ret)
  49. goto out;
  50. ret = nouveau_bo_map(chan->pushbuf_bo);
  51. if (ret)
  52. goto out;
  53. /* create DMA object covering the entire memtype where the push
  54. * buffer resides, userspace can submit its own push buffers from
  55. * anywhere within the same memtype.
  56. */
  57. chan->pushbuf_base = chan->pushbuf_bo->bo.offset;
  58. if (dev_priv->card_type >= NV_50) {
  59. ret = nouveau_bo_vma_add(chan->pushbuf_bo, chan->vm,
  60. &chan->pushbuf_vma);
  61. if (ret)
  62. goto out;
  63. if (dev_priv->card_type < NV_C0) {
  64. ret = nouveau_gpuobj_dma_new(chan,
  65. NV_CLASS_DMA_IN_MEMORY, 0,
  66. (1ULL << 40),
  67. NV_MEM_ACCESS_RO,
  68. NV_MEM_TARGET_VM,
  69. &chan->pushbuf);
  70. }
  71. chan->pushbuf_base = chan->pushbuf_vma.offset;
  72. } else
  73. if (chan->pushbuf_bo->bo.mem.mem_type == TTM_PL_TT) {
  74. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, 0,
  75. dev_priv->gart_info.aper_size,
  76. NV_MEM_ACCESS_RO,
  77. NV_MEM_TARGET_GART,
  78. &chan->pushbuf);
  79. } else
  80. if (dev_priv->card_type != NV_04) {
  81. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, 0,
  82. dev_priv->fb_available_size,
  83. NV_MEM_ACCESS_RO,
  84. NV_MEM_TARGET_VRAM,
  85. &chan->pushbuf);
  86. } else {
  87. /* NV04 cmdbuf hack, from original ddx.. not sure of it's
  88. * exact reason for existing :) PCI access to cmdbuf in
  89. * VRAM.
  90. */
  91. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  92. pci_resource_start(dev->pdev, 1),
  93. dev_priv->fb_available_size,
  94. NV_MEM_ACCESS_RO,
  95. NV_MEM_TARGET_PCI,
  96. &chan->pushbuf);
  97. }
  98. out:
  99. if (ret) {
  100. NV_ERROR(dev, "error initialising pushbuf: %d\n", ret);
  101. nouveau_bo_vma_del(chan->pushbuf_bo, &chan->pushbuf_vma);
  102. nouveau_gpuobj_ref(NULL, &chan->pushbuf);
  103. if (chan->pushbuf_bo) {
  104. nouveau_bo_unmap(chan->pushbuf_bo);
  105. nouveau_bo_ref(NULL, &chan->pushbuf_bo);
  106. }
  107. }
  108. return 0;
  109. }
  110. /* allocates and initializes a fifo for user space consumption */
  111. int
  112. nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret,
  113. struct drm_file *file_priv,
  114. uint32_t vram_handle, uint32_t gart_handle)
  115. {
  116. struct drm_nouveau_private *dev_priv = dev->dev_private;
  117. struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  118. struct nouveau_fence_priv *fence = dev_priv->fence.func;
  119. struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
  120. struct nouveau_channel *chan;
  121. unsigned long flags;
  122. int ret, i;
  123. /* allocate and lock channel structure */
  124. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  125. if (!chan)
  126. return -ENOMEM;
  127. chan->dev = dev;
  128. chan->file_priv = file_priv;
  129. chan->vram_handle = vram_handle;
  130. chan->gart_handle = gart_handle;
  131. kref_init(&chan->ref);
  132. atomic_set(&chan->users, 1);
  133. mutex_init(&chan->mutex);
  134. mutex_lock(&chan->mutex);
  135. /* allocate hw channel id */
  136. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  137. for (chan->id = 0; chan->id < pfifo->channels; chan->id++) {
  138. if ( dev_priv->card_type == NV_50 && chan->id == 0)
  139. continue;
  140. if (!dev_priv->channels.ptr[chan->id]) {
  141. nouveau_channel_ref(chan, &dev_priv->channels.ptr[chan->id]);
  142. break;
  143. }
  144. }
  145. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  146. if (chan->id == pfifo->channels) {
  147. mutex_unlock(&chan->mutex);
  148. kfree(chan);
  149. return -ENODEV;
  150. }
  151. NV_DEBUG(dev, "initialising channel %d\n", chan->id);
  152. /* setup channel's memory and vm */
  153. ret = nouveau_gpuobj_channel_init(chan, vram_handle, gart_handle);
  154. if (ret) {
  155. NV_ERROR(dev, "gpuobj %d\n", ret);
  156. nouveau_channel_put(&chan);
  157. return ret;
  158. }
  159. /* Allocate space for per-channel fixed notifier memory */
  160. ret = nouveau_notifier_init_channel(chan);
  161. if (ret) {
  162. NV_ERROR(dev, "ntfy %d\n", ret);
  163. nouveau_channel_put(&chan);
  164. return ret;
  165. }
  166. /* Allocate DMA push buffer */
  167. ret = nouveau_channel_pushbuf_init(chan);
  168. if (ret) {
  169. NV_ERROR(dev, "pushbuf %d\n", ret);
  170. nouveau_channel_put(&chan);
  171. return ret;
  172. }
  173. nouveau_dma_init(chan);
  174. chan->user_put = 0x40;
  175. chan->user_get = 0x44;
  176. if (dev_priv->card_type >= NV_50)
  177. chan->user_get_hi = 0x60;
  178. /* create fifo context */
  179. ret = pfifo->base.context_new(chan, NVOBJ_ENGINE_FIFO);
  180. if (ret) {
  181. nouveau_channel_put(&chan);
  182. return ret;
  183. }
  184. /* Insert NOPs for NOUVEAU_DMA_SKIPS */
  185. ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
  186. if (ret) {
  187. nouveau_channel_put(&chan);
  188. return ret;
  189. }
  190. for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
  191. OUT_RING (chan, 0x00000000);
  192. ret = nouveau_gpuobj_gr_new(chan, NvSw, nouveau_software_class(dev));
  193. if (ret) {
  194. nouveau_channel_put(&chan);
  195. return ret;
  196. }
  197. if (dev_priv->card_type < NV_C0) {
  198. ret = RING_SPACE(chan, 2);
  199. if (ret) {
  200. nouveau_channel_put(&chan);
  201. return ret;
  202. }
  203. BEGIN_NV04(chan, NvSubSw, NV01_SUBCHAN_OBJECT, 1);
  204. OUT_RING (chan, NvSw);
  205. FIRE_RING (chan);
  206. }
  207. FIRE_RING(chan);
  208. ret = fence->context_new(chan);
  209. if (ret) {
  210. nouveau_channel_put(&chan);
  211. return ret;
  212. }
  213. nouveau_debugfs_channel_init(chan);
  214. NV_DEBUG(dev, "channel %d initialised\n", chan->id);
  215. if (fpriv) {
  216. spin_lock(&fpriv->lock);
  217. list_add(&chan->list, &fpriv->channels);
  218. spin_unlock(&fpriv->lock);
  219. }
  220. *chan_ret = chan;
  221. return 0;
  222. }
  223. struct nouveau_channel *
  224. nouveau_channel_get_unlocked(struct nouveau_channel *ref)
  225. {
  226. struct nouveau_channel *chan = NULL;
  227. if (likely(ref && atomic_inc_not_zero(&ref->users)))
  228. nouveau_channel_ref(ref, &chan);
  229. return chan;
  230. }
  231. struct nouveau_channel *
  232. nouveau_channel_get(struct drm_file *file_priv, int id)
  233. {
  234. struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
  235. struct nouveau_channel *chan;
  236. spin_lock(&fpriv->lock);
  237. list_for_each_entry(chan, &fpriv->channels, list) {
  238. if (chan->id == id) {
  239. chan = nouveau_channel_get_unlocked(chan);
  240. spin_unlock(&fpriv->lock);
  241. mutex_lock(&chan->mutex);
  242. return chan;
  243. }
  244. }
  245. spin_unlock(&fpriv->lock);
  246. return ERR_PTR(-EINVAL);
  247. }
  248. void
  249. nouveau_channel_put_unlocked(struct nouveau_channel **pchan)
  250. {
  251. struct nouveau_channel *chan = *pchan;
  252. struct drm_device *dev = chan->dev;
  253. struct drm_nouveau_private *dev_priv = dev->dev_private;
  254. struct nouveau_fence_priv *fence = dev_priv->fence.func;
  255. unsigned long flags;
  256. int i;
  257. /* decrement the refcount, and we're done if there's still refs */
  258. if (likely(!atomic_dec_and_test(&chan->users))) {
  259. nouveau_channel_ref(NULL, pchan);
  260. return;
  261. }
  262. /* no one wants the channel anymore */
  263. NV_DEBUG(dev, "freeing channel %d\n", chan->id);
  264. nouveau_debugfs_channel_fini(chan);
  265. /* give it chance to idle */
  266. nouveau_channel_idle(chan);
  267. /* destroy the engine specific contexts */
  268. for (i = NVOBJ_ENGINE_NR - 1; i >= 0; i--) {
  269. if (chan->engctx[i])
  270. dev_priv->eng[i]->context_del(chan, i);
  271. }
  272. if (chan->fence)
  273. fence->context_del(chan);
  274. /* aside from its resources, the channel should now be dead,
  275. * remove it from the channel list
  276. */
  277. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  278. nouveau_channel_ref(NULL, &dev_priv->channels.ptr[chan->id]);
  279. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  280. /* destroy any resources the channel owned */
  281. nouveau_gpuobj_ref(NULL, &chan->pushbuf);
  282. if (chan->pushbuf_bo) {
  283. nouveau_bo_vma_del(chan->pushbuf_bo, &chan->pushbuf_vma);
  284. nouveau_bo_unmap(chan->pushbuf_bo);
  285. nouveau_bo_unpin(chan->pushbuf_bo);
  286. nouveau_bo_ref(NULL, &chan->pushbuf_bo);
  287. }
  288. nouveau_ramht_ref(NULL, &chan->ramht, chan);
  289. nouveau_notifier_takedown_channel(chan);
  290. nouveau_gpuobj_channel_takedown(chan);
  291. nouveau_channel_ref(NULL, pchan);
  292. }
  293. void
  294. nouveau_channel_put(struct nouveau_channel **pchan)
  295. {
  296. mutex_unlock(&(*pchan)->mutex);
  297. nouveau_channel_put_unlocked(pchan);
  298. }
  299. static void
  300. nouveau_channel_del(struct kref *ref)
  301. {
  302. struct nouveau_channel *chan =
  303. container_of(ref, struct nouveau_channel, ref);
  304. kfree(chan);
  305. }
  306. void
  307. nouveau_channel_ref(struct nouveau_channel *chan,
  308. struct nouveau_channel **pchan)
  309. {
  310. if (chan)
  311. kref_get(&chan->ref);
  312. if (*pchan)
  313. kref_put(&(*pchan)->ref, nouveau_channel_del);
  314. *pchan = chan;
  315. }
  316. int
  317. nouveau_channel_idle(struct nouveau_channel *chan)
  318. {
  319. struct drm_device *dev = chan->dev;
  320. struct nouveau_fence *fence = NULL;
  321. int ret;
  322. ret = nouveau_fence_new(chan, &fence);
  323. if (!ret) {
  324. ret = nouveau_fence_wait(fence, false, false);
  325. nouveau_fence_unref(&fence);
  326. }
  327. if (ret)
  328. NV_ERROR(dev, "Failed to idle channel %d.\n", chan->id);
  329. return ret;
  330. }
  331. /* cleans up all the fifos from file_priv */
  332. void
  333. nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv)
  334. {
  335. struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  336. struct nouveau_channel *chan;
  337. int i;
  338. if (!pfifo)
  339. return;
  340. NV_DEBUG(dev, "clearing FIFO enables from file_priv\n");
  341. for (i = 0; i < pfifo->channels; i++) {
  342. chan = nouveau_channel_get(file_priv, i);
  343. if (IS_ERR(chan))
  344. continue;
  345. list_del(&chan->list);
  346. atomic_dec(&chan->users);
  347. nouveau_channel_put(&chan);
  348. }
  349. }