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 | 944fc36 | 2014-07-09 22:08:15 -0400 | [diff] [blame] | 41 | return iommu_attach_device(iommu->domain, mmu->dev); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Rob Clark | d72ab59 | 2016-02-25 11:19:43 +0530 | [diff] [blame] | 44 | static void msm_iommu_detach(struct msm_mmu *mmu, const char * const *names, |
| 45 | int cnt) |
Stephane Viau | 87e956e | 2014-06-17 10:32:37 -0400 | [diff] [blame] | 46 | { |
| 47 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
Rob Clark | 944fc36 | 2014-07-09 22:08:15 -0400 | [diff] [blame] | 48 | iommu_detach_device(iommu->domain, mmu->dev); |
Stephane Viau | 87e956e | 2014-06-17 10:32:37 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 51 | static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova, |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 52 | struct sg_table *sgt, unsigned len, int prot) |
| 53 | { |
| 54 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
| 55 | struct iommu_domain *domain = iommu->domain; |
| 56 | struct scatterlist *sg; |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 57 | unsigned long da = iova; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 58 | unsigned int i, j; |
| 59 | int ret; |
| 60 | |
| 61 | if (!domain || !sgt) |
| 62 | return -EINVAL; |
| 63 | |
| 64 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
Archit Taneja | 69be1f4 | 2016-04-17 20:28:43 +0530 | [diff] [blame] | 65 | dma_addr_t pa = sg_phys(sg) - sg->offset; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 66 | size_t bytes = sg->length + sg->offset; |
| 67 | |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 68 | VERB("map[%d]: %08lx %08lx(%zx)", i, da, (unsigned long)pa, bytes); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 69 | |
| 70 | ret = iommu_map(domain, da, pa, bytes, prot); |
| 71 | if (ret) |
| 72 | goto fail; |
| 73 | |
| 74 | da += bytes; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | |
| 79 | fail: |
| 80 | da = iova; |
| 81 | |
| 82 | for_each_sg(sgt->sgl, sg, i, j) { |
| 83 | size_t bytes = sg->length + sg->offset; |
| 84 | iommu_unmap(domain, da, bytes); |
| 85 | da += bytes; |
| 86 | } |
| 87 | return ret; |
| 88 | } |
| 89 | |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 90 | static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 91 | struct sg_table *sgt, unsigned len) |
| 92 | { |
| 93 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
| 94 | struct iommu_domain *domain = iommu->domain; |
| 95 | struct scatterlist *sg; |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 96 | unsigned long da = iova; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 97 | int i; |
| 98 | |
| 99 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 100 | size_t bytes = sg->length + sg->offset; |
| 101 | size_t unmapped; |
| 102 | |
| 103 | unmapped = iommu_unmap(domain, da, bytes); |
| 104 | if (unmapped < bytes) |
| 105 | return unmapped; |
| 106 | |
Rob Clark | 78babc1 | 2016-11-11 12:06:46 -0500 | [diff] [blame] | 107 | VERB("unmap[%d]: %08lx(%zx)", i, da, bytes); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 108 | |
Fabian Frederick | cf3198c | 2014-06-15 00:24:26 +0200 | [diff] [blame] | 109 | BUG_ON(!PAGE_ALIGNED(bytes)); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 110 | |
| 111 | da += bytes; |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static void msm_iommu_destroy(struct msm_mmu *mmu) |
| 118 | { |
| 119 | struct msm_iommu *iommu = to_msm_iommu(mmu); |
| 120 | iommu_domain_free(iommu->domain); |
| 121 | kfree(iommu); |
| 122 | } |
| 123 | |
| 124 | static const struct msm_mmu_funcs funcs = { |
| 125 | .attach = msm_iommu_attach, |
Stephane Viau | 87e956e | 2014-06-17 10:32:37 -0400 | [diff] [blame] | 126 | .detach = msm_iommu_detach, |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 127 | .map = msm_iommu_map, |
| 128 | .unmap = msm_iommu_unmap, |
| 129 | .destroy = msm_iommu_destroy, |
| 130 | }; |
| 131 | |
Rob Clark | 944fc36 | 2014-07-09 22:08:15 -0400 | [diff] [blame] | 132 | 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] | 133 | { |
| 134 | struct msm_iommu *iommu; |
| 135 | |
| 136 | iommu = kzalloc(sizeof(*iommu), GFP_KERNEL); |
| 137 | if (!iommu) |
| 138 | return ERR_PTR(-ENOMEM); |
| 139 | |
| 140 | iommu->domain = domain; |
| 141 | msm_mmu_init(&iommu->base, dev, &funcs); |
Rob Clark | 7f8036b | 2016-12-07 11:13:53 -0500 | [diff] [blame^] | 142 | iommu_set_fault_handler(domain, msm_fault_handler, iommu); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 143 | |
| 144 | return &iommu->base; |
| 145 | } |