nouveau_prime.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "drmP.h"
  2. #include "drm.h"
  3. #include "nouveau_drv.h"
  4. #include "nouveau_drm.h"
  5. #include "nouveau_dma.h"
  6. #include <linux/dma-buf.h>
  7. static struct sg_table *nouveau_gem_map_dma_buf(struct dma_buf_attachment *attachment,
  8. enum dma_data_direction dir)
  9. {
  10. struct nouveau_bo *nvbo = attachment->dmabuf->priv;
  11. struct drm_device *dev = nvbo->gem->dev;
  12. int npages = nvbo->bo.num_pages;
  13. struct sg_table *sg;
  14. int nents;
  15. mutex_lock(&dev->struct_mutex);
  16. sg = drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages);
  17. nents = dma_map_sg(attachment->dev, sg->sgl, sg->nents, dir);
  18. mutex_unlock(&dev->struct_mutex);
  19. return sg;
  20. }
  21. static void nouveau_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
  22. struct sg_table *sg, enum dma_data_direction dir)
  23. {
  24. dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
  25. sg_free_table(sg);
  26. kfree(sg);
  27. }
  28. static void nouveau_gem_dmabuf_release(struct dma_buf *dma_buf)
  29. {
  30. struct nouveau_bo *nvbo = dma_buf->priv;
  31. if (nvbo->gem->export_dma_buf == dma_buf) {
  32. nvbo->gem->export_dma_buf = NULL;
  33. drm_gem_object_unreference_unlocked(nvbo->gem);
  34. }
  35. }
  36. static void *nouveau_gem_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
  37. {
  38. return NULL;
  39. }
  40. static void nouveau_gem_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  41. {
  42. }
  43. static void *nouveau_gem_kmap(struct dma_buf *dma_buf, unsigned long page_num)
  44. {
  45. return NULL;
  46. }
  47. static void nouveau_gem_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  48. {
  49. }
  50. struct dma_buf_ops nouveau_dmabuf_ops = {
  51. .map_dma_buf = nouveau_gem_map_dma_buf,
  52. .unmap_dma_buf = nouveau_gem_unmap_dma_buf,
  53. .release = nouveau_gem_dmabuf_release,
  54. .kmap = nouveau_gem_kmap,
  55. .kmap_atomic = nouveau_gem_kmap_atomic,
  56. .kunmap = nouveau_gem_kunmap,
  57. .kunmap_atomic = nouveau_gem_kunmap_atomic,
  58. };
  59. static int
  60. nouveau_prime_new(struct drm_device *dev,
  61. size_t size,
  62. struct sg_table *sg,
  63. struct nouveau_bo **pnvbo)
  64. {
  65. struct nouveau_bo *nvbo;
  66. u32 flags = 0;
  67. int ret;
  68. flags = TTM_PL_FLAG_TT;
  69. ret = nouveau_bo_new(dev, size, 0, flags, 0, 0,
  70. sg, pnvbo);
  71. if (ret)
  72. return ret;
  73. nvbo = *pnvbo;
  74. /* we restrict allowed domains on nv50+ to only the types
  75. * that were requested at creation time. not possibly on
  76. * earlier chips without busting the ABI.
  77. */
  78. nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART;
  79. nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
  80. if (!nvbo->gem) {
  81. nouveau_bo_ref(NULL, pnvbo);
  82. return -ENOMEM;
  83. }
  84. nvbo->gem->driver_private = nvbo;
  85. return 0;
  86. }
  87. struct dma_buf *nouveau_gem_prime_export(struct drm_device *dev,
  88. struct drm_gem_object *obj, int flags)
  89. {
  90. struct nouveau_bo *nvbo = nouveau_gem_object(obj);
  91. int ret = 0;
  92. /* pin buffer into GTT */
  93. ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_TT);
  94. if (ret)
  95. return ERR_PTR(-EINVAL);
  96. return dma_buf_export(nvbo, &nouveau_dmabuf_ops, obj->size, flags);
  97. }
  98. struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev,
  99. struct dma_buf *dma_buf)
  100. {
  101. struct dma_buf_attachment *attach;
  102. struct sg_table *sg;
  103. struct nouveau_bo *nvbo;
  104. int ret;
  105. if (dma_buf->ops == &nouveau_dmabuf_ops) {
  106. nvbo = dma_buf->priv;
  107. if (nvbo->gem) {
  108. if (nvbo->gem->dev == dev) {
  109. drm_gem_object_reference(nvbo->gem);
  110. return nvbo->gem;
  111. }
  112. }
  113. }
  114. /* need to attach */
  115. attach = dma_buf_attach(dma_buf, dev->dev);
  116. if (IS_ERR(attach))
  117. return ERR_PTR(PTR_ERR(attach));
  118. sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  119. if (IS_ERR(sg)) {
  120. ret = PTR_ERR(sg);
  121. goto fail_detach;
  122. }
  123. ret = nouveau_prime_new(dev, dma_buf->size, sg, &nvbo);
  124. if (ret)
  125. goto fail_unmap;
  126. nvbo->gem->import_attach = attach;
  127. return nvbo->gem;
  128. fail_unmap:
  129. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  130. fail_detach:
  131. dma_buf_detach(dma_buf, attach);
  132. return ERR_PTR(ret);
  133. }