blob: 41d807273c5c9a78ce1a54fa0eb541d26f448533 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* 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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023struct 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;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030 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
211static unsigned long allocate_iova_address(unsigned long size,
212 int subsys_id,
213 unsigned long align)
214{
215 struct mem_pool *pool = msm_subsystem_get_pool(subsys_id);
216 unsigned long iova;
217
218 iova = gen_pool_alloc_aligned(pool->gpool, size, ilog2(align));
219 if (iova)
220 pool->free -= size;
221
222 return iova;
223}
224
225static void free_iova_address(unsigned long iova,
226 unsigned long size,
227 int subsys_id)
228{
229 struct mem_pool *pool = msm_subsystem_get_pool(subsys_id);
230
231 pool->free += size;
232 gen_pool_free(pool->gpool, iova, size);
233}
234
235static int subsys_validate(int subsys_id)
236{
237 struct mem_pool *pool;
238 struct iommu_domain *subsys_domain;
239
240 if (!msm_subsystem_check_id(subsys_id)) {
241 WARN(1, "subsystem id is not valid. Caller should check this.");
242 return 0;
243 }
244
245 pool = msm_subsystem_get_pool(subsys_id);
246 subsys_domain = msm_subsystem_get_domain(subsys_id);
247
248 return subsys_domain && pool && pool->gpool;
249}
250
251phys_addr_t msm_subsystem_check_iova_mapping(int subsys_id, unsigned long iova)
252{
253 struct iommu_domain *subsys_domain;
254
255 if (!subsys_validate(subsys_id))
256 /*
257 * If the subsystem is not valid, assume a phys = iova
258 * mapping. Just return the iova in this case.
259 */
260 return iova;
261
262 subsys_domain = msm_subsystem_get_domain(subsys_id);
263
264 return iommu_iova_to_phys(subsys_domain, iova);
265}
266EXPORT_SYMBOL(msm_subsystem_check_iova_mapping);
267
268struct msm_mapped_buffer *msm_subsystem_map_buffer(unsigned long phys,
269 unsigned int length,
270 unsigned int flags,
271 int *subsys_ids,
272 unsigned int nsubsys)
273{
274 struct msm_mapped_buffer *buf, *err;
275 struct msm_buffer_node *node;
276 int i = 0, j = 0, ret;
277 unsigned long iova_start = 0, temp_phys, temp_va = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 struct iommu_domain *d = NULL;
279
280 if (!((flags & MSM_SUBSYSTEM_MAP_KADDR) ||
281 (flags & MSM_SUBSYSTEM_MAP_IOVA))) {
282 pr_warn("%s: no mapping flag was specified. The caller"
283 " should explicitly specify what to map in the"
284 " flags.\n", __func__);
285 err = ERR_PTR(-EINVAL);
286 goto outret;
287 }
288
289 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
290 if (!buf) {
291 err = ERR_PTR(-ENOMEM);
292 goto outret;
293 }
294
295 node = kzalloc(sizeof(*node), GFP_ATOMIC);
296 if (!node) {
297 err = ERR_PTR(-ENOMEM);
298 goto outkfreebuf;
299 }
300
301 node->phys = phys;
302
303 if (flags & MSM_SUBSYSTEM_MAP_KADDR) {
304 struct msm_buffer_node *old_buffer;
305
306 old_buffer = find_buffer_phys(phys);
307
308 if (old_buffer) {
309 WARN(1, "%s: Attempting to map %lx twice in the kernel"
310 " virtual space. Don't do that!\n", __func__,
311 phys);
312 err = ERR_PTR(-EINVAL);
313 goto outkfreenode;
314 }
315
316 if (flags & MSM_SUBSYSTEM_MAP_CACHED)
317 buf->vaddr = ioremap(phys, length);
318 else if (flags & MSM_SUBSYSTEM_MAP_KADDR)
319 buf->vaddr = ioremap_nocache(phys, length);
320 else {
321 pr_warn("%s: no cachability flag was indicated. Caller"
322 " must specify a cachability flag.\n",
323 __func__);
324 err = ERR_PTR(-EINVAL);
325 goto outkfreenode;
326 }
327
328 if (!buf->vaddr) {
329 pr_err("%s: could not ioremap\n", __func__);
330 err = ERR_PTR(-EINVAL);
331 goto outkfreenode;
332 }
333
334 if (add_buffer_phys(node)) {
335 err = ERR_PTR(-EINVAL);
336 goto outiounmap;
337 }
338 }
339
340 if ((flags & MSM_SUBSYSTEM_MAP_IOVA) && subsys_ids) {
Laura Abbott11962582011-08-02 16:29:21 -0700341 int min_align;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342
Laura Abbott11962582011-08-02 16:29:21 -0700343 length = round_up(length, SZ_4K);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344
345 buf->iova = kzalloc(sizeof(unsigned long)*nsubsys, GFP_ATOMIC);
346 if (!buf->iova) {
347 err = ERR_PTR(-ENOMEM);
348 goto outremovephys;
349 }
350
Laura Abbott675b31f2011-07-19 10:37:43 -0700351 /*
352 * The alignment must be specified as the exact value wanted
353 * e.g. 8k alignment must pass (0x2000 | other flags)
354 */
355 min_align = flags & ~(SZ_4K - 1);
356
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357 for (i = 0; i < nsubsys; i++) {
358 if (!subsys_validate(subsys_ids[i])) {
359 buf->iova[i] = phys;
360 continue;
361 }
362
363 d = msm_subsystem_get_domain(subsys_ids[i]);
364
365 iova_start = allocate_iova_address(length,
Laura Abbott675b31f2011-07-19 10:37:43 -0700366 subsys_ids[i],
Laura Abbott11962582011-08-02 16:29:21 -0700367 max(min_align, SZ_4K));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368
369 if (!iova_start) {
370 pr_err("%s: could not allocate iova address\n",
371 __func__);
372 continue;
373 }
374
375 temp_phys = phys;
376 temp_va = iova_start;
Laura Abbott11962582011-08-02 16:29:21 -0700377 for (j = length; j > 0; j -= SZ_4K,
378 temp_phys += SZ_4K,
379 temp_va += SZ_4K) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380 ret = iommu_map(d, temp_va, temp_phys,
Laura Abbott11962582011-08-02 16:29:21 -0700381 get_order(SZ_4K), 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382 if (ret) {
383 pr_err("%s: could not map iommu for"
384 " domain %p, iova %lx,"
385 " phys %lx\n", __func__, d,
386 temp_va, temp_phys);
387 err = ERR_PTR(-EINVAL);
388 goto outdomain;
389 }
390 }
391 buf->iova[i] = iova_start;
392 }
393
394 }
395
396 node->buf = buf;
397 node->subsystems = subsys_ids;
398 node->length = length;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 node->nsubsys = nsubsys;
400
401 if (add_buffer(node)) {
402 err = ERR_PTR(-EINVAL);
403 goto outiova;
404 }
405
406 return buf;
407
408outiova:
409 if (flags & MSM_SUBSYSTEM_MAP_IOVA)
Laura Abbott11962582011-08-02 16:29:21 -0700410 iommu_unmap(d, temp_va, get_order(SZ_4K));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411outdomain:
412 if (flags & MSM_SUBSYSTEM_MAP_IOVA) {
Laura Abbott11962582011-08-02 16:29:21 -0700413 for (j -= SZ_4K, temp_va -= SZ_4K;
414 j > 0; temp_va -= SZ_4K, j -= SZ_4K)
415 iommu_unmap(d, temp_va, get_order(SZ_4K));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416
417 for (i--; i >= 0; i--) {
418 if (!subsys_validate(subsys_ids[i]))
419 continue;
420
421 temp_va = buf->iova[i];
Laura Abbott11962582011-08-02 16:29:21 -0700422 for (j = length; j > 0; j -= SZ_4K,
423 temp_va += SZ_4K)
424 iommu_unmap(d, temp_va, get_order(SZ_4K));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425 free_iova_address(buf->iova[i], length, subsys_ids[i]);
426 }
427
428 kfree(buf->iova);
429 }
430
431outremovephys:
432 if (flags & MSM_SUBSYSTEM_MAP_KADDR)
433 remove_buffer_phys(node);
434outiounmap:
435 if (flags & MSM_SUBSYSTEM_MAP_KADDR)
436 iounmap(buf->vaddr);
437outkfreenode:
438 kfree(node);
439outkfreebuf:
440 kfree(buf);
441outret:
442 return err;
443}
444EXPORT_SYMBOL(msm_subsystem_map_buffer);
445
446int msm_subsystem_unmap_buffer(struct msm_mapped_buffer *buf)
447{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448 struct msm_buffer_node *node;
449 int i, j, ret;
450 unsigned long temp_va;
451
452 if (buf->vaddr)
453 node = find_buffer(buf->vaddr);
454 else
455 node = find_buffer(buf);
456
457 if (!node)
458 goto out;
459
460 if (node->buf != buf) {
461 pr_err("%s: caller must pass in the same buffer structure"
462 " returned from map_buffer when freeding\n", __func__);
463 goto out;
464 }
465
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466 if (buf->iova) {
467 for (i = 0; i < node->nsubsys; i++) {
468 struct iommu_domain *subsys_domain;
469
470 if (!subsys_validate(node->subsystems[i]))
471 continue;
472
473 subsys_domain = msm_subsystem_get_domain(
474 node->subsystems[i]);
475 temp_va = buf->iova[i];
Laura Abbott11962582011-08-02 16:29:21 -0700476 for (j = node->length; j > 0; j -= SZ_4K,
477 temp_va += SZ_4K) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 ret = iommu_unmap(subsys_domain, temp_va,
Laura Abbott11962582011-08-02 16:29:21 -0700479 get_order(SZ_4K));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 WARN(ret, "iommu_unmap returned a non-zero"
481 " value.\n");
482 }
483 free_iova_address(buf->iova[i], node->length,
484 node->subsystems[i]);
485 }
486 kfree(buf->iova);
487
488 }
489
490 if (buf->vaddr) {
491 remove_buffer_phys(node);
492 iounmap(buf->vaddr);
493 }
494
495 remove_buffer(node);
496 kfree(node);
497 kfree(buf);
498
499 return 0;
500out:
501 return -EINVAL;
502}
503EXPORT_SYMBOL(msm_subsystem_unmap_buffer);