Shalaj Jain | 4796930 | 2013-02-09 17:28:17 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2013, The Linux Foundation. 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> |
Olav Haugan | d77d12c | 2013-03-05 13:17:30 -0800 | [diff] [blame] | 22 | #include <linux/of.h> |
| 23 | #include <linux/of_address.h> |
| 24 | #include <linux/of_device.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 25 | #include <asm/sizes.h> |
| 26 | #include <asm/page.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 27 | #include <mach/iommu.h> |
| 28 | #include <mach/iommu_domains.h> |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 29 | #include <mach/socinfo.h> |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 30 | #include <mach/msm_subsystem_map.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -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 | |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 49 | bool msm_iommu_page_size_is_supported(unsigned long page_size) |
| 50 | { |
| 51 | return page_size == SZ_4K |
| 52 | || page_size == SZ_64K |
| 53 | || page_size == SZ_1M |
| 54 | || page_size == SZ_16M; |
| 55 | } |
| 56 | |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 57 | int msm_iommu_map_extra(struct iommu_domain *domain, |
| 58 | unsigned long start_iova, |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 59 | unsigned long phy_addr, |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 60 | unsigned long size, |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 61 | unsigned long page_size, |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 62 | int prot) |
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 | int ret = 0; |
| 65 | int i = 0; |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 66 | unsigned long temp_iova = start_iova; |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 67 | /* the extra "padding" should never be written to. map it |
| 68 | * read-only. */ |
| 69 | prot &= ~IOMMU_WRITE; |
| 70 | |
| 71 | if (msm_iommu_page_size_is_supported(page_size)) { |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 72 | struct scatterlist *sglist; |
| 73 | unsigned int nrpages = PFN_ALIGN(size) >> PAGE_SHIFT; |
| 74 | struct page *dummy_page = phys_to_page(phy_addr); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 75 | |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 76 | sglist = vmalloc(sizeof(*sglist) * nrpages); |
| 77 | if (!sglist) { |
| 78 | ret = -ENOMEM; |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 79 | goto out; |
| 80 | } |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 81 | |
| 82 | sg_init_table(sglist, nrpages); |
| 83 | |
| 84 | for (i = 0; i < nrpages; i++) |
| 85 | sg_set_page(&sglist[i], dummy_page, PAGE_SIZE, 0); |
| 86 | |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 87 | ret = iommu_map_range(domain, temp_iova, sglist, size, prot); |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 88 | if (ret) { |
| 89 | pr_err("%s: could not map extra %lx in domain %p\n", |
| 90 | __func__, start_iova, domain); |
| 91 | } |
| 92 | |
| 93 | vfree(sglist); |
| 94 | } else { |
| 95 | unsigned long order = get_order(page_size); |
| 96 | unsigned long aligned_size = ALIGN(size, page_size); |
| 97 | unsigned long nrpages = aligned_size >> (PAGE_SHIFT + order); |
| 98 | |
| 99 | for (i = 0; i < nrpages; i++) { |
| 100 | ret = iommu_map(domain, temp_iova, phy_addr, page_size, |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 101 | prot); |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 102 | if (ret) { |
| 103 | pr_err("%s: could not map %lx in domain %p, error: %d\n", |
| 104 | __func__, start_iova, domain, ret); |
| 105 | ret = -EAGAIN; |
| 106 | goto out; |
| 107 | } |
| 108 | temp_iova += page_size; |
| 109 | } |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 110 | } |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 111 | return ret; |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 112 | out: |
| 113 | for (; i > 0; --i) { |
| 114 | temp_iova -= page_size; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 115 | iommu_unmap(domain, start_iova, page_size); |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 116 | } |
Olav Haugan | 5e7befd | 2012-06-19 14:59:37 -0700 | [diff] [blame] | 117 | return ret; |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 118 | } |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 119 | |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 120 | void msm_iommu_unmap_extra(struct iommu_domain *domain, |
| 121 | unsigned long start_iova, |
| 122 | unsigned long size, |
| 123 | unsigned long page_size) |
| 124 | { |
| 125 | int i; |
| 126 | unsigned long order = get_order(page_size); |
| 127 | unsigned long aligned_size = ALIGN(size, page_size); |
| 128 | unsigned long nrpages = aligned_size >> (PAGE_SHIFT + order); |
| 129 | unsigned long temp_iova = start_iova; |
| 130 | |
| 131 | for (i = 0; i < nrpages; ++i) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 132 | iommu_unmap(domain, temp_iova, page_size); |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 133 | temp_iova += page_size; |
| 134 | } |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 137 | static int msm_iommu_map_iova_phys(struct iommu_domain *domain, |
| 138 | unsigned long iova, |
| 139 | unsigned long phys, |
| 140 | unsigned long size, |
| 141 | int cached) |
| 142 | { |
| 143 | int ret; |
| 144 | struct scatterlist *sglist; |
Laura Abbott | e543cfc | 2012-06-07 17:51:53 -0700 | [diff] [blame] | 145 | int prot = IOMMU_WRITE | IOMMU_READ; |
| 146 | prot |= cached ? IOMMU_CACHE : 0; |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 147 | |
| 148 | sglist = vmalloc(sizeof(*sglist)); |
| 149 | if (!sglist) { |
| 150 | ret = -ENOMEM; |
| 151 | goto err1; |
| 152 | } |
| 153 | |
| 154 | sg_init_table(sglist, 1); |
| 155 | sglist->length = size; |
| 156 | sglist->offset = 0; |
| 157 | sglist->dma_address = phys; |
| 158 | |
Laura Abbott | e543cfc | 2012-06-07 17:51:53 -0700 | [diff] [blame] | 159 | ret = iommu_map_range(domain, iova, sglist, size, prot); |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 160 | if (ret) { |
| 161 | pr_err("%s: could not map extra %lx in domain %p\n", |
| 162 | __func__, iova, domain); |
| 163 | } |
| 164 | |
| 165 | vfree(sglist); |
| 166 | err1: |
| 167 | return ret; |
| 168 | |
| 169 | } |
| 170 | |
| 171 | int msm_iommu_map_contig_buffer(unsigned long phys, |
| 172 | unsigned int domain_no, |
| 173 | unsigned int partition_no, |
| 174 | unsigned long size, |
| 175 | unsigned long align, |
| 176 | unsigned long cached, |
| 177 | unsigned long *iova_val) |
| 178 | { |
| 179 | unsigned long iova; |
| 180 | int ret; |
| 181 | |
| 182 | if (size & (align - 1)) |
| 183 | return -EINVAL; |
| 184 | |
Laura Abbott | 2030c1b | 2012-07-18 06:38:00 -0700 | [diff] [blame] | 185 | if (!msm_use_iommu()) { |
| 186 | *iova_val = phys; |
| 187 | return 0; |
| 188 | } |
| 189 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 190 | ret = msm_allocate_iova_address(domain_no, partition_no, size, align, |
| 191 | &iova); |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 192 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 193 | if (ret) |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 194 | return -ENOMEM; |
| 195 | |
| 196 | ret = msm_iommu_map_iova_phys(msm_get_iommu_domain(domain_no), iova, |
| 197 | phys, size, cached); |
| 198 | |
| 199 | if (ret) |
| 200 | msm_free_iova_address(iova, domain_no, partition_no, size); |
| 201 | else |
| 202 | *iova_val = iova; |
| 203 | |
| 204 | return ret; |
| 205 | } |
Laura Abbott | 33b30be | 2012-07-09 09:48:54 -0700 | [diff] [blame] | 206 | EXPORT_SYMBOL(msm_iommu_map_contig_buffer); |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 207 | |
| 208 | void msm_iommu_unmap_contig_buffer(unsigned long iova, |
| 209 | unsigned int domain_no, |
| 210 | unsigned int partition_no, |
| 211 | unsigned long size) |
| 212 | { |
Laura Abbott | 2030c1b | 2012-07-18 06:38:00 -0700 | [diff] [blame] | 213 | if (!msm_use_iommu()) |
| 214 | return; |
| 215 | |
Laura Abbott | d027fdb | 2012-04-17 16:22:24 -0700 | [diff] [blame] | 216 | iommu_unmap_range(msm_get_iommu_domain(domain_no), iova, size); |
| 217 | msm_free_iova_address(iova, domain_no, partition_no, size); |
| 218 | } |
Laura Abbott | 33b30be | 2012-07-09 09:48:54 -0700 | [diff] [blame] | 219 | EXPORT_SYMBOL(msm_iommu_unmap_contig_buffer); |
Laura Abbott | e956cce | 2011-10-25 13:33:20 -0700 | [diff] [blame] | 220 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 221 | static struct msm_iova_data *find_domain(int domain_num) |
| 222 | { |
| 223 | struct rb_root *root = &domain_root; |
| 224 | struct rb_node *p = root->rb_node; |
| 225 | |
| 226 | mutex_lock(&domain_mutex); |
| 227 | |
| 228 | while (p) { |
| 229 | struct msm_iova_data *node; |
| 230 | |
| 231 | node = rb_entry(p, struct msm_iova_data, node); |
| 232 | if (domain_num < node->domain_num) |
| 233 | p = p->rb_left; |
Laura Abbott | 723970d | 2012-06-05 15:01:16 -0700 | [diff] [blame] | 234 | else if (domain_num > node->domain_num) |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 235 | p = p->rb_right; |
| 236 | else { |
| 237 | mutex_unlock(&domain_mutex); |
| 238 | return node; |
| 239 | } |
| 240 | } |
| 241 | mutex_unlock(&domain_mutex); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static int add_domain(struct msm_iova_data *node) |
| 246 | { |
| 247 | struct rb_root *root = &domain_root; |
| 248 | struct rb_node **p = &root->rb_node; |
| 249 | struct rb_node *parent = NULL; |
| 250 | |
| 251 | mutex_lock(&domain_mutex); |
| 252 | while (*p) { |
| 253 | struct msm_iova_data *tmp; |
| 254 | parent = *p; |
| 255 | |
| 256 | tmp = rb_entry(parent, struct msm_iova_data, node); |
| 257 | |
| 258 | if (node->domain_num < tmp->domain_num) |
| 259 | p = &(*p)->rb_left; |
| 260 | else if (node->domain_num > tmp->domain_num) |
| 261 | p = &(*p)->rb_right; |
| 262 | else |
| 263 | BUG(); |
| 264 | } |
| 265 | rb_link_node(&node->node, parent, p); |
| 266 | rb_insert_color(&node->node, root); |
| 267 | mutex_unlock(&domain_mutex); |
| 268 | return 0; |
| 269 | } |
| 270 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 271 | struct iommu_domain *msm_get_iommu_domain(int domain_num) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 272 | { |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 273 | struct msm_iova_data *data; |
| 274 | |
| 275 | data = find_domain(domain_num); |
| 276 | |
| 277 | if (data) |
| 278 | return data->domain; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 279 | else |
| 280 | return NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 281 | } |
Laura Abbott | b1d6887 | 2012-10-17 10:50:39 -0700 | [diff] [blame] | 282 | EXPORT_SYMBOL(msm_get_iommu_domain); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 283 | |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 284 | int msm_find_domain_no(const struct iommu_domain *domain) |
| 285 | { |
| 286 | struct rb_root *root = &domain_root; |
| 287 | struct rb_node *n; |
| 288 | struct msm_iova_data *node; |
| 289 | int domain_num = -EINVAL; |
| 290 | |
| 291 | mutex_lock(&domain_mutex); |
| 292 | |
| 293 | for (n = rb_first(root); n; n = rb_next(n)) { |
| 294 | node = rb_entry(n, struct msm_iova_data, node); |
| 295 | if (node->domain == domain) { |
| 296 | domain_num = node->domain_num; |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | mutex_unlock(&domain_mutex); |
| 301 | return domain_num; |
| 302 | } |
| 303 | EXPORT_SYMBOL(msm_find_domain_no); |
| 304 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 305 | int msm_allocate_iova_address(unsigned int iommu_domain, |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 306 | unsigned int partition_no, |
| 307 | unsigned long size, |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 308 | unsigned long align, |
| 309 | unsigned long *iova) |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 310 | { |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 311 | struct msm_iova_data *data; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 312 | struct mem_pool *pool; |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 313 | unsigned long va; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 314 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 315 | data = find_domain(iommu_domain); |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 316 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 317 | if (!data) |
| 318 | return -EINVAL; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 319 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 320 | if (partition_no >= data->npools) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | pool = &data->pools[partition_no]; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 324 | |
| 325 | if (!pool->gpool) |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 326 | return -EINVAL; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 327 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 328 | va = gen_pool_alloc_aligned(pool->gpool, size, ilog2(align)); |
| 329 | if (va) { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 330 | pool->free -= size; |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 331 | /* Offset because genpool can't handle 0 addresses */ |
| 332 | if (pool->paddr == 0) |
| 333 | va -= SZ_4K; |
| 334 | *iova = va; |
| 335 | return 0; |
| 336 | } |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 337 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 338 | return -ENOMEM; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void msm_free_iova_address(unsigned long iova, |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 342 | unsigned int iommu_domain, |
| 343 | unsigned int partition_no, |
| 344 | unsigned long size) |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 345 | { |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 346 | struct msm_iova_data *data; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 347 | struct mem_pool *pool; |
| 348 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 349 | data = find_domain(iommu_domain); |
| 350 | |
| 351 | if (!data) { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 352 | WARN(1, "Invalid domain %d\n", iommu_domain); |
| 353 | return; |
| 354 | } |
| 355 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 356 | if (partition_no >= data->npools) { |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 357 | WARN(1, "Invalid partition %d for domain %d\n", |
| 358 | partition_no, iommu_domain); |
| 359 | return; |
| 360 | } |
| 361 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 362 | pool = &data->pools[partition_no]; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 363 | |
| 364 | if (!pool) |
| 365 | return; |
| 366 | |
| 367 | pool->free += size; |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 368 | |
| 369 | /* Offset because genpool can't handle 0 addresses */ |
| 370 | if (pool->paddr == 0) |
| 371 | iova += SZ_4K; |
| 372 | |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 373 | gen_pool_free(pool->gpool, iova, size); |
| 374 | } |
| 375 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 376 | int msm_register_domain(struct msm_iova_layout *layout) |
| 377 | { |
| 378 | int i; |
| 379 | struct msm_iova_data *data; |
| 380 | struct mem_pool *pools; |
Laura Abbott | 6a6ca55 | 2012-08-30 11:26:31 -0700 | [diff] [blame] | 381 | struct bus_type *bus; |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 382 | |
| 383 | if (!layout) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 387 | |
| 388 | if (!data) |
| 389 | return -ENOMEM; |
| 390 | |
| 391 | pools = kmalloc(sizeof(struct mem_pool) * layout->npartitions, |
| 392 | GFP_KERNEL); |
| 393 | |
| 394 | if (!pools) |
| 395 | goto out; |
| 396 | |
| 397 | for (i = 0; i < layout->npartitions; i++) { |
| 398 | if (layout->partitions[i].size == 0) |
| 399 | continue; |
| 400 | |
| 401 | pools[i].gpool = gen_pool_create(PAGE_SHIFT, -1); |
| 402 | |
| 403 | if (!pools[i].gpool) |
| 404 | continue; |
| 405 | |
| 406 | pools[i].paddr = layout->partitions[i].start; |
| 407 | pools[i].size = layout->partitions[i].size; |
| 408 | |
| 409 | /* |
| 410 | * genalloc can't handle a pool starting at address 0. |
| 411 | * For now, solve this problem by offsetting the value |
| 412 | * put in by 4k. |
| 413 | * gen pool address = actual address + 4k |
| 414 | */ |
| 415 | if (pools[i].paddr == 0) |
| 416 | layout->partitions[i].start += SZ_4K; |
| 417 | |
| 418 | if (gen_pool_add(pools[i].gpool, |
| 419 | layout->partitions[i].start, |
| 420 | layout->partitions[i].size, -1)) { |
| 421 | gen_pool_destroy(pools[i].gpool); |
| 422 | pools[i].gpool = NULL; |
| 423 | continue; |
| 424 | } |
| 425 | } |
| 426 | |
Laura Abbott | 6a6ca55 | 2012-08-30 11:26:31 -0700 | [diff] [blame] | 427 | bus = layout->is_secure == MSM_IOMMU_DOMAIN_SECURE ? |
| 428 | &msm_iommu_sec_bus_type : |
| 429 | &platform_bus_type; |
| 430 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 431 | data->pools = pools; |
| 432 | data->npools = layout->npartitions; |
| 433 | data->domain_num = atomic_inc_return(&domain_nums); |
Laura Abbott | 6a6ca55 | 2012-08-30 11:26:31 -0700 | [diff] [blame] | 434 | data->domain = iommu_domain_alloc(bus, layout->domain_flags); |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 435 | |
| 436 | add_domain(data); |
| 437 | |
| 438 | return data->domain_num; |
| 439 | |
| 440 | out: |
| 441 | kfree(data); |
| 442 | |
| 443 | return -EINVAL; |
| 444 | } |
Laura Abbott | 33b30be | 2012-07-09 09:48:54 -0700 | [diff] [blame] | 445 | EXPORT_SYMBOL(msm_register_domain); |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 446 | |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 447 | static int find_and_add_contexts(struct iommu_group *group, |
| 448 | const struct device_node *node, |
| 449 | unsigned int num_contexts) |
| 450 | { |
| 451 | unsigned int i; |
| 452 | struct device *ctx; |
| 453 | const char *name; |
| 454 | struct device_node *ctx_node; |
| 455 | int ret_val = 0; |
| 456 | |
| 457 | for (i = 0; i < num_contexts; ++i) { |
| 458 | ctx_node = of_parse_phandle((struct device_node *) node, |
| 459 | "qcom,iommu-contexts", i); |
| 460 | if (!ctx_node) { |
| 461 | pr_err("Unable to parse phandle #%u\n", i); |
| 462 | ret_val = -EINVAL; |
| 463 | goto out; |
| 464 | } |
| 465 | if (of_property_read_string(ctx_node, "label", &name)) { |
| 466 | pr_err("Could not find label property\n"); |
| 467 | ret_val = -EINVAL; |
| 468 | goto out; |
| 469 | } |
| 470 | ctx = msm_iommu_get_ctx(name); |
| 471 | if (!ctx) { |
| 472 | pr_err("Unable to find context %s\n", name); |
| 473 | ret_val = -EINVAL; |
| 474 | goto out; |
| 475 | } |
| 476 | iommu_group_add_device(group, ctx); |
| 477 | } |
| 478 | out: |
| 479 | return ret_val; |
| 480 | } |
| 481 | |
| 482 | static int create_and_add_domain(struct iommu_group *group, |
| 483 | const struct device_node *node) |
| 484 | { |
| 485 | unsigned int ret_val = 0; |
Shalaj Jain | 4796930 | 2013-02-09 17:28:17 -0800 | [diff] [blame] | 486 | unsigned int i, j; |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 487 | struct msm_iova_layout l; |
| 488 | struct msm_iova_partition *part = 0; |
| 489 | struct iommu_domain *domain = 0; |
| 490 | unsigned int *addr_array; |
| 491 | unsigned int array_size; |
| 492 | int domain_no; |
| 493 | int secure_domain; |
| 494 | int l2_redirect; |
| 495 | |
| 496 | if (of_get_property(node, "qcom,virtual-addr-pool", &array_size)) { |
| 497 | l.npartitions = array_size / sizeof(unsigned int) / 2; |
| 498 | part = kmalloc( |
| 499 | sizeof(struct msm_iova_partition) * l.npartitions, |
| 500 | GFP_KERNEL); |
| 501 | if (!part) { |
| 502 | pr_err("%s: could not allocate space for partition", |
| 503 | __func__); |
| 504 | ret_val = -ENOMEM; |
| 505 | goto out; |
| 506 | } |
| 507 | addr_array = kmalloc(array_size, GFP_KERNEL); |
| 508 | if (!addr_array) { |
| 509 | pr_err("%s: could not allocate space for partition", |
| 510 | __func__); |
| 511 | ret_val = -ENOMEM; |
| 512 | goto free_mem; |
| 513 | } |
| 514 | |
| 515 | ret_val = of_property_read_u32_array(node, |
| 516 | "qcom,virtual-addr-pool", |
| 517 | addr_array, |
| 518 | array_size/sizeof(unsigned int)); |
| 519 | if (ret_val) { |
| 520 | ret_val = -EINVAL; |
| 521 | goto free_mem; |
| 522 | } |
| 523 | |
Shalaj Jain | 4796930 | 2013-02-09 17:28:17 -0800 | [diff] [blame] | 524 | for (i = 0, j = 0; j < l.npartitions * 2; i++, j += 2) { |
| 525 | part[i].start = addr_array[j]; |
| 526 | part[i].size = addr_array[j+1]; |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 527 | } |
| 528 | } else { |
| 529 | l.npartitions = 1; |
| 530 | part = kmalloc( |
| 531 | sizeof(struct msm_iova_partition) * l.npartitions, |
| 532 | GFP_KERNEL); |
| 533 | if (!part) { |
| 534 | pr_err("%s: could not allocate space for partition", |
| 535 | __func__); |
| 536 | ret_val = -ENOMEM; |
| 537 | goto out; |
| 538 | } |
| 539 | part[0].start = 0x0; |
| 540 | part[0].size = 0xFFFFFFFF; |
| 541 | } |
| 542 | |
| 543 | l.partitions = part; |
| 544 | |
| 545 | secure_domain = of_property_read_bool(node, "qcom,secure-domain"); |
| 546 | l.is_secure = (secure_domain) ? MSM_IOMMU_DOMAIN_SECURE : 0; |
| 547 | |
| 548 | l2_redirect = of_property_read_bool(node, "qcom,l2-redirect"); |
| 549 | l.domain_flags = (l2_redirect) ? MSM_IOMMU_DOMAIN_PT_CACHEABLE : 0; |
| 550 | |
| 551 | domain_no = msm_register_domain(&l); |
| 552 | if (domain_no >= 0) |
| 553 | domain = msm_get_iommu_domain(domain_no); |
| 554 | else |
| 555 | ret_val = domain_no; |
| 556 | |
| 557 | iommu_group_set_iommudata(group, domain, NULL); |
| 558 | |
| 559 | free_mem: |
| 560 | kfree(part); |
| 561 | out: |
| 562 | return ret_val; |
| 563 | } |
| 564 | |
| 565 | static int iommu_domain_parse_dt(const struct device_node *dt_node) |
| 566 | { |
| 567 | struct device_node *node; |
| 568 | int sz; |
| 569 | unsigned int num_contexts; |
| 570 | int ret_val = 0; |
| 571 | struct iommu_group *group = 0; |
| 572 | const char *name; |
| 573 | |
| 574 | for_each_child_of_node(dt_node, node) { |
| 575 | group = iommu_group_alloc(); |
| 576 | if (IS_ERR(group)) { |
| 577 | ret_val = PTR_ERR(group); |
| 578 | goto out; |
| 579 | } |
| 580 | if (of_property_read_string(node, "label", &name)) { |
| 581 | ret_val = -EINVAL; |
| 582 | goto free_group; |
| 583 | } |
| 584 | iommu_group_set_name(group, name); |
| 585 | |
| 586 | if (!of_get_property(node, "qcom,iommu-contexts", &sz)) { |
| 587 | pr_err("Could not find qcom,iommu-contexts property\n"); |
| 588 | ret_val = -EINVAL; |
| 589 | goto free_group; |
| 590 | } |
| 591 | num_contexts = sz / sizeof(unsigned int); |
| 592 | |
| 593 | ret_val = find_and_add_contexts(group, node, num_contexts); |
| 594 | if (ret_val) { |
| 595 | ret_val = -EINVAL; |
| 596 | goto free_group; |
| 597 | } |
| 598 | ret_val = create_and_add_domain(group, node); |
| 599 | if (ret_val) { |
| 600 | ret_val = -EINVAL; |
| 601 | goto free_group; |
| 602 | } |
| 603 | } |
| 604 | free_group: |
| 605 | /* No iommu_group_free() function */ |
| 606 | out: |
| 607 | return ret_val; |
| 608 | } |
| 609 | |
| 610 | static int iommu_domain_probe(struct platform_device *pdev) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 611 | { |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 612 | struct iommu_domains_pdata *p = pdev->dev.platform_data; |
Laura Abbott | 9f4a8e6 | 2011-08-29 19:08:07 -0700 | [diff] [blame] | 613 | int i, j; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 614 | |
Chintan Pandya | ce27e56 | 2012-07-06 23:07:57 +0530 | [diff] [blame] | 615 | if (!msm_use_iommu()) |
| 616 | return -ENODEV; |
| 617 | |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 618 | if (pdev->dev.of_node) |
| 619 | return iommu_domain_parse_dt(pdev->dev.of_node); |
| 620 | else if (!p) |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 621 | return -ENODEV; |
| 622 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 623 | for (i = 0; i < p->ndomains; i++) { |
| 624 | struct msm_iova_layout l; |
| 625 | struct msm_iova_partition *part; |
| 626 | struct msm_iommu_domain *domains; |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 627 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 628 | domains = p->domains; |
| 629 | l.npartitions = domains[i].npools; |
| 630 | part = kmalloc( |
| 631 | sizeof(struct msm_iova_partition) * l.npartitions, |
| 632 | GFP_KERNEL); |
| 633 | |
| 634 | if (!part) { |
| 635 | pr_info("%s: could not allocate space for domain %d", |
| 636 | __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 637 | continue; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 638 | } |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 639 | |
| 640 | for (j = 0; j < l.npartitions; j++) { |
| 641 | part[j].start = p->domains[i].iova_pools[j].paddr; |
| 642 | part[j].size = p->domains[i].iova_pools[j].size; |
| 643 | } |
| 644 | |
| 645 | l.partitions = part; |
| 646 | |
| 647 | msm_register_domain(&l); |
| 648 | |
| 649 | kfree(part); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 652 | for (i = 0; i < p->nnames; i++) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 653 | struct device *ctx = msm_iommu_get_ctx( |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 654 | p->domain_names[i].name); |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 655 | struct iommu_domain *domain; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 656 | |
| 657 | if (!ctx) |
| 658 | continue; |
| 659 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 660 | domain = msm_get_iommu_domain(p->domain_names[i].domain); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 661 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 662 | if (!domain) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 663 | continue; |
| 664 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 665 | if (iommu_attach_device(domain, ctx)) { |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 666 | WARN(1, "%s: could not attach domain %p to context %s. iommu programming will not occur.\n", |
| 667 | __func__, domain, p->domain_names[i].name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 668 | continue; |
| 669 | } |
| 670 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 671 | return 0; |
| 672 | } |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 673 | |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 674 | static int __devexit iommu_domain_exit(struct platform_device *pdev) |
| 675 | { |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | static struct of_device_id msm_iommu_domain_match_table[] = { |
| 680 | { .name = "qcom,iommu-domains", }, |
| 681 | {} |
| 682 | }; |
| 683 | |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 684 | static struct platform_driver iommu_domain_driver = { |
| 685 | .driver = { |
| 686 | .name = "iommu_domains", |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 687 | .of_match_table = msm_iommu_domain_match_table, |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 688 | .owner = THIS_MODULE |
| 689 | }, |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 690 | .probe = iommu_domain_probe, |
| 691 | .remove = __devexit_p(iommu_domain_exit), |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 692 | }; |
| 693 | |
| 694 | static int __init msm_subsystem_iommu_init(void) |
| 695 | { |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 696 | int ret; |
| 697 | ret = platform_driver_register(&iommu_domain_driver); |
| 698 | if (ret != 0) |
| 699 | pr_err("Failed to register IOMMU domain driver\n"); |
| 700 | return ret; |
Laura Abbott | 0577d7b | 2012-04-17 11:14:30 -0700 | [diff] [blame] | 701 | } |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 702 | |
| 703 | static void __exit msm_subsystem_iommu_exit(void) |
| 704 | { |
| 705 | platform_driver_unregister(&iommu_domain_driver); |
| 706 | } |
| 707 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 708 | device_initcall(msm_subsystem_iommu_init); |
Olav Haugan | 35deadc | 2012-12-10 18:28:27 -0800 | [diff] [blame] | 709 | module_exit(msm_subsystem_iommu_exit); |
| 710 | |