nouveau_prime.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright 2011 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Dave Airlie
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_drm.h"
  27. #include "nouveau_dma.h"
  28. #include <linux/dma-buf.h>
  29. static struct sg_table *nouveau_gem_map_dma_buf(struct dma_buf_attachment *attachment,
  30. enum dma_data_direction dir)
  31. {
  32. struct nouveau_bo *nvbo = attachment->dmabuf->priv;
  33. struct drm_device *dev = nvbo->gem->dev;
  34. int npages = nvbo->bo.num_pages;
  35. struct sg_table *sg;
  36. int nents;
  37. mutex_lock(&dev->struct_mutex);
  38. sg = drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages);
  39. nents = dma_map_sg(attachment->dev, sg->sgl, sg->nents, dir);
  40. mutex_unlock(&dev->struct_mutex);
  41. return sg;
  42. }
  43. static void nouveau_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
  44. struct sg_table *sg, enum dma_data_direction dir)
  45. {
  46. dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
  47. sg_free_table(sg);
  48. kfree(sg);
  49. }
  50. static void nouveau_gem_dmabuf_release(struct dma_buf *dma_buf)
  51. {
  52. struct nouveau_bo *nvbo = dma_buf->priv;
  53. if (nvbo->gem->export_dma_buf == dma_buf) {
  54. nvbo->gem->export_dma_buf = NULL;
  55. drm_gem_object_unreference_unlocked(nvbo->gem);
  56. }
  57. }
  58. static void *nouveau_gem_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
  59. {
  60. return NULL;
  61. }
  62. static void nouveau_gem_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  63. {
  64. }
  65. static void *nouveau_gem_kmap(struct dma_buf *dma_buf, unsigned long page_num)
  66. {
  67. return NULL;
  68. }
  69. static void nouveau_gem_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  70. {
  71. }
  72. static int nouveau_gem_prime_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
  73. {
  74. return -EINVAL;
  75. }
  76. static void *nouveau_gem_prime_vmap(struct dma_buf *dma_buf)
  77. {
  78. struct nouveau_bo *nvbo = dma_buf->priv;
  79. struct drm_device *dev = nvbo->gem->dev;
  80. int ret;
  81. mutex_lock(&dev->struct_mutex);
  82. if (nvbo->vmapping_count) {
  83. nvbo->vmapping_count++;
  84. goto out_unlock;
  85. }
  86. ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.num_pages,
  87. &nvbo->dma_buf_vmap);
  88. if (ret) {
  89. mutex_unlock(&dev->struct_mutex);
  90. return ERR_PTR(ret);
  91. }
  92. nvbo->vmapping_count = 1;
  93. out_unlock:
  94. mutex_unlock(&dev->struct_mutex);
  95. return nvbo->dma_buf_vmap.virtual;
  96. }
  97. static void nouveau_gem_prime_vunmap(struct dma_buf *dma_buf, void *vaddr)
  98. {
  99. struct nouveau_bo *nvbo = dma_buf->priv;
  100. struct drm_device *dev = nvbo->gem->dev;
  101. mutex_lock(&dev->struct_mutex);
  102. nvbo->vmapping_count--;
  103. if (nvbo->vmapping_count == 0) {
  104. ttm_bo_kunmap(&nvbo->dma_buf_vmap);
  105. }
  106. mutex_unlock(&dev->struct_mutex);
  107. }
  108. static const struct dma_buf_ops nouveau_dmabuf_ops = {
  109. .map_dma_buf = nouveau_gem_map_dma_buf,
  110. .unmap_dma_buf = nouveau_gem_unmap_dma_buf,
  111. .release = nouveau_gem_dmabuf_release,
  112. .kmap = nouveau_gem_kmap,
  113. .kmap_atomic = nouveau_gem_kmap_atomic,
  114. .kunmap = nouveau_gem_kunmap,
  115. .kunmap_atomic = nouveau_gem_kunmap_atomic,
  116. .mmap = nouveau_gem_prime_mmap,
  117. .vmap = nouveau_gem_prime_vmap,
  118. .vunmap = nouveau_gem_prime_vunmap,
  119. };
  120. static int
  121. nouveau_prime_new(struct drm_device *dev,
  122. size_t size,
  123. struct sg_table *sg,
  124. struct nouveau_bo **pnvbo)
  125. {
  126. struct nouveau_bo *nvbo;
  127. u32 flags = 0;
  128. int ret;
  129. flags = TTM_PL_FLAG_TT;
  130. ret = nouveau_bo_new(dev, size, 0, flags, 0, 0,
  131. sg, pnvbo);
  132. if (ret)
  133. return ret;
  134. nvbo = *pnvbo;
  135. /* we restrict allowed domains on nv50+ to only the types
  136. * that were requested at creation time. not possibly on
  137. * earlier chips without busting the ABI.
  138. */
  139. nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART;
  140. nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
  141. if (!nvbo->gem) {
  142. nouveau_bo_ref(NULL, pnvbo);
  143. return -ENOMEM;
  144. }
  145. nvbo->gem->driver_private = nvbo;
  146. return 0;
  147. }
  148. struct dma_buf *nouveau_gem_prime_export(struct drm_device *dev,
  149. struct drm_gem_object *obj, int flags)
  150. {
  151. struct nouveau_bo *nvbo = nouveau_gem_object(obj);
  152. int ret = 0;
  153. /* pin buffer into GTT */
  154. ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_TT);
  155. if (ret)
  156. return ERR_PTR(-EINVAL);
  157. return dma_buf_export(nvbo, &nouveau_dmabuf_ops, obj->size, flags);
  158. }
  159. struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev,
  160. struct dma_buf *dma_buf)
  161. {
  162. struct dma_buf_attachment *attach;
  163. struct sg_table *sg;
  164. struct nouveau_bo *nvbo;
  165. int ret;
  166. if (dma_buf->ops == &nouveau_dmabuf_ops) {
  167. nvbo = dma_buf->priv;
  168. if (nvbo->gem) {
  169. if (nvbo->gem->dev == dev) {
  170. drm_gem_object_reference(nvbo->gem);
  171. return nvbo->gem;
  172. }
  173. }
  174. }
  175. /* need to attach */
  176. attach = dma_buf_attach(dma_buf, dev->dev);
  177. if (IS_ERR(attach))
  178. return ERR_PTR(PTR_ERR(attach));
  179. sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  180. if (IS_ERR(sg)) {
  181. ret = PTR_ERR(sg);
  182. goto fail_detach;
  183. }
  184. ret = nouveau_prime_new(dev, dma_buf->size, sg, &nvbo);
  185. if (ret)
  186. goto fail_unmap;
  187. nvbo->gem->import_attach = attach;
  188. return nvbo->gem;
  189. fail_unmap:
  190. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  191. fail_detach:
  192. dma_buf_detach(dma_buf, attach);
  193. return ERR_PTR(ret);
  194. }