msm_gem_vma.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 (!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. drm_mm_remove_node(&vma->node);
  46. vma->iova = 0;
  47. msm_gem_address_space_put(aspace);
  48. }
  49. int
  50. msm_gem_map_vma(struct msm_gem_address_space *aspace,
  51. struct msm_gem_vma *vma, struct sg_table *sgt, int npages)
  52. {
  53. int ret;
  54. if (WARN_ON(drm_mm_node_allocated(&vma->node)))
  55. return 0;
  56. ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages);
  57. if (ret)
  58. return ret;
  59. vma->iova = vma->node.start << PAGE_SHIFT;
  60. if (aspace->mmu) {
  61. unsigned size = npages << PAGE_SHIFT;
  62. ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
  63. size, IOMMU_READ | IOMMU_WRITE);
  64. }
  65. /* Get a reference to the aspace to keep it around */
  66. kref_get(&aspace->kref);
  67. return ret;
  68. }
  69. struct msm_gem_address_space *
  70. msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
  71. const char *name)
  72. {
  73. struct msm_gem_address_space *aspace;
  74. aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
  75. if (!aspace)
  76. return ERR_PTR(-ENOMEM);
  77. aspace->name = name;
  78. aspace->mmu = msm_iommu_new(dev, domain);
  79. drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
  80. (domain->geometry.aperture_end >> PAGE_SHIFT) - 1);
  81. kref_init(&aspace->kref);
  82. return aspace;
  83. }