nouveau_channel.c 14 KB

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