nouveau_channel.c 14 KB

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