blob: 11fe26cabba01af67c7a8a55d807e0db99726685 [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211phys_addr_t msm_subsystem_check_iova_mapping(int subsys_id, unsigned long iova)
212{
213 struct iommu_domain *subsys_domain;
214
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700215 if (!msm_use_iommu())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 /*
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700217 * If there is no iommu, Just return the iova in this case.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 */
219 return iova;
220
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700221 subsys_domain = msm_get_iommu_domain(msm_subsystem_get_domain_no
222 (subsys_id));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223
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;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 struct iommu_domain *d = NULL;
239
240 if (!((flags & MSM_SUBSYSTEM_MAP_KADDR) ||
241 (flags & MSM_SUBSYSTEM_MAP_IOVA))) {
242 pr_warn("%s: no mapping flag was specified. The caller"
243 " should explicitly specify what to map in the"
244 " flags.\n", __func__);
245 err = ERR_PTR(-EINVAL);
246 goto outret;
247 }
248
249 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
250 if (!buf) {
251 err = ERR_PTR(-ENOMEM);
252 goto outret;
253 }
254
255 node = kzalloc(sizeof(*node), GFP_ATOMIC);
256 if (!node) {
257 err = ERR_PTR(-ENOMEM);
258 goto outkfreebuf;
259 }
260
261 node->phys = phys;
262
263 if (flags & MSM_SUBSYSTEM_MAP_KADDR) {
264 struct msm_buffer_node *old_buffer;
265
266 old_buffer = find_buffer_phys(phys);
267
268 if (old_buffer) {
269 WARN(1, "%s: Attempting to map %lx twice in the kernel"
270 " virtual space. Don't do that!\n", __func__,
271 phys);
272 err = ERR_PTR(-EINVAL);
273 goto outkfreenode;
274 }
275
276 if (flags & MSM_SUBSYSTEM_MAP_CACHED)
277 buf->vaddr = ioremap(phys, length);
278 else if (flags & MSM_SUBSYSTEM_MAP_KADDR)
279 buf->vaddr = ioremap_nocache(phys, length);
280 else {
281 pr_warn("%s: no cachability flag was indicated. Caller"
282 " must specify a cachability flag.\n",
283 __func__);
284 err = ERR_PTR(-EINVAL);
285 goto outkfreenode;
286 }
287
288 if (!buf->vaddr) {
289 pr_err("%s: could not ioremap\n", __func__);
290 err = ERR_PTR(-EINVAL);
291 goto outkfreenode;
292 }
293
294 if (add_buffer_phys(node)) {
295 err = ERR_PTR(-EINVAL);
296 goto outiounmap;
297 }
298 }
299
300 if ((flags & MSM_SUBSYSTEM_MAP_IOVA) && subsys_ids) {
Laura Abbott11962582011-08-02 16:29:21 -0700301 int min_align;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302
Laura Abbott11962582011-08-02 16:29:21 -0700303 length = round_up(length, SZ_4K);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304
305 buf->iova = kzalloc(sizeof(unsigned long)*nsubsys, GFP_ATOMIC);
306 if (!buf->iova) {
307 err = ERR_PTR(-ENOMEM);
308 goto outremovephys;
309 }
310
Laura Abbott675b31f2011-07-19 10:37:43 -0700311 /*
312 * The alignment must be specified as the exact value wanted
313 * e.g. 8k alignment must pass (0x2000 | other flags)
314 */
315 min_align = flags & ~(SZ_4K - 1);
316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 for (i = 0; i < nsubsys; i++) {
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700318 unsigned int domain_no, partition_no;
319
320 if (!msm_use_iommu()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321 buf->iova[i] = phys;
322 continue;
323 }
324
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700325 d = msm_get_iommu_domain(
326 msm_subsystem_get_domain_no(subsys_ids[i]));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700328 if (!d) {
329 pr_err("%s: could not get domain for subsystem"
330 " %d\n", __func__, subsys_ids[i]);
331 continue;
332 }
333
334 domain_no = msm_subsystem_get_domain_no(subsys_ids[i]);
335 partition_no = msm_subsystem_get_partition_no(
336 subsys_ids[i]);
337
338 iova_start = msm_allocate_iova_address(domain_no,
339 partition_no,
340 length,
Laura Abbott11962582011-08-02 16:29:21 -0700341 max(min_align, SZ_4K));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342
343 if (!iova_start) {
344 pr_err("%s: could not allocate iova address\n",
345 __func__);
346 continue;
347 }
348
349 temp_phys = phys;
350 temp_va = iova_start;
Laura Abbott11962582011-08-02 16:29:21 -0700351 for (j = length; j > 0; j -= SZ_4K,
352 temp_phys += SZ_4K,
353 temp_va += SZ_4K) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354 ret = iommu_map(d, temp_va, temp_phys,
Laura Abbott11962582011-08-02 16:29:21 -0700355 get_order(SZ_4K), 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356 if (ret) {
357 pr_err("%s: could not map iommu for"
358 " domain %p, iova %lx,"
359 " phys %lx\n", __func__, d,
360 temp_va, temp_phys);
361 err = ERR_PTR(-EINVAL);
362 goto outdomain;
363 }
364 }
365 buf->iova[i] = iova_start;
366 }
367
368 }
369
370 node->buf = buf;
371 node->subsystems = subsys_ids;
372 node->length = length;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373 node->nsubsys = nsubsys;
374
375 if (add_buffer(node)) {
376 err = ERR_PTR(-EINVAL);
377 goto outiova;
378 }
379
380 return buf;
381
382outiova:
383 if (flags & MSM_SUBSYSTEM_MAP_IOVA)
Laura Abbott11962582011-08-02 16:29:21 -0700384 iommu_unmap(d, temp_va, get_order(SZ_4K));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385outdomain:
386 if (flags & MSM_SUBSYSTEM_MAP_IOVA) {
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700387 /* Unmap the rest of the current domain, i */
Laura Abbott11962582011-08-02 16:29:21 -0700388 for (j -= SZ_4K, temp_va -= SZ_4K;
389 j > 0; temp_va -= SZ_4K, j -= SZ_4K)
390 iommu_unmap(d, temp_va, get_order(SZ_4K));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700392 /* Unmap all the other domains */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 for (i--; i >= 0; i--) {
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700394 unsigned int domain_no, partition_no;
395 if (!msm_use_iommu())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396 continue;
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700397 domain_no = msm_subsystem_get_domain_no(subsys_ids[i]);
398 partition_no = msm_subsystem_get_partition_no(
399 subsys_ids[i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400
401 temp_va = buf->iova[i];
Laura Abbott11962582011-08-02 16:29:21 -0700402 for (j = length; j > 0; j -= SZ_4K,
403 temp_va += SZ_4K)
404 iommu_unmap(d, temp_va, get_order(SZ_4K));
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700405 msm_free_iova_address(buf->iova[i], domain_no,
406 partition_no, length);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407 }
408
409 kfree(buf->iova);
410 }
411
412outremovephys:
413 if (flags & MSM_SUBSYSTEM_MAP_KADDR)
414 remove_buffer_phys(node);
415outiounmap:
416 if (flags & MSM_SUBSYSTEM_MAP_KADDR)
417 iounmap(buf->vaddr);
418outkfreenode:
419 kfree(node);
420outkfreebuf:
421 kfree(buf);
422outret:
423 return err;
424}
425EXPORT_SYMBOL(msm_subsystem_map_buffer);
426
427int msm_subsystem_unmap_buffer(struct msm_mapped_buffer *buf)
428{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429 struct msm_buffer_node *node;
430 int i, j, ret;
431 unsigned long temp_va;
432
433 if (buf->vaddr)
434 node = find_buffer(buf->vaddr);
435 else
436 node = find_buffer(buf);
437
438 if (!node)
439 goto out;
440
441 if (node->buf != buf) {
442 pr_err("%s: caller must pass in the same buffer structure"
443 " returned from map_buffer when freeding\n", __func__);
444 goto out;
445 }
446
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447 if (buf->iova) {
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700448 if (msm_use_iommu())
449 for (i = 0; i < node->nsubsys; i++) {
450 struct iommu_domain *subsys_domain;
451 unsigned int domain_no, partition_no;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700453 subsys_domain = msm_get_iommu_domain(
454 msm_subsystem_get_domain_no(
455 node->subsystems[i]));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700457 domain_no = msm_subsystem_get_domain_no(
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 node->subsystems[i]);
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700459 partition_no = msm_subsystem_get_partition_no(
460 node->subsystems[i]);
461
462 temp_va = buf->iova[i];
463 for (j = node->length; j > 0; j -= SZ_4K,
464 temp_va += SZ_4K) {
465 ret = iommu_unmap(subsys_domain,
466 temp_va,
Laura Abbott11962582011-08-02 16:29:21 -0700467 get_order(SZ_4K));
Laura Abbott9f4a8e62011-08-29 19:08:07 -0700468 WARN(ret, "iommu_unmap returned a "
469 " non-zero value.\n");
470 }
471 msm_free_iova_address(buf->iova[i], domain_no,
472 partition_no, node->length);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700474 kfree(buf->iova);
475
476 }
477
478 if (buf->vaddr) {
479 remove_buffer_phys(node);
480 iounmap(buf->vaddr);
481 }
482
483 remove_buffer(node);
484 kfree(node);
485 kfree(buf);
486
487 return 0;
488out:
489 return -EINVAL;
490}
491EXPORT_SYMBOL(msm_subsystem_unmap_buffer);