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