nouveau_channel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. atomic_set(&chan->refcount, 1);
  117. mutex_init(&chan->mutex);
  118. mutex_lock(&chan->mutex);
  119. /* allocate hw channel id */
  120. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  121. for (chan->id = 0; chan->id < pfifo->channels; chan->id++) {
  122. if (!dev_priv->channels.ptr[chan->id]) {
  123. dev_priv->channels.ptr[chan->id] = chan;
  124. break;
  125. }
  126. }
  127. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  128. if (chan->id == pfifo->channels) {
  129. mutex_unlock(&chan->mutex);
  130. kfree(chan);
  131. return -ENODEV;
  132. }
  133. NV_DEBUG(dev, "initialising channel %d\n", chan->id);
  134. INIT_LIST_HEAD(&chan->nvsw.vbl_wait);
  135. INIT_LIST_HEAD(&chan->fence.pending);
  136. /* Allocate DMA push buffer */
  137. chan->pushbuf_bo = nouveau_channel_user_pushbuf_alloc(dev);
  138. if (!chan->pushbuf_bo) {
  139. ret = -ENOMEM;
  140. NV_ERROR(dev, "pushbuf %d\n", ret);
  141. nouveau_channel_put(&chan);
  142. return ret;
  143. }
  144. nouveau_dma_pre_init(chan);
  145. /* Locate channel's user control regs */
  146. if (dev_priv->card_type < NV_40)
  147. user = NV03_USER(chan->id);
  148. else
  149. if (dev_priv->card_type < NV_50)
  150. user = NV40_USER(chan->id);
  151. else
  152. user = NV50_USER(chan->id);
  153. chan->user = ioremap(pci_resource_start(dev->pdev, 0) + user,
  154. PAGE_SIZE);
  155. if (!chan->user) {
  156. NV_ERROR(dev, "ioremap of regs failed.\n");
  157. nouveau_channel_put(&chan);
  158. return -ENOMEM;
  159. }
  160. chan->user_put = 0x40;
  161. chan->user_get = 0x44;
  162. /* Allocate space for per-channel fixed notifier memory */
  163. ret = nouveau_notifier_init_channel(chan);
  164. if (ret) {
  165. NV_ERROR(dev, "ntfy %d\n", ret);
  166. nouveau_channel_put(&chan);
  167. return ret;
  168. }
  169. /* Setup channel's default objects */
  170. ret = nouveau_gpuobj_channel_init(chan, vram_handle, gart_handle);
  171. if (ret) {
  172. NV_ERROR(dev, "gpuobj %d\n", ret);
  173. nouveau_channel_put(&chan);
  174. return ret;
  175. }
  176. /* Create a dma object for the push buffer */
  177. ret = nouveau_channel_pushbuf_ctxdma_init(chan);
  178. if (ret) {
  179. NV_ERROR(dev, "pbctxdma %d\n", ret);
  180. nouveau_channel_put(&chan);
  181. return ret;
  182. }
  183. /* disable the fifo caches */
  184. pfifo->reassign(dev, false);
  185. /* Create a graphics context for new channel */
  186. ret = pgraph->create_context(chan);
  187. if (ret) {
  188. nouveau_channel_put(&chan);
  189. return ret;
  190. }
  191. /* Construct inital RAMFC for new channel */
  192. ret = pfifo->create_context(chan);
  193. if (ret) {
  194. nouveau_channel_put(&chan);
  195. return ret;
  196. }
  197. pfifo->reassign(dev, true);
  198. ret = nouveau_dma_init(chan);
  199. if (!ret)
  200. ret = nouveau_fence_channel_init(chan);
  201. if (ret) {
  202. nouveau_channel_put(&chan);
  203. return ret;
  204. }
  205. nouveau_debugfs_channel_init(chan);
  206. NV_DEBUG(dev, "channel %d initialised\n", chan->id);
  207. *chan_ret = chan;
  208. return 0;
  209. }
  210. struct nouveau_channel *
  211. nouveau_channel_get(struct drm_device *dev, struct drm_file *file_priv, int id)
  212. {
  213. struct drm_nouveau_private *dev_priv = dev->dev_private;
  214. struct nouveau_channel *chan = ERR_PTR(-ENODEV);
  215. unsigned long flags;
  216. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  217. chan = dev_priv->channels.ptr[id];
  218. if (unlikely(!chan || atomic_read(&chan->refcount) == 0)) {
  219. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  220. return ERR_PTR(-EINVAL);
  221. }
  222. if (unlikely(file_priv && chan->file_priv != file_priv)) {
  223. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  224. return ERR_PTR(-EINVAL);
  225. }
  226. atomic_inc(&chan->refcount);
  227. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  228. mutex_lock(&chan->mutex);
  229. return chan;
  230. }
  231. void
  232. nouveau_channel_put(struct nouveau_channel **pchan)
  233. {
  234. struct nouveau_channel *chan = *pchan;
  235. struct drm_device *dev = chan->dev;
  236. struct drm_nouveau_private *dev_priv = dev->dev_private;
  237. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  238. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  239. unsigned long flags;
  240. int ret;
  241. /* unlock the channel */
  242. mutex_unlock(&chan->mutex);
  243. /* decrement the refcount, and we're done if there's still refs */
  244. if (likely(!atomic_dec_and_test(&chan->refcount))) {
  245. *pchan = NULL;
  246. return;
  247. }
  248. /* noone wants the channel anymore */
  249. NV_DEBUG(dev, "freeing channel %d\n", chan->id);
  250. nouveau_debugfs_channel_fini(chan);
  251. *pchan = NULL;
  252. /* give it chance to idle */
  253. nouveau_fence_update(chan);
  254. if (chan->fence.sequence != chan->fence.sequence_ack) {
  255. struct nouveau_fence *fence = NULL;
  256. ret = nouveau_fence_new(chan, &fence, true);
  257. if (ret == 0) {
  258. ret = nouveau_fence_wait(fence, NULL, false, false);
  259. nouveau_fence_unref((void *)&fence);
  260. }
  261. if (ret)
  262. NV_ERROR(dev, "Failed to idle channel %d.\n", chan->id);
  263. }
  264. /* ensure all outstanding fences are signaled. they should be if the
  265. * above attempts at idling were OK, but if we failed this'll tell TTM
  266. * we're done with the buffers.
  267. */
  268. nouveau_fence_channel_fini(chan);
  269. /* boot it off the hardware */
  270. pfifo->reassign(dev, false);
  271. /* We want to give pgraph a chance to idle and get rid of all
  272. * potential errors. We need to do this without the context
  273. * switch lock held, otherwise the irq handler is unable to
  274. * process them.
  275. */
  276. if (pgraph->channel(dev) == chan)
  277. nouveau_wait_for_idle(dev);
  278. /* destroy the engine specific contexts */
  279. pfifo->destroy_context(chan);
  280. pgraph->destroy_context(chan);
  281. pfifo->reassign(dev, true);
  282. /* aside from its resources, the channel should now be dead,
  283. * remove it from the channel list
  284. */
  285. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  286. dev_priv->channels.ptr[chan->id] = NULL;
  287. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  288. /* destroy any resources the channel owned */
  289. nouveau_gpuobj_ref(NULL, &chan->pushbuf);
  290. if (chan->pushbuf_bo) {
  291. nouveau_bo_unmap(chan->pushbuf_bo);
  292. nouveau_bo_unpin(chan->pushbuf_bo);
  293. nouveau_bo_ref(NULL, &chan->pushbuf_bo);
  294. }
  295. nouveau_gpuobj_channel_takedown(chan);
  296. nouveau_notifier_takedown_channel(chan);
  297. if (chan->user)
  298. iounmap(chan->user);
  299. kfree(chan);
  300. }
  301. /* cleans up all the fifos from file_priv */
  302. void
  303. nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv)
  304. {
  305. struct drm_nouveau_private *dev_priv = dev->dev_private;
  306. struct nouveau_engine *engine = &dev_priv->engine;
  307. struct nouveau_channel *chan;
  308. int i;
  309. NV_DEBUG(dev, "clearing FIFO enables from file_priv\n");
  310. for (i = 0; i < engine->fifo.channels; i++) {
  311. chan = nouveau_channel_get(dev, file_priv, i);
  312. if (IS_ERR(chan))
  313. continue;
  314. atomic_dec(&chan->refcount);
  315. nouveau_channel_put(&chan);
  316. }
  317. }
  318. /***********************************
  319. * ioctls wrapping the functions
  320. ***********************************/
  321. static int
  322. nouveau_ioctl_fifo_alloc(struct drm_device *dev, void *data,
  323. struct drm_file *file_priv)
  324. {
  325. struct drm_nouveau_private *dev_priv = dev->dev_private;
  326. struct drm_nouveau_channel_alloc *init = data;
  327. struct nouveau_channel *chan;
  328. int ret;
  329. if (dev_priv->engine.graph.accel_blocked)
  330. return -ENODEV;
  331. if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
  332. return -EINVAL;
  333. ret = nouveau_channel_alloc(dev, &chan, file_priv,
  334. init->fb_ctxdma_handle,
  335. init->tt_ctxdma_handle);
  336. if (ret)
  337. return ret;
  338. init->channel = chan->id;
  339. if (chan->dma.ib_max)
  340. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
  341. NOUVEAU_GEM_DOMAIN_GART;
  342. else if (chan->pushbuf_bo->bo.mem.mem_type == TTM_PL_VRAM)
  343. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
  344. else
  345. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
  346. init->subchan[0].handle = NvM2MF;
  347. if (dev_priv->card_type < NV_50)
  348. init->subchan[0].grclass = 0x0039;
  349. else
  350. init->subchan[0].grclass = 0x5039;
  351. init->subchan[1].handle = NvSw;
  352. init->subchan[1].grclass = NV_SW;
  353. init->nr_subchan = 2;
  354. /* Named memory object area */
  355. ret = drm_gem_handle_create(file_priv, chan->notifier_bo->gem,
  356. &init->notifier_handle);
  357. if (ret == 0)
  358. atomic_inc(&chan->refcount); /* userspace reference */
  359. nouveau_channel_put(&chan);
  360. return ret;
  361. }
  362. static int
  363. nouveau_ioctl_fifo_free(struct drm_device *dev, void *data,
  364. struct drm_file *file_priv)
  365. {
  366. struct drm_nouveau_channel_free *req = data;
  367. struct nouveau_channel *chan;
  368. chan = nouveau_channel_get(dev, file_priv, req->channel);
  369. if (IS_ERR(chan))
  370. return PTR_ERR(chan);
  371. atomic_dec(&chan->refcount);
  372. nouveau_channel_put(&chan);
  373. return 0;
  374. }
  375. /***********************************
  376. * finally, the ioctl table
  377. ***********************************/
  378. struct drm_ioctl_desc nouveau_ioctls[] = {
  379. DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH),
  380. DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  381. DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_ioctl_fifo_alloc, DRM_UNLOCKED|DRM_AUTH),
  382. DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_ioctl_fifo_free, DRM_UNLOCKED|DRM_AUTH),
  383. DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH),
  384. DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_ioctl_notifier_alloc, DRM_UNLOCKED|DRM_AUTH),
  385. DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH),
  386. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH),
  387. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH),
  388. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
  389. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
  390. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH),
  391. };
  392. int nouveau_max_ioctl = DRM_ARRAY_SIZE(nouveau_ioctls);