nouveau_channel.c 13 KB

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