blob: 8f9464c2a8a1cf0e40109ad718e9e281d027eaea [file] [log] [blame]
Olav Hauganab77b1b2012-02-28 09:19:22 -08001/* Copyright (c) 2010-2012, Code Aurora Forum. 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>
Steve Mucklef132c6c2012-06-06 18:30:57 -070018#include <linux/vmalloc.h>
Laura Abbottd01221b2012-05-16 17:52:49 -070019#include <linux/rbtree.h>
20#include <linux/slab.h>
Olav Haugan16cdb412012-03-27 13:02:17 -070021#include <linux/vmalloc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <asm/sizes.h>
23#include <asm/page.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024#include <mach/iommu.h>
25#include <mach/iommu_domains.h>
Laura Abbott9f4a8e62011-08-29 19:08:07 -070026#include <mach/socinfo.h>
Laura Abbottd01221b2012-05-16 17:52:49 -070027#include <mach/msm_subsystem_map.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
Olav Haugan8726caf2012-05-10 15:11:35 -070029/* dummy 64K for overmapping */
30char iommu_dummy[2*SZ_64K-4];
Laura Abbotte956cce2011-10-25 13:33:20 -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
Laura Abbottd01221b2012-05-16 17:52:49 -070040static struct rb_root domain_root;
41DEFINE_MUTEX(domain_mutex);
42static atomic_t domain_nums = ATOMIC_INIT(-1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043
Laura Abbott2030c1b2012-07-18 06:38:00 -070044int msm_use_iommu()
45{
46 return iommu_present(&platform_bus_type);
47}
48
Laura Abbotte956cce2011-10-25 13:33:20 -070049int msm_iommu_map_extra(struct iommu_domain *domain,
50 unsigned long start_iova,
51 unsigned long size,
Olav Haugan8726caf2012-05-10 15:11:35 -070052 unsigned long page_size,
Laura Abbotte956cce2011-10-25 13:33:20 -070053 int cached)
54{
Olav Haugan5e7befd2012-06-19 14:59:37 -070055 int ret = 0;
56 int i = 0;
Olav Haugan8726caf2012-05-10 15:11:35 -070057 unsigned long phy_addr = ALIGN(virt_to_phys(iommu_dummy), page_size);
58 unsigned long temp_iova = start_iova;
Olav Haugan5e7befd2012-06-19 14:59:37 -070059 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 Abbotte956cce2011-10-25 13:33:20 -070063
Olav Haugan5e7befd2012-06-19 14:59:37 -070064 sglist = vmalloc(sizeof(*sglist) * nrpages);
65 if (!sglist) {
66 ret = -ENOMEM;
Olav Haugan8726caf2012-05-10 15:11:35 -070067 goto out;
68 }
Olav Haugan5e7befd2012-06-19 14:59:37 -070069
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 Abbotte956cce2011-10-25 13:33:20 -070098 }
Olav Haugan5e7befd2012-06-19 14:59:37 -070099 return ret;
Olav Haugan8726caf2012-05-10 15:11:35 -0700100out:
101 for (; i > 0; --i) {
102 temp_iova -= page_size;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700103 iommu_unmap(domain, start_iova, page_size);
Olav Haugan16cdb412012-03-27 13:02:17 -0700104 }
Olav Haugan5e7befd2012-06-19 14:59:37 -0700105 return ret;
Olav Haugan8726caf2012-05-10 15:11:35 -0700106}
Laura Abbotte956cce2011-10-25 13:33:20 -0700107
Olav Haugan8726caf2012-05-10 15:11:35 -0700108void 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 Mucklef132c6c2012-06-06 18:30:57 -0700120 iommu_unmap(domain, temp_iova, page_size);
Olav Haugan8726caf2012-05-10 15:11:35 -0700121 temp_iova += page_size;
122 }
Laura Abbotte956cce2011-10-25 13:33:20 -0700123}
124
Laura Abbottd027fdb2012-04-17 16:22:24 -0700125static 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 Abbotte543cfc2012-06-07 17:51:53 -0700133 int prot = IOMMU_WRITE | IOMMU_READ;
134 prot |= cached ? IOMMU_CACHE : 0;
Laura Abbottd027fdb2012-04-17 16:22:24 -0700135
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 Abbotte543cfc2012-06-07 17:51:53 -0700147 ret = iommu_map_range(domain, iova, sglist, size, prot);
Laura Abbottd027fdb2012-04-17 16:22:24 -0700148 if (ret) {
149 pr_err("%s: could not map extra %lx in domain %p\n",
150 __func__, iova, domain);
151 }
152
153 vfree(sglist);
154err1:
155 return ret;
156
157}
158
159int 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 Abbott2030c1b2012-07-18 06:38:00 -0700173 if (!msm_use_iommu()) {
174 *iova_val = phys;
175 return 0;
176 }
177
Laura Abbottd01221b2012-05-16 17:52:49 -0700178 ret = msm_allocate_iova_address(domain_no, partition_no, size, align,
179 &iova);
Laura Abbottd027fdb2012-04-17 16:22:24 -0700180
Laura Abbottd01221b2012-05-16 17:52:49 -0700181 if (ret)
Laura Abbottd027fdb2012-04-17 16:22:24 -0700182 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}
194
195void msm_iommu_unmap_contig_buffer(unsigned long iova,
196 unsigned int domain_no,
197 unsigned int partition_no,
198 unsigned long size)
199{
Laura Abbott2030c1b2012-07-18 06:38:00 -0700200 if (!msm_use_iommu())
201 return;
202
Laura Abbottd027fdb2012-04-17 16:22:24 -0700203 iommu_unmap_range(msm_get_iommu_domain(domain_no), iova, size);
204 msm_free_iova_address(iova, domain_no, partition_no, size);
205}
Laura Abbotte956cce2011-10-25 13:33:20 -0700206
Laura Abbottd01221b2012-05-16 17:52:49 -0700207static struct msm_iova_data *find_domain(int domain_num)
208{
209 struct rb_root *root = &domain_root;
210 struct rb_node *p = root->rb_node;
211
212 mutex_lock(&domain_mutex);
213
214 while (p) {
215 struct msm_iova_data *node;
216
217 node = rb_entry(p, struct msm_iova_data, node);
218 if (domain_num < node->domain_num)
219 p = p->rb_left;
Laura Abbott723970d2012-06-05 15:01:16 -0700220 else if (domain_num > node->domain_num)
Laura Abbottd01221b2012-05-16 17:52:49 -0700221 p = p->rb_right;
222 else {
223 mutex_unlock(&domain_mutex);
224 return node;
225 }
226 }
227 mutex_unlock(&domain_mutex);
228 return NULL;
229}
230
231static int add_domain(struct msm_iova_data *node)
232{
233 struct rb_root *root = &domain_root;
234 struct rb_node **p = &root->rb_node;
235 struct rb_node *parent = NULL;
236
237 mutex_lock(&domain_mutex);
238 while (*p) {
239 struct msm_iova_data *tmp;
240 parent = *p;
241
242 tmp = rb_entry(parent, struct msm_iova_data, node);
243
244 if (node->domain_num < tmp->domain_num)
245 p = &(*p)->rb_left;
246 else if (node->domain_num > tmp->domain_num)
247 p = &(*p)->rb_right;
248 else
249 BUG();
250 }
251 rb_link_node(&node->node, parent, p);
252 rb_insert_color(&node->node, root);
253 mutex_unlock(&domain_mutex);
254 return 0;
255}
256
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700257struct iommu_domain *msm_get_iommu_domain(int domain_num)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258{
Laura Abbottd01221b2012-05-16 17:52:49 -0700259 struct msm_iova_data *data;
260
261 data = find_domain(domain_num);
262
263 if (data)
264 return data->domain;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700265 else
266 return NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267}
268
Laura Abbottd01221b2012-05-16 17:52:49 -0700269int msm_allocate_iova_address(unsigned int iommu_domain,
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700270 unsigned int partition_no,
271 unsigned long size,
Laura Abbottd01221b2012-05-16 17:52:49 -0700272 unsigned long align,
273 unsigned long *iova)
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700274{
Laura Abbottd01221b2012-05-16 17:52:49 -0700275 struct msm_iova_data *data;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700276 struct mem_pool *pool;
Laura Abbottd01221b2012-05-16 17:52:49 -0700277 unsigned long va;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700278
Laura Abbottd01221b2012-05-16 17:52:49 -0700279 data = find_domain(iommu_domain);
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700280
Laura Abbottd01221b2012-05-16 17:52:49 -0700281 if (!data)
282 return -EINVAL;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700283
Laura Abbottd01221b2012-05-16 17:52:49 -0700284 if (partition_no >= data->npools)
285 return -EINVAL;
286
287 pool = &data->pools[partition_no];
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700288
289 if (!pool->gpool)
Laura Abbottd01221b2012-05-16 17:52:49 -0700290 return -EINVAL;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700291
Laura Abbottd01221b2012-05-16 17:52:49 -0700292 va = gen_pool_alloc_aligned(pool->gpool, size, ilog2(align));
293 if (va) {
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700294 pool->free -= size;
Laura Abbottd01221b2012-05-16 17:52:49 -0700295 /* Offset because genpool can't handle 0 addresses */
296 if (pool->paddr == 0)
297 va -= SZ_4K;
298 *iova = va;
299 return 0;
300 }
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700301
Laura Abbottd01221b2012-05-16 17:52:49 -0700302 return -ENOMEM;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700303}
304
305void msm_free_iova_address(unsigned long iova,
Laura Abbottd01221b2012-05-16 17:52:49 -0700306 unsigned int iommu_domain,
307 unsigned int partition_no,
308 unsigned long size)
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700309{
Laura Abbottd01221b2012-05-16 17:52:49 -0700310 struct msm_iova_data *data;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700311 struct mem_pool *pool;
312
Laura Abbottd01221b2012-05-16 17:52:49 -0700313 data = find_domain(iommu_domain);
314
315 if (!data) {
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700316 WARN(1, "Invalid domain %d\n", iommu_domain);
317 return;
318 }
319
Laura Abbottd01221b2012-05-16 17:52:49 -0700320 if (partition_no >= data->npools) {
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700321 WARN(1, "Invalid partition %d for domain %d\n",
322 partition_no, iommu_domain);
323 return;
324 }
325
Laura Abbottd01221b2012-05-16 17:52:49 -0700326 pool = &data->pools[partition_no];
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700327
328 if (!pool)
329 return;
330
331 pool->free += size;
Laura Abbottd01221b2012-05-16 17:52:49 -0700332
333 /* Offset because genpool can't handle 0 addresses */
334 if (pool->paddr == 0)
335 iova += SZ_4K;
336
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700337 gen_pool_free(pool->gpool, iova, size);
338}
339
Laura Abbottd01221b2012-05-16 17:52:49 -0700340int msm_register_domain(struct msm_iova_layout *layout)
341{
342 int i;
343 struct msm_iova_data *data;
344 struct mem_pool *pools;
345
346 if (!layout)
347 return -EINVAL;
348
349 data = kmalloc(sizeof(*data), GFP_KERNEL);
350
351 if (!data)
352 return -ENOMEM;
353
354 pools = kmalloc(sizeof(struct mem_pool) * layout->npartitions,
355 GFP_KERNEL);
356
357 if (!pools)
358 goto out;
359
360 for (i = 0; i < layout->npartitions; i++) {
361 if (layout->partitions[i].size == 0)
362 continue;
363
364 pools[i].gpool = gen_pool_create(PAGE_SHIFT, -1);
365
366 if (!pools[i].gpool)
367 continue;
368
369 pools[i].paddr = layout->partitions[i].start;
370 pools[i].size = layout->partitions[i].size;
371
372 /*
373 * genalloc can't handle a pool starting at address 0.
374 * For now, solve this problem by offsetting the value
375 * put in by 4k.
376 * gen pool address = actual address + 4k
377 */
378 if (pools[i].paddr == 0)
379 layout->partitions[i].start += SZ_4K;
380
381 if (gen_pool_add(pools[i].gpool,
382 layout->partitions[i].start,
383 layout->partitions[i].size, -1)) {
384 gen_pool_destroy(pools[i].gpool);
385 pools[i].gpool = NULL;
386 continue;
387 }
388 }
389
390 data->pools = pools;
391 data->npools = layout->npartitions;
392 data->domain_num = atomic_inc_return(&domain_nums);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700393 data->domain = iommu_domain_alloc(&platform_bus_type,
394 layout->domain_flags);
Laura Abbottd01221b2012-05-16 17:52:49 -0700395
396 add_domain(data);
397
398 return data->domain_num;
399
400out:
401 kfree(data);
402
403 return -EINVAL;
404}
405
Laura Abbott0577d7b2012-04-17 11:14:30 -0700406static int __init iommu_domain_probe(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407{
Laura Abbott0577d7b2012-04-17 11:14:30 -0700408 struct iommu_domains_pdata *p = pdev->dev.platform_data;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700409 int i, j;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410
Chintan Pandyace27e562012-07-06 23:07:57 +0530411 if (!msm_use_iommu())
412 return -ENODEV;
413
Laura Abbott0577d7b2012-04-17 11:14:30 -0700414 if (!p)
415 return -ENODEV;
416
Laura Abbottd01221b2012-05-16 17:52:49 -0700417 for (i = 0; i < p->ndomains; i++) {
418 struct msm_iova_layout l;
419 struct msm_iova_partition *part;
420 struct msm_iommu_domain *domains;
Laura Abbott0577d7b2012-04-17 11:14:30 -0700421
Laura Abbottd01221b2012-05-16 17:52:49 -0700422 domains = p->domains;
423 l.npartitions = domains[i].npools;
424 part = kmalloc(
425 sizeof(struct msm_iova_partition) * l.npartitions,
426 GFP_KERNEL);
427
428 if (!part) {
429 pr_info("%s: could not allocate space for domain %d",
430 __func__, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 continue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432 }
Laura Abbottd01221b2012-05-16 17:52:49 -0700433
434 for (j = 0; j < l.npartitions; j++) {
435 part[j].start = p->domains[i].iova_pools[j].paddr;
436 part[j].size = p->domains[i].iova_pools[j].size;
437 }
438
439 l.partitions = part;
440
441 msm_register_domain(&l);
442
443 kfree(part);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 }
445
Laura Abbott0577d7b2012-04-17 11:14:30 -0700446 for (i = 0; i < p->nnames; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447 struct device *ctx = msm_iommu_get_ctx(
Laura Abbott0577d7b2012-04-17 11:14:30 -0700448 p->domain_names[i].name);
Laura Abbottd01221b2012-05-16 17:52:49 -0700449 struct iommu_domain *domain;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450
451 if (!ctx)
452 continue;
453
Laura Abbottd01221b2012-05-16 17:52:49 -0700454 domain = msm_get_iommu_domain(p->domain_names[i].domain);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455
Laura Abbottd01221b2012-05-16 17:52:49 -0700456 if (!domain)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 continue;
458
Laura Abbottd01221b2012-05-16 17:52:49 -0700459 if (iommu_attach_device(domain, ctx)) {
460 WARN(1, "%s: could not attach domain %p to context %s."
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461 " iommu programming will not occur.\n",
Laura Abbottd01221b2012-05-16 17:52:49 -0700462 __func__, domain,
Laura Abbott0577d7b2012-04-17 11:14:30 -0700463 p->domain_names[i].name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 continue;
465 }
466 }
467
468 return 0;
469}
Laura Abbott0577d7b2012-04-17 11:14:30 -0700470
471static struct platform_driver iommu_domain_driver = {
472 .driver = {
473 .name = "iommu_domains",
474 .owner = THIS_MODULE
475 },
476};
477
478static int __init msm_subsystem_iommu_init(void)
479{
480 return platform_driver_probe(&iommu_domain_driver, iommu_domain_probe);
481}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482device_initcall(msm_subsystem_iommu_init);