Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 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 | |
| 18 | #include "msm_drv.h" |
| 19 | #include "msm_mmu.h" |
| 20 | |
| 21 | struct 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 Clark | 7f8036b | 2016-12-07 11:13:53 -0500 | [diff] [blame] | 27 | static int msm_fault_handler(struct iommu_domain *domain, struct device *dev, |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 28 | unsigned long iova, int flags, void *arg) |
| 29 | { |
Rob Clark | 7f8036b | 2016-12-07 11:13:53 -0500 | [diff] [blame] | 30 | struct msm_iommu *iommu = arg; |
| 31 | if (iommu->base.handler) |
| 32 | return iommu->base.handler(iommu->base.arg, iova, flags); |
Rob Clark | 6814dbf | 2014-08-09 09:07:25 -0400 | [diff] [blame] | 33 | pr_warn_ratelimited("*** fault: iova=%08lx, flags=%d\n", iova, flags); |
| 34 | return 0; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Rob Clark | d72ab59 | 2016-02-25 11:19:43 +0530 | [diff] [blame] | 37 | static int msm_iommu_attach(struct msm_mmu *mmu, const char * const *names, |
| 38 | int cnt) |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 39 | { |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 40 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
Rob Clark | cc69272 | 2017-02-15 16:00:25 -0500 | [diff] [blame^] | 41 | int ret; |
| 42 | |
| 43 | pm_runtime_get_sync(mmu->dev); |
| 44 | ret = iommu_attach_device(iommu->domain, mmu->dev); |
| 45 | pm_runtime_put_sync(mmu->dev); |
| 46 | |
| 47 | return ret; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Rob Clark | d72ab59 | 2016-02-25 11:19:43 +0530 | [diff] [blame] | 50 | static void msm_iommu_detach(struct msm_mmu *mmu, const char * const *names, |
| 51 | int cnt) |
Stephane Viau | 87e956e | 2014-06-17 10:32:37 -0400 | [diff] [blame] | 52 | { |
| 53 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
Rob Clark | cc69272 | 2017-02-15 16:00:25 -0500 | [diff] [blame^] | 54 | |
| 55 | pm_runtime_get_sync(mmu->dev); |
Rob Clark | 944fc36 | 2014-07-09 22:08:15 -0400 | [diff] [blame] | 56 | iommu_detach_device(iommu->domain, mmu->dev); |
Rob Clark | cc69272 | 2017-02-15 16:00:25 -0500 | [diff] [blame^] | 57 | pm_runtime_put_sync(mmu->dev); |
Stephane Viau | 87e956e | 2014-06-17 10:32:37 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 60 | static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova, |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 61 | struct sg_table *sgt, unsigned len, int prot) |
| 62 | { |
| 63 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
Rob Clark | f900004 | 2017-02-15 15:44:04 -0500 | [diff] [blame] | 64 | size_t ret; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 65 | |
Rob Clark | cc69272 | 2017-02-15 16:00:25 -0500 | [diff] [blame^] | 66 | // pm_runtime_get_sync(mmu->dev); |
Rob Clark | f900004 | 2017-02-15 15:44:04 -0500 | [diff] [blame] | 67 | ret = iommu_map_sg(iommu->domain, iova, sgt->sgl, sgt->nents, prot); |
Rob Clark | cc69272 | 2017-02-15 16:00:25 -0500 | [diff] [blame^] | 68 | // pm_runtime_put_sync(mmu->dev); |
Rob Clark | f900004 | 2017-02-15 15:44:04 -0500 | [diff] [blame] | 69 | WARN_ON(ret < 0); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 70 | |
Rob Clark | f900004 | 2017-02-15 15:44:04 -0500 | [diff] [blame] | 71 | return (ret == len) ? 0 : -EINVAL; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 74 | static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 75 | struct sg_table *sgt, unsigned len) |
| 76 | { |
| 77 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 78 | |
Rob Clark | cc69272 | 2017-02-15 16:00:25 -0500 | [diff] [blame^] | 79 | pm_runtime_get_sync(mmu->dev); |
Rob Clark | f900004 | 2017-02-15 15:44:04 -0500 | [diff] [blame] | 80 | iommu_unmap(iommu->domain, iova, len); |
Rob Clark | cc69272 | 2017-02-15 16:00:25 -0500 | [diff] [blame^] | 81 | pm_runtime_put_sync(mmu->dev); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static void msm_iommu_destroy(struct msm_mmu *mmu) |
| 87 | { |
| 88 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
| 89 | iommu_domain_free(iommu->domain); |
| 90 | kfree(iommu); |
| 91 | } |
| 92 | |
| 93 | static const struct msm_mmu_funcs funcs = { |
| 94 | .attach = msm_iommu_attach, |
Stephane Viau | 87e956e | 2014-06-17 10:32:37 -0400 | [diff] [blame] | 95 | .detach = msm_iommu_detach, |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 96 | .map = msm_iommu_map, |
| 97 | .unmap = msm_iommu_unmap, |
| 98 | .destroy = msm_iommu_destroy, |
| 99 | }; |
| 100 | |
Rob Clark | 944fc36 | 2014-07-09 22:08:15 -0400 | [diff] [blame] | 101 | struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain) |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 102 | { |
| 103 | struct msm_iommu *iommu; |
| 104 | |
| 105 | iommu = kzalloc(sizeof(*iommu), GFP_KERNEL); |
| 106 | if (!iommu) |
| 107 | return ERR_PTR(-ENOMEM); |
| 108 | |
| 109 | iommu->domain = domain; |
| 110 | msm_mmu_init(&iommu->base, dev, &funcs); |
Rob Clark | 7f8036b | 2016-12-07 11:13:53 -0500 | [diff] [blame] | 111 | iommu_set_fault_handler(domain, msm_fault_handler, iommu); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 112 | |
| 113 | return &iommu->base; |
| 114 | } |