blob: 6583fadc6e5b51fe1759927e77e3dd0fc5534f66 [file] [log] [blame]
Rob Clark871d8122013-11-16 12:56:06 -05001/*
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
18#include "msm_drv.h"
19#include "msm_mmu.h"
20
21struct msm_iommu {
22 struct msm_mmu base;
23 struct iommu_domain *domain;
24};
25#define to_msm_iommu(x) container_of(x, struct msm_iommu, base)
26
Rob Clark7f8036b2016-12-07 11:13:53 -050027static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
Rob Clark871d8122013-11-16 12:56:06 -050028 unsigned long iova, int flags, void *arg)
29{
Rob Clark7f8036b2016-12-07 11:13:53 -050030 struct msm_iommu *iommu = arg;
31 if (iommu->base.handler)
32 return iommu->base.handler(iommu->base.arg, iova, flags);
Rob Clark6814dbf2014-08-09 09:07:25 -040033 pr_warn_ratelimited("*** fault: iova=%08lx, flags=%d\n", iova, flags);
34 return 0;
Rob Clark871d8122013-11-16 12:56:06 -050035}
36
Rob Clarkd72ab592016-02-25 11:19:43 +053037static int msm_iommu_attach(struct msm_mmu *mmu, const char * const *names,
38 int cnt)
Rob Clark871d8122013-11-16 12:56:06 -050039{
Rob Clark871d8122013-11-16 12:56:06 -050040 struct msm_iommu *iommu = to_msm_iommu(mmu);
Rob Clark944fc362014-07-09 22:08:15 -040041 return iommu_attach_device(iommu->domain, mmu->dev);
Rob Clark871d8122013-11-16 12:56:06 -050042}
43
Rob Clarkd72ab592016-02-25 11:19:43 +053044static void msm_iommu_detach(struct msm_mmu *mmu, const char * const *names,
45 int cnt)
Stephane Viau87e956e2014-06-17 10:32:37 -040046{
47 struct msm_iommu *iommu = to_msm_iommu(mmu);
Rob Clark944fc362014-07-09 22:08:15 -040048 iommu_detach_device(iommu->domain, mmu->dev);
Stephane Viau87e956e2014-06-17 10:32:37 -040049}
50
Rob Clark78babc12016-11-11 12:06:46 -050051static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
Rob Clark871d8122013-11-16 12:56:06 -050052 struct sg_table *sgt, unsigned len, int prot)
53{
54 struct msm_iommu *iommu = to_msm_iommu(mmu);
Rob Clarkf9000042017-02-15 15:44:04 -050055 size_t ret;
Rob Clark871d8122013-11-16 12:56:06 -050056
Rob Clarkf9000042017-02-15 15:44:04 -050057 ret = iommu_map_sg(iommu->domain, iova, sgt->sgl, sgt->nents, prot);
58 WARN_ON(ret < 0);
Rob Clark871d8122013-11-16 12:56:06 -050059
Rob Clarkf9000042017-02-15 15:44:04 -050060 return (ret == len) ? 0 : -EINVAL;
Rob Clark871d8122013-11-16 12:56:06 -050061}
62
Rob Clark78babc12016-11-11 12:06:46 -050063static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova,
Rob Clark871d8122013-11-16 12:56:06 -050064 struct sg_table *sgt, unsigned len)
65{
66 struct msm_iommu *iommu = to_msm_iommu(mmu);
Rob Clark871d8122013-11-16 12:56:06 -050067
Rob Clarkf9000042017-02-15 15:44:04 -050068 iommu_unmap(iommu->domain, iova, len);
Rob Clark871d8122013-11-16 12:56:06 -050069
70 return 0;
71}
72
73static void msm_iommu_destroy(struct msm_mmu *mmu)
74{
75 struct msm_iommu *iommu = to_msm_iommu(mmu);
76 iommu_domain_free(iommu->domain);
77 kfree(iommu);
78}
79
80static const struct msm_mmu_funcs funcs = {
81 .attach = msm_iommu_attach,
Stephane Viau87e956e2014-06-17 10:32:37 -040082 .detach = msm_iommu_detach,
Rob Clark871d8122013-11-16 12:56:06 -050083 .map = msm_iommu_map,
84 .unmap = msm_iommu_unmap,
85 .destroy = msm_iommu_destroy,
86};
87
Rob Clark944fc362014-07-09 22:08:15 -040088struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain)
Rob Clark871d8122013-11-16 12:56:06 -050089{
90 struct msm_iommu *iommu;
91
92 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
93 if (!iommu)
94 return ERR_PTR(-ENOMEM);
95
96 iommu->domain = domain;
97 msm_mmu_init(&iommu->base, dev, &funcs);
Rob Clark7f8036b2016-12-07 11:13:53 -050098 iommu_set_fault_handler(domain, msm_fault_handler, iommu);
Rob Clark871d8122013-11-16 12:56:06 -050099
100 return &iommu->base;
101}