msm_gem_vma.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. if (ret)
  42. return ret;
  43. vma->iova = vma->node.start << PAGE_SHIFT;
  44. if (aspace->mmu) {
  45. unsigned size = npages << PAGE_SHIFT;
  46. ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
  47. size, IOMMU_READ | IOMMU_WRITE);
  48. }
  49. return ret;
  50. }
  51. void
  52. msm_gem_address_space_destroy(struct msm_gem_address_space *aspace)
  53. {
  54. drm_mm_takedown(&aspace->mm);
  55. if (aspace->mmu)
  56. aspace->mmu->funcs->destroy(aspace->mmu);
  57. kfree(aspace);
  58. }
  59. struct msm_gem_address_space *
  60. msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
  61. const char *name)
  62. {
  63. struct msm_gem_address_space *aspace;
  64. aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
  65. if (!aspace)
  66. return ERR_PTR(-ENOMEM);
  67. aspace->name = name;
  68. aspace->mmu = msm_iommu_new(dev, domain);
  69. drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
  70. (domain->geometry.aperture_end >> PAGE_SHIFT) - 1);
  71. return aspace;
  72. }