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