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