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 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 14 | #include <linux/init.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 15 | #include <linux/iommu.h> |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 16 | #include <linux/memory_alloc.h> |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 18 | #include <linux/vmalloc.h> |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 19 | #include <linux/rbtree.h> |
| 20 | #include <linux/slab.h> |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 21 | #include <linux/vmalloc.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 22 | #include <asm/sizes.h> |
| 23 | #include <asm/page.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 24 | #include <mach/iommu.h> |
| 25 | #include <mach/iommu_domains.h> |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 26 | #include <mach/socinfo.h> |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 27 | #include <mach/msm_subsystem_map.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 28 | |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 29 | /* dummy 64K for overmapping */ |
| 30 | char iommu_dummy[2*SZ_64K-4]; |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 31 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 32 | struct msm_iova_data { |
| 33 | struct rb_node node; |
| 34 | struct mem_pool *pools; |
| 35 | int npools; |
| 36 | struct iommu_domain *domain; |
| 37 | int domain_num; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 40 | static struct rb_root domain_root; |
| 41 | DEFINE_MUTEX(domain_mutex); |
| 42 | static atomic_t domain_nums = ATOMIC_INIT(-1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 43 | |
Laura Abbott | 2030c1b | 2012-07-18 06:38:00 -0700 | [diff] [blame] | 44 | int msm_use_iommu() |
| 45 | { |
| 46 | return iommu_present(&platform_bus_type); |
| 47 | } |
| 48 | |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 49 | int msm_iommu_map_extra(struct iommu_domain *domain, |
| 50 | unsigned long start_iova, |
| 51 | unsigned long size, |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 52 | unsigned long page_size, |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 53 | int cached) |
| 54 | { |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 55 | int ret = 0; |
| 56 | int i = 0; |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 57 | unsigned long phy_addr = ALIGN(virt_to_phys(iommu_dummy), page_size); |
| 58 | unsigned long temp_iova = start_iova; |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 59 | if (page_size == SZ_4K) { |
| 60 | struct scatterlist *sglist; |
| 61 | unsigned int nrpages = PFN_ALIGN(size) >> PAGE_SHIFT; |
| 62 | struct page *dummy_page = phys_to_page(phy_addr); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 63 | |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 64 | sglist = vmalloc(sizeof(*sglist) * nrpages); |
| 65 | if (!sglist) { |
| 66 | ret = -ENOMEM; |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 67 | goto out; |
| 68 | } |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 69 | |
| 70 | sg_init_table(sglist, nrpages); |
| 71 | |
| 72 | for (i = 0; i < nrpages; i++) |
| 73 | sg_set_page(&sglist[i], dummy_page, PAGE_SIZE, 0); |
| 74 | |
| 75 | ret = iommu_map_range(domain, temp_iova, sglist, size, cached); |
| 76 | if (ret) { |
| 77 | pr_err("%s: could not map extra %lx in domain %p\n", |
| 78 | __func__, start_iova, domain); |
| 79 | } |
| 80 | |
| 81 | vfree(sglist); |
| 82 | } else { |
| 83 | unsigned long order = get_order(page_size); |
| 84 | unsigned long aligned_size = ALIGN(size, page_size); |
| 85 | unsigned long nrpages = aligned_size >> (PAGE_SHIFT + order); |
| 86 | |
| 87 | for (i = 0; i < nrpages; i++) { |
| 88 | ret = iommu_map(domain, temp_iova, phy_addr, page_size, |
| 89 | cached); |
| 90 | if (ret) { |
| 91 | pr_err("%s: could not map %lx in domain %p, error: %d\n", |
| 92 | __func__, start_iova, domain, ret); |
| 93 | ret = -EAGAIN; |
| 94 | goto out; |
| 95 | } |
| 96 | temp_iova += page_size; |
| 97 | } |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 98 | } |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 99 | return ret; |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 100 | out: |
| 101 | for (; i > 0; --i) { |
| 102 | temp_iova -= page_size; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 103 | iommu_unmap(domain, start_iova, page_size); |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 104 | } |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 105 | return ret; |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 106 | } |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 107 | |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 108 | void msm_iommu_unmap_extra(struct iommu_domain *domain, |
| 109 | unsigned long start_iova, |
| 110 | unsigned long size, |
| 111 | unsigned long page_size) |
| 112 | { |
| 113 | int i; |
| 114 | unsigned long order = get_order(page_size); |
| 115 | unsigned long aligned_size = ALIGN(size, page_size); |
| 116 | unsigned long nrpages = aligned_size >> (PAGE_SHIFT + order); |
| 117 | unsigned long temp_iova = start_iova; |
| 118 | |
| 119 | for (i = 0; i < nrpages; ++i) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 120 | iommu_unmap(domain, temp_iova, page_size); |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 121 | temp_iova += page_size; |
| 122 | } |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 125 | static int msm_iommu_map_iova_phys(struct iommu_domain *domain, |
| 126 | unsigned long iova, |
| 127 | unsigned long phys, |
| 128 | unsigned long size, |
| 129 | int cached) |
| 130 | { |
| 131 | int ret; |
| 132 | struct scatterlist *sglist; |
Laura Abbott | e543cfc | 2012-06-07 17:51:53 -0700 | [diff] [blame] | 133 | int prot = IOMMU_WRITE | IOMMU_READ; |
| 134 | prot |= cached ? IOMMU_CACHE : 0; |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 135 | |
| 136 | sglist = vmalloc(sizeof(*sglist)); |
| 137 | if (!sglist) { |
| 138 | ret = -ENOMEM; |
| 139 | goto err1; |
| 140 | } |
| 141 | |
| 142 | sg_init_table(sglist, 1); |
| 143 | sglist->length = size; |
| 144 | sglist->offset = 0; |
| 145 | sglist->dma_address = phys; |
| 146 | |
Laura Abbott | e543cfc | 2012-06-07 17:51:53 -0700 | [diff] [blame] | 147 | ret = iommu_map_range(domain, iova, sglist, size, prot); |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 148 | if (ret) { |
| 149 | pr_err("%s: could not map extra %lx in domain %p\n", |
| 150 | __func__, iova, domain); |
| 151 | } |
| 152 | |
| 153 | vfree(sglist); |
| 154 | err1: |
| 155 | return ret; |
| 156 | |
| 157 | } |
| 158 | |
| 159 | int msm_iommu_map_contig_buffer(unsigned long phys, |
| 160 | unsigned int domain_no, |
| 161 | unsigned int partition_no, |
| 162 | unsigned long size, |
| 163 | unsigned long align, |
| 164 | unsigned long cached, |
| 165 | unsigned long *iova_val) |
| 166 | { |
| 167 | unsigned long iova; |
| 168 | int ret; |
| 169 | |
| 170 | if (size & (align - 1)) |
| 171 | return -EINVAL; |
| 172 | |
Laura Abbott | 2030c1b | 2012-07-18 06:38:00 -0700 | [diff] [blame] | 173 | if (!msm_use_iommu()) { |
| 174 | *iova_val = phys; |
| 175 | return 0; |
| 176 | } |
| 177 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 178 | ret = msm_allocate_iova_address(domain_no, partition_no, size, align, |
| 179 | &iova); |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 180 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 181 | if (ret) |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 182 | return -ENOMEM; |
| 183 | |
| 184 | ret = msm_iommu_map_iova_phys(msm_get_iommu_domain(domain_no), iova, |
| 185 | phys, size, cached); |
| 186 | |
| 187 | if (ret) |
| 188 | msm_free_iova_address(iova, domain_no, partition_no, size); |
| 189 | else |
| 190 | *iova_val = iova; |
| 191 | |
| 192 | return ret; |
| 193 | } |
Laura Abbott | 33b30be | 2012-07-09 09:48:54 -0700 | [diff] [blame] | 194 | EXPORT_SYMBOL(msm_iommu_map_contig_buffer); |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 195 | |
| 196 | void msm_iommu_unmap_contig_buffer(unsigned long iova, |
| 197 | unsigned int domain_no, |
| 198 | unsigned int partition_no, |
| 199 | unsigned long size) |
| 200 | { |
Laura Abbott | 2030c1b | 2012-07-18 06:38:00 -0700 | [diff] [blame] | 201 | if (!msm_use_iommu()) |
| 202 | return; |
| 203 | |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 204 | iommu_unmap_range(msm_get_iommu_domain(domain_no), iova, size); |
| 205 | msm_free_iova_address(iova, domain_no, partition_no, size); |
| 206 | } |
Laura Abbott | 33b30be | 2012-07-09 09:48:54 -0700 | [diff] [blame] | 207 | EXPORT_SYMBOL(msm_iommu_unmap_contig_buffer); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 208 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 209 | static struct msm_iova_data *find_domain(int domain_num) |
| 210 | { |
| 211 | struct rb_root *root = &domain_root; |
| 212 | struct rb_node *p = root->rb_node; |
| 213 | |
| 214 | mutex_lock(&domain_mutex); |
| 215 | |
| 216 | while (p) { |
| 217 | struct msm_iova_data *node; |
| 218 | |
| 219 | node = rb_entry(p, struct msm_iova_data, node); |
| 220 | if (domain_num < node->domain_num) |
| 221 | p = p->rb_left; |
Laura Abbott | 723970d | 2012-06-05 15:01:16 -0700 | [diff] [blame] | 222 | else if (domain_num > node->domain_num) |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 223 | p = p->rb_right; |
| 224 | else { |
| 225 | mutex_unlock(&domain_mutex); |
| 226 | return node; |
| 227 | } |
| 228 | } |
| 229 | mutex_unlock(&domain_mutex); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | static int add_domain(struct msm_iova_data *node) |
| 234 | { |
| 235 | struct rb_root *root = &domain_root; |
| 236 | struct rb_node **p = &root->rb_node; |
| 237 | struct rb_node *parent = NULL; |
| 238 | |
| 239 | mutex_lock(&domain_mutex); |
| 240 | while (*p) { |
| 241 | struct msm_iova_data *tmp; |
| 242 | parent = *p; |
| 243 | |
| 244 | tmp = rb_entry(parent, struct msm_iova_data, node); |
| 245 | |
| 246 | if (node->domain_num < tmp->domain_num) |
| 247 | p = &(*p)->rb_left; |
| 248 | else if (node->domain_num > tmp->domain_num) |
| 249 | p = &(*p)->rb_right; |
| 250 | else |
| 251 | BUG(); |
| 252 | } |
| 253 | rb_link_node(&node->node, parent, p); |
| 254 | rb_insert_color(&node->node, root); |
| 255 | mutex_unlock(&domain_mutex); |
| 256 | return 0; |
| 257 | } |
| 258 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 259 | struct iommu_domain *msm_get_iommu_domain(int domain_num) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 260 | { |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 261 | struct msm_iova_data *data; |
| 262 | |
| 263 | data = find_domain(domain_num); |
| 264 | |
| 265 | if (data) |
| 266 | return data->domain; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 267 | else |
| 268 | return NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 271 | int msm_allocate_iova_address(unsigned int iommu_domain, |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 272 | unsigned int partition_no, |
| 273 | unsigned long size, |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 274 | unsigned long align, |
| 275 | unsigned long *iova) |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 276 | { |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 277 | struct msm_iova_data *data; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 278 | struct mem_pool *pool; |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 279 | unsigned long va; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 280 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 281 | data = find_domain(iommu_domain); |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 282 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 283 | if (!data) |
| 284 | return -EINVAL; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 285 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 286 | if (partition_no >= data->npools) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | pool = &data->pools[partition_no]; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 290 | |
| 291 | if (!pool->gpool) |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 292 | return -EINVAL; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 293 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 294 | va = gen_pool_alloc_aligned(pool->gpool, size, ilog2(align)); |
| 295 | if (va) { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 296 | pool->free -= size; |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 297 | /* Offset because genpool can't handle 0 addresses */ |
| 298 | if (pool->paddr == 0) |
| 299 | va -= SZ_4K; |
| 300 | *iova = va; |
| 301 | return 0; |
| 302 | } |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 303 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 304 | return -ENOMEM; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void msm_free_iova_address(unsigned long iova, |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 308 | unsigned int iommu_domain, |
| 309 | unsigned int partition_no, |
| 310 | unsigned long size) |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 311 | { |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 312 | struct msm_iova_data *data; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 313 | struct mem_pool *pool; |
| 314 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 315 | data = find_domain(iommu_domain); |
| 316 | |
| 317 | if (!data) { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 318 | WARN(1, "Invalid domain %d\n", iommu_domain); |
| 319 | return; |
| 320 | } |
| 321 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 322 | if (partition_no >= data->npools) { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 323 | WARN(1, "Invalid partition %d for domain %d\n", |
| 324 | partition_no, iommu_domain); |
| 325 | return; |
| 326 | } |
| 327 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 328 | pool = &data->pools[partition_no]; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 329 | |
| 330 | if (!pool) |
| 331 | return; |
| 332 | |
| 333 | pool->free += size; |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 334 | |
| 335 | /* Offset because genpool can't handle 0 addresses */ |
| 336 | if (pool->paddr == 0) |
| 337 | iova += SZ_4K; |
| 338 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 339 | gen_pool_free(pool->gpool, iova, size); |
| 340 | } |
| 341 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 342 | int msm_register_domain(struct msm_iova_layout *layout) |
| 343 | { |
| 344 | int i; |
| 345 | struct msm_iova_data *data; |
| 346 | struct mem_pool *pools; |
| 347 | |
| 348 | if (!layout) |
| 349 | return -EINVAL; |
| 350 | |
| 351 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 352 | |
| 353 | if (!data) |
| 354 | return -ENOMEM; |
| 355 | |
| 356 | pools = kmalloc(sizeof(struct mem_pool) * layout->npartitions, |
| 357 | GFP_KERNEL); |
| 358 | |
| 359 | if (!pools) |
| 360 | goto out; |
| 361 | |
| 362 | for (i = 0; i < layout->npartitions; i++) { |
| 363 | if (layout->partitions[i].size == 0) |
| 364 | continue; |
| 365 | |
| 366 | pools[i].gpool = gen_pool_create(PAGE_SHIFT, -1); |
| 367 | |
| 368 | if (!pools[i].gpool) |
| 369 | continue; |
| 370 | |
| 371 | pools[i].paddr = layout->partitions[i].start; |
| 372 | pools[i].size = layout->partitions[i].size; |
| 373 | |
| 374 | /* |
| 375 | * genalloc can't handle a pool starting at address 0. |
| 376 | * For now, solve this problem by offsetting the value |
| 377 | * put in by 4k. |
| 378 | * gen pool address = actual address + 4k |
| 379 | */ |
| 380 | if (pools[i].paddr == 0) |
| 381 | layout->partitions[i].start += SZ_4K; |
| 382 | |
| 383 | if (gen_pool_add(pools[i].gpool, |
| 384 | layout->partitions[i].start, |
| 385 | layout->partitions[i].size, -1)) { |
| 386 | gen_pool_destroy(pools[i].gpool); |
| 387 | pools[i].gpool = NULL; |
| 388 | continue; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | data->pools = pools; |
| 393 | data->npools = layout->npartitions; |
| 394 | data->domain_num = atomic_inc_return(&domain_nums); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 395 | data->domain = iommu_domain_alloc(&platform_bus_type, |
| 396 | layout->domain_flags); |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 397 | |
| 398 | add_domain(data); |
| 399 | |
| 400 | return data->domain_num; |
| 401 | |
| 402 | out: |
| 403 | kfree(data); |
| 404 | |
| 405 | return -EINVAL; |
| 406 | } |
Laura Abbott | 33b30be | 2012-07-09 09:48:54 -0700 | [diff] [blame] | 407 | EXPORT_SYMBOL(msm_register_domain); |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 408 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 409 | static int __init iommu_domain_probe(struct platform_device *pdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 410 | { |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 411 | struct iommu_domains_pdata *p = pdev->dev.platform_data; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 412 | int i, j; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 413 | |
Chintan Pandya | ce27e56 | 2012-07-06 23:07:57 +0530 | [diff] [blame] | 414 | if (!msm_use_iommu()) |
| 415 | return -ENODEV; |
| 416 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 417 | if (!p) |
| 418 | return -ENODEV; |
| 419 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 420 | for (i = 0; i < p->ndomains; i++) { |
| 421 | struct msm_iova_layout l; |
| 422 | struct msm_iova_partition *part; |
| 423 | struct msm_iommu_domain *domains; |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 424 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 425 | domains = p->domains; |
| 426 | l.npartitions = domains[i].npools; |
| 427 | part = kmalloc( |
| 428 | sizeof(struct msm_iova_partition) * l.npartitions, |
| 429 | GFP_KERNEL); |
| 430 | |
| 431 | if (!part) { |
| 432 | pr_info("%s: could not allocate space for domain %d", |
| 433 | __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 434 | continue; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 435 | } |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 436 | |
| 437 | for (j = 0; j < l.npartitions; j++) { |
| 438 | part[j].start = p->domains[i].iova_pools[j].paddr; |
| 439 | part[j].size = p->domains[i].iova_pools[j].size; |
| 440 | } |
| 441 | |
| 442 | l.partitions = part; |
| 443 | |
| 444 | msm_register_domain(&l); |
| 445 | |
| 446 | kfree(part); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 449 | for (i = 0; i < p->nnames; i++) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 450 | struct device *ctx = msm_iommu_get_ctx( |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 451 | p->domain_names[i].name); |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 452 | struct iommu_domain *domain; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 453 | |
| 454 | if (!ctx) |
| 455 | continue; |
| 456 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 457 | domain = msm_get_iommu_domain(p->domain_names[i].domain); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 458 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 459 | if (!domain) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 460 | continue; |
| 461 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 462 | if (iommu_attach_device(domain, ctx)) { |
| 463 | WARN(1, "%s: could not attach domain %p to context %s." |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 464 | " iommu programming will not occur.\n", |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 465 | __func__, domain, |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 466 | p->domain_names[i].name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 467 | continue; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | } |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 473 | |
| 474 | static struct platform_driver iommu_domain_driver = { |
| 475 | .driver = { |
| 476 | .name = "iommu_domains", |
| 477 | .owner = THIS_MODULE |
| 478 | }, |
| 479 | }; |
| 480 | |
| 481 | static int __init msm_subsystem_iommu_init(void) |
| 482 | { |
| 483 | return platform_driver_probe(&iommu_domain_driver, iommu_domain_probe); |
| 484 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 485 | device_initcall(msm_subsystem_iommu_init); |