nouveau_channel.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 "nouveau_ramht.h"
  30. #include "nouveau_software.h"
  31. static int
  32. nouveau_channel_pushbuf_init(struct nouveau_channel *chan)
  33. {
  34. u32 mem = nouveau_vram_pushbuf ? TTM_PL_FLAG_VRAM : TTM_PL_FLAG_TT;
  35. struct drm_device *dev = chan->dev;
  36. struct drm_nouveau_private *dev_priv = dev->dev_private;
  37. int ret;
  38. /* allocate buffer object */
  39. ret = nouveau_bo_new(dev, 65536, 0, mem, 0, 0, NULL, &chan->pushbuf_bo);
  40. if (ret)
  41. goto out;
  42. ret = nouveau_bo_pin(chan->pushbuf_bo, mem);
  43. if (ret)
  44. goto out;
  45. ret = nouveau_bo_map(chan->pushbuf_bo);
  46. if (ret)
  47. goto out;
  48. /* create DMA object covering the entire memtype where the push
  49. * buffer resides, userspace can submit its own push buffers from
  50. * anywhere within the same memtype.
  51. */
  52. chan->pushbuf_base = chan->pushbuf_bo->bo.offset;
  53. if (dev_priv->card_type >= NV_50) {
  54. ret = nouveau_bo_vma_add(chan->pushbuf_bo, chan->vm,
  55. &chan->pushbuf_vma);
  56. if (ret)
  57. goto out;
  58. if (dev_priv->card_type < NV_C0) {
  59. ret = nouveau_gpuobj_dma_new(chan,
  60. NV_CLASS_DMA_IN_MEMORY, 0,
  61. (1ULL << 40),
  62. NV_MEM_ACCESS_RO,
  63. NV_MEM_TARGET_VM,
  64. &chan->pushbuf);
  65. }
  66. chan->pushbuf_base = chan->pushbuf_vma.offset;
  67. } else
  68. if (chan->pushbuf_bo->bo.mem.mem_type == TTM_PL_TT) {
  69. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, 0,
  70. dev_priv->gart_info.aper_size,
  71. NV_MEM_ACCESS_RO,
  72. NV_MEM_TARGET_GART,
  73. &chan->pushbuf);
  74. } else
  75. if (dev_priv->card_type != NV_04) {
  76. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, 0,
  77. dev_priv->fb_available_size,
  78. NV_MEM_ACCESS_RO,
  79. NV_MEM_TARGET_VRAM,
  80. &chan->pushbuf);
  81. } else {
  82. /* NV04 cmdbuf hack, from original ddx.. not sure of it's
  83. * exact reason for existing :) PCI access to cmdbuf in
  84. * VRAM.
  85. */
  86. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  87. pci_resource_start(dev->pdev, 1),
  88. dev_priv->fb_available_size,
  89. NV_MEM_ACCESS_RO,
  90. NV_MEM_TARGET_PCI,
  91. &chan->pushbuf);
  92. }
  93. out:
  94. if (ret) {
  95. NV_ERROR(dev, "error initialising pushbuf: %d\n", ret);
  96. nouveau_bo_vma_del(chan->pushbuf_bo, &chan->pushbuf_vma);
  97. nouveau_gpuobj_ref(NULL, &chan->pushbuf);
  98. if (chan->pushbuf_bo) {
  99. nouveau_bo_unmap(chan->pushbuf_bo);
  100. nouveau_bo_ref(NULL, &chan->pushbuf_bo);
  101. }
  102. }
  103. return 0;
  104. }
  105. /* allocates and initializes a fifo for user space consumption */
  106. int
  107. nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret,
  108. struct drm_file *file_priv,
  109. uint32_t vram_handle, uint32_t gart_handle)
  110. {
  111. struct drm_nouveau_private *dev_priv = dev->dev_private;
  112. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  113. struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
  114. struct nouveau_channel *chan;
  115. unsigned long flags;
  116. int ret, i;
  117. /* allocate and lock channel structure */
  118. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  119. if (!chan)
  120. return -ENOMEM;
  121. chan->dev = dev;
  122. chan->file_priv = file_priv;
  123. chan->vram_handle = vram_handle;
  124. chan->gart_handle = gart_handle;
  125. kref_init(&chan->ref);
  126. atomic_set(&chan->users, 1);
  127. mutex_init(&chan->mutex);
  128. mutex_lock(&chan->mutex);
  129. /* allocate hw channel id */
  130. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  131. for (chan->id = 0; chan->id < pfifo->channels; chan->id++) {
  132. if (!dev_priv->channels.ptr[chan->id]) {
  133. nouveau_channel_ref(chan, &dev_priv->channels.ptr[chan->id]);
  134. break;
  135. }
  136. }
  137. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  138. if (chan->id == pfifo->channels) {
  139. mutex_unlock(&chan->mutex);
  140. kfree(chan);
  141. return -ENODEV;
  142. }
  143. NV_DEBUG(dev, "initialising channel %d\n", chan->id);
  144. INIT_LIST_HEAD(&chan->fence.pending);
  145. spin_lock_init(&chan->fence.lock);
  146. /* setup channel's memory and vm */
  147. ret = nouveau_gpuobj_channel_init(chan, vram_handle, gart_handle);
  148. if (ret) {
  149. NV_ERROR(dev, "gpuobj %d\n", ret);
  150. nouveau_channel_put(&chan);
  151. return ret;
  152. }
  153. /* Allocate space for per-channel fixed notifier memory */
  154. ret = nouveau_notifier_init_channel(chan);
  155. if (ret) {
  156. NV_ERROR(dev, "ntfy %d\n", ret);
  157. nouveau_channel_put(&chan);
  158. return ret;
  159. }
  160. /* Allocate DMA push buffer */
  161. ret = nouveau_channel_pushbuf_init(chan);
  162. if (ret) {
  163. NV_ERROR(dev, "pushbuf %d\n", ret);
  164. nouveau_channel_put(&chan);
  165. return ret;
  166. }
  167. nouveau_dma_init(chan);
  168. chan->user_put = 0x40;
  169. chan->user_get = 0x44;
  170. if (dev_priv->card_type >= NV_50)
  171. chan->user_get_hi = 0x60;
  172. /* disable the fifo caches */
  173. pfifo->reassign(dev, false);
  174. /* Construct initial RAMFC for new channel */
  175. ret = pfifo->create_context(chan);
  176. if (ret) {
  177. nouveau_channel_put(&chan);
  178. return ret;
  179. }
  180. pfifo->reassign(dev, true);
  181. /* Insert NOPs for NOUVEAU_DMA_SKIPS */
  182. ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
  183. if (ret) {
  184. nouveau_channel_put(&chan);
  185. return ret;
  186. }
  187. for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
  188. OUT_RING (chan, 0x00000000);
  189. FIRE_RING(chan);
  190. ret = nouveau_gpuobj_gr_new(chan, NvSw, nouveau_software_class(dev));
  191. if (ret) {
  192. nouveau_channel_put(&chan);
  193. return ret;
  194. }
  195. ret = nouveau_fence_channel_init(chan);
  196. if (ret) {
  197. nouveau_channel_put(&chan);
  198. return ret;
  199. }
  200. nouveau_debugfs_channel_init(chan);
  201. NV_DEBUG(dev, "channel %d initialised\n", chan->id);
  202. if (fpriv) {
  203. spin_lock(&fpriv->lock);
  204. list_add(&chan->list, &fpriv->channels);
  205. spin_unlock(&fpriv->lock);
  206. }
  207. *chan_ret = chan;
  208. return 0;
  209. }
  210. struct nouveau_channel *
  211. nouveau_channel_get_unlocked(struct nouveau_channel *ref)
  212. {
  213. struct nouveau_channel *chan = NULL;
  214. if (likely(ref && atomic_inc_not_zero(&ref->users)))
  215. nouveau_channel_ref(ref, &chan);
  216. return chan;
  217. }
  218. struct nouveau_channel *
  219. nouveau_channel_get(struct drm_file *file_priv, int id)
  220. {
  221. struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
  222. struct nouveau_channel *chan;
  223. spin_lock(&fpriv->lock);
  224. list_for_each_entry(chan, &fpriv->channels, list) {
  225. if (chan->id == id) {
  226. chan = nouveau_channel_get_unlocked(chan);
  227. spin_unlock(&fpriv->lock);
  228. mutex_lock(&chan->mutex);
  229. return chan;
  230. }
  231. }
  232. spin_unlock(&fpriv->lock);
  233. return ERR_PTR(-EINVAL);
  234. }
  235. void
  236. nouveau_channel_put_unlocked(struct nouveau_channel **pchan)
  237. {
  238. struct nouveau_channel *chan = *pchan;
  239. struct drm_device *dev = chan->dev;
  240. struct drm_nouveau_private *dev_priv = dev->dev_private;
  241. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  242. unsigned long flags;
  243. int i;
  244. /* decrement the refcount, and we're done if there's still refs */
  245. if (likely(!atomic_dec_and_test(&chan->users))) {
  246. nouveau_channel_ref(NULL, pchan);
  247. return;
  248. }
  249. /* no one wants the channel anymore */
  250. NV_DEBUG(dev, "freeing channel %d\n", chan->id);
  251. nouveau_debugfs_channel_fini(chan);
  252. /* give it chance to idle */
  253. nouveau_channel_idle(chan);
  254. /* ensure all outstanding fences are signaled. they should be if the
  255. * above attempts at idling were OK, but if we failed this'll tell TTM
  256. * we're done with the buffers.
  257. */
  258. nouveau_fence_channel_fini(chan);
  259. /* boot it off the hardware */
  260. pfifo->reassign(dev, false);
  261. /* destroy the engine specific contexts */
  262. pfifo->destroy_context(chan);
  263. for (i = 0; i < NVOBJ_ENGINE_NR; i++) {
  264. if (chan->engctx[i])
  265. dev_priv->eng[i]->context_del(chan, i);
  266. }
  267. pfifo->reassign(dev, true);
  268. /* aside from its resources, the channel should now be dead,
  269. * remove it from the channel list
  270. */
  271. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  272. nouveau_channel_ref(NULL, &dev_priv->channels.ptr[chan->id]);
  273. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  274. /* destroy any resources the channel owned */
  275. nouveau_gpuobj_ref(NULL, &chan->pushbuf);
  276. if (chan->pushbuf_bo) {
  277. nouveau_bo_vma_del(chan->pushbuf_bo, &chan->pushbuf_vma);
  278. nouveau_bo_unmap(chan->pushbuf_bo);
  279. nouveau_bo_unpin(chan->pushbuf_bo);
  280. nouveau_bo_ref(NULL, &chan->pushbuf_bo);
  281. }
  282. nouveau_ramht_ref(NULL, &chan->ramht, chan);
  283. nouveau_notifier_takedown_channel(chan);
  284. nouveau_gpuobj_channel_takedown(chan);
  285. nouveau_channel_ref(NULL, pchan);
  286. }
  287. void
  288. nouveau_channel_put(struct nouveau_channel **pchan)
  289. {
  290. mutex_unlock(&(*pchan)->mutex);
  291. nouveau_channel_put_unlocked(pchan);
  292. }
  293. static void
  294. nouveau_channel_del(struct kref *ref)
  295. {
  296. struct nouveau_channel *chan =
  297. container_of(ref, struct nouveau_channel, ref);
  298. kfree(chan);
  299. }
  300. void
  301. nouveau_channel_ref(struct nouveau_channel *chan,
  302. struct nouveau_channel **pchan)
  303. {
  304. if (chan)
  305. kref_get(&chan->ref);
  306. if (*pchan)
  307. kref_put(&(*pchan)->ref, nouveau_channel_del);
  308. *pchan = chan;
  309. }
  310. void
  311. nouveau_channel_idle(struct nouveau_channel *chan)
  312. {
  313. struct drm_device *dev = chan->dev;
  314. struct nouveau_fence *fence = NULL;
  315. int ret;
  316. nouveau_fence_update(chan);
  317. if (chan->fence.sequence != chan->fence.sequence_ack) {
  318. ret = nouveau_fence_new(chan, &fence, true);
  319. if (!ret) {
  320. ret = nouveau_fence_wait(fence, false, false);
  321. nouveau_fence_unref(&fence);
  322. }
  323. if (ret)
  324. NV_ERROR(dev, "Failed to idle channel %d.\n", chan->id);
  325. }
  326. }
  327. /* cleans up all the fifos from file_priv */
  328. void
  329. nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv)
  330. {
  331. struct drm_nouveau_private *dev_priv = dev->dev_private;
  332. struct nouveau_engine *engine = &dev_priv->engine;
  333. struct nouveau_channel *chan;
  334. int i;
  335. NV_DEBUG(dev, "clearing FIFO enables from file_priv\n");
  336. for (i = 0; i < engine->fifo.channels; i++) {
  337. chan = nouveau_channel_get(file_priv, i);
  338. if (IS_ERR(chan))
  339. continue;
  340. list_del(&chan->list);
  341. atomic_dec(&chan->users);
  342. nouveau_channel_put(&chan);
  343. }
  344. }
  345. /***********************************
  346. * ioctls wrapping the functions
  347. ***********************************/
  348. static int
  349. nouveau_ioctl_fifo_alloc(struct drm_device *dev, void *data,
  350. struct drm_file *file_priv)
  351. {
  352. struct drm_nouveau_private *dev_priv = dev->dev_private;
  353. struct drm_nouveau_channel_alloc *init = data;
  354. struct nouveau_channel *chan;
  355. int ret;
  356. if (!dev_priv->eng[NVOBJ_ENGINE_GR])
  357. return -ENODEV;
  358. if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
  359. return -EINVAL;
  360. ret = nouveau_channel_alloc(dev, &chan, file_priv,
  361. init->fb_ctxdma_handle,
  362. init->tt_ctxdma_handle);
  363. if (ret)
  364. return ret;
  365. init->channel = chan->id;
  366. if (nouveau_vram_pushbuf == 0) {
  367. if (chan->dma.ib_max)
  368. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
  369. NOUVEAU_GEM_DOMAIN_GART;
  370. else if (chan->pushbuf_bo->bo.mem.mem_type == TTM_PL_VRAM)
  371. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
  372. else
  373. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
  374. } else {
  375. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
  376. }
  377. if (dev_priv->card_type < NV_C0) {
  378. init->subchan[0].handle = 0x00000000;
  379. init->subchan[0].grclass = 0x0000;
  380. init->subchan[1].handle = NvSw;
  381. init->subchan[1].grclass = NV_SW;
  382. init->nr_subchan = 2;
  383. }
  384. /* Named memory object area */
  385. ret = drm_gem_handle_create(file_priv, chan->notifier_bo->gem,
  386. &init->notifier_handle);
  387. if (ret == 0)
  388. atomic_inc(&chan->users); /* userspace reference */
  389. nouveau_channel_put(&chan);
  390. return ret;
  391. }
  392. static int
  393. nouveau_ioctl_fifo_free(struct drm_device *dev, void *data,
  394. struct drm_file *file_priv)
  395. {
  396. struct drm_nouveau_channel_free *req = data;
  397. struct nouveau_channel *chan;
  398. chan = nouveau_channel_get(file_priv, req->channel);
  399. if (IS_ERR(chan))
  400. return PTR_ERR(chan);
  401. list_del(&chan->list);
  402. atomic_dec(&chan->users);
  403. nouveau_channel_put(&chan);
  404. return 0;
  405. }
  406. /***********************************
  407. * finally, the ioctl table
  408. ***********************************/
  409. struct drm_ioctl_desc nouveau_ioctls[] = {
  410. DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH),
  411. DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  412. DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_ioctl_fifo_alloc, DRM_UNLOCKED|DRM_AUTH),
  413. DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_ioctl_fifo_free, DRM_UNLOCKED|DRM_AUTH),
  414. DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH),
  415. DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_ioctl_notifier_alloc, DRM_UNLOCKED|DRM_AUTH),
  416. DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH),
  417. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH),
  418. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH),
  419. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
  420. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
  421. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH),
  422. };
  423. int nouveau_max_ioctl = DRM_ARRAY_SIZE(nouveau_ioctls);