nouveau_channel.c 14 KB

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