blob: d37da30685ffae6078ba5fb4e3552a4d9af59a08 [file] [log] [blame]
Evan McClain97f044c2012-09-12 00:33:53 -04001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/io.h>
14#include <linux/types.h>
15#include <linux/err.h>
16#include <linux/slab.h>
17#include <linux/memory_alloc.h>
18#include <linux/module.h>
19#include <mach/iommu.h>
20#include <mach/iommu_domains.h>
21#include <mach/msm_subsystem_map.h>
22
23struct msm_buffer_node {
24 struct rb_node rb_node_all_buffer;
25 struct rb_node rb_node_paddr;
26 struct msm_mapped_buffer *buf;
27 unsigned long length;
28 unsigned int *subsystems;
29 unsigned int nsubsys;
30 unsigned int phys;
31};
32
33static struct rb_root buffer_root;
34static struct rb_root phys_root;
35DEFINE_MUTEX(msm_buffer_mutex);
36
37static struct msm_buffer_node *find_buffer(void *key)
38{
39 struct rb_root *root = &buffer_root;
40 struct rb_node *p = root->rb_node;
41
42 mutex_lock(&msm_buffer_mutex);
43
44 while (p) {
45 struct msm_buffer_node *node;
46
47 node = rb_entry(p, struct msm_buffer_node, rb_node_all_buffer);
48 if (node->buf->vaddr) {
49 if (key < node->buf->vaddr)
50 p = p->rb_left;
51 else if (key > node->buf->vaddr)
52 p = p->rb_right;
53 else {
54 mutex_unlock(&msm_buffer_mutex);
55 return node;
56 }
57 } else {
58 if (key < (void *)node->buf)
59 p = p->rb_left;
60 else if (key > (void *)node->buf)
61 p = p->rb_right;
62 else {
63 mutex_unlock(&msm_buffer_mutex);
64 return node;
65 }
66 }
67 }
68 mutex_unlock(&msm_buffer_mutex);
69 return NULL;
70}
71
72static struct msm_buffer_node *find_buffer_phys(unsigned int phys)
73{
74 struct rb_root *root = &phys_root;
75 struct rb_node *p = root->rb_node;
76
77 mutex_lock(&msm_buffer_mutex);
78
79 while (p) {
80 struct msm_buffer_node *node;
81
82 node = rb_entry(p, struct msm_buffer_node, rb_node_paddr);
83 if (phys < node->phys)
84 p = p->rb_left;
85 else if (phys > node->phys)
86 p = p->rb_right;
87 else {
88 mutex_unlock(&msm_buffer_mutex);
89 return node;
90 }
91 }
92 mutex_unlock(&msm_buffer_mutex);
93 return NULL;
94
95}
96
97static int add_buffer(struct msm_buffer_node *node)
98{
99 struct rb_root *root = &buffer_root;
100 struct rb_node **p = &root->rb_node;
101 struct rb_node *parent = NULL;
102 void *key;
103
104 if (node->buf->vaddr)
105 key = node->buf->vaddr;
106 else
107 key = node->buf;
108
109 mutex_lock(&msm_buffer_mutex);
110 while (*p) {
111 struct msm_buffer_node *tmp;
112 parent = *p;
113
114 tmp = rb_entry(parent, struct msm_buffer_node,
115 rb_node_all_buffer);
116
117 if (tmp->buf->vaddr) {
118 if (key < tmp->buf->vaddr)
119 p = &(*p)->rb_left;
120 else if (key > tmp->buf->vaddr)
121 p = &(*p)->rb_right;
122 else {
123 WARN(1, "tried to add buffer twice! buf = %p"
124 " vaddr = %p iova = %p", tmp->buf,
125 tmp->buf->vaddr,
126 tmp->buf->iova);
127 mutex_unlock(&msm_buffer_mutex);
128 return -EINVAL;
129
130 }
131 } else {
132 if (key < (void *)tmp->buf)
133 p = &(*p)->rb_left;
134 else if (key > (void *)tmp->buf)
135 p = &(*p)->rb_right;
136 else {
137 WARN(1, "tried to add buffer twice! buf = %p"
138 " vaddr = %p iova = %p", tmp->buf,
139 tmp->buf->vaddr,
140 tmp->buf->iova);
141 mutex_unlock(&msm_buffer_mutex);
142 return -EINVAL;
143 }
144 }
145 }
146 rb_link_node(&node->rb_node_all_buffer, parent, p);
147 rb_insert_color(&node->rb_node_all_buffer, root);
148 mutex_unlock(&msm_buffer_mutex);
149 return 0;
150}
151
152static int add_buffer_phys(struct msm_buffer_node *node)
153{
154 struct rb_root *root = &phys_root;
155 struct rb_node **p = &root->rb_node;
156 struct rb_node *parent = NULL;
157
158 mutex_lock(&msm_buffer_mutex);
159 while (*p) {
160 struct msm_buffer_node *tmp;
161 parent = *p;
162
163 tmp = rb_entry(parent, struct msm_buffer_node, rb_node_paddr);
164
165 if (node->phys < tmp->phys)
166 p = &(*p)->rb_left;
167 else if (node->phys > tmp->phys)
168 p = &(*p)->rb_right;
169 else {
170 WARN(1, "tried to add buffer twice! buf = %p"
171 " vaddr = %p iova = %p", tmp->buf,
172 tmp->buf->vaddr,
173 tmp->buf->iova);
174 mutex_unlock(&msm_buffer_mutex);
175 return -EINVAL;
176
177 }
178 }
179 rb_link_node(&node->rb_node_paddr, parent, p);
180 rb_insert_color(&node->rb_node_paddr, root);
181 mutex_unlock(&msm_buffer_mutex);
182 return 0;
183}
184
185static int remove_buffer(struct msm_buffer_node *victim_node)
186{
187 struct rb_root *root = &buffer_root;
188
189 if (!victim_node)
190 return -EINVAL;
191
192 mutex_lock(&msm_buffer_mutex);
193 rb_erase(&victim_node->rb_node_all_buffer, root);
194 mutex_unlock(&msm_buffer_mutex);
195 return 0;
196}
197
198static int remove_buffer_phys(struct msm_buffer_node *victim_node)
199{
200 struct rb_root *root = &phys_root;
201
202 if (!victim_node)
203 return -EINVAL;
204
205 mutex_lock(&msm_buffer_mutex);
206 rb_erase(&victim_node->rb_node_paddr, root);
207 mutex_unlock(&msm_buffer_mutex);
208 return 0;
209}
210
211phys_addr_t msm_subsystem_check_iova_mapping(int subsys_id, unsigned long iova)
212{
213 struct iommu_domain *subsys_domain;
214
215 if (!msm_use_iommu())
216 /*
217 * If there is no iommu, Just return the iova in this case.
218 */
219 return iova;
220
221 subsys_domain = msm_get_iommu_domain(msm_subsystem_get_domain_no
222 (subsys_id));
223
224 return iommu_iova_to_phys(subsys_domain, iova);
225}
226EXPORT_SYMBOL(msm_subsystem_check_iova_mapping);
227
228struct msm_mapped_buffer *msm_subsystem_map_buffer(unsigned long phys,
229 unsigned int length,
230 unsigned int flags,
231 int *subsys_ids,
232 unsigned int nsubsys)
233{
234 struct msm_mapped_buffer *buf, *err;
235 struct msm_buffer_node *node;
236 int i = 0, j = 0, ret;
237 unsigned long iova_start = 0, temp_phys, temp_va = 0;
238 struct iommu_domain *d = NULL;
239 int map_size = length;
240
241 if (!((flags & MSM_SUBSYSTEM_MAP_KADDR) ||
242 (flags & MSM_SUBSYSTEM_MAP_IOVA))) {
243 pr_warn("%s: no mapping flag was specified. The caller"
244 " should explicitly specify what to map in the"
245 " flags.\n", __func__);
246 err = ERR_PTR(-EINVAL);
247 goto outret;
248 }
249
250 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
251 if (!buf) {
252 err = ERR_PTR(-ENOMEM);
253 goto outret;
254 }
255
256 node = kzalloc(sizeof(*node), GFP_ATOMIC);
257 if (!node) {
258 err = ERR_PTR(-ENOMEM);
259 goto outkfreebuf;
260 }
261
262 node->phys = phys;
263
264 if (flags & MSM_SUBSYSTEM_MAP_KADDR) {
265 struct msm_buffer_node *old_buffer;
266
267 old_buffer = find_buffer_phys(phys);
268
269 if (old_buffer) {
270 WARN(1, "%s: Attempting to map %lx twice in the kernel"
271 " virtual space. Don't do that!\n", __func__,
272 phys);
273 err = ERR_PTR(-EINVAL);
274 goto outkfreenode;
275 }
276
277 if (flags & MSM_SUBSYSTEM_MAP_CACHED)
278 buf->vaddr = ioremap(phys, length);
279 else if (flags & MSM_SUBSYSTEM_MAP_KADDR)
280 buf->vaddr = ioremap_nocache(phys, length);
281 else {
282 pr_warn("%s: no cachability flag was indicated. Caller"
283 " must specify a cachability flag.\n",
284 __func__);
285 err = ERR_PTR(-EINVAL);
286 goto outkfreenode;
287 }
288
289 if (!buf->vaddr) {
290 pr_err("%s: could not ioremap\n", __func__);
291 err = ERR_PTR(-EINVAL);
292 goto outkfreenode;
293 }
294
295 if (add_buffer_phys(node)) {
296 err = ERR_PTR(-EINVAL);
297 goto outiounmap;
298 }
299 }
300
301 if ((flags & MSM_SUBSYSTEM_MAP_IOVA) && subsys_ids) {
302 int min_align;
303
304 length = round_up(length, SZ_4K);
305
306 if (flags & MSM_SUBSYSTEM_MAP_IOMMU_2X)
307 map_size = 2 * length;
308 else
309 map_size = length;
310
311 buf->iova = kzalloc(sizeof(unsigned long)*nsubsys, GFP_ATOMIC);
312 if (!buf->iova) {
313 err = ERR_PTR(-ENOMEM);
314 goto outremovephys;
315 }
316
317 /*
318 * The alignment must be specified as the exact value wanted
319 * e.g. 8k alignment must pass (0x2000 | other flags)
320 */
321 min_align = flags & ~(SZ_4K - 1);
322
323 for (i = 0; i < nsubsys; i++) {
324 unsigned int domain_no, partition_no;
325
326 if (!msm_use_iommu()) {
327 buf->iova[i] = phys;
328 continue;
329 }
330
331 d = msm_get_iommu_domain(
332 msm_subsystem_get_domain_no(subsys_ids[i]));
333
334 if (!d) {
335 pr_err("%s: could not get domain for subsystem"
336 " %d\n", __func__, subsys_ids[i]);
337 continue;
338 }
339
340 domain_no = msm_subsystem_get_domain_no(subsys_ids[i]);
341 partition_no = msm_subsystem_get_partition_no(
342 subsys_ids[i]);
343
344 iova_start = msm_allocate_iova_address(domain_no,
345 partition_no,
346 map_size,
347 max(min_align, SZ_4K));
348
349 if (!iova_start) {
350 pr_err("%s: could not allocate iova address\n",
351 __func__);
352 continue;
353 }
354
355 temp_phys = phys;
356 temp_va = iova_start;
357 for (j = length; j > 0; j -= SZ_4K,
358 temp_phys += SZ_4K,
359 temp_va += SZ_4K) {
360 ret = iommu_map(d, temp_va, temp_phys,
361 get_order(SZ_4K),
362 (IOMMU_READ | IOMMU_WRITE));
363 if (ret) {
364 pr_err("%s: could not map iommu for"
365 " domain %p, iova %lx,"
366 " phys %lx\n", __func__, d,
367 temp_va, temp_phys);
368 err = ERR_PTR(-EINVAL);
369 goto outdomain;
370 }
371 }
372 buf->iova[i] = iova_start;
373
374 if (flags & MSM_SUBSYSTEM_MAP_IOMMU_2X)
375 msm_iommu_map_extra
376 (d, temp_va, length, SZ_4K,
377 (IOMMU_READ | IOMMU_WRITE));
378 }
379
380 }
381
382 node->buf = buf;
383 node->subsystems = subsys_ids;
384 node->length = map_size;
385 node->nsubsys = nsubsys;
386
387 if (add_buffer(node)) {
388 err = ERR_PTR(-EINVAL);
389 goto outiova;
390 }
391
392 return buf;
393
394outiova:
395 if (flags & MSM_SUBSYSTEM_MAP_IOVA)
396 iommu_unmap(d, temp_va, get_order(SZ_4K));
397outdomain:
398 if (flags & MSM_SUBSYSTEM_MAP_IOVA) {
399 /* Unmap the rest of the current domain, i */
400 for (j -= SZ_4K, temp_va -= SZ_4K;
401 j > 0; temp_va -= SZ_4K, j -= SZ_4K)
402 iommu_unmap(d, temp_va, get_order(SZ_4K));
403
404 /* Unmap all the other domains */
405 for (i--; i >= 0; i--) {
406 unsigned int domain_no, partition_no;
407 if (!msm_use_iommu())
408 continue;
409 domain_no = msm_subsystem_get_domain_no(subsys_ids[i]);
410 partition_no = msm_subsystem_get_partition_no(
411 subsys_ids[i]);
412
413 temp_va = buf->iova[i];
414 for (j = length; j > 0; j -= SZ_4K,
415 temp_va += SZ_4K)
416 iommu_unmap(d, temp_va, get_order(SZ_4K));
417 msm_free_iova_address(buf->iova[i], domain_no,
418 partition_no, length);
419 }
420
421 kfree(buf->iova);
422 }
423
424outremovephys:
425 if (flags & MSM_SUBSYSTEM_MAP_KADDR)
426 remove_buffer_phys(node);
427outiounmap:
428 if (flags & MSM_SUBSYSTEM_MAP_KADDR)
429 iounmap(buf->vaddr);
430outkfreenode:
431 kfree(node);
432outkfreebuf:
433 kfree(buf);
434outret:
435 return err;
436}
437EXPORT_SYMBOL(msm_subsystem_map_buffer);
438
439int msm_subsystem_unmap_buffer(struct msm_mapped_buffer *buf)
440{
441 struct msm_buffer_node *node;
442 int i, j, ret;
443 unsigned long temp_va;
444
445 if (IS_ERR_OR_NULL(buf))
446 goto out;
447
448 if (buf->vaddr)
449 node = find_buffer(buf->vaddr);
450 else
451 node = find_buffer(buf);
452
453 if (!node)
454 goto out;
455
456 if (node->buf != buf) {
457 pr_err("%s: caller must pass in the same buffer structure"
458 " returned from map_buffer when freeding\n", __func__);
459 goto out;
460 }
461
462 if (buf->iova) {
463 if (msm_use_iommu())
464 for (i = 0; i < node->nsubsys; i++) {
465 struct iommu_domain *subsys_domain;
466 unsigned int domain_no, partition_no;
467
468 subsys_domain = msm_get_iommu_domain(
469 msm_subsystem_get_domain_no(
470 node->subsystems[i]));
471
472 domain_no = msm_subsystem_get_domain_no(
473 node->subsystems[i]);
474 partition_no = msm_subsystem_get_partition_no(
475 node->subsystems[i]);
476
477 temp_va = buf->iova[i];
478 for (j = node->length; j > 0; j -= SZ_4K,
479 temp_va += SZ_4K) {
480 ret = iommu_unmap(subsys_domain,
481 temp_va,
482 get_order(SZ_4K));
483 WARN(ret, "iommu_unmap returned a "
484 " non-zero value.\n");
485 }
486 msm_free_iova_address(buf->iova[i], domain_no,
487 partition_no, node->length);
488 }
489 kfree(buf->iova);
490
491 }
492
493 if (buf->vaddr) {
494 remove_buffer_phys(node);
495 iounmap(buf->vaddr);
496 }
497
498 remove_buffer(node);
499 kfree(node);
500 kfree(buf);
501
502 return 0;
503out:
504 return -EINVAL;
505}
506EXPORT_SYMBOL(msm_subsystem_unmap_buffer);