nouveau_channel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 tt_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. int channel, user;
  107. int ret;
  108. /*
  109. * Alright, here is the full story
  110. * Nvidia cards have multiple hw fifo contexts (praise them for that,
  111. * no complicated crash-prone context switches)
  112. * We allocate a new context for each app and let it write to it
  113. * directly (woo, full userspace command submission !)
  114. * When there are no more contexts, you lost
  115. */
  116. for (channel = 0; channel < pfifo->channels; channel++) {
  117. if (dev_priv->fifos[channel] == NULL)
  118. break;
  119. }
  120. /* no more fifos. you lost. */
  121. if (channel == pfifo->channels)
  122. return -EINVAL;
  123. dev_priv->fifos[channel] = kzalloc(sizeof(struct nouveau_channel),
  124. GFP_KERNEL);
  125. if (!dev_priv->fifos[channel])
  126. return -ENOMEM;
  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_channel_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. nouveau_fence_update(chan);
  224. if (chan->fence.sequence != chan->fence.sequence_ack) {
  225. struct nouveau_fence *fence = NULL;
  226. ret = nouveau_fence_new(chan, &fence, true);
  227. if (ret == 0) {
  228. ret = nouveau_fence_wait(fence, NULL, false, false);
  229. nouveau_fence_unref((void *)&fence);
  230. }
  231. if (ret)
  232. NV_ERROR(dev, "Failed to idle channel %d.\n", chan->id);
  233. }
  234. /* Ensure all outstanding fences are signaled. They should be if the
  235. * above attempts at idling were OK, but if we failed this'll tell TTM
  236. * we're done with the buffers.
  237. */
  238. nouveau_fence_channel_fini(chan);
  239. /* This will prevent pfifo from switching channels. */
  240. pfifo->reassign(dev, false);
  241. /* We want to give pgraph a chance to idle and get rid of all potential
  242. * errors. We need to do this before the lock, otherwise the irq handler
  243. * is unable to process them.
  244. */
  245. if (pgraph->channel(dev) == chan)
  246. nouveau_wait_for_idle(dev);
  247. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  248. pgraph->fifo_access(dev, false);
  249. if (pgraph->channel(dev) == chan)
  250. pgraph->unload_context(dev);
  251. pgraph->destroy_context(chan);
  252. pgraph->fifo_access(dev, true);
  253. if (pfifo->channel_id(dev) == chan->id) {
  254. pfifo->disable(dev);
  255. pfifo->unload_context(dev);
  256. pfifo->enable(dev);
  257. }
  258. pfifo->destroy_context(chan);
  259. pfifo->reassign(dev, true);
  260. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  261. /* Release the channel's resources */
  262. nouveau_gpuobj_ref(NULL, &chan->pushbuf);
  263. if (chan->pushbuf_bo) {
  264. nouveau_bo_unmap(chan->pushbuf_bo);
  265. nouveau_bo_unpin(chan->pushbuf_bo);
  266. nouveau_bo_ref(NULL, &chan->pushbuf_bo);
  267. }
  268. nouveau_gpuobj_channel_takedown(chan);
  269. nouveau_notifier_takedown_channel(chan);
  270. if (chan->user)
  271. iounmap(chan->user);
  272. dev_priv->fifos[chan->id] = NULL;
  273. kfree(chan);
  274. }
  275. /* cleans up all the fifos from file_priv */
  276. void
  277. nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv)
  278. {
  279. struct drm_nouveau_private *dev_priv = dev->dev_private;
  280. struct nouveau_engine *engine = &dev_priv->engine;
  281. int i;
  282. NV_DEBUG(dev, "clearing FIFO enables from file_priv\n");
  283. for (i = 0; i < engine->fifo.channels; i++) {
  284. struct nouveau_channel *chan = dev_priv->fifos[i];
  285. if (chan && chan->file_priv == file_priv)
  286. nouveau_channel_free(chan);
  287. }
  288. }
  289. int
  290. nouveau_channel_owner(struct drm_device *dev, struct drm_file *file_priv,
  291. int channel)
  292. {
  293. struct drm_nouveau_private *dev_priv = dev->dev_private;
  294. struct nouveau_engine *engine = &dev_priv->engine;
  295. if (channel >= engine->fifo.channels)
  296. return 0;
  297. if (dev_priv->fifos[channel] == NULL)
  298. return 0;
  299. return (dev_priv->fifos[channel]->file_priv == file_priv);
  300. }
  301. /***********************************
  302. * ioctls wrapping the functions
  303. ***********************************/
  304. static int
  305. nouveau_ioctl_fifo_alloc(struct drm_device *dev, void *data,
  306. struct drm_file *file_priv)
  307. {
  308. struct drm_nouveau_private *dev_priv = dev->dev_private;
  309. struct drm_nouveau_channel_alloc *init = data;
  310. struct nouveau_channel *chan;
  311. int ret;
  312. if (dev_priv->engine.graph.accel_blocked)
  313. return -ENODEV;
  314. if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
  315. return -EINVAL;
  316. ret = nouveau_channel_alloc(dev, &chan, file_priv,
  317. init->fb_ctxdma_handle,
  318. init->tt_ctxdma_handle);
  319. if (ret)
  320. return ret;
  321. init->channel = chan->id;
  322. if (chan->dma.ib_max)
  323. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
  324. NOUVEAU_GEM_DOMAIN_GART;
  325. else if (chan->pushbuf_bo->bo.mem.mem_type == TTM_PL_VRAM)
  326. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
  327. else
  328. init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
  329. init->subchan[0].handle = NvM2MF;
  330. if (dev_priv->card_type < NV_50)
  331. init->subchan[0].grclass = 0x0039;
  332. else
  333. init->subchan[0].grclass = 0x5039;
  334. init->subchan[1].handle = NvSw;
  335. init->subchan[1].grclass = NV_SW;
  336. init->nr_subchan = 2;
  337. /* Named memory object area */
  338. ret = drm_gem_handle_create(file_priv, chan->notifier_bo->gem,
  339. &init->notifier_handle);
  340. if (ret) {
  341. nouveau_channel_free(chan);
  342. return ret;
  343. }
  344. return 0;
  345. }
  346. static int
  347. nouveau_ioctl_fifo_free(struct drm_device *dev, void *data,
  348. struct drm_file *file_priv)
  349. {
  350. struct drm_nouveau_channel_free *cfree = data;
  351. struct nouveau_channel *chan;
  352. NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(cfree->channel, file_priv, chan);
  353. nouveau_channel_free(chan);
  354. return 0;
  355. }
  356. /***********************************
  357. * finally, the ioctl table
  358. ***********************************/
  359. struct drm_ioctl_desc nouveau_ioctls[] = {
  360. DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_ioctl_getparam, DRM_AUTH),
  361. DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_ioctl_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  362. DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_ioctl_fifo_alloc, DRM_AUTH),
  363. DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_ioctl_fifo_free, DRM_AUTH),
  364. DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_ioctl_grobj_alloc, DRM_AUTH),
  365. DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_ioctl_notifier_alloc, DRM_AUTH),
  366. DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_ioctl_gpuobj_free, DRM_AUTH),
  367. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_AUTH),
  368. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_AUTH),
  369. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_AUTH),
  370. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_AUTH),
  371. DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_AUTH),
  372. };
  373. int nouveau_max_ioctl = DRM_ARRAY_SIZE(nouveau_ioctls);