msm_gem_vma.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. void
  21. msm_gem_unmap_vma(struct msm_gem_address_space *aspace,
  22. struct msm_gem_vma *vma, struct sg_table *sgt)
  23. {
  24. if (!vma->iova)
  25. return;
  26. if (aspace->mmu) {
  27. unsigned size = vma->node.size << PAGE_SHIFT;
  28. aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, sgt, size);
  29. }
  30. drm_mm_remove_node(&vma->node);
  31. vma->iova = 0;
  32. }
  33. int
  34. msm_gem_map_vma(struct msm_gem_address_space *aspace,
  35. struct msm_gem_vma *vma, struct sg_table *sgt, int npages)
  36. {
  37. int ret;
  38. if (WARN_ON(drm_mm_node_allocated(&vma->node)))
  39. return 0;
  40. ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages,
  41. 0, DRM_MM_SEARCH_DEFAULT);
  42. if (ret)
  43. return ret;
  44. vma->iova = vma->node.start << PAGE_SHIFT;
  45. if (aspace->mmu) {
  46. unsigned size = npages << PAGE_SHIFT;
  47. ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
  48. size, IOMMU_READ | IOMMU_WRITE);
  49. }
  50. return ret;
  51. }
  52. void
  53. msm_gem_address_space_destroy(struct msm_gem_address_space *aspace)
  54. {
  55. drm_mm_takedown(&aspace->mm);
  56. if (aspace->mmu)
  57. aspace->mmu->funcs->destroy(aspace->mmu);
  58. kfree(aspace);
  59. }
  60. struct msm_gem_address_space *
  61. msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
  62. const char *name)
  63. {
  64. struct msm_gem_address_space *aspace;
  65. aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
  66. if (!aspace)
  67. return ERR_PTR(-ENOMEM);
  68. aspace->name = name;
  69. aspace->mmu = msm_iommu_new(dev, domain);
  70. drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
  71. (domain->geometry.aperture_end >> PAGE_SHIFT) - 1);
  72. return aspace;
  73. }