msm_iommu.c 3.5 KB

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