Olav Haugan | ab77b1b | 2012-02-28 09:19:22 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <mach/msm_subsystem_map.h> |
| 14 | #include <linux/memory_alloc.h> |
| 15 | #include <linux/iommu.h> |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 17 | #include <linux/vmalloc.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 18 | #include <asm/sizes.h> |
| 19 | #include <asm/page.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <mach/iommu.h> |
| 22 | #include <mach/iommu_domains.h> |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 23 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 25 | /* dummy 4k for overmapping */ |
| 26 | char iommu_dummy[2*PAGE_SIZE-4]; |
| 27 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 28 | struct msm_iommu_domain_state { |
| 29 | struct msm_iommu_domain *domains; |
| 30 | int ndomains; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 33 | static struct msm_iommu_domain_state domain_state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 34 | |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 35 | int msm_iommu_map_extra(struct iommu_domain *domain, |
| 36 | unsigned long start_iova, |
| 37 | unsigned long size, |
| 38 | int cached) |
| 39 | { |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 40 | int i, ret = 0; |
| 41 | struct scatterlist *sglist; |
| 42 | unsigned int nrpages = PFN_ALIGN(size) >> PAGE_SHIFT; |
| 43 | struct page *dummy_page = phys_to_page( |
| 44 | PFN_ALIGN(virt_to_phys(iommu_dummy))); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 45 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 46 | sglist = vmalloc(sizeof(*sglist) * nrpages); |
| 47 | if (!sglist) { |
| 48 | ret = -ENOMEM; |
| 49 | goto err1; |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 52 | sg_init_table(sglist, nrpages); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 53 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 54 | for (i = 0; i < nrpages; i++) |
| 55 | sg_set_page(&sglist[i], dummy_page, PAGE_SIZE, 0); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 56 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 57 | ret = iommu_map_range(domain, start_iova, sglist, size, cached); |
| 58 | if (ret) { |
| 59 | pr_err("%s: could not map extra %lx in domain %p\n", |
| 60 | __func__, start_iova, domain); |
| 61 | } |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 62 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 63 | vfree(sglist); |
| 64 | err1: |
| 65 | return ret; |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 68 | static int msm_iommu_map_iova_phys(struct iommu_domain *domain, |
| 69 | unsigned long iova, |
| 70 | unsigned long phys, |
| 71 | unsigned long size, |
| 72 | int cached) |
| 73 | { |
| 74 | int ret; |
| 75 | struct scatterlist *sglist; |
| 76 | |
| 77 | sglist = vmalloc(sizeof(*sglist)); |
| 78 | if (!sglist) { |
| 79 | ret = -ENOMEM; |
| 80 | goto err1; |
| 81 | } |
| 82 | |
| 83 | sg_init_table(sglist, 1); |
| 84 | sglist->length = size; |
| 85 | sglist->offset = 0; |
| 86 | sglist->dma_address = phys; |
| 87 | |
| 88 | ret = iommu_map_range(domain, iova, sglist, size, cached); |
| 89 | if (ret) { |
| 90 | pr_err("%s: could not map extra %lx in domain %p\n", |
| 91 | __func__, iova, domain); |
| 92 | } |
| 93 | |
| 94 | vfree(sglist); |
| 95 | err1: |
| 96 | return ret; |
| 97 | |
| 98 | } |
| 99 | |
| 100 | int msm_iommu_map_contig_buffer(unsigned long phys, |
| 101 | unsigned int domain_no, |
| 102 | unsigned int partition_no, |
| 103 | unsigned long size, |
| 104 | unsigned long align, |
| 105 | unsigned long cached, |
| 106 | unsigned long *iova_val) |
| 107 | { |
| 108 | unsigned long iova; |
| 109 | int ret; |
| 110 | |
| 111 | if (size & (align - 1)) |
| 112 | return -EINVAL; |
| 113 | |
| 114 | iova = msm_allocate_iova_address(domain_no, partition_no, size, align); |
| 115 | |
| 116 | if (!iova) |
| 117 | return -ENOMEM; |
| 118 | |
| 119 | ret = msm_iommu_map_iova_phys(msm_get_iommu_domain(domain_no), iova, |
| 120 | phys, size, cached); |
| 121 | |
| 122 | if (ret) |
| 123 | msm_free_iova_address(iova, domain_no, partition_no, size); |
| 124 | else |
| 125 | *iova_val = iova; |
| 126 | |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | void msm_iommu_unmap_contig_buffer(unsigned long iova, |
| 131 | unsigned int domain_no, |
| 132 | unsigned int partition_no, |
| 133 | unsigned long size) |
| 134 | { |
| 135 | iommu_unmap_range(msm_get_iommu_domain(domain_no), iova, size); |
| 136 | msm_free_iova_address(iova, domain_no, partition_no, size); |
| 137 | } |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 138 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 139 | struct iommu_domain *msm_get_iommu_domain(int domain_num) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 140 | { |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 141 | if (domain_num >= 0 && domain_num < domain_state.ndomains) |
| 142 | return domain_state.domains[domain_num].domain; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 143 | else |
| 144 | return NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 147 | unsigned long msm_allocate_iova_address(unsigned int iommu_domain, |
| 148 | unsigned int partition_no, |
| 149 | unsigned long size, |
| 150 | unsigned long align) |
| 151 | { |
| 152 | struct mem_pool *pool; |
| 153 | unsigned long iova; |
| 154 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 155 | if (iommu_domain >= domain_state.ndomains) |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 158 | if (partition_no >= domain_state.domains[iommu_domain].npools) |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 161 | pool = &domain_state.domains[iommu_domain].iova_pools[partition_no]; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 162 | |
| 163 | if (!pool->gpool) |
| 164 | return 0; |
| 165 | |
| 166 | iova = gen_pool_alloc_aligned(pool->gpool, size, ilog2(align)); |
| 167 | if (iova) |
| 168 | pool->free -= size; |
| 169 | |
| 170 | return iova; |
| 171 | } |
| 172 | |
| 173 | void msm_free_iova_address(unsigned long iova, |
| 174 | unsigned int iommu_domain, |
| 175 | unsigned int partition_no, |
| 176 | unsigned long size) |
| 177 | { |
| 178 | struct mem_pool *pool; |
| 179 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 180 | if (iommu_domain >= domain_state.ndomains) { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 181 | WARN(1, "Invalid domain %d\n", iommu_domain); |
| 182 | return; |
| 183 | } |
| 184 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 185 | if (partition_no >= domain_state.domains[iommu_domain].npools) { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 186 | WARN(1, "Invalid partition %d for domain %d\n", |
| 187 | partition_no, iommu_domain); |
| 188 | return; |
| 189 | } |
| 190 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 191 | pool = &domain_state.domains[iommu_domain].iova_pools[partition_no]; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 192 | |
| 193 | if (!pool) |
| 194 | return; |
| 195 | |
| 196 | pool->free += size; |
| 197 | gen_pool_free(pool->gpool, iova, size); |
| 198 | } |
| 199 | |
| 200 | int msm_use_iommu() |
| 201 | { |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 202 | /* |
| 203 | * If there are no domains, don't bother trying to use the iommu |
| 204 | */ |
| 205 | return domain_state.ndomains && iommu_found(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 208 | static int __init iommu_domain_probe(struct platform_device *pdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | { |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 210 | struct iommu_domains_pdata *p = pdev->dev.platform_data; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 211 | int i, j; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 212 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 213 | if (!p) |
| 214 | return -ENODEV; |
| 215 | |
| 216 | domain_state.domains = p->domains; |
| 217 | domain_state.ndomains = p->ndomains; |
| 218 | |
| 219 | for (i = 0; i < domain_state.ndomains; i++) { |
| 220 | domain_state.domains[i].domain = iommu_domain_alloc( |
| 221 | p->domain_alloc_flags); |
| 222 | if (!domain_state.domains[i].domain) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 223 | continue; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 224 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 225 | for (j = 0; j < domain_state.domains[i].npools; j++) { |
| 226 | struct mem_pool *pool = &domain_state.domains[i]. |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 227 | iova_pools[j]; |
| 228 | mutex_init(&pool->pool_mutex); |
Olav Haugan | 2d19103 | 2012-02-28 09:46:31 -0800 | [diff] [blame] | 229 | if (pool->size) { |
| 230 | pool->gpool = gen_pool_create(PAGE_SHIFT, -1); |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 231 | |
Olav Haugan | 2d19103 | 2012-02-28 09:46:31 -0800 | [diff] [blame] | 232 | if (!pool->gpool) { |
| 233 | pr_err("%s: could not allocate pool\n", |
| 234 | __func__); |
| 235 | pr_err("%s: domain %d iova space %d\n", |
| 236 | __func__, i, j); |
| 237 | continue; |
| 238 | } |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 239 | |
Olav Haugan | 2d19103 | 2012-02-28 09:46:31 -0800 | [diff] [blame] | 240 | if (gen_pool_add(pool->gpool, pool->paddr, |
| 241 | pool->size, -1)) { |
| 242 | pr_err("%s: could not add memory\n", |
| 243 | __func__); |
| 244 | pr_err("%s: domain %d pool %d\n", |
| 245 | __func__, i, j); |
| 246 | gen_pool_destroy(pool->gpool); |
| 247 | pool->gpool = NULL; |
| 248 | continue; |
| 249 | } |
| 250 | } else { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 251 | pool->gpool = NULL; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 252 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 256 | for (i = 0; i < p->nnames; i++) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 257 | int domain_idx; |
| 258 | struct device *ctx = msm_iommu_get_ctx( |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 259 | p->domain_names[i].name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 260 | |
| 261 | if (!ctx) |
| 262 | continue; |
| 263 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 264 | domain_idx = p->domain_names[i].domain; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 265 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 266 | if (!domain_state.domains[domain_idx].domain) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 267 | continue; |
| 268 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 269 | if (iommu_attach_device(domain_state.domains[domain_idx].domain, |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 270 | ctx)) { |
| 271 | WARN(1, "%s: could not attach domain %d to context %s." |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 272 | " iommu programming will not occur.\n", |
| 273 | __func__, domain_idx, |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 274 | p->domain_names[i].name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 275 | continue; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 281 | |
| 282 | static struct platform_driver iommu_domain_driver = { |
| 283 | .driver = { |
| 284 | .name = "iommu_domains", |
| 285 | .owner = THIS_MODULE |
| 286 | }, |
| 287 | }; |
| 288 | |
| 289 | static int __init msm_subsystem_iommu_init(void) |
| 290 | { |
| 291 | return platform_driver_probe(&iommu_domain_driver, iommu_domain_probe); |
| 292 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 293 | device_initcall(msm_subsystem_iommu_init); |