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 | |
| 27 | static int msm_fault_handler(struct iommu_domain *iommu, struct device *dev, |
| 28 | unsigned long iova, int flags, void *arg) |
| 29 | { |
Rob Clark | 6814dbf | 2014-08-09 09:07:25 -0400 | [diff] [blame] | 30 | pr_warn_ratelimited("*** fault: iova=%08lx, flags=%d\n", iova, flags); |
| 31 | return 0; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static int msm_iommu_attach(struct msm_mmu *mmu, const char **names, int cnt) |
| 35 | { |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 36 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
Rob Clark | 944fc36 | 2014-07-09 22:08:15 -0400 | [diff] [blame] | 37 | return iommu_attach_device(iommu->domain, mmu->dev); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Stephane Viau | 87e956e | 2014-06-17 10:32:37 -0400 | [diff] [blame] | 40 | static void msm_iommu_detach(struct msm_mmu *mmu, const char **names, int cnt) |
| 41 | { |
| 42 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
Rob Clark | 944fc36 | 2014-07-09 22:08:15 -0400 | [diff] [blame] | 43 | iommu_detach_device(iommu->domain, mmu->dev); |
Stephane Viau | 87e956e | 2014-06-17 10:32:37 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 46 | static int msm_iommu_map(struct msm_mmu *mmu, uint32_t iova, |
| 47 | struct sg_table *sgt, unsigned len, int prot) |
| 48 | { |
| 49 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
| 50 | struct iommu_domain *domain = iommu->domain; |
| 51 | struct scatterlist *sg; |
| 52 | unsigned int da = iova; |
| 53 | unsigned int i, j; |
| 54 | int ret; |
| 55 | |
| 56 | if (!domain || !sgt) |
| 57 | return -EINVAL; |
| 58 | |
| 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 | |
Thierry Reding | fc99f97 | 2015-04-09 16:39:51 +0200 | [diff] [blame] | 63 | VERB("map[%d]: %08x %08x(%zx)", i, iova, pa, bytes); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 64 | |
| 65 | ret = iommu_map(domain, da, pa, bytes, prot); |
| 66 | if (ret) |
| 67 | goto fail; |
| 68 | |
| 69 | da += bytes; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | |
| 74 | fail: |
| 75 | da = iova; |
| 76 | |
| 77 | for_each_sg(sgt->sgl, sg, i, j) { |
| 78 | size_t bytes = sg->length + sg->offset; |
| 79 | iommu_unmap(domain, da, bytes); |
| 80 | da += bytes; |
| 81 | } |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | static int msm_iommu_unmap(struct msm_mmu *mmu, uint32_t iova, |
| 86 | struct sg_table *sgt, unsigned len) |
| 87 | { |
| 88 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
| 89 | struct iommu_domain *domain = iommu->domain; |
| 90 | struct scatterlist *sg; |
| 91 | unsigned int da = iova; |
| 92 | int i; |
| 93 | |
| 94 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 95 | size_t bytes = sg->length + sg->offset; |
| 96 | size_t unmapped; |
| 97 | |
| 98 | unmapped = iommu_unmap(domain, da, bytes); |
| 99 | if (unmapped < bytes) |
| 100 | return unmapped; |
| 101 | |
Thierry Reding | fc99f97 | 2015-04-09 16:39:51 +0200 | [diff] [blame] | 102 | VERB("unmap[%d]: %08x(%zx)", i, iova, bytes); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 103 | |
Fabian Frederick | cf3198c | 2014-06-15 00:24:26 +0200 | [diff] [blame] | 104 | BUG_ON(!PAGE_ALIGNED(bytes)); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 105 | |
| 106 | da += bytes; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 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 | |
| 119 | static const struct msm_mmu_funcs funcs = { |
| 120 | .attach = msm_iommu_attach, |
Stephane Viau | 87e956e | 2014-06-17 10:32:37 -0400 | [diff] [blame] | 121 | .detach = msm_iommu_detach, |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 122 | .map = msm_iommu_map, |
| 123 | .unmap = msm_iommu_unmap, |
| 124 | .destroy = msm_iommu_destroy, |
| 125 | }; |
| 126 | |
Rob Clark | 944fc36 | 2014-07-09 22:08:15 -0400 | [diff] [blame] | 127 | 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] | 128 | { |
| 129 | struct msm_iommu *iommu; |
| 130 | |
| 131 | iommu = kzalloc(sizeof(*iommu), GFP_KERNEL); |
| 132 | if (!iommu) |
| 133 | return ERR_PTR(-ENOMEM); |
| 134 | |
| 135 | iommu->domain = domain; |
| 136 | msm_mmu_init(&iommu->base, dev, &funcs); |
| 137 | iommu_set_fault_handler(domain, msm_fault_handler, dev); |
| 138 | |
| 139 | return &iommu->base; |
| 140 | } |