msm_iommu.c 3.4 KB

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