nouveau_channel.c 14 KB

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