msm_iommu.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <[email protected]>
  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. int ret;
  38. pm_runtime_get_sync(mmu->dev);
  39. ret = iommu_attach_device(iommu->domain, mmu->dev);
  40. pm_runtime_put_sync(mmu->dev);
  41. return ret;
  42. }
  43. static void msm_iommu_detach(struct msm_mmu *mmu, const char * const *names,
  44. int cnt)
  45. {
  46. struct msm_iommu *iommu = to_msm_iommu(mmu);
  47. pm_runtime_get_sync(mmu->dev);
  48. iommu_detach_device(iommu->domain, mmu->dev);
  49. pm_runtime_put_sync(mmu->dev);
  50. }
  51. static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
  52. struct sg_table *sgt, unsigned len, int prot)
  53. {
  54. struct msm_iommu *iommu = to_msm_iommu(mmu);
  55. size_t ret;
  56. // pm_runtime_get_sync(mmu->dev);
  57. ret = iommu_map_sg(iommu->domain, iova, sgt->sgl, sgt->orig_nents, prot);
  58. // pm_runtime_put_sync(mmu->dev);
  59. WARN_ON(!ret);
  60. return (ret == len) ? 0 : -EINVAL;
  61. }
  62. static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova,
  63. struct sg_table *sgt, unsigned len)
  64. {
  65. struct msm_iommu *iommu = to_msm_iommu(mmu);
  66. pm_runtime_get_sync(mmu->dev);
  67. iommu_unmap(iommu->domain, iova, len);
  68. pm_runtime_put_sync(mmu->dev);
  69. return 0;
  70. }
  71. static void msm_iommu_destroy(struct msm_mmu *mmu)
  72. {
  73. struct msm_iommu *iommu = to_msm_iommu(mmu);
  74. iommu_domain_free(iommu->domain);
  75. kfree(iommu);
  76. }
  77. static const struct msm_mmu_funcs funcs = {
  78. .attach = msm_iommu_attach,
  79. .detach = msm_iommu_detach,
  80. .map = msm_iommu_map,
  81. .unmap = msm_iommu_unmap,
  82. .destroy = msm_iommu_destroy,
  83. };
  84. struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain)
  85. {
  86. struct msm_iommu *iommu;
  87. iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
  88. if (!iommu)
  89. return ERR_PTR(-ENOMEM);
  90. iommu->domain = domain;
  91. msm_mmu_init(&iommu->base, dev, &funcs);
  92. iommu_set_fault_handler(domain, msm_fault_handler, iommu);
  93. return &iommu->base;
  94. }