blob: 12f5a8e9b27381612aec257124b507d955e4d948 [file] [log] [blame]
Shalaj Jain47969302013-02-09 17:28:17 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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 Mucklef132c6c2012-06-06 18:30:57 -070013#include <linux/module.h>
Laura Abbottd01221b2012-05-16 17:52:49 -070014#include <linux/init.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/iommu.h>
Laura Abbottd01221b2012-05-16 17:52:49 -070016#include <linux/memory_alloc.h>
Laura Abbott0577d7b2012-04-17 11:14:30 -070017#include <linux/platform_device.h>
Laura Abbottd01221b2012-05-16 17:52:49 -070018#include <linux/rbtree.h>
19#include <linux/slab.h>
Olav Haugan16cdb412012-03-27 13:02:17 -070020#include <linux/vmalloc.h>
Olav Haugand77d12c2013-03-05 13:17:30 -080021#include <linux/of.h>
22#include <linux/of_address.h>
23#include <linux/of_device.h>
Olav Hauganffa22b82013-04-26 13:47:11 -070024#include <linux/idr.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include <asm/sizes.h>
26#include <asm/page.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <mach/iommu.h>
28#include <mach/iommu_domains.h>
Olav Haugana9820ce2013-03-22 14:50:28 -070029#include <mach/msm_iommu_priv.h>
Laura Abbott9f4a8e62011-08-29 19:08:07 -070030#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031
Laura Abbottd01221b2012-05-16 17:52:49 -070032struct 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 Huntsman3f2bc4d2011-08-16 17:27:22 -070038};
39
Olav Hauganffa22b82013-04-26 13:47:11 -070040struct msm_iommu_data_entry {
41 struct list_head list;
42 void *data;
43};
44
Laura Abbottd01221b2012-05-16 17:52:49 -070045static struct rb_root domain_root;
46DEFINE_MUTEX(domain_mutex);
Olav Hauganffa22b82013-04-26 13:47:11 -070047static DEFINE_IDA(domain_nums);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048
Olav Haugana9820ce2013-03-22 14:50:28 -070049void msm_iommu_set_client_name(struct iommu_domain *domain, char const *name)
50{
51 struct msm_iommu_priv *priv = domain->priv;
52 priv->client_name = name;
53}
54
Laura Abbott2030c1b2012-07-18 06:38:00 -070055int msm_use_iommu()
56{
57 return iommu_present(&platform_bus_type);
58}
59
Mitchel Humpherysaf3b5222013-01-15 15:38:52 -080060bool msm_iommu_page_size_is_supported(unsigned long page_size)
61{
62 return page_size == SZ_4K
63 || page_size == SZ_64K
64 || page_size == SZ_1M
65 || page_size == SZ_16M;
66}
67
Laura Abbotte956cce2011-10-25 13:33:20 -070068int msm_iommu_map_extra(struct iommu_domain *domain,
69 unsigned long start_iova,
Laura Abbott3cfa2e62013-03-13 17:48:26 -070070 phys_addr_t phy_addr,
Laura Abbotte956cce2011-10-25 13:33:20 -070071 unsigned long size,
Olav Haugan8726caf2012-05-10 15:11:35 -070072 unsigned long page_size,
Mitchel Humpherysaf3b5222013-01-15 15:38:52 -080073 int prot)
Laura Abbotte956cce2011-10-25 13:33:20 -070074{
Olav Haugan5e7befd2012-06-19 14:59:37 -070075 int ret = 0;
76 int i = 0;
Olav Haugan8726caf2012-05-10 15:11:35 -070077 unsigned long temp_iova = start_iova;
Mitchel Humpherysaf3b5222013-01-15 15:38:52 -080078 /* the extra "padding" should never be written to. map it
79 * read-only. */
80 prot &= ~IOMMU_WRITE;
81
82 if (msm_iommu_page_size_is_supported(page_size)) {
Olav Haugan5e7befd2012-06-19 14:59:37 -070083 struct scatterlist *sglist;
84 unsigned int nrpages = PFN_ALIGN(size) >> PAGE_SHIFT;
85 struct page *dummy_page = phys_to_page(phy_addr);
Laura Abbotte956cce2011-10-25 13:33:20 -070086
Olav Haugan5e7befd2012-06-19 14:59:37 -070087 sglist = vmalloc(sizeof(*sglist) * nrpages);
88 if (!sglist) {
89 ret = -ENOMEM;
Olav Haugan8726caf2012-05-10 15:11:35 -070090 goto out;
91 }
Olav Haugan5e7befd2012-06-19 14:59:37 -070092
93 sg_init_table(sglist, nrpages);
94
95 for (i = 0; i < nrpages; i++)
96 sg_set_page(&sglist[i], dummy_page, PAGE_SIZE, 0);
97
Mitchel Humpherysaf3b5222013-01-15 15:38:52 -080098 ret = iommu_map_range(domain, temp_iova, sglist, size, prot);
Olav Haugan5e7befd2012-06-19 14:59:37 -070099 if (ret) {
100 pr_err("%s: could not map extra %lx in domain %p\n",
101 __func__, start_iova, domain);
102 }
103
104 vfree(sglist);
105 } else {
106 unsigned long order = get_order(page_size);
107 unsigned long aligned_size = ALIGN(size, page_size);
108 unsigned long nrpages = aligned_size >> (PAGE_SHIFT + order);
109
110 for (i = 0; i < nrpages; i++) {
111 ret = iommu_map(domain, temp_iova, phy_addr, page_size,
Mitchel Humpherysaf3b5222013-01-15 15:38:52 -0800112 prot);
Olav Haugan5e7befd2012-06-19 14:59:37 -0700113 if (ret) {
114 pr_err("%s: could not map %lx in domain %p, error: %d\n",
115 __func__, start_iova, domain, ret);
116 ret = -EAGAIN;
117 goto out;
118 }
119 temp_iova += page_size;
120 }
Laura Abbotte956cce2011-10-25 13:33:20 -0700121 }
Olav Haugan5e7befd2012-06-19 14:59:37 -0700122 return ret;
Olav Haugan8726caf2012-05-10 15:11:35 -0700123out:
124 for (; i > 0; --i) {
125 temp_iova -= page_size;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700126 iommu_unmap(domain, start_iova, page_size);
Olav Haugan16cdb412012-03-27 13:02:17 -0700127 }
Olav Haugan5e7befd2012-06-19 14:59:37 -0700128 return ret;
Olav Haugan8726caf2012-05-10 15:11:35 -0700129}
Laura Abbotte956cce2011-10-25 13:33:20 -0700130
Olav Haugan8726caf2012-05-10 15:11:35 -0700131void msm_iommu_unmap_extra(struct iommu_domain *domain,
132 unsigned long start_iova,
133 unsigned long size,
134 unsigned long page_size)
135{
136 int i;
137 unsigned long order = get_order(page_size);
138 unsigned long aligned_size = ALIGN(size, page_size);
139 unsigned long nrpages = aligned_size >> (PAGE_SHIFT + order);
140 unsigned long temp_iova = start_iova;
141
142 for (i = 0; i < nrpages; ++i) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700143 iommu_unmap(domain, temp_iova, page_size);
Olav Haugan8726caf2012-05-10 15:11:35 -0700144 temp_iova += page_size;
145 }
Laura Abbotte956cce2011-10-25 13:33:20 -0700146}
147
Laura Abbottd027fdb2012-04-17 16:22:24 -0700148static int msm_iommu_map_iova_phys(struct iommu_domain *domain,
149 unsigned long iova,
Laura Abbott3cfa2e62013-03-13 17:48:26 -0700150 phys_addr_t phys,
Laura Abbottd027fdb2012-04-17 16:22:24 -0700151 unsigned long size,
152 int cached)
153{
154 int ret;
155 struct scatterlist *sglist;
Laura Abbotte543cfc2012-06-07 17:51:53 -0700156 int prot = IOMMU_WRITE | IOMMU_READ;
157 prot |= cached ? IOMMU_CACHE : 0;
Laura Abbottd027fdb2012-04-17 16:22:24 -0700158
159 sglist = vmalloc(sizeof(*sglist));
160 if (!sglist) {
161 ret = -ENOMEM;
162 goto err1;
163 }
164
165 sg_init_table(sglist, 1);
166 sglist->length = size;
167 sglist->offset = 0;
168 sglist->dma_address = phys;
169
Laura Abbotte543cfc2012-06-07 17:51:53 -0700170 ret = iommu_map_range(domain, iova, sglist, size, prot);
Laura Abbottd027fdb2012-04-17 16:22:24 -0700171 if (ret) {
172 pr_err("%s: could not map extra %lx in domain %p\n",
173 __func__, iova, domain);
174 }
175
176 vfree(sglist);
177err1:
178 return ret;
179
180}
181
Laura Abbott3cfa2e62013-03-13 17:48:26 -0700182int msm_iommu_map_contig_buffer(phys_addr_t phys,
Laura Abbottd027fdb2012-04-17 16:22:24 -0700183 unsigned int domain_no,
184 unsigned int partition_no,
185 unsigned long size,
186 unsigned long align,
187 unsigned long cached,
188 unsigned long *iova_val)
189{
190 unsigned long iova;
191 int ret;
192
193 if (size & (align - 1))
194 return -EINVAL;
195
Laura Abbott2030c1b2012-07-18 06:38:00 -0700196 if (!msm_use_iommu()) {
197 *iova_val = phys;
198 return 0;
199 }
200
Laura Abbottd01221b2012-05-16 17:52:49 -0700201 ret = msm_allocate_iova_address(domain_no, partition_no, size, align,
202 &iova);
Laura Abbottd027fdb2012-04-17 16:22:24 -0700203
Laura Abbottd01221b2012-05-16 17:52:49 -0700204 if (ret)
Laura Abbottd027fdb2012-04-17 16:22:24 -0700205 return -ENOMEM;
206
207 ret = msm_iommu_map_iova_phys(msm_get_iommu_domain(domain_no), iova,
208 phys, size, cached);
209
210 if (ret)
211 msm_free_iova_address(iova, domain_no, partition_no, size);
212 else
213 *iova_val = iova;
214
215 return ret;
216}
Laura Abbott33b30be2012-07-09 09:48:54 -0700217EXPORT_SYMBOL(msm_iommu_map_contig_buffer);
Laura Abbottd027fdb2012-04-17 16:22:24 -0700218
219void msm_iommu_unmap_contig_buffer(unsigned long iova,
220 unsigned int domain_no,
221 unsigned int partition_no,
222 unsigned long size)
223{
Laura Abbott2030c1b2012-07-18 06:38:00 -0700224 if (!msm_use_iommu())
225 return;
226
Laura Abbottd027fdb2012-04-17 16:22:24 -0700227 iommu_unmap_range(msm_get_iommu_domain(domain_no), iova, size);
228 msm_free_iova_address(iova, domain_no, partition_no, size);
229}
Laura Abbott33b30be2012-07-09 09:48:54 -0700230EXPORT_SYMBOL(msm_iommu_unmap_contig_buffer);
Laura Abbotte956cce2011-10-25 13:33:20 -0700231
Laura Abbottd01221b2012-05-16 17:52:49 -0700232static struct msm_iova_data *find_domain(int domain_num)
233{
234 struct rb_root *root = &domain_root;
235 struct rb_node *p = root->rb_node;
236
237 mutex_lock(&domain_mutex);
238
239 while (p) {
240 struct msm_iova_data *node;
241
242 node = rb_entry(p, struct msm_iova_data, node);
243 if (domain_num < node->domain_num)
244 p = p->rb_left;
Laura Abbott723970d2012-06-05 15:01:16 -0700245 else if (domain_num > node->domain_num)
Laura Abbottd01221b2012-05-16 17:52:49 -0700246 p = p->rb_right;
247 else {
248 mutex_unlock(&domain_mutex);
249 return node;
250 }
251 }
252 mutex_unlock(&domain_mutex);
253 return NULL;
254}
255
256static int add_domain(struct msm_iova_data *node)
257{
258 struct rb_root *root = &domain_root;
259 struct rb_node **p = &root->rb_node;
260 struct rb_node *parent = NULL;
261
262 mutex_lock(&domain_mutex);
263 while (*p) {
264 struct msm_iova_data *tmp;
265 parent = *p;
266
267 tmp = rb_entry(parent, struct msm_iova_data, node);
268
269 if (node->domain_num < tmp->domain_num)
270 p = &(*p)->rb_left;
271 else if (node->domain_num > tmp->domain_num)
272 p = &(*p)->rb_right;
273 else
274 BUG();
275 }
276 rb_link_node(&node->node, parent, p);
277 rb_insert_color(&node->node, root);
278 mutex_unlock(&domain_mutex);
279 return 0;
280}
281
Olav Hauganffa22b82013-04-26 13:47:11 -0700282static int remove_domain(struct iommu_domain *domain)
283{
284 struct rb_root *root = &domain_root;
285 struct rb_node *n;
286 struct msm_iova_data *node;
287 int ret = -EINVAL;
288
289 mutex_lock(&domain_mutex);
290
291 for (n = rb_first(root); n; n = rb_next(n)) {
292 node = rb_entry(n, struct msm_iova_data, node);
293 if (node->domain == domain) {
294 rb_erase(&node->node, &domain_root);
295 ret = 0;
296 break;
297 }
298 }
299 mutex_unlock(&domain_mutex);
300 return ret;
301}
302
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700303struct iommu_domain *msm_get_iommu_domain(int domain_num)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304{
Laura Abbottd01221b2012-05-16 17:52:49 -0700305 struct msm_iova_data *data;
306
307 data = find_domain(domain_num);
308
309 if (data)
310 return data->domain;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700311 else
312 return NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313}
Laura Abbottb1d68872012-10-17 10:50:39 -0700314EXPORT_SYMBOL(msm_get_iommu_domain);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315
Olav Haugan35deadc2012-12-10 18:28:27 -0800316int msm_find_domain_no(const struct iommu_domain *domain)
317{
318 struct rb_root *root = &domain_root;
319 struct rb_node *n;
320 struct msm_iova_data *node;
321 int domain_num = -EINVAL;
322
323 mutex_lock(&domain_mutex);
324
325 for (n = rb_first(root); n; n = rb_next(n)) {
326 node = rb_entry(n, struct msm_iova_data, node);
327 if (node->domain == domain) {
328 domain_num = node->domain_num;
329 break;
330 }
331 }
332 mutex_unlock(&domain_mutex);
333 return domain_num;
334}
335EXPORT_SYMBOL(msm_find_domain_no);
336
Olav Hauganffa22b82013-04-26 13:47:11 -0700337static struct msm_iova_data *msm_domain_to_iova_data(struct iommu_domain
338 const *domain)
339{
340 struct rb_root *root = &domain_root;
341 struct rb_node *n;
342 struct msm_iova_data *node;
343 struct msm_iova_data *iova_data = ERR_PTR(-EINVAL);
344
345 mutex_lock(&domain_mutex);
346
347 for (n = rb_first(root); n; n = rb_next(n)) {
348 node = rb_entry(n, struct msm_iova_data, node);
349 if (node->domain == domain) {
350 iova_data = node;
351 break;
352 }
353 }
354 mutex_unlock(&domain_mutex);
355 return iova_data;
356}
357
Laura Abbottd01221b2012-05-16 17:52:49 -0700358int msm_allocate_iova_address(unsigned int iommu_domain,
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700359 unsigned int partition_no,
360 unsigned long size,
Laura Abbottd01221b2012-05-16 17:52:49 -0700361 unsigned long align,
362 unsigned long *iova)
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700363{
Laura Abbottd01221b2012-05-16 17:52:49 -0700364 struct msm_iova_data *data;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700365 struct mem_pool *pool;
Laura Abbottd01221b2012-05-16 17:52:49 -0700366 unsigned long va;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700367
Laura Abbottd01221b2012-05-16 17:52:49 -0700368 data = find_domain(iommu_domain);
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700369
Laura Abbottd01221b2012-05-16 17:52:49 -0700370 if (!data)
371 return -EINVAL;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700372
Laura Abbottd01221b2012-05-16 17:52:49 -0700373 if (partition_no >= data->npools)
374 return -EINVAL;
375
376 pool = &data->pools[partition_no];
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700377
378 if (!pool->gpool)
Laura Abbottd01221b2012-05-16 17:52:49 -0700379 return -EINVAL;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700380
Laura Abbott594e9cd2013-04-27 18:27:38 -0700381 mutex_lock(&pool->pool_mutex);
Laura Abbottd01221b2012-05-16 17:52:49 -0700382 va = gen_pool_alloc_aligned(pool->gpool, size, ilog2(align));
Laura Abbott594e9cd2013-04-27 18:27:38 -0700383 mutex_unlock(&pool->pool_mutex);
Laura Abbottd01221b2012-05-16 17:52:49 -0700384 if (va) {
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700385 pool->free -= size;
Laura Abbottd01221b2012-05-16 17:52:49 -0700386 /* Offset because genpool can't handle 0 addresses */
387 if (pool->paddr == 0)
388 va -= SZ_4K;
389 *iova = va;
390 return 0;
391 }
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700392
Laura Abbottd01221b2012-05-16 17:52:49 -0700393 return -ENOMEM;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700394}
395
396void msm_free_iova_address(unsigned long iova,
Laura Abbottd01221b2012-05-16 17:52:49 -0700397 unsigned int iommu_domain,
398 unsigned int partition_no,
399 unsigned long size)
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700400{
Laura Abbottd01221b2012-05-16 17:52:49 -0700401 struct msm_iova_data *data;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700402 struct mem_pool *pool;
403
Laura Abbottd01221b2012-05-16 17:52:49 -0700404 data = find_domain(iommu_domain);
405
406 if (!data) {
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700407 WARN(1, "Invalid domain %d\n", iommu_domain);
408 return;
409 }
410
Laura Abbottd01221b2012-05-16 17:52:49 -0700411 if (partition_no >= data->npools) {
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700412 WARN(1, "Invalid partition %d for domain %d\n",
413 partition_no, iommu_domain);
414 return;
415 }
416
Laura Abbottd01221b2012-05-16 17:52:49 -0700417 pool = &data->pools[partition_no];
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700418
419 if (!pool)
420 return;
421
422 pool->free += size;
Laura Abbottd01221b2012-05-16 17:52:49 -0700423
424 /* Offset because genpool can't handle 0 addresses */
425 if (pool->paddr == 0)
426 iova += SZ_4K;
427
Laura Abbott594e9cd2013-04-27 18:27:38 -0700428 mutex_lock(&pool->pool_mutex);
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700429 gen_pool_free(pool->gpool, iova, size);
Laura Abbott594e9cd2013-04-27 18:27:38 -0700430 mutex_unlock(&pool->pool_mutex);
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700431}
432
Laura Abbottd01221b2012-05-16 17:52:49 -0700433int msm_register_domain(struct msm_iova_layout *layout)
434{
435 int i;
436 struct msm_iova_data *data;
437 struct mem_pool *pools;
Laura Abbott6a6ca552012-08-30 11:26:31 -0700438 struct bus_type *bus;
Laura Abbottd01221b2012-05-16 17:52:49 -0700439
440 if (!layout)
441 return -EINVAL;
442
443 data = kmalloc(sizeof(*data), GFP_KERNEL);
444
445 if (!data)
446 return -ENOMEM;
447
Olav Hauganffa22b82013-04-26 13:47:11 -0700448 pools = kzalloc(sizeof(struct mem_pool) * layout->npartitions,
Laura Abbottd01221b2012-05-16 17:52:49 -0700449 GFP_KERNEL);
450
451 if (!pools)
Olav Hauganffa22b82013-04-26 13:47:11 -0700452 goto free_data;
Laura Abbottd01221b2012-05-16 17:52:49 -0700453
454 for (i = 0; i < layout->npartitions; i++) {
455 if (layout->partitions[i].size == 0)
456 continue;
457
458 pools[i].gpool = gen_pool_create(PAGE_SHIFT, -1);
459
460 if (!pools[i].gpool)
461 continue;
462
463 pools[i].paddr = layout->partitions[i].start;
464 pools[i].size = layout->partitions[i].size;
Laura Abbott594e9cd2013-04-27 18:27:38 -0700465 mutex_init(&pools[i].pool_mutex);
Laura Abbottd01221b2012-05-16 17:52:49 -0700466
467 /*
468 * genalloc can't handle a pool starting at address 0.
469 * For now, solve this problem by offsetting the value
470 * put in by 4k.
471 * gen pool address = actual address + 4k
472 */
473 if (pools[i].paddr == 0)
474 layout->partitions[i].start += SZ_4K;
475
476 if (gen_pool_add(pools[i].gpool,
477 layout->partitions[i].start,
478 layout->partitions[i].size, -1)) {
479 gen_pool_destroy(pools[i].gpool);
480 pools[i].gpool = NULL;
481 continue;
482 }
483 }
484
Laura Abbott6a6ca552012-08-30 11:26:31 -0700485 bus = layout->is_secure == MSM_IOMMU_DOMAIN_SECURE ?
486 &msm_iommu_sec_bus_type :
487 &platform_bus_type;
488
Laura Abbottd01221b2012-05-16 17:52:49 -0700489 data->pools = pools;
490 data->npools = layout->npartitions;
Olav Hauganffa22b82013-04-26 13:47:11 -0700491 data->domain_num = ida_simple_get(&domain_nums, 0, 0, GFP_KERNEL);
492 if (data->domain_num < 0)
493 goto free_pools;
494
Laura Abbott6a6ca552012-08-30 11:26:31 -0700495 data->domain = iommu_domain_alloc(bus, layout->domain_flags);
Olav Haugane7837212013-03-22 14:49:47 -0700496 if (!data->domain)
Olav Hauganffa22b82013-04-26 13:47:11 -0700497 goto free_domain_num;
Laura Abbottd01221b2012-05-16 17:52:49 -0700498
Olav Haugana9820ce2013-03-22 14:50:28 -0700499 msm_iommu_set_client_name(data->domain, layout->client_name);
500
Laura Abbottd01221b2012-05-16 17:52:49 -0700501 add_domain(data);
502
503 return data->domain_num;
504
Olav Hauganffa22b82013-04-26 13:47:11 -0700505free_domain_num:
506 ida_simple_remove(&domain_nums, data->domain_num);
507
508free_pools:
509 for (i = 0; i < layout->npartitions; i++) {
510 if (pools[i].gpool)
511 gen_pool_destroy(pools[i].gpool);
512 }
513 kfree(pools);
514free_data:
Laura Abbottd01221b2012-05-16 17:52:49 -0700515 kfree(data);
516
517 return -EINVAL;
518}
Laura Abbott33b30be2012-07-09 09:48:54 -0700519EXPORT_SYMBOL(msm_register_domain);
Laura Abbottd01221b2012-05-16 17:52:49 -0700520
Olav Hauganffa22b82013-04-26 13:47:11 -0700521int msm_unregister_domain(struct iommu_domain *domain)
522{
523 unsigned int i;
524 struct msm_iova_data *data = msm_domain_to_iova_data(domain);
525
526 if (IS_ERR_OR_NULL(data)) {
527 pr_err("%s: Could not find iova_data\n", __func__);
528 return -EINVAL;
529 }
530
531 if (remove_domain(data->domain)) {
532 pr_err("%s: Domain not found. Failed to remove domain\n",
533 __func__);
534 }
535
536 iommu_domain_free(domain);
537
538 ida_simple_remove(&domain_nums, data->domain_num);
539
540 for (i = 0; i < data->npools; ++i)
541 gen_pool_destroy(data->pools[i].gpool);
542
543 kfree(data->pools);
544 kfree(data);
545 return 0;
546}
547EXPORT_SYMBOL(msm_unregister_domain);
548
Olav Haugan35deadc2012-12-10 18:28:27 -0800549static int find_and_add_contexts(struct iommu_group *group,
550 const struct device_node *node,
551 unsigned int num_contexts)
552{
553 unsigned int i;
554 struct device *ctx;
555 const char *name;
556 struct device_node *ctx_node;
557 int ret_val = 0;
558
559 for (i = 0; i < num_contexts; ++i) {
560 ctx_node = of_parse_phandle((struct device_node *) node,
561 "qcom,iommu-contexts", i);
562 if (!ctx_node) {
563 pr_err("Unable to parse phandle #%u\n", i);
564 ret_val = -EINVAL;
565 goto out;
566 }
567 if (of_property_read_string(ctx_node, "label", &name)) {
568 pr_err("Could not find label property\n");
569 ret_val = -EINVAL;
570 goto out;
571 }
572 ctx = msm_iommu_get_ctx(name);
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700573 if (IS_ERR(ctx)) {
574 ret_val = PTR_ERR(ctx);
Olav Haugan35deadc2012-12-10 18:28:27 -0800575 goto out;
576 }
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700577
578 ret_val = iommu_group_add_device(group, ctx);
579 if (ret_val)
580 goto out;
Olav Haugan35deadc2012-12-10 18:28:27 -0800581 }
582out:
583 return ret_val;
584}
585
586static int create_and_add_domain(struct iommu_group *group,
Olav Haugana9820ce2013-03-22 14:50:28 -0700587 struct device_node const *node,
588 char const *name)
Olav Haugan35deadc2012-12-10 18:28:27 -0800589{
590 unsigned int ret_val = 0;
Shalaj Jain47969302013-02-09 17:28:17 -0800591 unsigned int i, j;
Olav Haugan35deadc2012-12-10 18:28:27 -0800592 struct msm_iova_layout l;
593 struct msm_iova_partition *part = 0;
594 struct iommu_domain *domain = 0;
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700595 unsigned int *addr_array = 0;
Olav Haugan35deadc2012-12-10 18:28:27 -0800596 unsigned int array_size;
597 int domain_no;
598 int secure_domain;
599 int l2_redirect;
600
601 if (of_get_property(node, "qcom,virtual-addr-pool", &array_size)) {
602 l.npartitions = array_size / sizeof(unsigned int) / 2;
603 part = kmalloc(
604 sizeof(struct msm_iova_partition) * l.npartitions,
605 GFP_KERNEL);
606 if (!part) {
607 pr_err("%s: could not allocate space for partition",
608 __func__);
609 ret_val = -ENOMEM;
610 goto out;
611 }
612 addr_array = kmalloc(array_size, GFP_KERNEL);
613 if (!addr_array) {
614 pr_err("%s: could not allocate space for partition",
615 __func__);
616 ret_val = -ENOMEM;
617 goto free_mem;
618 }
619
620 ret_val = of_property_read_u32_array(node,
621 "qcom,virtual-addr-pool",
622 addr_array,
623 array_size/sizeof(unsigned int));
624 if (ret_val) {
625 ret_val = -EINVAL;
626 goto free_mem;
627 }
628
Shalaj Jain47969302013-02-09 17:28:17 -0800629 for (i = 0, j = 0; j < l.npartitions * 2; i++, j += 2) {
630 part[i].start = addr_array[j];
631 part[i].size = addr_array[j+1];
Olav Haugan35deadc2012-12-10 18:28:27 -0800632 }
633 } else {
634 l.npartitions = 1;
635 part = kmalloc(
636 sizeof(struct msm_iova_partition) * l.npartitions,
637 GFP_KERNEL);
638 if (!part) {
639 pr_err("%s: could not allocate space for partition",
640 __func__);
641 ret_val = -ENOMEM;
642 goto out;
643 }
644 part[0].start = 0x0;
645 part[0].size = 0xFFFFFFFF;
646 }
647
Olav Haugana9820ce2013-03-22 14:50:28 -0700648 l.client_name = name;
Olav Haugan35deadc2012-12-10 18:28:27 -0800649 l.partitions = part;
650
651 secure_domain = of_property_read_bool(node, "qcom,secure-domain");
652 l.is_secure = (secure_domain) ? MSM_IOMMU_DOMAIN_SECURE : 0;
653
654 l2_redirect = of_property_read_bool(node, "qcom,l2-redirect");
655 l.domain_flags = (l2_redirect) ? MSM_IOMMU_DOMAIN_PT_CACHEABLE : 0;
656
657 domain_no = msm_register_domain(&l);
658 if (domain_no >= 0)
659 domain = msm_get_iommu_domain(domain_no);
660 else
661 ret_val = domain_no;
662
663 iommu_group_set_iommudata(group, domain, NULL);
664
665free_mem:
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700666 kfree(addr_array);
Olav Haugan35deadc2012-12-10 18:28:27 -0800667 kfree(part);
668out:
669 return ret_val;
670}
671
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700672static int __msm_group_get_domain(struct device *dev, void *data)
673{
674 struct msm_iommu_data_entry *list_entry;
675 struct list_head *dev_list = data;
676 int ret_val = 0;
677
678 list_entry = kmalloc(sizeof(*list_entry), GFP_KERNEL);
679 if (list_entry) {
680 list_entry->data = dev;
681 list_add(&list_entry->list, dev_list);
682 } else {
683 ret_val = -ENOMEM;
684 }
685
686 return ret_val;
687}
688
689static void __msm_iommu_group_remove_device(struct iommu_group *grp)
690{
691 struct msm_iommu_data_entry *tmp;
692 struct msm_iommu_data_entry *list_entry;
693 struct list_head dev_list;
694
695 INIT_LIST_HEAD(&dev_list);
696 iommu_group_for_each_dev(grp, &dev_list, __msm_group_get_domain);
697
698 list_for_each_entry_safe(list_entry, tmp, &dev_list, list) {
699 iommu_group_remove_device(list_entry->data);
700 list_del(&list_entry->list);
701 kfree(list_entry);
702 }
703}
704
705
Olav Haugan35deadc2012-12-10 18:28:27 -0800706static int iommu_domain_parse_dt(const struct device_node *dt_node)
707{
708 struct device_node *node;
709 int sz;
710 unsigned int num_contexts;
711 int ret_val = 0;
712 struct iommu_group *group = 0;
713 const char *name;
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700714 struct msm_iommu_data_entry *grp_list_entry;
715 struct msm_iommu_data_entry *tmp;
716 struct list_head iommu_group_list;
717 INIT_LIST_HEAD(&iommu_group_list);
Olav Haugan35deadc2012-12-10 18:28:27 -0800718
719 for_each_child_of_node(dt_node, node) {
720 group = iommu_group_alloc();
721 if (IS_ERR(group)) {
722 ret_val = PTR_ERR(group);
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700723 group = 0;
724 goto free_group;
Olav Haugan35deadc2012-12-10 18:28:27 -0800725 }
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700726
727 /* This is only needed to clean up memory if something fails */
728 grp_list_entry = kmalloc(sizeof(*grp_list_entry),
729 GFP_KERNEL);
730 if (grp_list_entry) {
731 grp_list_entry->data = group;
732 list_add(&grp_list_entry->list, &iommu_group_list);
733 } else {
734 ret_val = -ENOMEM;
735 goto free_group;
736 }
737
Olav Haugan35deadc2012-12-10 18:28:27 -0800738 if (of_property_read_string(node, "label", &name)) {
739 ret_val = -EINVAL;
740 goto free_group;
741 }
742 iommu_group_set_name(group, name);
743
744 if (!of_get_property(node, "qcom,iommu-contexts", &sz)) {
745 pr_err("Could not find qcom,iommu-contexts property\n");
746 ret_val = -EINVAL;
747 goto free_group;
748 }
749 num_contexts = sz / sizeof(unsigned int);
750
751 ret_val = find_and_add_contexts(group, node, num_contexts);
752 if (ret_val) {
Olav Haugan35deadc2012-12-10 18:28:27 -0800753 goto free_group;
754 }
Olav Haugana9820ce2013-03-22 14:50:28 -0700755 ret_val = create_and_add_domain(group, node, name);
Olav Haugan35deadc2012-12-10 18:28:27 -0800756 if (ret_val) {
757 ret_val = -EINVAL;
758 goto free_group;
759 }
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700760
761 /* Remove reference to the group that is taken when the group
762 * is allocated. This will ensure that when all the devices in
763 * the group are removed the group will be released.
764 */
765 iommu_group_put(group);
Olav Haugan35deadc2012-12-10 18:28:27 -0800766 }
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700767
768 list_for_each_entry_safe(grp_list_entry, tmp, &iommu_group_list, list) {
769 list_del(&grp_list_entry->list);
770 kfree(grp_list_entry);
771 }
772 goto out;
773
Olav Haugan35deadc2012-12-10 18:28:27 -0800774free_group:
Olav Haugan08fb8ac2013-05-15 15:30:07 -0700775 list_for_each_entry_safe(grp_list_entry, tmp, &iommu_group_list, list) {
776 struct iommu_domain *d;
777
778 d = iommu_group_get_iommudata(grp_list_entry->data);
779 if (d)
780 msm_unregister_domain(d);
781
782 __msm_iommu_group_remove_device(grp_list_entry->data);
783 list_del(&grp_list_entry->list);
784 kfree(grp_list_entry);
785 }
786 iommu_group_put(group);
Olav Haugan35deadc2012-12-10 18:28:27 -0800787out:
788 return ret_val;
789}
790
791static int iommu_domain_probe(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792{
Laura Abbott0577d7b2012-04-17 11:14:30 -0700793 struct iommu_domains_pdata *p = pdev->dev.platform_data;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700794 int i, j;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795
Chintan Pandyace27e562012-07-06 23:07:57 +0530796 if (!msm_use_iommu())
797 return -ENODEV;
798
Olav Haugan35deadc2012-12-10 18:28:27 -0800799 if (pdev->dev.of_node)
800 return iommu_domain_parse_dt(pdev->dev.of_node);
801 else if (!p)
Laura Abbott0577d7b2012-04-17 11:14:30 -0700802 return -ENODEV;
803
Laura Abbottd01221b2012-05-16 17:52:49 -0700804 for (i = 0; i < p->ndomains; i++) {
805 struct msm_iova_layout l;
806 struct msm_iova_partition *part;
807 struct msm_iommu_domain *domains;
Laura Abbott0577d7b2012-04-17 11:14:30 -0700808
Laura Abbottd01221b2012-05-16 17:52:49 -0700809 domains = p->domains;
810 l.npartitions = domains[i].npools;
811 part = kmalloc(
812 sizeof(struct msm_iova_partition) * l.npartitions,
813 GFP_KERNEL);
814
815 if (!part) {
816 pr_info("%s: could not allocate space for domain %d",
817 __func__, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 }
Laura Abbottd01221b2012-05-16 17:52:49 -0700820
821 for (j = 0; j < l.npartitions; j++) {
822 part[j].start = p->domains[i].iova_pools[j].paddr;
823 part[j].size = p->domains[i].iova_pools[j].size;
824 }
825
826 l.partitions = part;
827
828 msm_register_domain(&l);
829
830 kfree(part);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 }
832
Laura Abbott0577d7b2012-04-17 11:14:30 -0700833 for (i = 0; i < p->nnames; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834 struct device *ctx = msm_iommu_get_ctx(
Laura Abbott0577d7b2012-04-17 11:14:30 -0700835 p->domain_names[i].name);
Laura Abbottd01221b2012-05-16 17:52:49 -0700836 struct iommu_domain *domain;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837
838 if (!ctx)
839 continue;
840
Laura Abbottd01221b2012-05-16 17:52:49 -0700841 domain = msm_get_iommu_domain(p->domain_names[i].domain);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842
Laura Abbottd01221b2012-05-16 17:52:49 -0700843 if (!domain)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844 continue;
845
Laura Abbottd01221b2012-05-16 17:52:49 -0700846 if (iommu_attach_device(domain, ctx)) {
Olav Haugan35deadc2012-12-10 18:28:27 -0800847 WARN(1, "%s: could not attach domain %p to context %s. iommu programming will not occur.\n",
848 __func__, domain, p->domain_names[i].name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849 continue;
850 }
851 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 return 0;
853}
Laura Abbott0577d7b2012-04-17 11:14:30 -0700854
Olav Haugan35deadc2012-12-10 18:28:27 -0800855static int __devexit iommu_domain_exit(struct platform_device *pdev)
856{
857 return 0;
858}
859
860static struct of_device_id msm_iommu_domain_match_table[] = {
861 { .name = "qcom,iommu-domains", },
862 {}
863};
864
Laura Abbott0577d7b2012-04-17 11:14:30 -0700865static struct platform_driver iommu_domain_driver = {
866 .driver = {
867 .name = "iommu_domains",
Olav Haugan35deadc2012-12-10 18:28:27 -0800868 .of_match_table = msm_iommu_domain_match_table,
Laura Abbott0577d7b2012-04-17 11:14:30 -0700869 .owner = THIS_MODULE
870 },
Olav Haugan35deadc2012-12-10 18:28:27 -0800871 .probe = iommu_domain_probe,
872 .remove = __devexit_p(iommu_domain_exit),
Laura Abbott0577d7b2012-04-17 11:14:30 -0700873};
874
875static int __init msm_subsystem_iommu_init(void)
876{
Olav Haugan35deadc2012-12-10 18:28:27 -0800877 int ret;
878 ret = platform_driver_register(&iommu_domain_driver);
879 if (ret != 0)
880 pr_err("Failed to register IOMMU domain driver\n");
881 return ret;
Laura Abbott0577d7b2012-04-17 11:14:30 -0700882}
Olav Haugan35deadc2012-12-10 18:28:27 -0800883
884static void __exit msm_subsystem_iommu_exit(void)
885{
886 platform_driver_unregister(&iommu_domain_driver);
887}
888
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889device_initcall(msm_subsystem_iommu_init);
Olav Haugan35deadc2012-12-10 18:28:27 -0800890module_exit(msm_subsystem_iommu_exit);
891