nouveau_channel.c 13 KB

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