Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * drivers/gpu/ion/ion.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
| 5 | * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/file.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/anon_inodes.h> |
| 23 | #include <linux/ion.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/memblock.h> |
| 26 | #include <linux/miscdevice.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/mm_types.h> |
| 29 | #include <linux/rbtree.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/seq_file.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/debugfs.h> |
| 35 | #include <linux/dma-buf.h> |
| 36 | |
| 37 | #include <mach/iommu_domains.h> |
| 38 | #include "ion_priv.h" |
| 39 | #define DEBUG |
| 40 | |
| 41 | struct ion_device { |
| 42 | struct miscdevice dev; |
| 43 | struct rb_root buffers; |
| 44 | struct mutex lock; |
| 45 | struct rb_root heaps; |
| 46 | long (*custom_ioctl) (struct ion_client *client, unsigned int cmd, |
| 47 | unsigned long arg); |
| 48 | struct rb_root clients; |
| 49 | struct dentry *debug_root; |
| 50 | }; |
| 51 | |
| 52 | struct ion_client { |
| 53 | struct rb_node node; |
| 54 | struct ion_device *dev; |
| 55 | struct rb_root handles; |
| 56 | struct mutex lock; |
| 57 | unsigned int heap_mask; |
| 58 | char *name; |
| 59 | struct task_struct *task; |
| 60 | pid_t pid; |
| 61 | struct dentry *debug_root; |
| 62 | }; |
| 63 | |
| 64 | struct ion_handle { |
| 65 | struct kref ref; |
| 66 | struct ion_client *client; |
| 67 | struct ion_buffer *buffer; |
| 68 | struct rb_node node; |
| 69 | unsigned int kmap_cnt; |
| 70 | unsigned int iommu_map_cnt; |
| 71 | }; |
| 72 | |
| 73 | static void ion_iommu_release(struct kref *kref); |
| 74 | |
| 75 | static int ion_validate_buffer_flags(struct ion_buffer *buffer, |
| 76 | unsigned long flags) |
| 77 | { |
| 78 | if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt || |
| 79 | buffer->iommu_map_cnt) { |
| 80 | if (buffer->flags != flags) { |
| 81 | pr_err("%s: buffer was already mapped with flags %lx," |
| 82 | " cannot map with flags %lx\n", __func__, |
| 83 | buffer->flags, flags); |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | } else { |
| 88 | buffer->flags = flags; |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static void ion_buffer_add(struct ion_device *dev, |
| 94 | struct ion_buffer *buffer) |
| 95 | { |
| 96 | struct rb_node **p = &dev->buffers.rb_node; |
| 97 | struct rb_node *parent = NULL; |
| 98 | struct ion_buffer *entry; |
| 99 | |
| 100 | while (*p) { |
| 101 | parent = *p; |
| 102 | entry = rb_entry(parent, struct ion_buffer, node); |
| 103 | |
| 104 | if (buffer < entry) { |
| 105 | p = &(*p)->rb_left; |
| 106 | } else if (buffer > entry) { |
| 107 | p = &(*p)->rb_right; |
| 108 | } else { |
| 109 | pr_err("%s: buffer already found.", __func__); |
| 110 | BUG(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | rb_link_node(&buffer->node, parent, p); |
| 115 | rb_insert_color(&buffer->node, &dev->buffers); |
| 116 | } |
| 117 | |
| 118 | static void ion_iommu_add(struct ion_buffer *buffer, |
| 119 | struct ion_iommu_map *iommu) |
| 120 | { |
| 121 | struct rb_node **p = &buffer->iommu_maps.rb_node; |
| 122 | struct rb_node *parent = NULL; |
| 123 | struct ion_iommu_map *entry; |
| 124 | |
| 125 | while (*p) { |
| 126 | parent = *p; |
| 127 | entry = rb_entry(parent, struct ion_iommu_map, node); |
| 128 | |
| 129 | if (iommu->key < entry->key) { |
| 130 | p = &(*p)->rb_left; |
| 131 | } else if (iommu->key > entry->key) { |
| 132 | p = &(*p)->rb_right; |
| 133 | } else { |
| 134 | pr_err("%s: buffer %p already has mapping for domain %d" |
| 135 | " and partition %d\n", __func__, |
| 136 | buffer, |
| 137 | iommu_map_domain(iommu), |
| 138 | iommu_map_partition(iommu)); |
| 139 | BUG(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | rb_link_node(&iommu->node, parent, p); |
| 144 | rb_insert_color(&iommu->node, &buffer->iommu_maps); |
| 145 | |
| 146 | } |
| 147 | |
| 148 | static struct ion_iommu_map *ion_iommu_lookup(struct ion_buffer *buffer, |
| 149 | unsigned int domain_no, |
| 150 | unsigned int partition_no) |
| 151 | { |
| 152 | struct rb_node **p = &buffer->iommu_maps.rb_node; |
| 153 | struct rb_node *parent = NULL; |
| 154 | struct ion_iommu_map *entry; |
| 155 | uint64_t key = domain_no; |
| 156 | key = key << 32 | partition_no; |
| 157 | |
| 158 | while (*p) { |
| 159 | parent = *p; |
| 160 | entry = rb_entry(parent, struct ion_iommu_map, node); |
| 161 | |
| 162 | if (key < entry->key) |
| 163 | p = &(*p)->rb_left; |
| 164 | else if (key > entry->key) |
| 165 | p = &(*p)->rb_right; |
| 166 | else |
| 167 | return entry; |
| 168 | } |
| 169 | |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | static struct ion_buffer *ion_buffer_create(struct ion_heap *heap, |
| 174 | struct ion_device *dev, |
| 175 | unsigned long len, |
| 176 | unsigned long align, |
| 177 | unsigned long flags) |
| 178 | { |
| 179 | struct ion_buffer *buffer; |
| 180 | struct sg_table *table; |
| 181 | int ret; |
| 182 | |
| 183 | buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL); |
| 184 | if (!buffer) |
| 185 | return ERR_PTR(-ENOMEM); |
| 186 | |
| 187 | buffer->heap = heap; |
| 188 | kref_init(&buffer->ref); |
| 189 | |
| 190 | ret = heap->ops->allocate(heap, buffer, len, align, flags); |
| 191 | if (ret) { |
| 192 | kfree(buffer); |
| 193 | return ERR_PTR(ret); |
| 194 | } |
| 195 | |
| 196 | buffer->dev = dev; |
| 197 | buffer->size = len; |
| 198 | |
| 199 | table = buffer->heap->ops->map_dma(buffer->heap, buffer); |
| 200 | if (IS_ERR_OR_NULL(table)) { |
| 201 | heap->ops->free(buffer); |
| 202 | kfree(buffer); |
| 203 | return ERR_PTR(PTR_ERR(table)); |
| 204 | } |
| 205 | buffer->sg_table = table; |
| 206 | |
| 207 | mutex_init(&buffer->lock); |
| 208 | ion_buffer_add(dev, buffer); |
| 209 | return buffer; |
| 210 | } |
| 211 | |
| 212 | static void ion_iommu_delayed_unmap(struct ion_buffer *buffer) |
| 213 | { |
| 214 | struct ion_iommu_map *iommu_map; |
| 215 | struct rb_node *node; |
| 216 | const struct rb_root *rb = &(buffer->iommu_maps); |
| 217 | unsigned long ref_count; |
| 218 | unsigned int delayed_unmap; |
| 219 | |
| 220 | mutex_lock(&buffer->lock); |
| 221 | |
| 222 | while ((node = rb_first(rb)) != 0) { |
| 223 | iommu_map = rb_entry(node, struct ion_iommu_map, node); |
| 224 | ref_count = atomic_read(&iommu_map->ref.refcount); |
| 225 | delayed_unmap = iommu_map->flags & ION_IOMMU_UNMAP_DELAYED; |
| 226 | |
| 227 | if ((delayed_unmap && ref_count > 1) || !delayed_unmap) { |
| 228 | pr_err("%s: Virtual memory address leak in domain %u, partition %u\n", |
| 229 | __func__, iommu_map->domain_info[DI_DOMAIN_NUM], |
| 230 | iommu_map->domain_info[DI_PARTITION_NUM]); |
| 231 | } |
| 232 | |
| 233 | kref_init(&iommu_map->ref); |
| 234 | kref_put(&iommu_map->ref, ion_iommu_release); |
| 235 | } |
| 236 | |
| 237 | mutex_unlock(&buffer->lock); |
| 238 | } |
| 239 | |
| 240 | static void ion_buffer_destroy(struct kref *kref) |
| 241 | { |
| 242 | struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref); |
| 243 | struct ion_device *dev = buffer->dev; |
| 244 | |
| 245 | if (WARN_ON(buffer->kmap_cnt > 0)) |
| 246 | buffer->heap->ops->unmap_kernel(buffer->heap, buffer); |
| 247 | |
| 248 | buffer->heap->ops->unmap_dma(buffer->heap, buffer); |
| 249 | |
| 250 | ion_iommu_delayed_unmap(buffer); |
| 251 | buffer->heap->ops->free(buffer); |
| 252 | mutex_lock(&dev->lock); |
| 253 | rb_erase(&buffer->node, &dev->buffers); |
| 254 | mutex_unlock(&dev->lock); |
| 255 | kfree(buffer); |
| 256 | } |
| 257 | |
| 258 | static void ion_buffer_get(struct ion_buffer *buffer) |
| 259 | { |
| 260 | kref_get(&buffer->ref); |
| 261 | } |
| 262 | |
| 263 | static int ion_buffer_put(struct ion_buffer *buffer) |
| 264 | { |
| 265 | return kref_put(&buffer->ref, ion_buffer_destroy); |
| 266 | } |
| 267 | |
| 268 | static struct ion_handle *ion_handle_create(struct ion_client *client, |
| 269 | struct ion_buffer *buffer) |
| 270 | { |
| 271 | struct ion_handle *handle; |
| 272 | |
| 273 | handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL); |
| 274 | if (!handle) |
| 275 | return ERR_PTR(-ENOMEM); |
| 276 | kref_init(&handle->ref); |
| 277 | rb_init_node(&handle->node); |
| 278 | handle->client = client; |
| 279 | ion_buffer_get(buffer); |
| 280 | handle->buffer = buffer; |
| 281 | |
| 282 | return handle; |
| 283 | } |
| 284 | |
| 285 | static void ion_handle_kmap_put(struct ion_handle *); |
| 286 | |
| 287 | static void ion_handle_destroy(struct kref *kref) |
| 288 | { |
| 289 | struct ion_handle *handle = container_of(kref, struct ion_handle, ref); |
| 290 | struct ion_client *client = handle->client; |
| 291 | struct ion_buffer *buffer = handle->buffer; |
| 292 | |
| 293 | mutex_lock(&buffer->lock); |
| 294 | while (handle->kmap_cnt) |
| 295 | ion_handle_kmap_put(handle); |
| 296 | mutex_unlock(&buffer->lock); |
| 297 | |
| 298 | if (!RB_EMPTY_NODE(&handle->node)) |
| 299 | rb_erase(&handle->node, &client->handles); |
| 300 | |
| 301 | ion_buffer_put(buffer); |
| 302 | kfree(handle); |
| 303 | } |
| 304 | |
| 305 | struct ion_buffer *ion_handle_buffer(struct ion_handle *handle) |
| 306 | { |
| 307 | return handle->buffer; |
| 308 | } |
| 309 | |
| 310 | static void ion_handle_get(struct ion_handle *handle) |
| 311 | { |
| 312 | kref_get(&handle->ref); |
| 313 | } |
| 314 | |
| 315 | static int ion_handle_put(struct ion_handle *handle) |
| 316 | { |
| 317 | return kref_put(&handle->ref, ion_handle_destroy); |
| 318 | } |
| 319 | |
| 320 | static struct ion_handle *ion_handle_lookup(struct ion_client *client, |
| 321 | struct ion_buffer *buffer) |
| 322 | { |
| 323 | struct rb_node *n; |
| 324 | |
| 325 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 326 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 327 | node); |
| 328 | if (handle->buffer == buffer) |
| 329 | return handle; |
| 330 | } |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | static bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle) |
| 335 | { |
| 336 | struct rb_node *n = client->handles.rb_node; |
| 337 | |
| 338 | while (n) { |
| 339 | struct ion_handle *handle_node = rb_entry(n, struct ion_handle, |
| 340 | node); |
| 341 | if (handle < handle_node) |
| 342 | n = n->rb_left; |
| 343 | else if (handle > handle_node) |
| 344 | n = n->rb_right; |
| 345 | else |
| 346 | return true; |
| 347 | } |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | static void ion_handle_add(struct ion_client *client, struct ion_handle *handle) |
| 352 | { |
| 353 | struct rb_node **p = &client->handles.rb_node; |
| 354 | struct rb_node *parent = NULL; |
| 355 | struct ion_handle *entry; |
| 356 | |
| 357 | while (*p) { |
| 358 | parent = *p; |
| 359 | entry = rb_entry(parent, struct ion_handle, node); |
| 360 | |
| 361 | if (handle < entry) |
| 362 | p = &(*p)->rb_left; |
| 363 | else if (handle > entry) |
| 364 | p = &(*p)->rb_right; |
| 365 | else |
| 366 | WARN(1, "%s: buffer already found.", __func__); |
| 367 | } |
| 368 | |
| 369 | rb_link_node(&handle->node, parent, p); |
| 370 | rb_insert_color(&handle->node, &client->handles); |
| 371 | } |
| 372 | |
| 373 | struct ion_handle *ion_alloc(struct ion_client *client, size_t len, |
| 374 | size_t align, unsigned int flags) |
| 375 | { |
| 376 | struct rb_node *n; |
| 377 | struct ion_handle *handle; |
| 378 | struct ion_device *dev = client->dev; |
| 379 | struct ion_buffer *buffer = NULL; |
| 380 | unsigned long secure_allocation = flags & ION_SECURE; |
| 381 | const unsigned int MAX_DBG_STR_LEN = 64; |
| 382 | char dbg_str[MAX_DBG_STR_LEN]; |
| 383 | unsigned int dbg_str_idx = 0; |
| 384 | |
| 385 | dbg_str[0] = '\0'; |
| 386 | |
| 387 | if (WARN_ON(!len)) |
| 388 | return ERR_PTR(-EINVAL); |
| 389 | |
| 390 | len = PAGE_ALIGN(len); |
| 391 | |
| 392 | mutex_lock(&dev->lock); |
| 393 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 394 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
| 395 | |
| 396 | if (!((1 << heap->type) & client->heap_mask)) |
| 397 | continue; |
| 398 | |
| 399 | if (!((1 << heap->id) & flags)) |
| 400 | continue; |
| 401 | |
| 402 | if (secure_allocation && (heap->type != ION_HEAP_TYPE_CP)) |
| 403 | continue; |
| 404 | buffer = ion_buffer_create(heap, dev, len, align, flags); |
| 405 | if (!IS_ERR_OR_NULL(buffer)) |
| 406 | break; |
| 407 | if (dbg_str_idx < MAX_DBG_STR_LEN) { |
| 408 | unsigned int len_left = MAX_DBG_STR_LEN-dbg_str_idx-1; |
| 409 | int ret_value = snprintf(&dbg_str[dbg_str_idx], |
| 410 | len_left, "%s ", heap->name); |
| 411 | if (ret_value >= len_left) { |
| 412 | |
| 413 | dbg_str[MAX_DBG_STR_LEN-1] = '\0'; |
| 414 | dbg_str_idx = MAX_DBG_STR_LEN; |
| 415 | } else if (ret_value >= 0) { |
| 416 | dbg_str_idx += ret_value; |
| 417 | } else { |
| 418 | |
| 419 | dbg_str[MAX_DBG_STR_LEN-1] = '\0'; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | mutex_unlock(&dev->lock); |
| 424 | |
| 425 | if (buffer == NULL) |
| 426 | return ERR_PTR(-ENODEV); |
| 427 | |
| 428 | if (IS_ERR(buffer)) { |
| 429 | pr_debug("ION is unable to allocate 0x%x bytes (alignment: " |
| 430 | "0x%x) from heap(s) %sfor client %s with heap " |
| 431 | "mask 0x%x\n", |
| 432 | len, align, dbg_str, client->name, client->heap_mask); |
| 433 | return ERR_PTR(PTR_ERR(buffer)); |
| 434 | } |
| 435 | |
| 436 | buffer->creator = client; |
| 437 | handle = ion_handle_create(client, buffer); |
| 438 | |
| 439 | ion_buffer_put(buffer); |
| 440 | |
| 441 | if (!IS_ERR(handle)) { |
| 442 | mutex_lock(&client->lock); |
| 443 | ion_handle_add(client, handle); |
| 444 | mutex_unlock(&client->lock); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | return handle; |
| 449 | } |
| 450 | EXPORT_SYMBOL(ion_alloc); |
| 451 | |
| 452 | void ion_free(struct ion_client *client, struct ion_handle *handle) |
| 453 | { |
| 454 | bool valid_handle; |
| 455 | |
| 456 | BUG_ON(client != handle->client); |
| 457 | |
| 458 | mutex_lock(&client->lock); |
| 459 | valid_handle = ion_handle_validate(client, handle); |
| 460 | if (!valid_handle) { |
| 461 | mutex_unlock(&client->lock); |
| 462 | WARN(1, "%s: invalid handle passed to free.\n", __func__); |
| 463 | return; |
| 464 | } |
| 465 | ion_handle_put(handle); |
| 466 | mutex_unlock(&client->lock); |
| 467 | } |
| 468 | EXPORT_SYMBOL(ion_free); |
| 469 | |
| 470 | int ion_phys(struct ion_client *client, struct ion_handle *handle, |
| 471 | ion_phys_addr_t *addr, size_t *len) |
| 472 | { |
| 473 | struct ion_buffer *buffer; |
| 474 | int ret; |
| 475 | |
| 476 | mutex_lock(&client->lock); |
| 477 | if (!ion_handle_validate(client, handle)) { |
| 478 | mutex_unlock(&client->lock); |
| 479 | return -EINVAL; |
| 480 | } |
| 481 | |
| 482 | buffer = handle->buffer; |
| 483 | |
| 484 | if (!buffer->heap->ops->phys) { |
| 485 | pr_err("%s: ion_phys is not implemented by this heap.\n", |
| 486 | __func__); |
| 487 | mutex_unlock(&client->lock); |
| 488 | return -ENODEV; |
| 489 | } |
| 490 | mutex_unlock(&client->lock); |
| 491 | ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len); |
| 492 | return ret; |
| 493 | } |
| 494 | EXPORT_SYMBOL(ion_phys); |
| 495 | |
| 496 | static void *ion_buffer_kmap_get(struct ion_buffer *buffer) |
| 497 | { |
| 498 | void *vaddr; |
| 499 | |
| 500 | if (buffer->kmap_cnt) { |
| 501 | buffer->kmap_cnt++; |
| 502 | return buffer->vaddr; |
| 503 | } |
| 504 | vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer); |
| 505 | if (IS_ERR_OR_NULL(vaddr)) |
| 506 | return vaddr; |
| 507 | buffer->vaddr = vaddr; |
| 508 | buffer->kmap_cnt++; |
| 509 | return vaddr; |
| 510 | } |
| 511 | |
| 512 | static void *ion_handle_kmap_get(struct ion_handle *handle) |
| 513 | { |
| 514 | struct ion_buffer *buffer = handle->buffer; |
| 515 | void *vaddr; |
| 516 | |
| 517 | if (handle->kmap_cnt) { |
| 518 | handle->kmap_cnt++; |
| 519 | return buffer->vaddr; |
| 520 | } |
| 521 | vaddr = ion_buffer_kmap_get(buffer); |
| 522 | if (IS_ERR_OR_NULL(vaddr)) |
| 523 | return vaddr; |
| 524 | handle->kmap_cnt++; |
| 525 | return vaddr; |
| 526 | } |
| 527 | |
| 528 | static void ion_buffer_kmap_put(struct ion_buffer *buffer) |
| 529 | { |
| 530 | buffer->kmap_cnt--; |
| 531 | if (!buffer->kmap_cnt) { |
| 532 | buffer->heap->ops->unmap_kernel(buffer->heap, buffer); |
| 533 | buffer->vaddr = NULL; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | static void ion_handle_kmap_put(struct ion_handle *handle) |
| 538 | { |
| 539 | struct ion_buffer *buffer = handle->buffer; |
| 540 | |
| 541 | handle->kmap_cnt--; |
| 542 | if (!handle->kmap_cnt) |
| 543 | ion_buffer_kmap_put(buffer); |
| 544 | } |
| 545 | |
| 546 | static struct ion_iommu_map *__ion_iommu_map(struct ion_buffer *buffer, |
| 547 | int domain_num, int partition_num, unsigned long align, |
| 548 | unsigned long iova_length, unsigned long flags, |
| 549 | unsigned long *iova) |
| 550 | { |
| 551 | struct ion_iommu_map *data; |
| 552 | int ret; |
| 553 | |
| 554 | data = kmalloc(sizeof(*data), GFP_ATOMIC); |
| 555 | |
| 556 | if (!data) |
| 557 | return ERR_PTR(-ENOMEM); |
| 558 | |
| 559 | data->buffer = buffer; |
| 560 | iommu_map_domain(data) = domain_num; |
| 561 | iommu_map_partition(data) = partition_num; |
| 562 | |
| 563 | ret = buffer->heap->ops->map_iommu(buffer, data, |
| 564 | domain_num, |
| 565 | partition_num, |
| 566 | align, |
| 567 | iova_length, |
| 568 | flags); |
| 569 | |
| 570 | if (ret) |
| 571 | goto out; |
| 572 | |
| 573 | kref_init(&data->ref); |
| 574 | *iova = data->iova_addr; |
| 575 | |
| 576 | ion_iommu_add(buffer, data); |
| 577 | |
| 578 | return data; |
| 579 | |
| 580 | out: |
| 581 | kfree(data); |
| 582 | return ERR_PTR(ret); |
| 583 | } |
| 584 | |
| 585 | int ion_map_iommu(struct ion_client *client, struct ion_handle *handle, |
| 586 | int domain_num, int partition_num, unsigned long align, |
| 587 | unsigned long iova_length, unsigned long *iova, |
| 588 | unsigned long *buffer_size, |
| 589 | unsigned long flags, unsigned long iommu_flags) |
| 590 | { |
| 591 | struct ion_buffer *buffer; |
| 592 | struct ion_iommu_map *iommu_map; |
| 593 | int ret = 0; |
| 594 | |
| 595 | if (ION_IS_CACHED(flags)) { |
| 596 | pr_err("%s: Cannot map iommu as cached.\n", __func__); |
| 597 | return -EINVAL; |
| 598 | } |
| 599 | |
| 600 | mutex_lock(&client->lock); |
| 601 | if (!ion_handle_validate(client, handle)) { |
| 602 | pr_err("%s: invalid handle passed to map_kernel.\n", |
| 603 | __func__); |
| 604 | mutex_unlock(&client->lock); |
| 605 | return -EINVAL; |
| 606 | } |
| 607 | |
| 608 | buffer = handle->buffer; |
| 609 | mutex_lock(&buffer->lock); |
| 610 | |
| 611 | if (!handle->buffer->heap->ops->map_iommu) { |
| 612 | pr_err("%s: map_iommu is not implemented by this heap.\n", |
| 613 | __func__); |
| 614 | ret = -ENODEV; |
| 615 | goto out; |
| 616 | } |
| 617 | |
| 618 | if (!iova_length) |
| 619 | iova_length = buffer->size; |
| 620 | |
| 621 | if (buffer->size > iova_length) { |
| 622 | pr_debug("%s: iova length %lx is not at least buffer size" |
| 623 | " %x\n", __func__, iova_length, buffer->size); |
| 624 | ret = -EINVAL; |
| 625 | goto out; |
| 626 | } |
| 627 | |
| 628 | if (buffer->size & ~PAGE_MASK) { |
| 629 | pr_debug("%s: buffer size %x is not aligned to %lx", __func__, |
| 630 | buffer->size, PAGE_SIZE); |
| 631 | ret = -EINVAL; |
| 632 | goto out; |
| 633 | } |
| 634 | |
| 635 | if (iova_length & ~PAGE_MASK) { |
| 636 | pr_debug("%s: iova_length %lx is not aligned to %lx", __func__, |
| 637 | iova_length, PAGE_SIZE); |
| 638 | ret = -EINVAL; |
| 639 | goto out; |
| 640 | } |
| 641 | |
| 642 | iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num); |
| 643 | if (!iommu_map) { |
| 644 | iommu_map = __ion_iommu_map(buffer, domain_num, partition_num, |
| 645 | align, iova_length, flags, iova); |
| 646 | if (!IS_ERR_OR_NULL(iommu_map)) { |
| 647 | iommu_map->flags = iommu_flags; |
| 648 | |
| 649 | if (iommu_map->flags & ION_IOMMU_UNMAP_DELAYED) |
| 650 | kref_get(&iommu_map->ref); |
| 651 | } |
| 652 | } else { |
| 653 | if (iommu_map->flags != iommu_flags) { |
| 654 | pr_err("%s: handle %p is already mapped with iommu flags %lx, trying to map with flags %lx\n", |
| 655 | __func__, handle, |
| 656 | iommu_map->flags, iommu_flags); |
| 657 | ret = -EINVAL; |
| 658 | } else if (iommu_map->mapped_size != iova_length) { |
| 659 | pr_err("%s: handle %p is already mapped with length" |
| 660 | " %x, trying to map with length %lx\n", |
| 661 | __func__, handle, iommu_map->mapped_size, |
| 662 | iova_length); |
| 663 | ret = -EINVAL; |
| 664 | } else { |
| 665 | kref_get(&iommu_map->ref); |
| 666 | *iova = iommu_map->iova_addr; |
| 667 | } |
| 668 | } |
| 669 | if (!ret) |
| 670 | buffer->iommu_map_cnt++; |
| 671 | *buffer_size = buffer->size; |
| 672 | out: |
| 673 | mutex_unlock(&buffer->lock); |
| 674 | mutex_unlock(&client->lock); |
| 675 | return ret; |
| 676 | } |
| 677 | EXPORT_SYMBOL(ion_map_iommu); |
| 678 | |
| 679 | static void ion_iommu_release(struct kref *kref) |
| 680 | { |
| 681 | struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map, |
| 682 | ref); |
| 683 | struct ion_buffer *buffer = map->buffer; |
| 684 | |
| 685 | rb_erase(&map->node, &buffer->iommu_maps); |
| 686 | buffer->heap->ops->unmap_iommu(map); |
| 687 | kfree(map); |
| 688 | } |
| 689 | |
| 690 | void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle, |
| 691 | int domain_num, int partition_num) |
| 692 | { |
| 693 | struct ion_iommu_map *iommu_map; |
| 694 | struct ion_buffer *buffer; |
| 695 | |
| 696 | mutex_lock(&client->lock); |
| 697 | buffer = handle->buffer; |
| 698 | |
| 699 | mutex_lock(&buffer->lock); |
| 700 | |
| 701 | iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num); |
| 702 | |
| 703 | if (!iommu_map) { |
| 704 | WARN(1, "%s: (%d,%d) was never mapped for %p\n", __func__, |
| 705 | domain_num, partition_num, buffer); |
| 706 | goto out; |
| 707 | } |
| 708 | |
| 709 | kref_put(&iommu_map->ref, ion_iommu_release); |
| 710 | |
| 711 | buffer->iommu_map_cnt--; |
| 712 | out: |
| 713 | mutex_unlock(&buffer->lock); |
| 714 | |
| 715 | mutex_unlock(&client->lock); |
| 716 | |
| 717 | } |
| 718 | EXPORT_SYMBOL(ion_unmap_iommu); |
| 719 | |
| 720 | void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle, |
| 721 | unsigned long flags) |
| 722 | { |
| 723 | struct ion_buffer *buffer; |
| 724 | void *vaddr; |
| 725 | |
| 726 | mutex_lock(&client->lock); |
| 727 | if (!ion_handle_validate(client, handle)) { |
| 728 | pr_err("%s: invalid handle passed to map_kernel.\n", |
| 729 | __func__); |
| 730 | mutex_unlock(&client->lock); |
| 731 | return ERR_PTR(-EINVAL); |
| 732 | } |
| 733 | |
| 734 | buffer = handle->buffer; |
| 735 | |
| 736 | if (!handle->buffer->heap->ops->map_kernel) { |
| 737 | pr_err("%s: map_kernel is not implemented by this heap.\n", |
| 738 | __func__); |
| 739 | mutex_unlock(&client->lock); |
| 740 | return ERR_PTR(-ENODEV); |
| 741 | } |
| 742 | |
| 743 | if (ion_validate_buffer_flags(buffer, flags)) { |
| 744 | mutex_unlock(&client->lock); |
| 745 | return ERR_PTR(-EEXIST); |
| 746 | } |
| 747 | |
| 748 | mutex_lock(&buffer->lock); |
| 749 | vaddr = ion_handle_kmap_get(handle); |
| 750 | mutex_unlock(&buffer->lock); |
| 751 | mutex_unlock(&client->lock); |
| 752 | return vaddr; |
| 753 | } |
| 754 | EXPORT_SYMBOL(ion_map_kernel); |
| 755 | |
| 756 | void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle) |
| 757 | { |
| 758 | struct ion_buffer *buffer; |
| 759 | |
| 760 | mutex_lock(&client->lock); |
| 761 | buffer = handle->buffer; |
| 762 | mutex_lock(&buffer->lock); |
| 763 | ion_handle_kmap_put(handle); |
| 764 | mutex_unlock(&buffer->lock); |
| 765 | mutex_unlock(&client->lock); |
| 766 | } |
| 767 | EXPORT_SYMBOL(ion_unmap_kernel); |
| 768 | |
| 769 | static int check_vaddr_bounds(unsigned long start, unsigned long end) |
| 770 | { |
| 771 | struct mm_struct *mm = current->active_mm; |
| 772 | struct vm_area_struct *vma; |
| 773 | int ret = 1; |
| 774 | |
| 775 | if (end < start) |
| 776 | goto out; |
| 777 | |
| 778 | down_read(&mm->mmap_sem); |
| 779 | vma = find_vma(mm, start); |
| 780 | if (vma && vma->vm_start < end) { |
| 781 | if (start < vma->vm_start) |
| 782 | goto out_up; |
| 783 | if (end > vma->vm_end) |
| 784 | goto out_up; |
| 785 | ret = 0; |
| 786 | } |
| 787 | |
| 788 | out_up: |
| 789 | up_read(&mm->mmap_sem); |
| 790 | out: |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle, |
| 795 | void *uaddr, unsigned long offset, unsigned long len, |
| 796 | unsigned int cmd) |
| 797 | { |
| 798 | struct ion_buffer *buffer; |
| 799 | int ret = -EINVAL; |
| 800 | |
| 801 | mutex_lock(&client->lock); |
| 802 | if (!ion_handle_validate(client, handle)) { |
| 803 | pr_err("%s: invalid handle passed to do_cache_op.\n", |
| 804 | __func__); |
| 805 | mutex_unlock(&client->lock); |
| 806 | return -EINVAL; |
| 807 | } |
| 808 | buffer = handle->buffer; |
| 809 | mutex_lock(&buffer->lock); |
| 810 | |
| 811 | if (!ION_IS_CACHED(buffer->flags)) { |
| 812 | ret = 0; |
| 813 | goto out; |
| 814 | } |
| 815 | |
| 816 | if (!handle->buffer->heap->ops->cache_op) { |
| 817 | pr_err("%s: cache_op is not implemented by this heap.\n", |
| 818 | __func__); |
| 819 | ret = -ENODEV; |
| 820 | goto out; |
| 821 | } |
| 822 | |
| 823 | |
| 824 | ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr, |
| 825 | offset, len, cmd); |
| 826 | |
| 827 | out: |
| 828 | mutex_unlock(&buffer->lock); |
| 829 | mutex_unlock(&client->lock); |
| 830 | return ret; |
| 831 | |
| 832 | } |
| 833 | EXPORT_SYMBOL(ion_do_cache_op); |
| 834 | |
| 835 | static int ion_debug_client_show(struct seq_file *s, void *unused) |
| 836 | { |
| 837 | struct ion_client *client = s->private; |
| 838 | struct rb_node *n; |
| 839 | struct rb_node *n2; |
| 840 | |
| 841 | seq_printf(s, "%16.16s: %16.16s : %16.16s : %12.12s : %12.12s : %s\n", |
| 842 | "heap_name", "size_in_bytes", "handle refcount", |
| 843 | "buffer", "physical", "[domain,partition] - virt"); |
| 844 | |
| 845 | mutex_lock(&client->lock); |
| 846 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 847 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 848 | node); |
| 849 | enum ion_heap_type type = handle->buffer->heap->type; |
| 850 | |
| 851 | seq_printf(s, "%16.16s: %16x : %16d : %12p", |
| 852 | handle->buffer->heap->name, |
| 853 | handle->buffer->size, |
| 854 | atomic_read(&handle->ref.refcount), |
| 855 | handle->buffer); |
| 856 | |
| 857 | if (type == ION_HEAP_TYPE_SYSTEM_CONTIG || |
| 858 | type == ION_HEAP_TYPE_CARVEOUT || |
| 859 | type == ION_HEAP_TYPE_CP) |
| 860 | seq_printf(s, " : %12lx", handle->buffer->priv_phys); |
| 861 | else |
| 862 | seq_printf(s, " : %12s", "N/A"); |
| 863 | |
| 864 | for (n2 = rb_first(&handle->buffer->iommu_maps); n2; |
| 865 | n2 = rb_next(n2)) { |
| 866 | struct ion_iommu_map *imap = |
| 867 | rb_entry(n2, struct ion_iommu_map, node); |
| 868 | seq_printf(s, " : [%d,%d] - %8lx", |
| 869 | imap->domain_info[DI_DOMAIN_NUM], |
| 870 | imap->domain_info[DI_PARTITION_NUM], |
| 871 | imap->iova_addr); |
| 872 | } |
| 873 | seq_printf(s, "\n"); |
| 874 | } |
| 875 | mutex_unlock(&client->lock); |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | static int ion_debug_client_open(struct inode *inode, struct file *file) |
| 881 | { |
| 882 | return single_open(file, ion_debug_client_show, inode->i_private); |
| 883 | } |
| 884 | |
| 885 | static const struct file_operations debug_client_fops = { |
| 886 | .open = ion_debug_client_open, |
| 887 | .read = seq_read, |
| 888 | .llseek = seq_lseek, |
| 889 | .release = single_release, |
| 890 | }; |
| 891 | |
| 892 | struct ion_client *ion_client_create(struct ion_device *dev, |
| 893 | unsigned int heap_mask, |
| 894 | const char *name) |
| 895 | { |
| 896 | struct ion_client *client; |
| 897 | struct task_struct *task; |
| 898 | struct rb_node **p; |
| 899 | struct rb_node *parent = NULL; |
| 900 | struct ion_client *entry; |
| 901 | pid_t pid; |
| 902 | unsigned int name_len; |
| 903 | |
| 904 | if (!name) { |
| 905 | pr_err("%s: Name cannot be null\n", __func__); |
| 906 | return ERR_PTR(-EINVAL); |
| 907 | } |
| 908 | name_len = strnlen(name, 64); |
| 909 | |
| 910 | get_task_struct(current->group_leader); |
| 911 | task_lock(current->group_leader); |
| 912 | pid = task_pid_nr(current->group_leader); |
| 913 | if (current->group_leader->flags & PF_KTHREAD) { |
| 914 | put_task_struct(current->group_leader); |
| 915 | task = NULL; |
| 916 | } else { |
| 917 | task = current->group_leader; |
| 918 | } |
| 919 | task_unlock(current->group_leader); |
| 920 | |
| 921 | client = kzalloc(sizeof(struct ion_client), GFP_KERNEL); |
| 922 | if (!client) { |
| 923 | if (task) |
| 924 | put_task_struct(current->group_leader); |
| 925 | return ERR_PTR(-ENOMEM); |
| 926 | } |
| 927 | |
| 928 | client->dev = dev; |
| 929 | client->handles = RB_ROOT; |
| 930 | mutex_init(&client->lock); |
| 931 | |
| 932 | client->name = kzalloc(name_len+1, GFP_KERNEL); |
| 933 | if (!client->name) { |
| 934 | put_task_struct(current->group_leader); |
| 935 | kfree(client); |
| 936 | return ERR_PTR(-ENOMEM); |
| 937 | } else { |
| 938 | strlcpy(client->name, name, name_len+1); |
| 939 | } |
| 940 | |
| 941 | client->heap_mask = heap_mask; |
| 942 | client->task = task; |
| 943 | client->pid = pid; |
| 944 | |
| 945 | mutex_lock(&dev->lock); |
| 946 | p = &dev->clients.rb_node; |
| 947 | while (*p) { |
| 948 | parent = *p; |
| 949 | entry = rb_entry(parent, struct ion_client, node); |
| 950 | |
| 951 | if (client < entry) |
| 952 | p = &(*p)->rb_left; |
| 953 | else if (client > entry) |
| 954 | p = &(*p)->rb_right; |
| 955 | } |
| 956 | rb_link_node(&client->node, parent, p); |
| 957 | rb_insert_color(&client->node, &dev->clients); |
| 958 | |
| 959 | |
| 960 | client->debug_root = debugfs_create_file(name, 0664, |
| 961 | dev->debug_root, client, |
| 962 | &debug_client_fops); |
| 963 | mutex_unlock(&dev->lock); |
| 964 | |
| 965 | pr_info("%s: create ion_client (%s) at %p\n", __func__, client->name, client); |
| 966 | return client; |
| 967 | } |
| 968 | |
| 969 | void ion_client_destroy(struct ion_client *client) |
| 970 | { |
| 971 | struct ion_device *dev = client->dev; |
| 972 | struct rb_node *n; |
| 973 | |
| 974 | pr_info("%s: destroy ion_client %p (%s)\n", __func__, client, client->name); |
| 975 | while ((n = rb_first(&client->handles))) { |
| 976 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 977 | node); |
| 978 | mutex_lock(&client->lock); |
| 979 | ion_handle_destroy(&handle->ref); |
| 980 | mutex_unlock(&client->lock); |
| 981 | } |
| 982 | mutex_lock(&dev->lock); |
| 983 | if (client->task) |
| 984 | put_task_struct(client->task); |
| 985 | rb_erase(&client->node, &dev->clients); |
| 986 | debugfs_remove_recursive(client->debug_root); |
| 987 | mutex_unlock(&dev->lock); |
| 988 | |
| 989 | kfree(client->name); |
| 990 | kfree(client); |
| 991 | } |
| 992 | EXPORT_SYMBOL(ion_client_destroy); |
| 993 | |
| 994 | int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle, |
| 995 | unsigned long *flags) |
| 996 | { |
| 997 | struct ion_buffer *buffer; |
| 998 | |
| 999 | mutex_lock(&client->lock); |
| 1000 | if (!ion_handle_validate(client, handle)) { |
| 1001 | pr_err("%s: invalid handle passed to %s.\n", |
| 1002 | __func__, __func__); |
| 1003 | mutex_unlock(&client->lock); |
| 1004 | return -EINVAL; |
| 1005 | } |
| 1006 | buffer = handle->buffer; |
| 1007 | mutex_lock(&buffer->lock); |
| 1008 | *flags = buffer->flags; |
| 1009 | mutex_unlock(&buffer->lock); |
| 1010 | mutex_unlock(&client->lock); |
| 1011 | |
| 1012 | return 0; |
| 1013 | } |
| 1014 | EXPORT_SYMBOL(ion_handle_get_flags); |
| 1015 | |
| 1016 | int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle, |
| 1017 | unsigned long *size) |
| 1018 | { |
| 1019 | struct ion_buffer *buffer; |
| 1020 | |
| 1021 | mutex_lock(&client->lock); |
| 1022 | if (!ion_handle_validate(client, handle)) { |
| 1023 | pr_err("%s: invalid handle passed to %s.\n", |
| 1024 | __func__, __func__); |
| 1025 | mutex_unlock(&client->lock); |
| 1026 | return -EINVAL; |
| 1027 | } |
| 1028 | buffer = handle->buffer; |
| 1029 | mutex_lock(&buffer->lock); |
| 1030 | *size = buffer->size; |
| 1031 | mutex_unlock(&buffer->lock); |
| 1032 | mutex_unlock(&client->lock); |
| 1033 | |
| 1034 | return 0; |
| 1035 | } |
| 1036 | EXPORT_SYMBOL(ion_handle_get_size); |
| 1037 | |
| 1038 | struct sg_table *ion_sg_table(struct ion_client *client, |
| 1039 | struct ion_handle *handle) |
| 1040 | { |
| 1041 | struct ion_buffer *buffer; |
| 1042 | struct sg_table *table; |
| 1043 | |
| 1044 | mutex_lock(&client->lock); |
| 1045 | if (!ion_handle_validate(client, handle)) { |
| 1046 | pr_err("%s: invalid handle passed to map_dma.\n", |
| 1047 | __func__); |
| 1048 | mutex_unlock(&client->lock); |
| 1049 | return ERR_PTR(-EINVAL); |
| 1050 | } |
| 1051 | buffer = handle->buffer; |
| 1052 | table = buffer->sg_table; |
| 1053 | mutex_unlock(&client->lock); |
| 1054 | return table; |
| 1055 | } |
| 1056 | EXPORT_SYMBOL(ion_sg_table); |
| 1057 | |
| 1058 | static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment, |
| 1059 | enum dma_data_direction direction) |
| 1060 | { |
| 1061 | struct dma_buf *dmabuf = attachment->dmabuf; |
| 1062 | struct ion_buffer *buffer = dmabuf->priv; |
| 1063 | |
| 1064 | return buffer->sg_table; |
| 1065 | } |
| 1066 | |
| 1067 | static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment, |
| 1068 | struct sg_table *table, |
| 1069 | enum dma_data_direction direction) |
| 1070 | { |
| 1071 | } |
| 1072 | |
| 1073 | static void ion_vma_open(struct vm_area_struct *vma) |
| 1074 | { |
| 1075 | struct ion_buffer *buffer = vma->vm_private_data; |
| 1076 | |
| 1077 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1078 | |
| 1079 | mutex_lock(&buffer->lock); |
| 1080 | buffer->umap_cnt++; |
| 1081 | mutex_unlock(&buffer->lock); |
| 1082 | } |
| 1083 | |
| 1084 | static void ion_vma_close(struct vm_area_struct *vma) |
| 1085 | { |
| 1086 | struct ion_buffer *buffer = vma->vm_private_data; |
| 1087 | |
| 1088 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1089 | |
| 1090 | mutex_lock(&buffer->lock); |
| 1091 | buffer->umap_cnt--; |
| 1092 | mutex_unlock(&buffer->lock); |
| 1093 | |
| 1094 | if (buffer->heap->ops->unmap_user) |
| 1095 | buffer->heap->ops->unmap_user(buffer->heap, buffer); |
| 1096 | } |
| 1097 | |
| 1098 | static struct vm_operations_struct ion_vm_ops = { |
| 1099 | .open = ion_vma_open, |
| 1100 | .close = ion_vma_close, |
| 1101 | }; |
| 1102 | |
| 1103 | static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) |
| 1104 | { |
| 1105 | struct ion_buffer *buffer = dmabuf->priv; |
| 1106 | int ret; |
| 1107 | |
| 1108 | if (!buffer->heap->ops->map_user) { |
| 1109 | pr_err("%s: this heap does not define a method for mapping " |
| 1110 | "to userspace\n", __func__); |
| 1111 | return -EINVAL; |
| 1112 | } |
| 1113 | |
| 1114 | mutex_lock(&buffer->lock); |
| 1115 | |
| 1116 | ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma); |
| 1117 | |
| 1118 | if (ret) { |
| 1119 | mutex_unlock(&buffer->lock); |
| 1120 | pr_err("%s: failure mapping buffer to userspace\n", |
| 1121 | __func__); |
| 1122 | } else { |
| 1123 | buffer->umap_cnt++; |
| 1124 | mutex_unlock(&buffer->lock); |
| 1125 | |
| 1126 | vma->vm_ops = &ion_vm_ops; |
| 1127 | vma->vm_private_data = buffer; |
| 1128 | } |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
| 1132 | static void ion_dma_buf_release(struct dma_buf *dmabuf) |
| 1133 | { |
| 1134 | struct ion_buffer *buffer = dmabuf->priv; |
| 1135 | ion_buffer_put(buffer); |
| 1136 | } |
| 1137 | |
| 1138 | static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset) |
| 1139 | { |
| 1140 | struct ion_buffer *buffer = dmabuf->priv; |
| 1141 | return buffer->vaddr + offset; |
| 1142 | } |
| 1143 | |
| 1144 | static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset, |
| 1145 | void *ptr) |
| 1146 | { |
| 1147 | return; |
| 1148 | } |
| 1149 | |
| 1150 | static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, |
| 1151 | size_t len, |
| 1152 | enum dma_data_direction direction) |
| 1153 | { |
| 1154 | struct ion_buffer *buffer = dmabuf->priv; |
| 1155 | void *vaddr; |
| 1156 | |
| 1157 | if (!buffer->heap->ops->map_kernel) { |
| 1158 | pr_err("%s: map kernel is not implemented by this heap.\n", |
| 1159 | __func__); |
| 1160 | return -ENODEV; |
| 1161 | } |
| 1162 | |
| 1163 | mutex_lock(&buffer->lock); |
| 1164 | vaddr = ion_buffer_kmap_get(buffer); |
| 1165 | mutex_unlock(&buffer->lock); |
| 1166 | if (IS_ERR(vaddr)) |
| 1167 | return PTR_ERR(vaddr); |
| 1168 | if (!vaddr) |
| 1169 | return -ENOMEM; |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
| 1173 | static void ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, |
| 1174 | size_t len, |
| 1175 | enum dma_data_direction direction) |
| 1176 | { |
| 1177 | struct ion_buffer *buffer = dmabuf->priv; |
| 1178 | |
| 1179 | mutex_lock(&buffer->lock); |
| 1180 | ion_buffer_kmap_put(buffer); |
| 1181 | mutex_unlock(&buffer->lock); |
| 1182 | } |
| 1183 | |
| 1184 | struct dma_buf_ops dma_buf_ops = { |
| 1185 | .map_dma_buf = ion_map_dma_buf, |
| 1186 | .unmap_dma_buf = ion_unmap_dma_buf, |
| 1187 | .mmap = ion_mmap, |
| 1188 | .release = ion_dma_buf_release, |
| 1189 | .begin_cpu_access = ion_dma_buf_begin_cpu_access, |
| 1190 | .end_cpu_access = ion_dma_buf_end_cpu_access, |
| 1191 | .kmap_atomic = ion_dma_buf_kmap, |
| 1192 | .kunmap_atomic = ion_dma_buf_kunmap, |
| 1193 | .kmap = ion_dma_buf_kmap, |
| 1194 | .kunmap = ion_dma_buf_kunmap, |
| 1195 | }; |
| 1196 | |
| 1197 | static int ion_share_set_flags(struct ion_client *client, |
| 1198 | struct ion_handle *handle, |
| 1199 | unsigned long flags) |
| 1200 | { |
| 1201 | struct ion_buffer *buffer; |
| 1202 | bool valid_handle; |
| 1203 | unsigned long ion_flags = ION_SET_CACHE(CACHED); |
| 1204 | if (flags & O_DSYNC) |
| 1205 | ion_flags = ION_SET_CACHE(UNCACHED); |
| 1206 | |
| 1207 | mutex_lock(&client->lock); |
| 1208 | valid_handle = ion_handle_validate(client, handle); |
| 1209 | mutex_unlock(&client->lock); |
| 1210 | if (!valid_handle) { |
| 1211 | WARN(1, "%s: invalid handle passed to set_flags.\n", __func__); |
| 1212 | return -EINVAL; |
| 1213 | } |
| 1214 | |
| 1215 | buffer = handle->buffer; |
| 1216 | |
| 1217 | mutex_lock(&buffer->lock); |
| 1218 | if (ion_validate_buffer_flags(buffer, ion_flags)) { |
| 1219 | mutex_unlock(&buffer->lock); |
| 1220 | return -EEXIST; |
| 1221 | } |
| 1222 | mutex_unlock(&buffer->lock); |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle) |
| 1228 | { |
| 1229 | struct ion_buffer *buffer; |
| 1230 | struct dma_buf *dmabuf; |
| 1231 | bool valid_handle; |
| 1232 | int fd; |
| 1233 | |
| 1234 | mutex_lock(&client->lock); |
| 1235 | valid_handle = ion_handle_validate(client, handle); |
| 1236 | mutex_unlock(&client->lock); |
| 1237 | if (!valid_handle) { |
| 1238 | WARN(1, "%s: invalid handle passed to share.\n", __func__); |
| 1239 | return -EINVAL; |
| 1240 | } |
| 1241 | |
| 1242 | buffer = handle->buffer; |
| 1243 | ion_buffer_get(buffer); |
| 1244 | dmabuf = dma_buf_export(buffer, &dma_buf_ops, buffer->size, O_RDWR); |
| 1245 | if (IS_ERR(dmabuf)) { |
| 1246 | ion_buffer_put(buffer); |
| 1247 | return PTR_ERR(dmabuf); |
| 1248 | } |
| 1249 | fd = dma_buf_fd(dmabuf, O_CLOEXEC); |
| 1250 | if (fd < 0) |
| 1251 | dma_buf_put(dmabuf); |
| 1252 | |
| 1253 | return fd; |
| 1254 | } |
| 1255 | EXPORT_SYMBOL(ion_share_dma_buf); |
| 1256 | |
| 1257 | struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd) |
| 1258 | { |
| 1259 | struct dma_buf *dmabuf; |
| 1260 | struct ion_buffer *buffer; |
| 1261 | struct ion_handle *handle; |
| 1262 | |
| 1263 | dmabuf = dma_buf_get(fd); |
| 1264 | if (IS_ERR_OR_NULL(dmabuf)) |
| 1265 | return ERR_PTR(PTR_ERR(dmabuf)); |
| 1266 | |
| 1267 | |
| 1268 | if (dmabuf->ops != &dma_buf_ops) { |
| 1269 | pr_err("%s: can not import dmabuf from another exporter\n", |
| 1270 | __func__); |
| 1271 | dma_buf_put(dmabuf); |
| 1272 | return ERR_PTR(-EINVAL); |
| 1273 | } |
| 1274 | buffer = dmabuf->priv; |
| 1275 | |
| 1276 | mutex_lock(&client->lock); |
| 1277 | |
| 1278 | handle = ion_handle_lookup(client, buffer); |
| 1279 | if (!IS_ERR_OR_NULL(handle)) { |
| 1280 | ion_handle_get(handle); |
| 1281 | goto end; |
| 1282 | } |
| 1283 | handle = ion_handle_create(client, buffer); |
| 1284 | if (IS_ERR_OR_NULL(handle)) |
| 1285 | goto end; |
| 1286 | ion_handle_add(client, handle); |
| 1287 | end: |
| 1288 | mutex_unlock(&client->lock); |
| 1289 | dma_buf_put(dmabuf); |
| 1290 | return handle; |
| 1291 | } |
| 1292 | EXPORT_SYMBOL(ion_import_dma_buf); |
| 1293 | |
| 1294 | static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 1295 | { |
| 1296 | struct ion_client *client = filp->private_data; |
| 1297 | |
| 1298 | switch (cmd) { |
| 1299 | case ION_IOC_ALLOC: |
| 1300 | { |
| 1301 | struct ion_allocation_data data; |
| 1302 | |
| 1303 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 1304 | return -EFAULT; |
| 1305 | data.handle = ion_alloc(client, data.len, data.align, |
| 1306 | data.flags); |
| 1307 | |
| 1308 | if (IS_ERR(data.handle)) |
| 1309 | return PTR_ERR(data.handle); |
| 1310 | |
| 1311 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) { |
| 1312 | ion_free(client, data.handle); |
| 1313 | return -EFAULT; |
| 1314 | } |
| 1315 | break; |
| 1316 | } |
| 1317 | case ION_IOC_FREE: |
| 1318 | { |
| 1319 | struct ion_handle_data data; |
| 1320 | bool valid; |
| 1321 | |
| 1322 | if (copy_from_user(&data, (void __user *)arg, |
| 1323 | sizeof(struct ion_handle_data))) |
| 1324 | return -EFAULT; |
| 1325 | mutex_lock(&client->lock); |
| 1326 | valid = ion_handle_validate(client, data.handle); |
| 1327 | mutex_unlock(&client->lock); |
| 1328 | if (!valid) |
| 1329 | return -EINVAL; |
| 1330 | ion_free(client, data.handle); |
| 1331 | break; |
| 1332 | } |
| 1333 | case ION_IOC_MAP: |
| 1334 | case ION_IOC_SHARE: |
| 1335 | { |
| 1336 | struct ion_fd_data data; |
| 1337 | int ret; |
| 1338 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 1339 | return -EFAULT; |
| 1340 | |
| 1341 | ret = ion_share_set_flags(client, data.handle, filp->f_flags); |
| 1342 | if (ret) |
| 1343 | return ret; |
| 1344 | |
| 1345 | data.fd = ion_share_dma_buf(client, data.handle); |
| 1346 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) |
| 1347 | return -EFAULT; |
| 1348 | if (data.fd < 0) |
| 1349 | return data.fd; |
| 1350 | break; |
| 1351 | } |
| 1352 | case ION_IOC_IMPORT: |
| 1353 | { |
| 1354 | struct ion_fd_data data; |
| 1355 | int ret = 0; |
| 1356 | if (copy_from_user(&data, (void __user *)arg, |
| 1357 | sizeof(struct ion_fd_data))) |
| 1358 | return -EFAULT; |
| 1359 | data.handle = ion_import_dma_buf(client, data.fd); |
| 1360 | if (IS_ERR(data.handle)) |
| 1361 | data.handle = NULL; |
| 1362 | if (copy_to_user((void __user *)arg, &data, |
| 1363 | sizeof(struct ion_fd_data))) |
| 1364 | return -EFAULT; |
| 1365 | if (ret < 0) |
| 1366 | return ret; |
| 1367 | break; |
| 1368 | } |
| 1369 | case ION_IOC_CUSTOM: |
| 1370 | { |
| 1371 | struct ion_device *dev = client->dev; |
| 1372 | struct ion_custom_data data; |
| 1373 | |
| 1374 | if (!dev->custom_ioctl) |
| 1375 | return -ENOTTY; |
| 1376 | if (copy_from_user(&data, (void __user *)arg, |
| 1377 | sizeof(struct ion_custom_data))) |
| 1378 | return -EFAULT; |
| 1379 | return dev->custom_ioctl(client, data.cmd, data.arg); |
| 1380 | } |
| 1381 | case ION_IOC_CLEAN_CACHES: |
| 1382 | case ION_IOC_INV_CACHES: |
| 1383 | case ION_IOC_CLEAN_INV_CACHES: |
| 1384 | { |
| 1385 | struct ion_flush_data data; |
| 1386 | unsigned long start, end; |
| 1387 | struct ion_handle *handle = NULL; |
| 1388 | int ret; |
| 1389 | |
| 1390 | if (copy_from_user(&data, (void __user *)arg, |
| 1391 | sizeof(struct ion_flush_data))) |
| 1392 | return -EFAULT; |
| 1393 | |
| 1394 | start = (unsigned long) data.vaddr; |
| 1395 | end = (unsigned long) data.vaddr + data.length; |
| 1396 | |
| 1397 | if (check_vaddr_bounds(start, end)) { |
| 1398 | pr_err("%s: virtual address %p is out of bounds\n", |
| 1399 | __func__, data.vaddr); |
| 1400 | return -EINVAL; |
| 1401 | } |
| 1402 | |
| 1403 | if (!data.handle) { |
| 1404 | handle = ion_import_dma_buf(client, data.fd); |
| 1405 | if (IS_ERR(handle)) { |
| 1406 | pr_info("%s: Could not import handle: %d\n", |
| 1407 | __func__, (int)handle); |
| 1408 | return -EINVAL; |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | ret = ion_do_cache_op(client, |
| 1413 | data.handle ? data.handle : handle, |
| 1414 | data.vaddr, data.offset, data.length, |
| 1415 | cmd); |
| 1416 | |
| 1417 | if (!data.handle) |
| 1418 | ion_free(client, handle); |
| 1419 | |
| 1420 | if (ret < 0) |
| 1421 | return ret; |
| 1422 | break; |
| 1423 | |
| 1424 | } |
| 1425 | case ION_IOC_GET_FLAGS: |
| 1426 | { |
| 1427 | struct ion_flag_data data; |
| 1428 | int ret; |
| 1429 | if (copy_from_user(&data, (void __user *)arg, |
| 1430 | sizeof(struct ion_flag_data))) |
| 1431 | return -EFAULT; |
| 1432 | |
| 1433 | ret = ion_handle_get_flags(client, data.handle, &data.flags); |
| 1434 | if (ret < 0) |
| 1435 | return ret; |
| 1436 | if (copy_to_user((void __user *)arg, &data, |
| 1437 | sizeof(struct ion_flag_data))) |
| 1438 | return -EFAULT; |
| 1439 | break; |
| 1440 | } |
| 1441 | default: |
| 1442 | return -ENOTTY; |
| 1443 | } |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
| 1447 | static int ion_release(struct inode *inode, struct file *file) |
| 1448 | { |
| 1449 | struct ion_client *client = file->private_data; |
| 1450 | |
| 1451 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1452 | ion_client_destroy(client); |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | static int ion_open(struct inode *inode, struct file *file) |
| 1457 | { |
| 1458 | struct miscdevice *miscdev = file->private_data; |
| 1459 | struct ion_device *dev = container_of(miscdev, struct ion_device, dev); |
| 1460 | struct ion_client *client; |
| 1461 | char debug_name[64]; |
| 1462 | char task_comm[TASK_COMM_LEN]; |
| 1463 | |
| 1464 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1465 | if (current->group_leader->flags & PF_KTHREAD) { |
| 1466 | snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader)); |
| 1467 | } else { |
| 1468 | strcpy(debug_name, get_task_comm(task_comm, current->group_leader)); |
| 1469 | } |
| 1470 | |
| 1471 | client = ion_client_create(dev, -1, debug_name); |
| 1472 | if (IS_ERR_OR_NULL(client)) |
| 1473 | return PTR_ERR(client); |
| 1474 | file->private_data = client; |
| 1475 | |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | static const struct file_operations ion_fops = { |
| 1480 | .owner = THIS_MODULE, |
| 1481 | .open = ion_open, |
| 1482 | .release = ion_release, |
| 1483 | .unlocked_ioctl = ion_ioctl, |
| 1484 | }; |
| 1485 | |
| 1486 | static size_t ion_debug_heap_total(struct ion_client *client, |
| 1487 | enum ion_heap_ids id) |
| 1488 | { |
| 1489 | size_t size = 0; |
| 1490 | struct rb_node *n; |
| 1491 | |
| 1492 | mutex_lock(&client->lock); |
| 1493 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 1494 | struct ion_handle *handle = rb_entry(n, |
| 1495 | struct ion_handle, |
| 1496 | node); |
| 1497 | if (handle->buffer->heap->id == id) |
| 1498 | size += handle->buffer->size; |
| 1499 | } |
| 1500 | mutex_unlock(&client->lock); |
| 1501 | return size; |
| 1502 | } |
| 1503 | |
| 1504 | static int ion_debug_find_buffer_owner(const struct ion_client *client, |
| 1505 | const struct ion_buffer *buf) |
| 1506 | { |
| 1507 | struct rb_node *n; |
| 1508 | |
| 1509 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 1510 | const struct ion_handle *handle = rb_entry(n, |
| 1511 | const struct ion_handle, |
| 1512 | node); |
| 1513 | if (handle->buffer == buf) |
| 1514 | return 1; |
| 1515 | } |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | static void ion_debug_mem_map_add(struct rb_root *mem_map, |
| 1520 | struct mem_map_data *data) |
| 1521 | { |
| 1522 | struct rb_node **p = &mem_map->rb_node; |
| 1523 | struct rb_node *parent = NULL; |
| 1524 | struct mem_map_data *entry; |
| 1525 | |
| 1526 | while (*p) { |
| 1527 | parent = *p; |
| 1528 | entry = rb_entry(parent, struct mem_map_data, node); |
| 1529 | |
| 1530 | if (data->addr < entry->addr) { |
| 1531 | p = &(*p)->rb_left; |
| 1532 | } else if (data->addr > entry->addr) { |
| 1533 | p = &(*p)->rb_right; |
| 1534 | } else { |
| 1535 | pr_err("%s: mem_map_data already found.", __func__); |
| 1536 | BUG(); |
| 1537 | } |
| 1538 | } |
| 1539 | rb_link_node(&data->node, parent, p); |
| 1540 | rb_insert_color(&data->node, mem_map); |
| 1541 | } |
| 1542 | |
| 1543 | const char *ion_debug_locate_owner(const struct ion_device *dev, |
| 1544 | const struct ion_buffer *buffer) |
| 1545 | { |
| 1546 | struct rb_node *j; |
| 1547 | const char *client_name = NULL; |
| 1548 | |
| 1549 | for (j = rb_first(&dev->clients); j && !client_name; |
| 1550 | j = rb_next(j)) { |
| 1551 | struct ion_client *client = rb_entry(j, struct ion_client, |
| 1552 | node); |
| 1553 | if (ion_debug_find_buffer_owner(client, buffer)) |
| 1554 | client_name = client->name; |
| 1555 | } |
| 1556 | return client_name; |
| 1557 | } |
| 1558 | |
| 1559 | void ion_debug_mem_map_create(struct seq_file *s, struct ion_heap *heap, |
| 1560 | struct rb_root *mem_map) |
| 1561 | { |
| 1562 | struct ion_device *dev = heap->dev; |
| 1563 | struct rb_node *n; |
| 1564 | |
| 1565 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1566 | struct ion_buffer *buffer = |
| 1567 | rb_entry(n, struct ion_buffer, node); |
| 1568 | if (buffer->heap->id == heap->id) { |
| 1569 | struct mem_map_data *data = |
| 1570 | kzalloc(sizeof(*data), GFP_KERNEL); |
| 1571 | if (!data) { |
| 1572 | seq_printf(s, "ERROR: out of memory. " |
| 1573 | "Part of memory map will not be logged\n"); |
| 1574 | break; |
| 1575 | } |
| 1576 | if (heap->id == ION_IOMMU_HEAP_ID) { |
| 1577 | data->addr = (unsigned long)buffer; |
| 1578 | } else { |
| 1579 | data->addr = buffer->priv_phys; |
| 1580 | data->addr_end = buffer->priv_phys + buffer->size-1; |
| 1581 | } |
| 1582 | data->size = buffer->size; |
| 1583 | data->client_name = ion_debug_locate_owner(dev, buffer); |
| 1584 | |
| 1585 | { |
| 1586 | |
| 1587 | struct rb_node *p = NULL; |
| 1588 | struct ion_client *entry = NULL; |
| 1589 | |
| 1590 | for (p = rb_first(&dev->clients); p && !data->creator_name; |
| 1591 | p = rb_next(p)) { |
| 1592 | entry = rb_entry(p, struct ion_client, node); |
| 1593 | if (entry == buffer->creator) |
| 1594 | data->creator_name = entry->name; |
| 1595 | } |
| 1596 | } |
| 1597 | ion_debug_mem_map_add(mem_map, data); |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | static void ion_debug_mem_map_destroy(struct rb_root *mem_map) |
| 1603 | { |
| 1604 | if (mem_map) { |
| 1605 | struct rb_node *n; |
| 1606 | while ((n = rb_first(mem_map)) != 0) { |
| 1607 | struct mem_map_data *data = |
| 1608 | rb_entry(n, struct mem_map_data, node); |
| 1609 | rb_erase(&data->node, mem_map); |
| 1610 | kfree(data); |
| 1611 | } |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | static void ion_heap_print_debug(struct seq_file *s, struct ion_heap *heap) |
| 1616 | { |
| 1617 | if (heap->ops->print_debug) { |
| 1618 | struct rb_root mem_map = RB_ROOT; |
| 1619 | ion_debug_mem_map_create(s, heap, &mem_map); |
| 1620 | heap->ops->print_debug(heap, s, &mem_map); |
| 1621 | ion_debug_mem_map_destroy(&mem_map); |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | static int ion_debug_heap_show(struct seq_file *s, void *unused) |
| 1626 | { |
| 1627 | struct ion_heap *heap = s->private; |
| 1628 | struct ion_device *dev = heap->dev; |
| 1629 | struct rb_node *n; |
| 1630 | |
| 1631 | mutex_lock(&dev->lock); |
| 1632 | seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size"); |
| 1633 | |
| 1634 | for (n = rb_first(&dev->clients); n; n = rb_next(n)) { |
| 1635 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1636 | node); |
| 1637 | size_t size = ion_debug_heap_total(client, heap->id); |
| 1638 | if (!size) |
| 1639 | continue; |
| 1640 | if (client->task) { |
| 1641 | char task_comm[TASK_COMM_LEN]; |
| 1642 | |
| 1643 | get_task_comm(task_comm, client->task); |
| 1644 | seq_printf(s, "%16.s %16u %16u\n", task_comm, |
| 1645 | client->pid, size); |
| 1646 | } else { |
| 1647 | seq_printf(s, "%16.s %16u %16u\n", client->name, |
| 1648 | client->pid, size); |
| 1649 | } |
| 1650 | } |
| 1651 | ion_heap_print_debug(s, heap); |
| 1652 | mutex_unlock(&dev->lock); |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | static int ion_debug_heap_open(struct inode *inode, struct file *file) |
| 1657 | { |
| 1658 | return single_open(file, ion_debug_heap_show, inode->i_private); |
| 1659 | } |
| 1660 | |
| 1661 | static const struct file_operations debug_heap_fops = { |
| 1662 | .open = ion_debug_heap_open, |
| 1663 | .read = seq_read, |
| 1664 | .llseek = seq_lseek, |
| 1665 | .release = single_release, |
| 1666 | }; |
| 1667 | |
| 1668 | void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap) |
| 1669 | { |
| 1670 | struct rb_node **p = &dev->heaps.rb_node; |
| 1671 | struct rb_node *parent = NULL; |
| 1672 | struct ion_heap *entry; |
| 1673 | |
| 1674 | if (!heap->ops->allocate || !heap->ops->free || !heap->ops->map_dma || |
| 1675 | !heap->ops->unmap_dma) |
| 1676 | pr_err("%s: can not add heap with invalid ops struct.\n", |
| 1677 | __func__); |
| 1678 | |
| 1679 | heap->dev = dev; |
| 1680 | mutex_lock(&dev->lock); |
| 1681 | while (*p) { |
| 1682 | parent = *p; |
| 1683 | entry = rb_entry(parent, struct ion_heap, node); |
| 1684 | |
| 1685 | if (heap->id < entry->id) { |
| 1686 | p = &(*p)->rb_left; |
| 1687 | } else if (heap->id > entry->id ) { |
| 1688 | p = &(*p)->rb_right; |
| 1689 | } else { |
| 1690 | pr_err("%s: can not insert multiple heaps with " |
| 1691 | "id %d\n", __func__, heap->id); |
| 1692 | goto end; |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | rb_link_node(&heap->node, parent, p); |
| 1697 | rb_insert_color(&heap->node, &dev->heaps); |
| 1698 | debugfs_create_file(heap->name, 0664, dev->debug_root, heap, |
| 1699 | &debug_heap_fops); |
| 1700 | end: |
| 1701 | mutex_unlock(&dev->lock); |
| 1702 | } |
| 1703 | |
| 1704 | int ion_secure_heap(struct ion_device *dev, int heap_id, int version, |
| 1705 | void *data) |
| 1706 | { |
| 1707 | struct rb_node *n; |
| 1708 | int ret_val = 0; |
| 1709 | |
| 1710 | mutex_lock(&dev->lock); |
| 1711 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 1712 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
| 1713 | if (heap->type != ION_HEAP_TYPE_CP) |
| 1714 | continue; |
| 1715 | if (ION_HEAP(heap->id) != heap_id) |
| 1716 | continue; |
| 1717 | if (heap->ops->secure_heap) |
| 1718 | ret_val = heap->ops->secure_heap(heap, version, data); |
| 1719 | else |
| 1720 | ret_val = -EINVAL; |
| 1721 | break; |
| 1722 | } |
| 1723 | mutex_unlock(&dev->lock); |
| 1724 | return ret_val; |
| 1725 | } |
| 1726 | EXPORT_SYMBOL(ion_secure_heap); |
| 1727 | |
| 1728 | int ion_unsecure_heap(struct ion_device *dev, int heap_id, int version, |
| 1729 | void *data) |
| 1730 | { |
| 1731 | struct rb_node *n; |
| 1732 | int ret_val = 0; |
| 1733 | |
| 1734 | mutex_lock(&dev->lock); |
| 1735 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 1736 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
| 1737 | if (heap->type != ION_HEAP_TYPE_CP) |
| 1738 | continue; |
| 1739 | if (ION_HEAP(heap->id) != heap_id) |
| 1740 | continue; |
| 1741 | if (heap->ops->secure_heap) |
| 1742 | ret_val = heap->ops->unsecure_heap(heap, version, data); |
| 1743 | else |
| 1744 | ret_val = -EINVAL; |
| 1745 | break; |
| 1746 | } |
| 1747 | mutex_unlock(&dev->lock); |
| 1748 | return ret_val; |
| 1749 | } |
| 1750 | EXPORT_SYMBOL(ion_unsecure_heap); |
| 1751 | |
| 1752 | static int ion_debug_leak_show(struct seq_file *s, void *unused) |
| 1753 | { |
| 1754 | struct ion_device *dev = s->private; |
| 1755 | struct rb_node *n; |
| 1756 | struct rb_node *n2; |
| 1757 | |
| 1758 | |
| 1759 | seq_printf(s, "%16.s %12.s %16.s %16.s %16.s\n", "buffer", "physical", "heap", "size", |
| 1760 | "ref cnt"); |
| 1761 | mutex_lock(&dev->lock); |
| 1762 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1763 | struct ion_buffer *buf = rb_entry(n, struct ion_buffer, |
| 1764 | node); |
| 1765 | |
| 1766 | buf->marked = 1; |
| 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | for (n = rb_first(&dev->clients); n; n = rb_next(n)) { |
| 1771 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1772 | node); |
| 1773 | |
| 1774 | mutex_lock(&client->lock); |
| 1775 | for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) { |
| 1776 | struct ion_handle *handle = rb_entry(n2, |
| 1777 | struct ion_handle, node); |
| 1778 | |
| 1779 | handle->buffer->marked = 0; |
| 1780 | |
| 1781 | } |
| 1782 | mutex_unlock(&client->lock); |
| 1783 | |
| 1784 | } |
| 1785 | |
| 1786 | |
| 1787 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1788 | struct ion_buffer *buf = rb_entry(n, struct ion_buffer, |
| 1789 | node); |
| 1790 | enum ion_heap_type type = buf->heap->type; |
| 1791 | |
| 1792 | if (buf->marked == 1) { |
| 1793 | seq_printf(s, "%16.x", (int)buf); |
| 1794 | |
| 1795 | if (type == ION_HEAP_TYPE_SYSTEM_CONTIG || |
| 1796 | type == ION_HEAP_TYPE_CARVEOUT || |
| 1797 | type == ION_HEAP_TYPE_CP) |
| 1798 | seq_printf(s, " %12lx", buf->priv_phys); |
| 1799 | else |
| 1800 | seq_printf(s, " %12s", "N/A"); |
| 1801 | |
| 1802 | seq_printf(s, " %16.s %16.x %16.d\n", |
| 1803 | buf->heap->name, buf->size, |
| 1804 | atomic_read(&buf->ref.refcount)); |
| 1805 | } |
| 1806 | } |
| 1807 | mutex_unlock(&dev->lock); |
| 1808 | return 0; |
| 1809 | } |
| 1810 | |
| 1811 | static int ion_debug_leak_open(struct inode *inode, struct file *file) |
| 1812 | { |
| 1813 | return single_open(file, ion_debug_leak_show, inode->i_private); |
| 1814 | } |
| 1815 | |
| 1816 | static const struct file_operations debug_leak_fops = { |
| 1817 | .open = ion_debug_leak_open, |
| 1818 | .read = seq_read, |
| 1819 | .llseek = seq_lseek, |
| 1820 | .release = single_release, |
| 1821 | }; |
| 1822 | |
| 1823 | |
| 1824 | |
| 1825 | struct ion_device *ion_device_create(long (*custom_ioctl) |
| 1826 | (struct ion_client *client, |
| 1827 | unsigned int cmd, |
| 1828 | unsigned long arg)) |
| 1829 | { |
| 1830 | struct ion_device *idev; |
| 1831 | int ret; |
| 1832 | |
| 1833 | idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL); |
| 1834 | if (!idev) |
| 1835 | return ERR_PTR(-ENOMEM); |
| 1836 | |
| 1837 | idev->dev.minor = MISC_DYNAMIC_MINOR; |
| 1838 | idev->dev.name = "ion"; |
| 1839 | idev->dev.fops = &ion_fops; |
| 1840 | idev->dev.parent = NULL; |
| 1841 | ret = misc_register(&idev->dev); |
| 1842 | if (ret) { |
| 1843 | pr_err("ion: failed to register misc device.\n"); |
| 1844 | return ERR_PTR(ret); |
| 1845 | } |
| 1846 | |
| 1847 | idev->debug_root = debugfs_create_dir("ion", NULL); |
| 1848 | if (IS_ERR_OR_NULL(idev->debug_root)) |
| 1849 | pr_err("ion: failed to create debug files.\n"); |
| 1850 | |
| 1851 | idev->custom_ioctl = custom_ioctl; |
| 1852 | idev->buffers = RB_ROOT; |
| 1853 | mutex_init(&idev->lock); |
| 1854 | idev->heaps = RB_ROOT; |
| 1855 | idev->clients = RB_ROOT; |
| 1856 | debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev, |
| 1857 | &debug_leak_fops); |
| 1858 | return idev; |
| 1859 | } |
| 1860 | |
| 1861 | void ion_device_destroy(struct ion_device *dev) |
| 1862 | { |
| 1863 | misc_deregister(&dev->dev); |
| 1864 | |
| 1865 | kfree(dev); |
| 1866 | } |
| 1867 | |
| 1868 | void __init ion_reserve(struct ion_platform_data *data) |
| 1869 | { |
| 1870 | int i, ret; |
| 1871 | |
| 1872 | for (i = 0; i < data->nr; i++) { |
| 1873 | if (data->heaps[i].size == 0) |
| 1874 | continue; |
| 1875 | ret = memblock_reserve(data->heaps[i].base, |
| 1876 | data->heaps[i].size); |
| 1877 | if (ret) |
| 1878 | pr_err("memblock reserve of %x@%lx failed\n", |
| 1879 | data->heaps[i].size, |
| 1880 | data->heaps[i].base); |
| 1881 | } |
| 1882 | } |