nouveau_channel.c 14 KB

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