msm_iommu.c 3.9 KB

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