msm_iommu.c 3.4 KB

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