nouveau_channel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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.mm_node->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.mm_node->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. drm_get_resource_start(dev, 1),
  63. dev_priv->fb_available_size,
  64. NV_DMA_ACCESS_RO,
  65. NV_DMA_TARGET_PCI, &pushbuf);
  66. chan->pushbuf_base = pb->bo.mem.mm_node->start << PAGE_SHIFT;
  67. }
  68. ret = nouveau_gpuobj_ref_add(dev, chan, 0, pushbuf, &chan->pushbuf);
  69. if (ret) {
  70. NV_ERROR(dev, "Error referencing pushbuf ctxdma: %d\n", ret);
  71. if (pushbuf != dev_priv->gart_info.sg_ctxdma)
  72. nouveau_gpuobj_del(dev, &pushbuf);
  73. return ret;
  74. }
  75. return 0;
  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. return pushbuf;
  99. }
  100. /* allocates and initializes a fifo for user space consumption */
  101. int
  102. nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret,
  103. struct drm_file *file_priv,
  104. uint32_t vram_handle, uint32_t tt_handle)
  105. {
  106. struct drm_nouveau_private *dev_priv = dev->dev_private;
  107. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  108. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  109. struct nouveau_channel *chan;
  110. int channel, user;
  111. int ret;
  112. /*
  113. * Alright, here is the full story
  114. * Nvidia cards have multiple hw fifo contexts (praise them for that,
  115. * no complicated crash-prone context switches)
  116. * We allocate a new context for each app and let it write to it
  117. * directly (woo, full userspace command submission !)
  118. * When there are no more contexts, you lost
  119. */
  120. for (channel = 0; channel < pfifo->channels; channel++) {
  121. if (dev_priv->fifos[channel] == NULL)
  122. break;
  123. }
  124. /* no more fifos. you lost. */
  125. if (channel == pfifo->channels)
  126. return -EINVAL;
  127. dev_priv->fifos[channel] = kzalloc(sizeof(struct nouveau_channel),
  128. GFP_KERNEL);
  129. if (!dev_priv->fifos[channel])
  130. return -ENOMEM;
  131. chan = dev_priv->fifos[channel];
  132. INIT_LIST_HEAD(&chan->nvsw.vbl_wait);
  133. INIT_LIST_HEAD(&chan->fence.pending);
  134. chan->dev = dev;
  135. chan->id = channel;
  136. chan->file_priv = file_priv;
  137. chan->vram_handle = vram_handle;
  138. chan->gart_handle = tt_handle;
  139. NV_INFO(dev, "Allocating FIFO number %d\n", channel);
  140. /* Allocate DMA push buffer */
  141. chan->pushbuf_bo = nouveau_channel_user_pushbuf_alloc(dev);
  142. if (!chan->pushbuf_bo) {
  143. ret = -ENOMEM;
  144. NV_ERROR(dev, "pushbuf %d\n", ret);
  145. nouveau_channel_free(chan);
  146. return ret;
  147. }
  148. nouveau_dma_pre_init(chan);
  149. /* Locate channel's user control regs */
  150. if (dev_priv->card_type < NV_40)
  151. user = NV03_USER(channel);
  152. else
  153. if (dev_priv->card_type < NV_50)
  154. user = NV40_USER(channel);
  155. else
  156. user = NV50_USER(channel);
  157. chan->user = ioremap(pci_resource_start(dev->pdev, 0) + user,
  158. PAGE_SIZE);
  159. if (!chan->user) {
  160. NV_ERROR(dev, "ioremap of regs failed.\n");
  161. nouveau_channel_free(chan);
  162. return -ENOMEM;
  163. }
  164. chan->user_put = 0x40;
  165. chan->user_get = 0x44;
  166. /* Allocate space for per-channel fixed notifier memory */
  167. ret = nouveau_notifier_init_channel(chan);
  168. if (ret) {
  169. NV_ERROR(dev, "ntfy %d\n", ret);
  170. nouveau_channel_free(chan);
  171. return ret;
  172. }
  173. /* Setup channel's default objects */
  174. ret = nouveau_gpuobj_channel_init(chan, vram_handle, tt_handle);
  175. if (ret) {
  176. NV_ERROR(dev, "gpuobj %d\n", ret);
  177. nouveau_channel_free(chan);
  178. return ret;
  179. }
  180. /* Create a dma object for the push buffer */
  181. ret = nouveau_channel_pushbuf_ctxdma_init(chan);
  182. if (ret) {
  183. NV_ERROR(dev, "pbctxdma %d\n", ret);
  184. nouveau_channel_free(chan);
  185. return ret;
  186. }
  187. /* disable the fifo caches */
  188. pfifo->reassign(dev, false);
  189. /* Create a graphics context for new channel */
  190. ret = pgraph->create_context(chan);
  191. if (ret) {
  192. nouveau_channel_free(chan);
  193. return ret;
  194. }
  195. /* Construct inital RAMFC for new channel */
  196. ret = pfifo->create_context(chan);
  197. if (ret) {
  198. nouveau_channel_free(chan);
  199. return ret;
  200. }
  201. pfifo->reassign(dev, true);
  202. ret = nouveau_dma_init(chan);
  203. if (!ret)
  204. ret = nouveau_fence_init(chan);
  205. if (ret) {
  206. nouveau_channel_free(chan);
  207. return ret;
  208. }
  209. nouveau_debugfs_channel_init(chan);
  210. NV_INFO(dev, "%s: initialised FIFO %d\n", __func__, channel);
  211. *chan_ret = chan;
  212. return 0;
  213. }
  214. /* stops a fifo */
  215. void
  216. nouveau_channel_free(struct nouveau_channel *chan)
  217. {
  218. struct drm_device *dev = chan->dev;
  219. struct drm_nouveau_private *dev_priv = dev->dev_private;
  220. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  221. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  222. unsigned long flags;
  223. int ret;
  224. NV_INFO(dev, "%s: freeing fifo %d\n", __func__, chan->id);
  225. nouveau_debugfs_channel_fini(chan);
  226. /* Give outstanding push buffers a chance to complete */
  227. spin_lock_irqsave(&chan->fence.lock, flags);
  228. nouveau_fence_update(chan);
  229. spin_unlock_irqrestore(&chan->fence.lock, flags);
  230. if (chan->fence.sequence != chan->fence.sequence_ack) {
  231. struct nouveau_fence *fence = NULL;
  232. ret = nouveau_fence_new(chan, &fence, true);
  233. if (ret == 0) {
  234. ret = nouveau_fence_wait(fence, NULL, false, false);
  235. nouveau_fence_unref((void *)&fence);
  236. }
  237. if (ret)
  238. NV_ERROR(dev, "Failed to idle channel %d.\n", chan->id);
  239. }
  240. /* Ensure all outstanding fences are signaled. They should be if the
  241. * above attempts at idling were OK, but if we failed this'll tell TTM
  242. * we're done with the buffers.
  243. */
  244. nouveau_fence_fini(chan);
  245. /* This will prevent pfifo from switching channels. */
  246. pfifo->reassign(dev, false);
  247. /* We want to give pgraph a chance to idle and get rid of all potential
  248. * errors. We need to do this before the lock, otherwise the irq handler
  249. * is unable to process them.
  250. */
  251. if (pgraph->channel(dev) == chan)
  252. nouveau_wait_for_idle(dev);
  253. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  254. pgraph->fifo_access(dev, false);
  255. if (pgraph->channel(dev) == chan)
  256. pgraph->unload_context(dev);
  257. pgraph->destroy_context(chan);
  258. pgraph->fifo_access(dev, true);
  259. if (pfifo->channel_id(dev) == chan->id) {
  260. pfifo->disable(dev);
  261. pfifo->unload_context(dev);
  262. pfifo->enable(dev);
  263. }
  264. pfifo->destroy_context(chan);
  265. pfifo->reassign(dev, true);
  266. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  267. /* Release the channel's resources */
  268. nouveau_gpuobj_ref_del(dev, &chan->pushbuf);
  269. if (chan->pushbuf_bo) {
  270. nouveau_bo_unpin(chan->pushbuf_bo);
  271. nouveau_bo_ref(NULL, &chan->pushbuf_bo);
  272. }
  273. nouveau_gpuobj_channel_takedown(chan);
  274. nouveau_notifier_takedown_channel(chan);
  275. if (chan->user)
  276. iounmap(chan->user);
  277. dev_priv->fifos[chan->id] = NULL;
  278. kfree(chan);
  279. }
  280. /* cleans up all the fifos from file_priv */
  281. void
  282. nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv)
  283. {
  284. struct drm_nouveau_private *dev_priv = dev->dev_private;
  285. struct nouveau_engine *engine = &dev_priv->engine;
  286. int i;
  287. NV_DEBUG(dev, "clearing FIFO enables from file_priv\n");
  288. for (i = 0; i < engine->fifo.channels; i++) {
  289. struct nouveau_channel *chan = dev_priv->fifos[i];
  290. if (chan && chan->file_priv == file_priv)
  291. nouveau_channel_free(chan);
  292. }
  293. }
  294. int
  295. nouveau_channel_owner(struct drm_device *dev, struct drm_file *file_priv,
  296. int channel)
  297. {
  298. struct drm_nouveau_private *dev_priv = dev->dev_private;
  299. struct nouveau_engine *engine = &dev_priv->engine;
  300. if (channel >= engine->fifo.channels)
  301. return 0;
  302. if (dev_priv->fifos[channel] == NULL)
  303. return 0;
  304. return (dev_priv->fifos[channel]->file_priv == file_priv);
  305. }
  306. /***********************************
  307. * ioctls wrapping the functions
  308. ***********************************/
  309. static int
  310. nouveau_ioctl_fifo_alloc(struct drm_device *dev, void *data,
  311. struct drm_file *file_priv)
  312. {
  313. struct drm_nouveau_private *dev_priv = dev->dev_private;
  314. struct drm_nouveau_channel_alloc *init = data;
  315. struct nouveau_channel *chan;
  316. int ret;
  317. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  318. if (dev_priv->engine.graph.accel_blocked)
  319. return -ENODEV;
  320. if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
  321. return -EINVAL;
  322. ret = nouveau_channel_alloc(dev, &chan, file_priv,
  323. init->fb_ctxdma_handle,
  324. init->tt_ctxdma_handle);
  325. if (ret)
  326. return ret;
  327. init->channel = chan->id;
  328. if (chan->dma.ib_max)
  329. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
  330. NOUVEAU_GEM_DOMAIN_GART;
  331. else if (chan->pushbuf_bo->bo.mem.mem_type == TTM_PL_VRAM)
  332. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
  333. else
  334. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
  335. init->subchan[0].handle = NvM2MF;
  336. if (dev_priv->card_type < NV_50)
  337. init->subchan[0].grclass = 0x0039;
  338. else
  339. init->subchan[0].grclass = 0x5039;
  340. init->subchan[1].handle = NvSw;
  341. init->subchan[1].grclass = NV_SW;
  342. init->nr_subchan = 2;
  343. /* Named memory object area */
  344. ret = drm_gem_handle_create(file_priv, chan->notifier_bo->gem,
  345. &init->notifier_handle);
  346. if (ret) {
  347. nouveau_channel_free(chan);
  348. return ret;
  349. }
  350. return 0;
  351. }
  352. static int
  353. nouveau_ioctl_fifo_free(struct drm_device *dev, void *data,
  354. struct drm_file *file_priv)
  355. {
  356. struct drm_nouveau_channel_free *cfree = data;
  357. struct nouveau_channel *chan;
  358. NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
  359. NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(cfree->channel, file_priv, chan);
  360. nouveau_channel_free(chan);
  361. return 0;
  362. }
  363. /***********************************
  364. * finally, the ioctl table
  365. ***********************************/
  366. struct drm_ioctl_desc nouveau_ioctls[] = {
  367. DRM_IOCTL_DEF(DRM_NOUVEAU_GETPARAM, nouveau_ioctl_getparam, DRM_AUTH),
  368. DRM_IOCTL_DEF(DRM_NOUVEAU_SETPARAM, nouveau_ioctl_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  369. DRM_IOCTL_DEF(DRM_NOUVEAU_CHANNEL_ALLOC, nouveau_ioctl_fifo_alloc, DRM_AUTH),
  370. DRM_IOCTL_DEF(DRM_NOUVEAU_CHANNEL_FREE, nouveau_ioctl_fifo_free, DRM_AUTH),
  371. DRM_IOCTL_DEF(DRM_NOUVEAU_GROBJ_ALLOC, nouveau_ioctl_grobj_alloc, DRM_AUTH),
  372. DRM_IOCTL_DEF(DRM_NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_ioctl_notifier_alloc, DRM_AUTH),
  373. DRM_IOCTL_DEF(DRM_NOUVEAU_GPUOBJ_FREE, nouveau_ioctl_gpuobj_free, DRM_AUTH),
  374. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_AUTH),
  375. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_AUTH),
  376. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_AUTH),
  377. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_AUTH),
  378. DRM_IOCTL_DEF(DRM_NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_AUTH),
  379. };
  380. int nouveau_max_ioctl = DRM_ARRAY_SIZE(nouveau_ioctls);