msm_iommu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2013 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_mmu.h"
  19. struct msm_iommu {
  20. struct msm_mmu base;
  21. struct iommu_domain *domain;
  22. };
  23. #define to_msm_iommu(x) container_of(x, struct msm_iommu, base)
  24. static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
  25. unsigned long iova, int flags, void *arg)
  26. {
  27. struct msm_iommu *iommu = arg;
  28. if (iommu->base.handler)
  29. return iommu->base.handler(iommu->base.arg, iova, flags);
  30. pr_warn_ratelimited("*** fault: iova=%08lx, flags=%d\n", iova, flags);
  31. return 0;
  32. }
  33. static int msm_iommu_attach(struct msm_mmu *mmu, const char * const *names,
  34. int cnt)
  35. {
  36. struct msm_iommu *iommu = to_msm_iommu(mmu);
  37. return iommu_attach_device(iommu->domain, mmu->dev);
  38. }
  39. static void msm_iommu_detach(struct msm_mmu *mmu, const char * const *names,
  40. int cnt)
  41. {
  42. struct msm_iommu *iommu = to_msm_iommu(mmu);
  43. iommu_detach_device(iommu->domain, mmu->dev);
  44. }
  45. static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
  46. struct sg_table *sgt, unsigned len, int prot)
  47. {
  48. struct msm_iommu *iommu = to_msm_iommu(mmu);
  49. struct iommu_domain *domain = iommu->domain;
  50. struct scatterlist *sg;
  51. unsigned long da = iova;
  52. unsigned int i, j;
  53. int ret;
  54. if (!domain || !sgt)
  55. return -EINVAL;
  56. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  57. dma_addr_t pa = sg_phys(sg) - sg->offset;
  58. size_t bytes = sg->length + sg->offset;
  59. VERB("map[%d]: %08lx %08lx(%zx)", i, da, (unsigned long)pa, bytes);
  60. ret = iommu_map(domain, da, pa, bytes, prot);
  61. if (ret)
  62. goto fail;
  63. da += bytes;
  64. }
  65. return 0;
  66. fail:
  67. da = iova;
  68. for_each_sg(sgt->sgl, sg, i, j) {
  69. size_t bytes = sg->length + sg->offset;
  70. iommu_unmap(domain, da, bytes);
  71. da += bytes;
  72. }
  73. return ret;
  74. }
  75. static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova,
  76. struct sg_table *sgt, unsigned len)
  77. {
  78. struct msm_iommu *iommu = to_msm_iommu(mmu);
  79. struct iommu_domain *domain = iommu->domain;
  80. struct scatterlist *sg;
  81. unsigned long da = iova;
  82. int i;
  83. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  84. size_t bytes = sg->length + sg->offset;
  85. size_t unmapped;
  86. unmapped = iommu_unmap(domain, da, bytes);
  87. if (unmapped < bytes)
  88. return unmapped;
  89. VERB("unmap[%d]: %08lx(%zx)", i, da, bytes);
  90. BUG_ON(!PAGE_ALIGNED(bytes));
  91. da += bytes;
  92. }
  93. return 0;
  94. }
  95. static void msm_iommu_destroy(struct msm_mmu *mmu)
  96. {
  97. struct msm_iommu *iommu = to_msm_iommu(mmu);
  98. iommu_domain_free(iommu->domain);
  99. kfree(iommu);
  100. }
  101. static const struct msm_mmu_funcs funcs = {
  102. .attach = msm_iommu_attach,
  103. .detach = msm_iommu_detach,
  104. .map = msm_iommu_map,
  105. .unmap = msm_iommu_unmap,
  106. .destroy = msm_iommu_destroy,
  107. };
  108. struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain)
  109. {
  110. struct msm_iommu *iommu;
  111. iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
  112. if (!iommu)
  113. return ERR_PTR(-ENOMEM);
  114. iommu->domain = domain;
  115. msm_mmu_init(&iommu->base, dev, &funcs);
  116. iommu_set_fault_handler(domain, msm_fault_handler, iommu);
  117. return &iommu->base;
  118. }