nouveau_channel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. uint32_t start = pb->bo.mem.mm_node->start << PAGE_SHIFT;
  37. int ret;
  38. if (pb->bo.mem.mem_type == TTM_PL_TT) {
  39. ret = nouveau_gpuobj_gart_dma_new(chan, 0,
  40. dev_priv->gart_info.aper_size,
  41. NV_DMA_ACCESS_RO, &pushbuf,
  42. NULL);
  43. chan->pushbuf_base = start;
  44. } else
  45. if (dev_priv->card_type != NV_04) {
  46. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, 0,
  47. dev_priv->fb_available_size,
  48. NV_DMA_ACCESS_RO,
  49. NV_DMA_TARGET_VIDMEM, &pushbuf);
  50. chan->pushbuf_base = start;
  51. } else {
  52. /* NV04 cmdbuf hack, from original ddx.. not sure of it's
  53. * exact reason for existing :) PCI access to cmdbuf in
  54. * VRAM.
  55. */
  56. ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
  57. drm_get_resource_start(dev, 1),
  58. dev_priv->fb_available_size,
  59. NV_DMA_ACCESS_RO,
  60. NV_DMA_TARGET_PCI, &pushbuf);
  61. chan->pushbuf_base = start;
  62. }
  63. ret = nouveau_gpuobj_ref_add(dev, chan, 0, pushbuf, &chan->pushbuf);
  64. if (ret) {
  65. NV_ERROR(dev, "Error referencing pushbuf ctxdma: %d\n", ret);
  66. if (pushbuf != dev_priv->gart_info.sg_ctxdma)
  67. nouveau_gpuobj_del(dev, &pushbuf);
  68. return ret;
  69. }
  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 tt_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. int channel, user;
  106. int ret;
  107. /*
  108. * Alright, here is the full story
  109. * Nvidia cards have multiple hw fifo contexts (praise them for that,
  110. * no complicated crash-prone context switches)
  111. * We allocate a new context for each app and let it write to it
  112. * directly (woo, full userspace command submission !)
  113. * When there are no more contexts, you lost
  114. */
  115. for (channel = 0; channel < pfifo->channels; channel++) {
  116. if (dev_priv->fifos[channel] == NULL)
  117. break;
  118. }
  119. /* no more fifos. you lost. */
  120. if (channel == pfifo->channels)
  121. return -EINVAL;
  122. dev_priv->fifos[channel] = kzalloc(sizeof(struct nouveau_channel),
  123. GFP_KERNEL);
  124. if (!dev_priv->fifos[channel])
  125. return -ENOMEM;
  126. dev_priv->fifo_alloc_count++;
  127. chan = dev_priv->fifos[channel];
  128. INIT_LIST_HEAD(&chan->nvsw.vbl_wait);
  129. INIT_LIST_HEAD(&chan->fence.pending);
  130. chan->dev = dev;
  131. chan->id = channel;
  132. chan->file_priv = file_priv;
  133. chan->vram_handle = vram_handle;
  134. chan->gart_handle = tt_handle;
  135. NV_INFO(dev, "Allocating FIFO number %d\n", channel);
  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_free(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(channel);
  148. else
  149. if (dev_priv->card_type < NV_50)
  150. user = NV40_USER(channel);
  151. else
  152. user = NV50_USER(channel);
  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_free(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_free(chan);
  167. return ret;
  168. }
  169. /* Setup channel's default objects */
  170. ret = nouveau_gpuobj_channel_init(chan, vram_handle, tt_handle);
  171. if (ret) {
  172. NV_ERROR(dev, "gpuobj %d\n", ret);
  173. nouveau_channel_free(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_free(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_free(chan);
  189. return ret;
  190. }
  191. /* Construct inital RAMFC for new channel */
  192. ret = pfifo->create_context(chan);
  193. if (ret) {
  194. nouveau_channel_free(chan);
  195. return ret;
  196. }
  197. pfifo->reassign(dev, true);
  198. ret = nouveau_dma_init(chan);
  199. if (!ret)
  200. ret = nouveau_fence_init(chan);
  201. if (ret) {
  202. nouveau_channel_free(chan);
  203. return ret;
  204. }
  205. nouveau_debugfs_channel_init(chan);
  206. NV_INFO(dev, "%s: initialised FIFO %d\n", __func__, channel);
  207. *chan_ret = chan;
  208. return 0;
  209. }
  210. /* stops a fifo */
  211. void
  212. nouveau_channel_free(struct nouveau_channel *chan)
  213. {
  214. struct drm_device *dev = chan->dev;
  215. struct drm_nouveau_private *dev_priv = dev->dev_private;
  216. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  217. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  218. unsigned long flags;
  219. int ret;
  220. NV_INFO(dev, "%s: freeing fifo %d\n", __func__, chan->id);
  221. nouveau_debugfs_channel_fini(chan);
  222. /* Give outstanding push buffers a chance to complete */
  223. spin_lock_irqsave(&chan->fence.lock, flags);
  224. nouveau_fence_update(chan);
  225. spin_unlock_irqrestore(&chan->fence.lock, flags);
  226. if (chan->fence.sequence != chan->fence.sequence_ack) {
  227. struct nouveau_fence *fence = NULL;
  228. ret = nouveau_fence_new(chan, &fence, true);
  229. if (ret == 0) {
  230. ret = nouveau_fence_wait(fence, NULL, false, false);
  231. nouveau_fence_unref((void *)&fence);
  232. }
  233. if (ret)
  234. NV_ERROR(dev, "Failed to idle channel %d.\n", chan->id);
  235. }
  236. /* Ensure all outstanding fences are signaled. They should be if the
  237. * above attempts at idling were OK, but if we failed this'll tell TTM
  238. * we're done with the buffers.
  239. */
  240. nouveau_fence_fini(chan);
  241. /* Ensure the channel is no longer active on the GPU */
  242. pfifo->reassign(dev, false);
  243. if (pgraph->channel(dev) == chan) {
  244. pgraph->fifo_access(dev, false);
  245. pgraph->unload_context(dev);
  246. pgraph->fifo_access(dev, true);
  247. }
  248. pgraph->destroy_context(chan);
  249. if (pfifo->channel_id(dev) == chan->id) {
  250. pfifo->disable(dev);
  251. pfifo->unload_context(dev);
  252. pfifo->enable(dev);
  253. }
  254. pfifo->destroy_context(chan);
  255. pfifo->reassign(dev, true);
  256. /* Release the channel's resources */
  257. nouveau_gpuobj_ref_del(dev, &chan->pushbuf);
  258. if (chan->pushbuf_bo) {
  259. nouveau_bo_unpin(chan->pushbuf_bo);
  260. nouveau_bo_ref(NULL, &chan->pushbuf_bo);
  261. }
  262. nouveau_gpuobj_channel_takedown(chan);
  263. nouveau_notifier_takedown_channel(chan);
  264. if (chan->user)
  265. iounmap(chan->user);
  266. dev_priv->fifos[chan->id] = NULL;
  267. dev_priv->fifo_alloc_count--;
  268. kfree(chan);
  269. }
  270. /* cleans up all the fifos from file_priv */
  271. void
  272. nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv)
  273. {
  274. struct drm_nouveau_private *dev_priv = dev->dev_private;
  275. struct nouveau_engine *engine = &dev_priv->engine;
  276. int i;
  277. NV_DEBUG(dev, "clearing FIFO enables from file_priv\n");
  278. for (i = 0; i < engine->fifo.channels; i++) {
  279. struct nouveau_channel *chan = dev_priv->fifos[i];
  280. if (chan && chan->file_priv == file_priv)
  281. nouveau_channel_free(chan);
  282. }
  283. }
  284. int
  285. nouveau_channel_owner(struct drm_device *dev, struct drm_file *file_priv,
  286. int channel)
  287. {
  288. struct drm_nouveau_private *dev_priv = dev->dev_private;
  289. struct nouveau_engine *engine = &dev_priv->engine;
  290. if (channel >= engine->fifo.channels)
  291. return 0;
  292. if (dev_priv->fifos[channel] == NULL)
  293. return 0;
  294. return (dev_priv->fifos[channel]->file_priv == file_priv);
  295. }
  296. /***********************************
  297. * ioctls wrapping the functions
  298. ***********************************/
  299. static int
  300. nouveau_ioctl_fifo_alloc(struct drm_device *dev, void *data,
  301. struct drm_file *file_priv)
  302. {
  303. struct drm_nouveau_private *dev_priv = dev->dev_private;
  304. struct drm_nouveau_channel_alloc *init = data;
  305. struct nouveau_channel *chan;
  306. int ret;
  307. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  308. if (dev_priv->engine.graph.accel_blocked)
  309. return -ENODEV;
  310. if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
  311. return -EINVAL;
  312. ret = nouveau_channel_alloc(dev, &chan, file_priv,
  313. init->fb_ctxdma_handle,
  314. init->tt_ctxdma_handle);
  315. if (ret)
  316. return ret;
  317. init->channel = chan->id;
  318. init->subchan[0].handle = NvM2MF;
  319. if (dev_priv->card_type < NV_50)
  320. init->subchan[0].grclass = 0x0039;
  321. else
  322. init->subchan[0].grclass = 0x5039;
  323. init->subchan[1].handle = NvSw;
  324. init->subchan[1].grclass = NV_SW;
  325. init->nr_subchan = 2;
  326. /* Named memory object area */
  327. ret = drm_gem_handle_create(file_priv, chan->notifier_bo->gem,
  328. &init->notifier_handle);
  329. if (ret) {
  330. nouveau_channel_free(chan);
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. static int
  336. nouveau_ioctl_fifo_free(struct drm_device *dev, void *data,
  337. struct drm_file *file_priv)
  338. {
  339. struct drm_nouveau_channel_free *cfree = data;
  340. struct nouveau_channel *chan;
  341. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  342. NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(cfree->channel, file_priv, chan);
  343. nouveau_channel_free(chan);
  344. return 0;
  345. }
  346. /***********************************
  347. * finally, the ioctl table
  348. ***********************************/
  349. struct drm_ioctl_desc nouveau_ioctls[] = {
  350. DRM_IOCTL_DEF(DRM_NOUVEAU_CARD_INIT, nouveau_ioctl_card_init, DRM_AUTH),
  351. DRM_IOCTL_DEF(DRM_NOUVEAU_GETPARAM, nouveau_ioctl_getparam, DRM_AUTH),
  352. DRM_IOCTL_DEF(DRM_NOUVEAU_SETPARAM, nouveau_ioctl_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  353. DRM_IOCTL_DEF(DRM_NOUVEAU_CHANNEL_ALLOC, nouveau_ioctl_fifo_alloc, DRM_AUTH),
  354. DRM_IOCTL_DEF(DRM_NOUVEAU_CHANNEL_FREE, nouveau_ioctl_fifo_free, DRM_AUTH),
  355. DRM_IOCTL_DEF(DRM_NOUVEAU_GROBJ_ALLOC, nouveau_ioctl_grobj_alloc, DRM_AUTH),
  356. DRM_IOCTL_DEF(DRM_NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_ioctl_notifier_alloc, DRM_AUTH),
  357. DRM_IOCTL_DEF(DRM_NOUVEAU_GPUOBJ_FREE, nouveau_ioctl_gpuobj_free, DRM_AUTH),
  358. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_AUTH),
  359. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_AUTH),
  360. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_PUSHBUF_CALL, nouveau_gem_ioctl_pushbuf_call, DRM_AUTH),
  361. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_PIN, nouveau_gem_ioctl_pin, DRM_AUTH),
  362. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_UNPIN, nouveau_gem_ioctl_unpin, DRM_AUTH),
  363. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_AUTH),
  364. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_AUTH),
  365. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_AUTH),
  366. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_PUSHBUF_CALL2, nouveau_gem_ioctl_pushbuf_call2, DRM_AUTH),
  367. };
  368. int nouveau_max_ioctl = DRM_ARRAY_SIZE(nouveau_ioctls);