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