nouveau_channel.c 13 KB

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