msm_gem_vma.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2016 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "msm_drv.h"
  18. #include "msm_gem.h"
  19. #include "msm_mmu.h"
  20. static void
  21. msm_gem_address_space_destroy(struct kref *kref)
  22. {
  23. struct msm_gem_address_space *aspace = container_of(kref,
  24. struct msm_gem_address_space, kref);
  25. drm_mm_takedown(&aspace->mm);
  26. if (aspace->mmu)
  27. aspace->mmu->funcs->destroy(aspace->mmu);
  28. kfree(aspace);
  29. }
  30. void msm_gem_address_space_put(struct msm_gem_address_space *aspace)
  31. {
  32. if (aspace)
  33. kref_put(&aspace->kref, msm_gem_address_space_destroy);
  34. }
  35. void
  36. msm_gem_unmap_vma(struct msm_gem_address_space *aspace,
  37. struct msm_gem_vma *vma, struct sg_table *sgt)
  38. {
  39. if (!aspace || !vma->iova)
  40. return;
  41. if (aspace->mmu) {
  42. unsigned size = vma->node.size << PAGE_SHIFT;
  43. aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, sgt, size);
  44. }
  45. spin_lock(&aspace->lock);
  46. drm_mm_remove_node(&vma->node);
  47. spin_unlock(&aspace->lock);
  48. vma->iova = 0;
  49. msm_gem_address_space_put(aspace);
  50. }
  51. int
  52. msm_gem_map_vma(struct msm_gem_address_space *aspace,
  53. struct msm_gem_vma *vma, struct sg_table *sgt, int npages)
  54. {
  55. int ret;
  56. spin_lock(&aspace->lock);
  57. if (WARN_ON(drm_mm_node_allocated(&vma->node))) {
  58. spin_unlock(&aspace->lock);
  59. return 0;
  60. }
  61. ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages);
  62. spin_unlock(&aspace->lock);
  63. if (ret)
  64. return ret;
  65. vma->iova = vma->node.start << PAGE_SHIFT;
  66. if (aspace->mmu) {
  67. unsigned size = npages << PAGE_SHIFT;
  68. ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
  69. size, IOMMU_READ | IOMMU_WRITE);
  70. }
  71. /* Get a reference to the aspace to keep it around */
  72. kref_get(&aspace->kref);
  73. return ret;
  74. }
  75. struct msm_gem_address_space *
  76. msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
  77. const char *name)
  78. {
  79. struct msm_gem_address_space *aspace;
  80. u64 size = domain->geometry.aperture_end -
  81. domain->geometry.aperture_start;
  82. aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
  83. if (!aspace)
  84. return ERR_PTR(-ENOMEM);
  85. spin_lock_init(&aspace->lock);
  86. aspace->name = name;
  87. aspace->mmu = msm_iommu_new(dev, domain);
  88. drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
  89. size >> PAGE_SHIFT);
  90. kref_init(&aspace->kref);
  91. return aspace;
  92. }