Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1 | /* |
Sriram Raghunathan | 7e41617 | 2015-09-22 22:35:51 +0530 | [diff] [blame] | 2 | * |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 3 | * drivers/staging/android/ion/ion.c |
| 4 | * |
| 5 | * Copyright (C) 2011 Google, Inc. |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/device.h> |
Sachin Kamat | ab0c069 | 2014-01-27 12:17:05 +0530 | [diff] [blame] | 19 | #include <linux/err.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 20 | #include <linux/file.h> |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 21 | #include <linux/freezer.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 22 | #include <linux/fs.h> |
| 23 | #include <linux/anon_inodes.h> |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 25 | #include <linux/list.h> |
Rebecca Schultz Zavin | 2991b7a | 2013-12-13 14:23:38 -0800 | [diff] [blame] | 26 | #include <linux/memblock.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 27 | #include <linux/miscdevice.h> |
| 28 | #include <linux/export.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/mm_types.h> |
| 31 | #include <linux/rbtree.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 32 | #include <linux/slab.h> |
| 33 | #include <linux/seq_file.h> |
| 34 | #include <linux/uaccess.h> |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 35 | #include <linux/vmalloc.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 36 | #include <linux/debugfs.h> |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 37 | #include <linux/dma-buf.h> |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 38 | #include <linux/idr.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 39 | |
| 40 | #include "ion.h" |
| 41 | #include "ion_priv.h" |
Rom Lemarchand | 827c849 | 2013-12-13 14:24:55 -0800 | [diff] [blame] | 42 | #include "compat_ion.h" |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 43 | |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 44 | bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer) |
| 45 | { |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 46 | return (buffer->flags & ION_FLAG_CACHED) && |
| 47 | !(buffer->flags & ION_FLAG_CACHED_NEEDS_SYNC); |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 50 | bool ion_buffer_cached(struct ion_buffer *buffer) |
| 51 | { |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 52 | return !!(buffer->flags & ION_FLAG_CACHED); |
| 53 | } |
| 54 | |
| 55 | static inline struct page *ion_buffer_page(struct page *page) |
| 56 | { |
| 57 | return (struct page *)((unsigned long)page & ~(1UL)); |
| 58 | } |
| 59 | |
| 60 | static inline bool ion_buffer_page_is_dirty(struct page *page) |
| 61 | { |
| 62 | return !!((unsigned long)page & 1UL); |
| 63 | } |
| 64 | |
| 65 | static inline void ion_buffer_page_dirty(struct page **page) |
| 66 | { |
| 67 | *page = (struct page *)((unsigned long)(*page) | 1UL); |
| 68 | } |
| 69 | |
| 70 | static inline void ion_buffer_page_clean(struct page **page) |
| 71 | { |
| 72 | *page = (struct page *)((unsigned long)(*page) & ~(1UL)); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 75 | /* this function should only be called while dev->lock is held */ |
| 76 | static void ion_buffer_add(struct ion_device *dev, |
| 77 | struct ion_buffer *buffer) |
| 78 | { |
| 79 | struct rb_node **p = &dev->buffers.rb_node; |
| 80 | struct rb_node *parent = NULL; |
| 81 | struct ion_buffer *entry; |
| 82 | |
| 83 | while (*p) { |
| 84 | parent = *p; |
| 85 | entry = rb_entry(parent, struct ion_buffer, node); |
| 86 | |
| 87 | if (buffer < entry) { |
| 88 | p = &(*p)->rb_left; |
| 89 | } else if (buffer > entry) { |
| 90 | p = &(*p)->rb_right; |
| 91 | } else { |
| 92 | pr_err("%s: buffer already found.", __func__); |
| 93 | BUG(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | rb_link_node(&buffer->node, parent, p); |
| 98 | rb_insert_color(&buffer->node, &dev->buffers); |
| 99 | } |
| 100 | |
| 101 | /* this function should only be called while dev->lock is held */ |
| 102 | static struct ion_buffer *ion_buffer_create(struct ion_heap *heap, |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 103 | struct ion_device *dev, |
| 104 | unsigned long len, |
| 105 | unsigned long align, |
| 106 | unsigned long flags) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 107 | { |
| 108 | struct ion_buffer *buffer; |
Rebecca Schultz Zavin | 29ae6bc | 2013-12-13 14:23:43 -0800 | [diff] [blame] | 109 | struct sg_table *table; |
Rebecca Schultz Zavin | a46b6b2 | 2013-12-13 14:23:46 -0800 | [diff] [blame] | 110 | struct scatterlist *sg; |
| 111 | int i, ret; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 112 | |
Ben Marsh | 411059f | 2016-03-28 19:26:19 +0200 | [diff] [blame] | 113 | buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 114 | if (!buffer) |
| 115 | return ERR_PTR(-ENOMEM); |
| 116 | |
| 117 | buffer->heap = heap; |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 118 | buffer->flags = flags; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 119 | kref_init(&buffer->ref); |
| 120 | |
| 121 | ret = heap->ops->allocate(heap, buffer, len, align, flags); |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 122 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 123 | if (ret) { |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 124 | if (!(heap->flags & ION_HEAP_FLAG_DEFER_FREE)) |
| 125 | goto err2; |
| 126 | |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 127 | ion_heap_freelist_drain(heap, 0); |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 128 | ret = heap->ops->allocate(heap, buffer, len, align, |
| 129 | flags); |
| 130 | if (ret) |
| 131 | goto err2; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 132 | } |
Rebecca Schultz Zavin | 29ae6bc | 2013-12-13 14:23:43 -0800 | [diff] [blame] | 133 | |
Laura Abbott | f82ad60 | 2016-08-08 09:52:56 -0700 | [diff] [blame] | 134 | if (buffer->sg_table == NULL) { |
| 135 | WARN_ONCE(1, "This heap needs to set the sgtable"); |
Rohit kumar | a56d092 | 2015-09-30 11:07:35 +0530 | [diff] [blame] | 136 | ret = -EINVAL; |
| 137 | goto err1; |
Rebecca Schultz Zavin | 29ae6bc | 2013-12-13 14:23:43 -0800 | [diff] [blame] | 138 | } |
Rohit kumar | a56d092 | 2015-09-30 11:07:35 +0530 | [diff] [blame] | 139 | |
Laura Abbott | f82ad60 | 2016-08-08 09:52:56 -0700 | [diff] [blame] | 140 | table = buffer->sg_table; |
| 141 | buffer->dev = dev; |
| 142 | buffer->size = len; |
| 143 | |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 144 | if (ion_buffer_fault_user_mappings(buffer)) { |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 145 | int num_pages = PAGE_ALIGN(buffer->size) / PAGE_SIZE; |
| 146 | struct scatterlist *sg; |
| 147 | int i, j, k = 0; |
| 148 | |
| 149 | buffer->pages = vmalloc(sizeof(struct page *) * num_pages); |
| 150 | if (!buffer->pages) { |
| 151 | ret = -ENOMEM; |
Laura Abbott | f82ad60 | 2016-08-08 09:52:56 -0700 | [diff] [blame] | 152 | goto err1; |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 153 | } |
Rebecca Schultz Zavin | 29ae6bc | 2013-12-13 14:23:43 -0800 | [diff] [blame] | 154 | |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 155 | for_each_sg(table->sgl, sg, table->nents, i) { |
| 156 | struct page *page = sg_page(sg); |
| 157 | |
Colin Cross | 06e0dca | 2013-12-13 14:25:02 -0800 | [diff] [blame] | 158 | for (j = 0; j < sg->length / PAGE_SIZE; j++) |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 159 | buffer->pages[k++] = page++; |
| 160 | } |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | buffer->dev = dev; |
| 164 | buffer->size = len; |
| 165 | INIT_LIST_HEAD(&buffer->vmas); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 166 | mutex_init(&buffer->lock); |
Sriram Raghunathan | 7e41617 | 2015-09-22 22:35:51 +0530 | [diff] [blame] | 167 | /* |
| 168 | * this will set up dma addresses for the sglist -- it is not |
| 169 | * technically correct as per the dma api -- a specific |
| 170 | * device isn't really taking ownership here. However, in practice on |
| 171 | * our systems the only dma_address space is physical addresses. |
| 172 | * Additionally, we can't afford the overhead of invalidating every |
| 173 | * allocation via dma_map_sg. The implicit contract here is that |
| 174 | * memory coming from the heaps is ready for dma, ie if it has a |
| 175 | * cached mapping that mapping has been invalidated |
| 176 | */ |
Liviu Dudau | 70bc916 | 2016-01-21 11:57:47 +0000 | [diff] [blame] | 177 | for_each_sg(buffer->sg_table->sgl, sg, buffer->sg_table->nents, i) { |
Rebecca Schultz Zavin | a46b6b2 | 2013-12-13 14:23:46 -0800 | [diff] [blame] | 178 | sg_dma_address(sg) = sg_phys(sg); |
Liviu Dudau | 70bc916 | 2016-01-21 11:57:47 +0000 | [diff] [blame] | 179 | sg_dma_len(sg) = sg->length; |
| 180 | } |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 181 | mutex_lock(&dev->buffer_lock); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 182 | ion_buffer_add(dev, buffer); |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 183 | mutex_unlock(&dev->buffer_lock); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 184 | return buffer; |
Rebecca Schultz Zavin | d3c0bce | 2013-12-13 14:24:04 -0800 | [diff] [blame] | 185 | |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 186 | err1: |
Rohit kumar | a56d092 | 2015-09-30 11:07:35 +0530 | [diff] [blame] | 187 | heap->ops->free(buffer); |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 188 | err2: |
Rebecca Schultz Zavin | d3c0bce | 2013-12-13 14:24:04 -0800 | [diff] [blame] | 189 | kfree(buffer); |
| 190 | return ERR_PTR(ret); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 193 | void ion_buffer_destroy(struct ion_buffer *buffer) |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 194 | { |
Laura Abbott | 8da07ee | 2018-05-14 14:35:09 -0700 | [diff] [blame] | 195 | if (buffer->kmap_cnt > 0) { |
| 196 | pr_warn_once("%s: buffer still mapped in the kernel\n", |
| 197 | __func__); |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 198 | buffer->heap->ops->unmap_kernel(buffer->heap, buffer); |
Laura Abbott | 8da07ee | 2018-05-14 14:35:09 -0700 | [diff] [blame] | 199 | } |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 200 | buffer->heap->ops->free(buffer); |
Markus Elfring | 698f140 | 2014-11-23 18:48:15 +0100 | [diff] [blame] | 201 | vfree(buffer->pages); |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 202 | kfree(buffer); |
| 203 | } |
| 204 | |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 205 | static void _ion_buffer_destroy(struct kref *kref) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 206 | { |
| 207 | struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref); |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 208 | struct ion_heap *heap = buffer->heap; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 209 | struct ion_device *dev = buffer->dev; |
| 210 | |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 211 | mutex_lock(&dev->buffer_lock); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 212 | rb_erase(&buffer->node, &dev->buffers); |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 213 | mutex_unlock(&dev->buffer_lock); |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 214 | |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 215 | if (heap->flags & ION_HEAP_FLAG_DEFER_FREE) |
| 216 | ion_heap_freelist_add(heap, buffer); |
| 217 | else |
| 218 | ion_buffer_destroy(buffer); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static void ion_buffer_get(struct ion_buffer *buffer) |
| 222 | { |
| 223 | kref_get(&buffer->ref); |
| 224 | } |
| 225 | |
| 226 | static int ion_buffer_put(struct ion_buffer *buffer) |
| 227 | { |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 228 | return kref_put(&buffer->ref, _ion_buffer_destroy); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 231 | static void ion_buffer_add_to_handle(struct ion_buffer *buffer) |
| 232 | { |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 233 | mutex_lock(&buffer->lock); |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 234 | buffer->handle_count++; |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 235 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static void ion_buffer_remove_from_handle(struct ion_buffer *buffer) |
| 239 | { |
| 240 | /* |
| 241 | * when a buffer is removed from a handle, if it is not in |
| 242 | * any other handles, copy the taskcomm and the pid of the |
| 243 | * process it's being removed from into the buffer. At this |
| 244 | * point there will be no way to track what processes this buffer is |
| 245 | * being used by, it only exists as a dma_buf file descriptor. |
| 246 | * The taskcomm and pid can provide a debug hint as to where this fd |
| 247 | * is in the system |
| 248 | */ |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 249 | mutex_lock(&buffer->lock); |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 250 | buffer->handle_count--; |
| 251 | BUG_ON(buffer->handle_count < 0); |
| 252 | if (!buffer->handle_count) { |
| 253 | struct task_struct *task; |
| 254 | |
| 255 | task = current->group_leader; |
| 256 | get_task_comm(buffer->task_comm, task); |
| 257 | buffer->pid = task_pid_nr(task); |
| 258 | } |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 259 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 262 | static struct ion_handle *ion_handle_create(struct ion_client *client, |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 263 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 264 | { |
| 265 | struct ion_handle *handle; |
| 266 | |
Ben Marsh | 411059f | 2016-03-28 19:26:19 +0200 | [diff] [blame] | 267 | handle = kzalloc(sizeof(*handle), GFP_KERNEL); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 268 | if (!handle) |
| 269 | return ERR_PTR(-ENOMEM); |
| 270 | kref_init(&handle->ref); |
| 271 | RB_CLEAR_NODE(&handle->node); |
| 272 | handle->client = client; |
| 273 | ion_buffer_get(buffer); |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 274 | ion_buffer_add_to_handle(buffer); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 275 | handle->buffer = buffer; |
| 276 | |
| 277 | return handle; |
| 278 | } |
| 279 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 280 | static void ion_handle_kmap_put(struct ion_handle *); |
| 281 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 282 | static void ion_handle_destroy(struct kref *kref) |
| 283 | { |
| 284 | struct ion_handle *handle = container_of(kref, struct ion_handle, ref); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 285 | struct ion_client *client = handle->client; |
| 286 | struct ion_buffer *buffer = handle->buffer; |
| 287 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 288 | mutex_lock(&buffer->lock); |
Laura Abbott | 2900cd7 | 2013-12-13 14:23:48 -0800 | [diff] [blame] | 289 | while (handle->kmap_cnt) |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 290 | ion_handle_kmap_put(handle); |
| 291 | mutex_unlock(&buffer->lock); |
| 292 | |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 293 | idr_remove(&client->idr, handle->id); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 294 | if (!RB_EMPTY_NODE(&handle->node)) |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 295 | rb_erase(&handle->node, &client->handles); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 296 | |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 297 | ion_buffer_remove_from_handle(buffer); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 298 | ion_buffer_put(buffer); |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 299 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 300 | kfree(handle); |
| 301 | } |
| 302 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 303 | static void ion_handle_get(struct ion_handle *handle) |
| 304 | { |
| 305 | kref_get(&handle->ref); |
| 306 | } |
| 307 | |
Laura Abbott | b1fa6d8 | 2016-09-07 11:49:58 -0700 | [diff] [blame] | 308 | int ion_handle_put_nolock(struct ion_handle *handle) |
EunTaik Lee | 9590232 | 2016-02-24 04:38:06 +0000 | [diff] [blame] | 309 | { |
Johanna Abrahamsson | 4505246 | 2016-08-24 00:41:54 +0200 | [diff] [blame] | 310 | return kref_put(&handle->ref, ion_handle_destroy); |
EunTaik Lee | 9590232 | 2016-02-24 04:38:06 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Laura Abbott | b1fa6d8 | 2016-09-07 11:49:58 -0700 | [diff] [blame] | 313 | int ion_handle_put(struct ion_handle *handle) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 314 | { |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 315 | struct ion_client *client = handle->client; |
| 316 | int ret; |
| 317 | |
| 318 | mutex_lock(&client->lock); |
EunTaik Lee | 9590232 | 2016-02-24 04:38:06 +0000 | [diff] [blame] | 319 | ret = ion_handle_put_nolock(handle); |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 320 | mutex_unlock(&client->lock); |
| 321 | |
| 322 | return ret; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static struct ion_handle *ion_handle_lookup(struct ion_client *client, |
| 326 | struct ion_buffer *buffer) |
| 327 | { |
Colin Cross | e1cf368 | 2013-12-13 14:24:51 -0800 | [diff] [blame] | 328 | struct rb_node *n = client->handles.rb_node; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 329 | |
Colin Cross | e1cf368 | 2013-12-13 14:24:51 -0800 | [diff] [blame] | 330 | while (n) { |
| 331 | struct ion_handle *entry = rb_entry(n, struct ion_handle, node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 332 | |
Colin Cross | e1cf368 | 2013-12-13 14:24:51 -0800 | [diff] [blame] | 333 | if (buffer < entry->buffer) |
| 334 | n = n->rb_left; |
| 335 | else if (buffer > entry->buffer) |
| 336 | n = n->rb_right; |
| 337 | else |
| 338 | return entry; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 339 | } |
Colin Cross | 9e90765 | 2013-12-13 14:24:49 -0800 | [diff] [blame] | 340 | return ERR_PTR(-EINVAL); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Laura Abbott | b1fa6d8 | 2016-09-07 11:49:58 -0700 | [diff] [blame] | 343 | struct ion_handle *ion_handle_get_by_id_nolock(struct ion_client *client, |
| 344 | int id) |
EunTaik Lee | 9590232 | 2016-02-24 04:38:06 +0000 | [diff] [blame] | 345 | { |
| 346 | struct ion_handle *handle; |
| 347 | |
| 348 | handle = idr_find(&client->idr, id); |
| 349 | if (handle) |
| 350 | ion_handle_get(handle); |
| 351 | |
| 352 | return handle ? handle : ERR_PTR(-EINVAL); |
| 353 | } |
| 354 | |
Laura Abbott | b1fa6d8 | 2016-09-07 11:49:58 -0700 | [diff] [blame] | 355 | struct ion_handle *ion_handle_get_by_id(struct ion_client *client, |
Markus Böhme | 0045c8d | 2016-04-06 23:53:39 +0200 | [diff] [blame] | 356 | int id) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 357 | { |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 358 | struct ion_handle *handle; |
| 359 | |
| 360 | mutex_lock(&client->lock); |
EunTaik Lee | 9590232 | 2016-02-24 04:38:06 +0000 | [diff] [blame] | 361 | handle = ion_handle_get_by_id_nolock(client, id); |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 362 | mutex_unlock(&client->lock); |
| 363 | |
EunTaik Lee | 9590232 | 2016-02-24 04:38:06 +0000 | [diff] [blame] | 364 | return handle; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 365 | } |
| 366 | |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 367 | static bool ion_handle_validate(struct ion_client *client, |
| 368 | struct ion_handle *handle) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 369 | { |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 370 | WARN_ON(!mutex_is_locked(&client->lock)); |
Daeseok Youn | 5110898 | 2014-02-10 20:16:50 +0900 | [diff] [blame] | 371 | return idr_find(&client->idr, handle->id) == handle; |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static int ion_handle_add(struct ion_client *client, struct ion_handle *handle) |
| 375 | { |
Colin Cross | b26661d | 2013-12-13 14:25:05 -0800 | [diff] [blame] | 376 | int id; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 377 | struct rb_node **p = &client->handles.rb_node; |
| 378 | struct rb_node *parent = NULL; |
| 379 | struct ion_handle *entry; |
| 380 | |
Colin Cross | b26661d | 2013-12-13 14:25:05 -0800 | [diff] [blame] | 381 | id = idr_alloc(&client->idr, handle, 1, 0, GFP_KERNEL); |
| 382 | if (id < 0) |
| 383 | return id; |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 384 | |
Colin Cross | b26661d | 2013-12-13 14:25:05 -0800 | [diff] [blame] | 385 | handle->id = id; |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 386 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 387 | while (*p) { |
| 388 | parent = *p; |
| 389 | entry = rb_entry(parent, struct ion_handle, node); |
| 390 | |
Colin Cross | e1cf368 | 2013-12-13 14:24:51 -0800 | [diff] [blame] | 391 | if (handle->buffer < entry->buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 392 | p = &(*p)->rb_left; |
Colin Cross | e1cf368 | 2013-12-13 14:24:51 -0800 | [diff] [blame] | 393 | else if (handle->buffer > entry->buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 394 | p = &(*p)->rb_right; |
| 395 | else |
| 396 | WARN(1, "%s: buffer already found.", __func__); |
| 397 | } |
| 398 | |
| 399 | rb_link_node(&handle->node, parent, p); |
| 400 | rb_insert_color(&handle->node, &client->handles); |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 401 | |
| 402 | return 0; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | struct ion_handle *ion_alloc(struct ion_client *client, size_t len, |
Rebecca Schultz Zavin | 38eeeb5 | 2013-12-13 14:24:28 -0800 | [diff] [blame] | 406 | size_t align, unsigned int heap_id_mask, |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 407 | unsigned int flags) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 408 | { |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 409 | struct ion_handle *handle; |
| 410 | struct ion_device *dev = client->dev; |
| 411 | struct ion_buffer *buffer = NULL; |
Rebecca Schultz Zavin | cd69488 | 2013-12-13 14:24:25 -0800 | [diff] [blame] | 412 | struct ion_heap *heap; |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 413 | int ret; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 414 | |
Colin Cross | e61fc91 | 2013-12-13 19:26:14 -0800 | [diff] [blame] | 415 | pr_debug("%s: len %zu align %zu heap_id_mask %u flags %x\n", __func__, |
Rebecca Schultz Zavin | 38eeeb5 | 2013-12-13 14:24:28 -0800 | [diff] [blame] | 416 | len, align, heap_id_mask, flags); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 417 | /* |
| 418 | * traverse the list of heaps available in this system in priority |
| 419 | * order. If the heap type is supported by the client, and matches the |
| 420 | * request of the caller allocate from it. Repeat until allocate has |
| 421 | * succeeded or all heaps have been tried |
| 422 | */ |
KyongHo Cho | 54ac0784 | 2013-12-13 14:23:39 -0800 | [diff] [blame] | 423 | len = PAGE_ALIGN(len); |
| 424 | |
Colin Cross | a14baf7 | 2013-12-13 14:25:00 -0800 | [diff] [blame] | 425 | if (!len) |
| 426 | return ERR_PTR(-EINVAL); |
| 427 | |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 428 | down_read(&dev->lock); |
Rebecca Schultz Zavin | cd69488 | 2013-12-13 14:24:25 -0800 | [diff] [blame] | 429 | plist_for_each_entry(heap, &dev->heaps, node) { |
Rebecca Schultz Zavin | 38eeeb5 | 2013-12-13 14:24:28 -0800 | [diff] [blame] | 430 | /* if the caller didn't specify this heap id */ |
| 431 | if (!((1 << heap->id) & heap_id_mask)) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 432 | continue; |
| 433 | buffer = ion_buffer_create(heap, dev, len, align, flags); |
Colin Cross | 9e90765 | 2013-12-13 14:24:49 -0800 | [diff] [blame] | 434 | if (!IS_ERR(buffer)) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 435 | break; |
| 436 | } |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 437 | up_read(&dev->lock); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 438 | |
KyongHo Cho | 54ac0784 | 2013-12-13 14:23:39 -0800 | [diff] [blame] | 439 | if (buffer == NULL) |
| 440 | return ERR_PTR(-ENODEV); |
| 441 | |
| 442 | if (IS_ERR(buffer)) |
Iulia Manda | 464a502 | 2014-03-11 20:14:36 +0200 | [diff] [blame] | 443 | return ERR_CAST(buffer); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 444 | |
| 445 | handle = ion_handle_create(client, buffer); |
| 446 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 447 | /* |
| 448 | * ion_buffer_create will create a buffer with a ref_cnt of 1, |
| 449 | * and ion_handle_create will take a second reference, drop one here |
| 450 | */ |
| 451 | ion_buffer_put(buffer); |
| 452 | |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 453 | if (IS_ERR(handle)) |
| 454 | return handle; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 455 | |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 456 | mutex_lock(&client->lock); |
| 457 | ret = ion_handle_add(client, handle); |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 458 | mutex_unlock(&client->lock); |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 459 | if (ret) { |
| 460 | ion_handle_put(handle); |
| 461 | handle = ERR_PTR(ret); |
| 462 | } |
Rebecca Schultz Zavin | 29ae6bc | 2013-12-13 14:23:43 -0800 | [diff] [blame] | 463 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 464 | return handle; |
| 465 | } |
Olav Haugan | ee4c8aa | 2013-12-13 14:23:55 -0800 | [diff] [blame] | 466 | EXPORT_SYMBOL(ion_alloc); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 467 | |
Laura Abbott | b1fa6d8 | 2016-09-07 11:49:58 -0700 | [diff] [blame] | 468 | void ion_free_nolock(struct ion_client *client, |
| 469 | struct ion_handle *handle) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 470 | { |
Johanna Abrahamsson | c2bbedf | 2016-08-24 00:02:45 +0200 | [diff] [blame] | 471 | if (!ion_handle_validate(client, handle)) { |
Olav Haugan | a9bb075 | 2013-12-13 14:23:54 -0800 | [diff] [blame] | 472 | WARN(1, "%s: invalid handle passed to free.\n", __func__); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 473 | return; |
| 474 | } |
EunTaik Lee | 9590232 | 2016-02-24 04:38:06 +0000 | [diff] [blame] | 475 | ion_handle_put_nolock(handle); |
| 476 | } |
| 477 | |
| 478 | void ion_free(struct ion_client *client, struct ion_handle *handle) |
| 479 | { |
| 480 | BUG_ON(client != handle->client); |
| 481 | |
| 482 | mutex_lock(&client->lock); |
| 483 | ion_free_nolock(client, handle); |
Rebecca Schultz Zavin | 0e9c03a | 2013-12-13 14:24:01 -0800 | [diff] [blame] | 484 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 485 | } |
Olav Haugan | ee4c8aa | 2013-12-13 14:23:55 -0800 | [diff] [blame] | 486 | EXPORT_SYMBOL(ion_free); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 487 | |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 488 | static void *ion_buffer_kmap_get(struct ion_buffer *buffer) |
| 489 | { |
| 490 | void *vaddr; |
| 491 | |
| 492 | if (buffer->kmap_cnt) { |
| 493 | buffer->kmap_cnt++; |
| 494 | return buffer->vaddr; |
| 495 | } |
| 496 | vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer); |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 497 | if (WARN_ONCE(vaddr == NULL, |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 498 | "heap->ops->map_kernel should return ERR_PTR on error")) |
Colin Cross | 9e90765 | 2013-12-13 14:24:49 -0800 | [diff] [blame] | 499 | return ERR_PTR(-EINVAL); |
| 500 | if (IS_ERR(vaddr)) |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 501 | return vaddr; |
| 502 | buffer->vaddr = vaddr; |
| 503 | buffer->kmap_cnt++; |
| 504 | return vaddr; |
| 505 | } |
| 506 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 507 | static void *ion_handle_kmap_get(struct ion_handle *handle) |
| 508 | { |
| 509 | struct ion_buffer *buffer = handle->buffer; |
| 510 | void *vaddr; |
| 511 | |
| 512 | if (handle->kmap_cnt) { |
| 513 | handle->kmap_cnt++; |
| 514 | return buffer->vaddr; |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 515 | } |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 516 | vaddr = ion_buffer_kmap_get(buffer); |
Colin Cross | 9e90765 | 2013-12-13 14:24:49 -0800 | [diff] [blame] | 517 | if (IS_ERR(vaddr)) |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 518 | return vaddr; |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 519 | handle->kmap_cnt++; |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 520 | return vaddr; |
| 521 | } |
| 522 | |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 523 | static void ion_buffer_kmap_put(struct ion_buffer *buffer) |
| 524 | { |
| 525 | buffer->kmap_cnt--; |
| 526 | if (!buffer->kmap_cnt) { |
| 527 | buffer->heap->ops->unmap_kernel(buffer->heap, buffer); |
| 528 | buffer->vaddr = NULL; |
| 529 | } |
| 530 | } |
| 531 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 532 | static void ion_handle_kmap_put(struct ion_handle *handle) |
| 533 | { |
| 534 | struct ion_buffer *buffer = handle->buffer; |
| 535 | |
Mitchel Humpherys | 22f6b97 | 2014-05-23 13:01:22 -0700 | [diff] [blame] | 536 | if (!handle->kmap_cnt) { |
| 537 | WARN(1, "%s: Double unmap detected! bailing...\n", __func__); |
| 538 | return; |
| 539 | } |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 540 | handle->kmap_cnt--; |
| 541 | if (!handle->kmap_cnt) |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 542 | ion_buffer_kmap_put(buffer); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 545 | void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle) |
| 546 | { |
| 547 | struct ion_buffer *buffer; |
| 548 | void *vaddr; |
| 549 | |
| 550 | mutex_lock(&client->lock); |
| 551 | if (!ion_handle_validate(client, handle)) { |
| 552 | pr_err("%s: invalid handle passed to map_kernel.\n", |
| 553 | __func__); |
| 554 | mutex_unlock(&client->lock); |
| 555 | return ERR_PTR(-EINVAL); |
| 556 | } |
| 557 | |
| 558 | buffer = handle->buffer; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 559 | |
| 560 | if (!handle->buffer->heap->ops->map_kernel) { |
| 561 | pr_err("%s: map_kernel is not implemented by this heap.\n", |
| 562 | __func__); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 563 | mutex_unlock(&client->lock); |
| 564 | return ERR_PTR(-ENODEV); |
| 565 | } |
| 566 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 567 | mutex_lock(&buffer->lock); |
| 568 | vaddr = ion_handle_kmap_get(handle); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 569 | mutex_unlock(&buffer->lock); |
| 570 | mutex_unlock(&client->lock); |
| 571 | return vaddr; |
| 572 | } |
Olav Haugan | ee4c8aa | 2013-12-13 14:23:55 -0800 | [diff] [blame] | 573 | EXPORT_SYMBOL(ion_map_kernel); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 574 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 575 | void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle) |
| 576 | { |
| 577 | struct ion_buffer *buffer; |
| 578 | |
| 579 | mutex_lock(&client->lock); |
| 580 | buffer = handle->buffer; |
| 581 | mutex_lock(&buffer->lock); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 582 | ion_handle_kmap_put(handle); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 583 | mutex_unlock(&buffer->lock); |
| 584 | mutex_unlock(&client->lock); |
| 585 | } |
Olav Haugan | ee4c8aa | 2013-12-13 14:23:55 -0800 | [diff] [blame] | 586 | EXPORT_SYMBOL(ion_unmap_kernel); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 587 | |
Neil Zhang | 948c4db | 2016-01-26 17:39:06 +0800 | [diff] [blame] | 588 | static struct mutex debugfs_mutex; |
| 589 | static struct rb_root *ion_root_client; |
| 590 | static int is_client_alive(struct ion_client *client) |
| 591 | { |
| 592 | struct rb_node *node; |
| 593 | struct ion_client *tmp; |
| 594 | struct ion_device *dev; |
| 595 | |
| 596 | node = ion_root_client->rb_node; |
| 597 | dev = container_of(ion_root_client, struct ion_device, clients); |
| 598 | |
| 599 | down_read(&dev->lock); |
| 600 | while (node) { |
| 601 | tmp = rb_entry(node, struct ion_client, node); |
| 602 | if (client < tmp) { |
| 603 | node = node->rb_left; |
| 604 | } else if (client > tmp) { |
| 605 | node = node->rb_right; |
| 606 | } else { |
| 607 | up_read(&dev->lock); |
| 608 | return 1; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | up_read(&dev->lock); |
| 613 | return 0; |
| 614 | } |
| 615 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 616 | static int ion_debug_client_show(struct seq_file *s, void *unused) |
| 617 | { |
| 618 | struct ion_client *client = s->private; |
| 619 | struct rb_node *n; |
Rebecca Schultz Zavin | 38eeeb5 | 2013-12-13 14:24:28 -0800 | [diff] [blame] | 620 | size_t sizes[ION_NUM_HEAP_IDS] = {0}; |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 621 | const char *names[ION_NUM_HEAP_IDS] = {NULL}; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 622 | int i; |
| 623 | |
Neil Zhang | 948c4db | 2016-01-26 17:39:06 +0800 | [diff] [blame] | 624 | mutex_lock(&debugfs_mutex); |
| 625 | if (!is_client_alive(client)) { |
Alistair Strachan | 7cc0d0f | 2018-04-25 22:11:01 -0700 | [diff] [blame] | 626 | seq_printf(s, "ion_client 0x%pK dead, can't dump its buffers\n", |
Neil Zhang | 948c4db | 2016-01-26 17:39:06 +0800 | [diff] [blame] | 627 | client); |
| 628 | mutex_unlock(&debugfs_mutex); |
| 629 | return 0; |
| 630 | } |
| 631 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 632 | mutex_lock(&client->lock); |
| 633 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 634 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 635 | node); |
Rebecca Schultz Zavin | 38eeeb5 | 2013-12-13 14:24:28 -0800 | [diff] [blame] | 636 | unsigned int id = handle->buffer->heap->id; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 637 | |
Rebecca Schultz Zavin | 38eeeb5 | 2013-12-13 14:24:28 -0800 | [diff] [blame] | 638 | if (!names[id]) |
| 639 | names[id] = handle->buffer->heap->name; |
| 640 | sizes[id] += handle->buffer->size; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 641 | } |
| 642 | mutex_unlock(&client->lock); |
Neil Zhang | 948c4db | 2016-01-26 17:39:06 +0800 | [diff] [blame] | 643 | mutex_unlock(&debugfs_mutex); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 644 | |
| 645 | seq_printf(s, "%16.16s: %16.16s\n", "heap_name", "size_in_bytes"); |
Rebecca Schultz Zavin | 38eeeb5 | 2013-12-13 14:24:28 -0800 | [diff] [blame] | 646 | for (i = 0; i < ION_NUM_HEAP_IDS; i++) { |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 647 | if (!names[i]) |
| 648 | continue; |
Colin Cross | e61fc91 | 2013-12-13 19:26:14 -0800 | [diff] [blame] | 649 | seq_printf(s, "%16.16s: %16zu\n", names[i], sizes[i]); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 650 | } |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | static int ion_debug_client_open(struct inode *inode, struct file *file) |
| 655 | { |
| 656 | return single_open(file, ion_debug_client_show, inode->i_private); |
| 657 | } |
| 658 | |
| 659 | static const struct file_operations debug_client_fops = { |
| 660 | .open = ion_debug_client_open, |
| 661 | .read = seq_read, |
| 662 | .llseek = seq_lseek, |
| 663 | .release = single_release, |
| 664 | }; |
| 665 | |
Mitchel Humpherys | 2803ac7 | 2014-02-17 13:58:37 -0800 | [diff] [blame] | 666 | static int ion_get_client_serial(const struct rb_root *root, |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 667 | const unsigned char *name) |
Mitchel Humpherys | 2803ac7 | 2014-02-17 13:58:37 -0800 | [diff] [blame] | 668 | { |
| 669 | int serial = -1; |
| 670 | struct rb_node *node; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 671 | |
Mitchel Humpherys | 2803ac7 | 2014-02-17 13:58:37 -0800 | [diff] [blame] | 672 | for (node = rb_first(root); node; node = rb_next(node)) { |
| 673 | struct ion_client *client = rb_entry(node, struct ion_client, |
Didik Setiawan | b2bcdad | 2016-08-24 16:08:01 +0700 | [diff] [blame] | 674 | node); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 675 | |
Mitchel Humpherys | 2803ac7 | 2014-02-17 13:58:37 -0800 | [diff] [blame] | 676 | if (strcmp(client->name, name)) |
| 677 | continue; |
| 678 | serial = max(serial, client->display_serial); |
| 679 | } |
| 680 | return serial + 1; |
| 681 | } |
| 682 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 683 | struct ion_client *ion_client_create(struct ion_device *dev, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 684 | const char *name) |
| 685 | { |
| 686 | struct ion_client *client; |
| 687 | struct task_struct *task; |
| 688 | struct rb_node **p; |
| 689 | struct rb_node *parent = NULL; |
| 690 | struct ion_client *entry; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 691 | pid_t pid; |
| 692 | |
Mitchel Humpherys | 2803ac7 | 2014-02-17 13:58:37 -0800 | [diff] [blame] | 693 | if (!name) { |
| 694 | pr_err("%s: Name cannot be null\n", __func__); |
| 695 | return ERR_PTR(-EINVAL); |
| 696 | } |
| 697 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 698 | get_task_struct(current->group_leader); |
| 699 | task_lock(current->group_leader); |
| 700 | pid = task_pid_nr(current->group_leader); |
Sriram Raghunathan | 7e41617 | 2015-09-22 22:35:51 +0530 | [diff] [blame] | 701 | /* |
| 702 | * don't bother to store task struct for kernel threads, |
| 703 | * they can't be killed anyway |
| 704 | */ |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 705 | if (current->group_leader->flags & PF_KTHREAD) { |
| 706 | put_task_struct(current->group_leader); |
| 707 | task = NULL; |
| 708 | } else { |
| 709 | task = current->group_leader; |
| 710 | } |
| 711 | task_unlock(current->group_leader); |
| 712 | |
Ben Marsh | 411059f | 2016-03-28 19:26:19 +0200 | [diff] [blame] | 713 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
Mitchel Humpherys | ae5cbf4 | 2014-02-17 13:58:36 -0800 | [diff] [blame] | 714 | if (!client) |
| 715 | goto err_put_task_struct; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 716 | |
| 717 | client->dev = dev; |
| 718 | client->handles = RB_ROOT; |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 719 | idr_init(&client->idr); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 720 | mutex_init(&client->lock); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 721 | client->task = task; |
| 722 | client->pid = pid; |
Mitchel Humpherys | ae5cbf4 | 2014-02-17 13:58:36 -0800 | [diff] [blame] | 723 | client->name = kstrdup(name, GFP_KERNEL); |
| 724 | if (!client->name) |
| 725 | goto err_free_client; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 726 | |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 727 | down_write(&dev->lock); |
Mitchel Humpherys | 2803ac7 | 2014-02-17 13:58:37 -0800 | [diff] [blame] | 728 | client->display_serial = ion_get_client_serial(&dev->clients, name); |
| 729 | client->display_name = kasprintf( |
| 730 | GFP_KERNEL, "%s-%d", name, client->display_serial); |
| 731 | if (!client->display_name) { |
| 732 | up_write(&dev->lock); |
| 733 | goto err_free_client_name; |
| 734 | } |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 735 | p = &dev->clients.rb_node; |
| 736 | while (*p) { |
| 737 | parent = *p; |
| 738 | entry = rb_entry(parent, struct ion_client, node); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 739 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 740 | if (client < entry) |
| 741 | p = &(*p)->rb_left; |
| 742 | else if (client > entry) |
| 743 | p = &(*p)->rb_right; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 744 | } |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 745 | rb_link_node(&client->node, parent, p); |
| 746 | rb_insert_color(&client->node, &dev->clients); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 747 | |
Mitchel Humpherys | 2803ac7 | 2014-02-17 13:58:37 -0800 | [diff] [blame] | 748 | client->debug_root = debugfs_create_file(client->display_name, 0664, |
Didik Setiawan | b2bcdad | 2016-08-24 16:08:01 +0700 | [diff] [blame] | 749 | dev->clients_debug_root, |
| 750 | client, &debug_client_fops); |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 751 | if (!client->debug_root) { |
| 752 | char buf[256], *path; |
Phong Tran | 04e1435 | 2014-08-13 20:37:05 +0700 | [diff] [blame] | 753 | |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 754 | path = dentry_path(dev->clients_debug_root, buf, 256); |
| 755 | pr_err("Failed to create client debugfs at %s/%s\n", |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 756 | path, client->display_name); |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 759 | up_write(&dev->lock); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 760 | |
| 761 | return client; |
Mitchel Humpherys | ae5cbf4 | 2014-02-17 13:58:36 -0800 | [diff] [blame] | 762 | |
Mitchel Humpherys | 2803ac7 | 2014-02-17 13:58:37 -0800 | [diff] [blame] | 763 | err_free_client_name: |
| 764 | kfree(client->name); |
Mitchel Humpherys | ae5cbf4 | 2014-02-17 13:58:36 -0800 | [diff] [blame] | 765 | err_free_client: |
| 766 | kfree(client); |
| 767 | err_put_task_struct: |
| 768 | if (task) |
| 769 | put_task_struct(current->group_leader); |
| 770 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 771 | } |
Johan Mossberg | 9122fe8 | 2013-12-13 14:24:29 -0800 | [diff] [blame] | 772 | EXPORT_SYMBOL(ion_client_create); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 773 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 774 | void ion_client_destroy(struct ion_client *client) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 775 | { |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 776 | struct ion_device *dev = client->dev; |
| 777 | struct rb_node *n; |
| 778 | |
Neil Zhang | 948c4db | 2016-01-26 17:39:06 +0800 | [diff] [blame] | 779 | mutex_lock(&debugfs_mutex); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 780 | while ((n = rb_first(&client->handles))) { |
| 781 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 782 | node); |
| 783 | ion_handle_destroy(&handle->ref); |
| 784 | } |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 785 | |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 786 | idr_destroy(&client->idr); |
| 787 | |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 788 | down_write(&dev->lock); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 789 | if (client->task) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 790 | put_task_struct(client->task); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 791 | rb_erase(&client->node, &dev->clients); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 792 | debugfs_remove_recursive(client->debug_root); |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 793 | up_write(&dev->lock); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 794 | |
Mitchel Humpherys | 2803ac7 | 2014-02-17 13:58:37 -0800 | [diff] [blame] | 795 | kfree(client->display_name); |
Mitchel Humpherys | ae5cbf4 | 2014-02-17 13:58:36 -0800 | [diff] [blame] | 796 | kfree(client->name); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 797 | kfree(client); |
Neil Zhang | 948c4db | 2016-01-26 17:39:06 +0800 | [diff] [blame] | 798 | mutex_unlock(&debugfs_mutex); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 799 | } |
Olav Haugan | ee4c8aa | 2013-12-13 14:23:55 -0800 | [diff] [blame] | 800 | EXPORT_SYMBOL(ion_client_destroy); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 801 | |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 802 | static void ion_buffer_sync_for_device(struct ion_buffer *buffer, |
| 803 | struct device *dev, |
| 804 | enum dma_data_direction direction); |
| 805 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 806 | static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment, |
| 807 | enum dma_data_direction direction) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 808 | { |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 809 | struct dma_buf *dmabuf = attachment->dmabuf; |
| 810 | struct ion_buffer *buffer = dmabuf->priv; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 811 | |
Rebecca Schultz Zavin | 0b9ec1c | 2013-12-13 14:23:52 -0800 | [diff] [blame] | 812 | ion_buffer_sync_for_device(buffer, attachment->dev, direction); |
Rebecca Schultz Zavin | 29ae6bc | 2013-12-13 14:23:43 -0800 | [diff] [blame] | 813 | return buffer->sg_table; |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment, |
| 817 | struct sg_table *table, |
| 818 | enum dma_data_direction direction) |
| 819 | { |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Colin Cross | e946b20 | 2013-12-13 14:25:01 -0800 | [diff] [blame] | 822 | void ion_pages_sync_for_device(struct device *dev, struct page *page, |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 823 | size_t size, enum dma_data_direction dir) |
Colin Cross | e946b20 | 2013-12-13 14:25:01 -0800 | [diff] [blame] | 824 | { |
| 825 | struct scatterlist sg; |
| 826 | |
| 827 | sg_init_table(&sg, 1); |
| 828 | sg_set_page(&sg, page, size, 0); |
| 829 | /* |
| 830 | * This is not correct - sg_dma_address needs a dma_addr_t that is valid |
Tapasweni Pathak | 8e4ec4f | 2014-10-06 11:26:39 +0530 | [diff] [blame] | 831 | * for the targeted device, but this works on the currently targeted |
Colin Cross | e946b20 | 2013-12-13 14:25:01 -0800 | [diff] [blame] | 832 | * hardware. |
| 833 | */ |
| 834 | sg_dma_address(&sg) = page_to_phys(page); |
| 835 | dma_sync_sg_for_device(dev, &sg, 1, dir); |
| 836 | } |
| 837 | |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 838 | struct ion_vma_list { |
| 839 | struct list_head list; |
| 840 | struct vm_area_struct *vma; |
| 841 | }; |
| 842 | |
| 843 | static void ion_buffer_sync_for_device(struct ion_buffer *buffer, |
| 844 | struct device *dev, |
| 845 | enum dma_data_direction dir) |
| 846 | { |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 847 | struct ion_vma_list *vma_list; |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 848 | int pages = PAGE_ALIGN(buffer->size) / PAGE_SIZE; |
| 849 | int i; |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 850 | |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 851 | if (!ion_buffer_fault_user_mappings(buffer)) |
Rebecca Schultz Zavin | 0b9ec1c | 2013-12-13 14:23:52 -0800 | [diff] [blame] | 852 | return; |
| 853 | |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 854 | mutex_lock(&buffer->lock); |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 855 | for (i = 0; i < pages; i++) { |
| 856 | struct page *page = buffer->pages[i]; |
| 857 | |
| 858 | if (ion_buffer_page_is_dirty(page)) |
Colin Cross | e946b20 | 2013-12-13 14:25:01 -0800 | [diff] [blame] | 859 | ion_pages_sync_for_device(dev, ion_buffer_page(page), |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 860 | PAGE_SIZE, dir); |
Colin Cross | e946b20 | 2013-12-13 14:25:01 -0800 | [diff] [blame] | 861 | |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 862 | ion_buffer_page_clean(buffer->pages + i); |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 863 | } |
| 864 | list_for_each_entry(vma_list, &buffer->vmas, list) { |
| 865 | struct vm_area_struct *vma = vma_list->vma; |
| 866 | |
| 867 | zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, |
| 868 | NULL); |
| 869 | } |
| 870 | mutex_unlock(&buffer->lock); |
| 871 | } |
| 872 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 873 | static int ion_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 874 | { |
| 875 | struct ion_buffer *buffer = vma->vm_private_data; |
Colin Cross | 462be0c6 | 2013-12-13 19:26:24 -0800 | [diff] [blame] | 876 | unsigned long pfn; |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 877 | int ret; |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 878 | |
| 879 | mutex_lock(&buffer->lock); |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 880 | ion_buffer_page_dirty(buffer->pages + vmf->pgoff); |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 881 | BUG_ON(!buffer->pages || !buffer->pages[vmf->pgoff]); |
Colin Cross | 462be0c6 | 2013-12-13 19:26:24 -0800 | [diff] [blame] | 882 | |
| 883 | pfn = page_to_pfn(ion_buffer_page(buffer->pages[vmf->pgoff])); |
| 884 | ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 885 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 886 | if (ret) |
| 887 | return VM_FAULT_ERROR; |
| 888 | |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 889 | return VM_FAULT_NOPAGE; |
| 890 | } |
| 891 | |
| 892 | static void ion_vm_open(struct vm_area_struct *vma) |
| 893 | { |
| 894 | struct ion_buffer *buffer = vma->vm_private_data; |
| 895 | struct ion_vma_list *vma_list; |
| 896 | |
Ben Marsh | 411059f | 2016-03-28 19:26:19 +0200 | [diff] [blame] | 897 | vma_list = kmalloc(sizeof(*vma_list), GFP_KERNEL); |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 898 | if (!vma_list) |
| 899 | return; |
| 900 | vma_list->vma = vma; |
| 901 | mutex_lock(&buffer->lock); |
| 902 | list_add(&vma_list->list, &buffer->vmas); |
| 903 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static void ion_vm_close(struct vm_area_struct *vma) |
| 907 | { |
| 908 | struct ion_buffer *buffer = vma->vm_private_data; |
| 909 | struct ion_vma_list *vma_list, *tmp; |
| 910 | |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 911 | mutex_lock(&buffer->lock); |
| 912 | list_for_each_entry_safe(vma_list, tmp, &buffer->vmas, list) { |
| 913 | if (vma_list->vma != vma) |
| 914 | continue; |
| 915 | list_del(&vma_list->list); |
| 916 | kfree(vma_list); |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 917 | break; |
| 918 | } |
| 919 | mutex_unlock(&buffer->lock); |
| 920 | } |
| 921 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 922 | static const struct vm_operations_struct ion_vma_ops = { |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 923 | .open = ion_vm_open, |
| 924 | .close = ion_vm_close, |
| 925 | .fault = ion_vm_fault, |
| 926 | }; |
| 927 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 928 | static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 929 | { |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 930 | struct ion_buffer *buffer = dmabuf->priv; |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 931 | int ret = 0; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 932 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 933 | if (!buffer->heap->ops->map_user) { |
Iulia Manda | 7287bb5 | 2014-03-11 20:10:37 +0200 | [diff] [blame] | 934 | pr_err("%s: this heap does not define a method for mapping to userspace\n", |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 935 | __func__); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 936 | return -EINVAL; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 937 | } |
| 938 | |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 939 | if (ion_buffer_fault_user_mappings(buffer)) { |
Colin Cross | 462be0c6 | 2013-12-13 19:26:24 -0800 | [diff] [blame] | 940 | vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | |
| 941 | VM_DONTDUMP; |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 942 | vma->vm_private_data = buffer; |
| 943 | vma->vm_ops = &ion_vma_ops; |
| 944 | ion_vm_open(vma); |
Rebecca Schultz Zavin | 856661d | 2013-12-13 14:24:05 -0800 | [diff] [blame] | 945 | return 0; |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 946 | } |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 947 | |
Rebecca Schultz Zavin | 856661d | 2013-12-13 14:24:05 -0800 | [diff] [blame] | 948 | if (!(buffer->flags & ION_FLAG_CACHED)) |
| 949 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); |
| 950 | |
| 951 | mutex_lock(&buffer->lock); |
| 952 | /* now map it to userspace */ |
| 953 | ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma); |
| 954 | mutex_unlock(&buffer->lock); |
| 955 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 956 | if (ret) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 957 | pr_err("%s: failure mapping buffer to userspace\n", |
| 958 | __func__); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 959 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 960 | return ret; |
| 961 | } |
| 962 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 963 | static void ion_dma_buf_release(struct dma_buf *dmabuf) |
| 964 | { |
| 965 | struct ion_buffer *buffer = dmabuf->priv; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 966 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 967 | ion_buffer_put(buffer); |
| 968 | } |
| 969 | |
| 970 | static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset) |
| 971 | { |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 972 | struct ion_buffer *buffer = dmabuf->priv; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 973 | |
Greg Hackmann | 12edf53 | 2013-12-13 14:24:00 -0800 | [diff] [blame] | 974 | return buffer->vaddr + offset * PAGE_SIZE; |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset, |
| 978 | void *ptr) |
| 979 | { |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 980 | } |
| 981 | |
Tiago Vignatti | 831e9da | 2015-12-22 19:36:45 -0200 | [diff] [blame] | 982 | static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 983 | enum dma_data_direction direction) |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 984 | { |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 985 | struct ion_buffer *buffer = dmabuf->priv; |
| 986 | void *vaddr; |
| 987 | |
| 988 | if (!buffer->heap->ops->map_kernel) { |
| 989 | pr_err("%s: map kernel is not implemented by this heap.\n", |
| 990 | __func__); |
| 991 | return -ENODEV; |
| 992 | } |
| 993 | |
| 994 | mutex_lock(&buffer->lock); |
| 995 | vaddr = ion_buffer_kmap_get(buffer); |
| 996 | mutex_unlock(&buffer->lock); |
Sachin Kamat | ab0c069 | 2014-01-27 12:17:05 +0530 | [diff] [blame] | 997 | return PTR_ERR_OR_ZERO(vaddr); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 1000 | static int ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, |
| 1001 | enum dma_data_direction direction) |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1002 | { |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 1003 | struct ion_buffer *buffer = dmabuf->priv; |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1004 | |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 1005 | mutex_lock(&buffer->lock); |
| 1006 | ion_buffer_kmap_put(buffer); |
| 1007 | mutex_unlock(&buffer->lock); |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 1008 | |
| 1009 | return 0; |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 1010 | } |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1011 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 1012 | static struct dma_buf_ops dma_buf_ops = { |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1013 | .map_dma_buf = ion_map_dma_buf, |
| 1014 | .unmap_dma_buf = ion_unmap_dma_buf, |
| 1015 | .mmap = ion_mmap, |
| 1016 | .release = ion_dma_buf_release, |
Rebecca Schultz Zavin | 0f34faf | 2013-12-13 14:23:42 -0800 | [diff] [blame] | 1017 | .begin_cpu_access = ion_dma_buf_begin_cpu_access, |
| 1018 | .end_cpu_access = ion_dma_buf_end_cpu_access, |
| 1019 | .kmap_atomic = ion_dma_buf_kmap, |
| 1020 | .kunmap_atomic = ion_dma_buf_kunmap, |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1021 | .kmap = ion_dma_buf_kmap, |
| 1022 | .kunmap = ion_dma_buf_kunmap, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1023 | }; |
| 1024 | |
Johan Mossberg | 22ba432 | 2013-12-13 14:24:34 -0800 | [diff] [blame] | 1025 | struct dma_buf *ion_share_dma_buf(struct ion_client *client, |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 1026 | struct ion_handle *handle) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1027 | { |
Dmitry Kalinkin | 5605b18 | 2015-07-13 15:50:30 +0300 | [diff] [blame] | 1028 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1029 | struct ion_buffer *buffer; |
| 1030 | struct dma_buf *dmabuf; |
| 1031 | bool valid_handle; |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 1032 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1033 | mutex_lock(&client->lock); |
| 1034 | valid_handle = ion_handle_validate(client, handle); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1035 | if (!valid_handle) { |
Olav Haugan | a9bb075 | 2013-12-13 14:23:54 -0800 | [diff] [blame] | 1036 | WARN(1, "%s: invalid handle passed to share.\n", __func__); |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 1037 | mutex_unlock(&client->lock); |
Johan Mossberg | 22ba432 | 2013-12-13 14:24:34 -0800 | [diff] [blame] | 1038 | return ERR_PTR(-EINVAL); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1039 | } |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1040 | buffer = handle->buffer; |
| 1041 | ion_buffer_get(buffer); |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 1042 | mutex_unlock(&client->lock); |
| 1043 | |
Sumit Semwal | 72449cb | 2015-02-21 09:00:17 +0530 | [diff] [blame] | 1044 | exp_info.ops = &dma_buf_ops; |
| 1045 | exp_info.size = buffer->size; |
| 1046 | exp_info.flags = O_RDWR; |
| 1047 | exp_info.priv = buffer; |
| 1048 | |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 1049 | dmabuf = dma_buf_export(&exp_info); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1050 | if (IS_ERR(dmabuf)) { |
| 1051 | ion_buffer_put(buffer); |
Johan Mossberg | 22ba432 | 2013-12-13 14:24:34 -0800 | [diff] [blame] | 1052 | return dmabuf; |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1053 | } |
Johan Mossberg | 22ba432 | 2013-12-13 14:24:34 -0800 | [diff] [blame] | 1054 | |
| 1055 | return dmabuf; |
| 1056 | } |
| 1057 | EXPORT_SYMBOL(ion_share_dma_buf); |
| 1058 | |
| 1059 | int ion_share_dma_buf_fd(struct ion_client *client, struct ion_handle *handle) |
| 1060 | { |
| 1061 | struct dma_buf *dmabuf; |
| 1062 | int fd; |
| 1063 | |
| 1064 | dmabuf = ion_share_dma_buf(client, handle); |
| 1065 | if (IS_ERR(dmabuf)) |
| 1066 | return PTR_ERR(dmabuf); |
| 1067 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1068 | fd = dma_buf_fd(dmabuf, O_CLOEXEC); |
Laura Abbott | 55808b8 | 2013-12-13 14:23:57 -0800 | [diff] [blame] | 1069 | if (fd < 0) |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1070 | dma_buf_put(dmabuf); |
Laura Abbott | 55808b8 | 2013-12-13 14:23:57 -0800 | [diff] [blame] | 1071 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1072 | return fd; |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1073 | } |
Johan Mossberg | 22ba432 | 2013-12-13 14:24:34 -0800 | [diff] [blame] | 1074 | EXPORT_SYMBOL(ion_share_dma_buf_fd); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1075 | |
Rohit kumar | 9f90381 | 2016-01-12 09:31:46 +0530 | [diff] [blame] | 1076 | struct ion_handle *ion_import_dma_buf(struct ion_client *client, |
| 1077 | struct dma_buf *dmabuf) |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1078 | { |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1079 | struct ion_buffer *buffer; |
| 1080 | struct ion_handle *handle; |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 1081 | int ret; |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1082 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1083 | /* if this memory came from ion */ |
| 1084 | |
| 1085 | if (dmabuf->ops != &dma_buf_ops) { |
| 1086 | pr_err("%s: can not import dmabuf from another exporter\n", |
| 1087 | __func__); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1088 | return ERR_PTR(-EINVAL); |
| 1089 | } |
| 1090 | buffer = dmabuf->priv; |
| 1091 | |
| 1092 | mutex_lock(&client->lock); |
| 1093 | /* if a handle exists for this buffer just take a reference to it */ |
| 1094 | handle = ion_handle_lookup(client, buffer); |
Colin Cross | 9e90765 | 2013-12-13 14:24:49 -0800 | [diff] [blame] | 1095 | if (!IS_ERR(handle)) { |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1096 | ion_handle_get(handle); |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 1097 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1098 | goto end; |
| 1099 | } |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 1100 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1101 | handle = ion_handle_create(client, buffer); |
Shawn Lin | 6fa92e2 | 2015-09-09 15:41:52 +0800 | [diff] [blame] | 1102 | if (IS_ERR(handle)) { |
| 1103 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1104 | goto end; |
Shawn Lin | 6fa92e2 | 2015-09-09 15:41:52 +0800 | [diff] [blame] | 1105 | } |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 1106 | |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 1107 | ret = ion_handle_add(client, handle); |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 1108 | mutex_unlock(&client->lock); |
Colin Cross | 47b4045 | 2013-12-13 14:24:50 -0800 | [diff] [blame] | 1109 | if (ret) { |
| 1110 | ion_handle_put(handle); |
| 1111 | handle = ERR_PTR(ret); |
| 1112 | } |
Colin Cross | 83271f6 | 2013-12-13 14:24:59 -0800 | [diff] [blame] | 1113 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1114 | end: |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1115 | return handle; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1116 | } |
Olav Haugan | ee4c8aa | 2013-12-13 14:23:55 -0800 | [diff] [blame] | 1117 | EXPORT_SYMBOL(ion_import_dma_buf); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1118 | |
Rohit kumar | 9f90381 | 2016-01-12 09:31:46 +0530 | [diff] [blame] | 1119 | struct ion_handle *ion_import_dma_buf_fd(struct ion_client *client, int fd) |
| 1120 | { |
| 1121 | struct dma_buf *dmabuf; |
| 1122 | struct ion_handle *handle; |
| 1123 | |
| 1124 | dmabuf = dma_buf_get(fd); |
| 1125 | if (IS_ERR(dmabuf)) |
| 1126 | return ERR_CAST(dmabuf); |
| 1127 | |
| 1128 | handle = ion_import_dma_buf(client, dmabuf); |
| 1129 | dma_buf_put(dmabuf); |
| 1130 | return handle; |
| 1131 | } |
| 1132 | EXPORT_SYMBOL(ion_import_dma_buf_fd); |
| 1133 | |
Laura Abbott | b1fa6d8 | 2016-09-07 11:49:58 -0700 | [diff] [blame] | 1134 | int ion_sync_for_device(struct ion_client *client, int fd) |
Rebecca Schultz Zavin | 0b9ec1c | 2013-12-13 14:23:52 -0800 | [diff] [blame] | 1135 | { |
| 1136 | struct dma_buf *dmabuf; |
| 1137 | struct ion_buffer *buffer; |
| 1138 | |
| 1139 | dmabuf = dma_buf_get(fd); |
Colin Cross | 9e90765 | 2013-12-13 14:24:49 -0800 | [diff] [blame] | 1140 | if (IS_ERR(dmabuf)) |
Rebecca Schultz Zavin | 0b9ec1c | 2013-12-13 14:23:52 -0800 | [diff] [blame] | 1141 | return PTR_ERR(dmabuf); |
| 1142 | |
| 1143 | /* if this memory came from ion */ |
| 1144 | if (dmabuf->ops != &dma_buf_ops) { |
| 1145 | pr_err("%s: can not sync dmabuf from another exporter\n", |
| 1146 | __func__); |
| 1147 | dma_buf_put(dmabuf); |
| 1148 | return -EINVAL; |
| 1149 | } |
| 1150 | buffer = dmabuf->priv; |
Rebecca Schultz Zavin | 856661d | 2013-12-13 14:24:05 -0800 | [diff] [blame] | 1151 | |
| 1152 | dma_sync_sg_for_device(NULL, buffer->sg_table->sgl, |
| 1153 | buffer->sg_table->nents, DMA_BIDIRECTIONAL); |
Rebecca Schultz Zavin | 0b9ec1c | 2013-12-13 14:23:52 -0800 | [diff] [blame] | 1154 | dma_buf_put(dmabuf); |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
Laura Abbott | 02b2380 | 2016-09-07 11:49:59 -0700 | [diff] [blame] | 1158 | int ion_query_heaps(struct ion_client *client, struct ion_heap_query *query) |
| 1159 | { |
| 1160 | struct ion_device *dev = client->dev; |
| 1161 | struct ion_heap_data __user *buffer = u64_to_user_ptr(query->heaps); |
| 1162 | int ret = -EINVAL, cnt = 0, max_cnt; |
| 1163 | struct ion_heap *heap; |
| 1164 | struct ion_heap_data hdata; |
| 1165 | |
| 1166 | memset(&hdata, 0, sizeof(hdata)); |
| 1167 | |
| 1168 | down_read(&dev->lock); |
| 1169 | if (!buffer) { |
| 1170 | query->cnt = dev->heap_cnt; |
| 1171 | ret = 0; |
| 1172 | goto out; |
| 1173 | } |
| 1174 | |
| 1175 | if (query->cnt <= 0) |
| 1176 | goto out; |
| 1177 | |
| 1178 | max_cnt = query->cnt; |
| 1179 | |
| 1180 | plist_for_each_entry(heap, &dev->heaps, node) { |
| 1181 | strncpy(hdata.name, heap->name, MAX_HEAP_NAME); |
| 1182 | hdata.name[sizeof(hdata.name) - 1] = '\0'; |
| 1183 | hdata.type = heap->type; |
| 1184 | hdata.heap_id = heap->id; |
| 1185 | |
Dan Carpenter | cf55902 | 2016-10-13 15:55:08 +0300 | [diff] [blame] | 1186 | if (copy_to_user(&buffer[cnt], &hdata, sizeof(hdata))) { |
| 1187 | ret = -EFAULT; |
| 1188 | goto out; |
| 1189 | } |
Laura Abbott | 02b2380 | 2016-09-07 11:49:59 -0700 | [diff] [blame] | 1190 | |
| 1191 | cnt++; |
| 1192 | if (cnt >= max_cnt) |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | query->cnt = cnt; |
| 1197 | out: |
| 1198 | up_read(&dev->lock); |
| 1199 | return ret; |
| 1200 | } |
| 1201 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1202 | static int ion_release(struct inode *inode, struct file *file) |
| 1203 | { |
| 1204 | struct ion_client *client = file->private_data; |
| 1205 | |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1206 | ion_client_destroy(client); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static int ion_open(struct inode *inode, struct file *file) |
| 1211 | { |
| 1212 | struct miscdevice *miscdev = file->private_data; |
| 1213 | struct ion_device *dev = container_of(miscdev, struct ion_device, dev); |
| 1214 | struct ion_client *client; |
Laura Abbott | 483ed03 | 2014-02-17 13:58:35 -0800 | [diff] [blame] | 1215 | char debug_name[64]; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1216 | |
Laura Abbott | 483ed03 | 2014-02-17 13:58:35 -0800 | [diff] [blame] | 1217 | snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader)); |
| 1218 | client = ion_client_create(dev, debug_name); |
Colin Cross | 9e90765 | 2013-12-13 14:24:49 -0800 | [diff] [blame] | 1219 | if (IS_ERR(client)) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1220 | return PTR_ERR(client); |
| 1221 | file->private_data = client; |
| 1222 | |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | static const struct file_operations ion_fops = { |
| 1227 | .owner = THIS_MODULE, |
| 1228 | .open = ion_open, |
| 1229 | .release = ion_release, |
| 1230 | .unlocked_ioctl = ion_ioctl, |
Rom Lemarchand | 827c849 | 2013-12-13 14:24:55 -0800 | [diff] [blame] | 1231 | .compat_ioctl = compat_ion_ioctl, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1232 | }; |
| 1233 | |
| 1234 | static size_t ion_debug_heap_total(struct ion_client *client, |
Rebecca Schultz Zavin | 2bb9f50 | 2013-12-13 14:24:30 -0800 | [diff] [blame] | 1235 | unsigned int id) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1236 | { |
| 1237 | size_t size = 0; |
| 1238 | struct rb_node *n; |
| 1239 | |
| 1240 | mutex_lock(&client->lock); |
| 1241 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 1242 | struct ion_handle *handle = rb_entry(n, |
| 1243 | struct ion_handle, |
| 1244 | node); |
Rebecca Schultz Zavin | 2bb9f50 | 2013-12-13 14:24:30 -0800 | [diff] [blame] | 1245 | if (handle->buffer->heap->id == id) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1246 | size += handle->buffer->size; |
| 1247 | } |
| 1248 | mutex_unlock(&client->lock); |
| 1249 | return size; |
| 1250 | } |
| 1251 | |
| 1252 | static int ion_debug_heap_show(struct seq_file *s, void *unused) |
| 1253 | { |
| 1254 | struct ion_heap *heap = s->private; |
| 1255 | struct ion_device *dev = heap->dev; |
| 1256 | struct rb_node *n; |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 1257 | size_t total_size = 0; |
| 1258 | size_t total_orphaned_size = 0; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1259 | |
Rasmus Villemoes | b569396 | 2015-02-20 14:13:19 +0100 | [diff] [blame] | 1260 | seq_printf(s, "%16s %16s %16s\n", "client", "pid", "size"); |
Iulia Manda | 164ad86 | 2014-03-11 20:12:29 +0200 | [diff] [blame] | 1261 | seq_puts(s, "----------------------------------------------------\n"); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1262 | |
Neil Zhang | 948c4db | 2016-01-26 17:39:06 +0800 | [diff] [blame] | 1263 | mutex_lock(&debugfs_mutex); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1264 | for (n = rb_first(&dev->clients); n; n = rb_next(n)) { |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1265 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1266 | node); |
Rebecca Schultz Zavin | 2bb9f50 | 2013-12-13 14:24:30 -0800 | [diff] [blame] | 1267 | size_t size = ion_debug_heap_total(client, heap->id); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1268 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1269 | if (!size) |
| 1270 | continue; |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1271 | if (client->task) { |
| 1272 | char task_comm[TASK_COMM_LEN]; |
| 1273 | |
| 1274 | get_task_comm(task_comm, client->task); |
Rasmus Villemoes | b569396 | 2015-02-20 14:13:19 +0100 | [diff] [blame] | 1275 | seq_printf(s, "%16s %16u %16zu\n", task_comm, |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1276 | client->pid, size); |
| 1277 | } else { |
Rasmus Villemoes | b569396 | 2015-02-20 14:13:19 +0100 | [diff] [blame] | 1278 | seq_printf(s, "%16s %16u %16zu\n", client->name, |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1279 | client->pid, size); |
| 1280 | } |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1281 | } |
Neil Zhang | 948c4db | 2016-01-26 17:39:06 +0800 | [diff] [blame] | 1282 | mutex_unlock(&debugfs_mutex); |
| 1283 | |
Iulia Manda | 164ad86 | 2014-03-11 20:12:29 +0200 | [diff] [blame] | 1284 | seq_puts(s, "----------------------------------------------------\n"); |
| 1285 | seq_puts(s, "orphaned allocations (info is from last known client):\n"); |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 1286 | mutex_lock(&dev->buffer_lock); |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 1287 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1288 | struct ion_buffer *buffer = rb_entry(n, struct ion_buffer, |
| 1289 | node); |
Rebecca Schultz Zavin | 2bb9f50 | 2013-12-13 14:24:30 -0800 | [diff] [blame] | 1290 | if (buffer->heap->id != heap->id) |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 1291 | continue; |
| 1292 | total_size += buffer->size; |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 1293 | if (!buffer->handle_count) { |
Rasmus Villemoes | b569396 | 2015-02-20 14:13:19 +0100 | [diff] [blame] | 1294 | seq_printf(s, "%16s %16u %16zu %d %d\n", |
Colin Cross | e61fc91 | 2013-12-13 19:26:14 -0800 | [diff] [blame] | 1295 | buffer->task_comm, buffer->pid, |
| 1296 | buffer->size, buffer->kmap_cnt, |
Benjamin Gaignard | 092c354 | 2013-12-13 14:24:22 -0800 | [diff] [blame] | 1297 | atomic_read(&buffer->ref.refcount)); |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 1298 | total_orphaned_size += buffer->size; |
| 1299 | } |
| 1300 | } |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 1301 | mutex_unlock(&dev->buffer_lock); |
Iulia Manda | 164ad86 | 2014-03-11 20:12:29 +0200 | [diff] [blame] | 1302 | seq_puts(s, "----------------------------------------------------\n"); |
Rasmus Villemoes | b569396 | 2015-02-20 14:13:19 +0100 | [diff] [blame] | 1303 | seq_printf(s, "%16s %16zu\n", "total orphaned", |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 1304 | total_orphaned_size); |
Rasmus Villemoes | b569396 | 2015-02-20 14:13:19 +0100 | [diff] [blame] | 1305 | seq_printf(s, "%16s %16zu\n", "total ", total_size); |
Colin Cross | 2540c73 | 2013-12-13 14:24:47 -0800 | [diff] [blame] | 1306 | if (heap->flags & ION_HEAP_FLAG_DEFER_FREE) |
Rasmus Villemoes | b569396 | 2015-02-20 14:13:19 +0100 | [diff] [blame] | 1307 | seq_printf(s, "%16s %16zu\n", "deferred free", |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 1308 | heap->free_list_size); |
Iulia Manda | 164ad86 | 2014-03-11 20:12:29 +0200 | [diff] [blame] | 1309 | seq_puts(s, "----------------------------------------------------\n"); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 1310 | |
| 1311 | if (heap->debug_show) |
| 1312 | heap->debug_show(heap, s, unused); |
Rebecca Schultz Zavin | 5ad7bc3 | 2013-12-13 14:24:03 -0800 | [diff] [blame] | 1313 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1314 | return 0; |
| 1315 | } |
| 1316 | |
| 1317 | static int ion_debug_heap_open(struct inode *inode, struct file *file) |
| 1318 | { |
| 1319 | return single_open(file, ion_debug_heap_show, inode->i_private); |
| 1320 | } |
| 1321 | |
| 1322 | static const struct file_operations debug_heap_fops = { |
| 1323 | .open = ion_debug_heap_open, |
| 1324 | .read = seq_read, |
| 1325 | .llseek = seq_lseek, |
| 1326 | .release = single_release, |
| 1327 | }; |
| 1328 | |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 1329 | static int debug_shrink_set(void *data, u64 val) |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 1330 | { |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 1331 | struct ion_heap *heap = data; |
| 1332 | struct shrink_control sc; |
| 1333 | int objs; |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 1334 | |
Derek Yerger | 3b0ae7b | 2016-03-11 17:31:18 -0500 | [diff] [blame] | 1335 | sc.gfp_mask = GFP_HIGHUSER; |
Gioh Kim | aeb7fa7 | 2015-07-06 15:14:41 +0900 | [diff] [blame] | 1336 | sc.nr_to_scan = val; |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 1337 | |
Gioh Kim | aeb7fa7 | 2015-07-06 15:14:41 +0900 | [diff] [blame] | 1338 | if (!val) { |
| 1339 | objs = heap->shrinker.count_objects(&heap->shrinker, &sc); |
| 1340 | sc.nr_to_scan = objs; |
| 1341 | } |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 1342 | |
Gioh Kim | aeb7fa7 | 2015-07-06 15:14:41 +0900 | [diff] [blame] | 1343 | heap->shrinker.scan_objects(&heap->shrinker, &sc); |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 1344 | return 0; |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 1345 | } |
| 1346 | |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 1347 | static int debug_shrink_get(void *data, u64 *val) |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 1348 | { |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 1349 | struct ion_heap *heap = data; |
| 1350 | struct shrink_control sc; |
| 1351 | int objs; |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 1352 | |
Derek Yerger | 3b0ae7b | 2016-03-11 17:31:18 -0500 | [diff] [blame] | 1353 | sc.gfp_mask = GFP_HIGHUSER; |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 1354 | sc.nr_to_scan = 0; |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 1355 | |
Gioh Kim | aeb7fa7 | 2015-07-06 15:14:41 +0900 | [diff] [blame] | 1356 | objs = heap->shrinker.count_objects(&heap->shrinker, &sc); |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 1357 | *val = objs; |
| 1358 | return 0; |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 1359 | } |
| 1360 | |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 1361 | DEFINE_SIMPLE_ATTRIBUTE(debug_shrink_fops, debug_shrink_get, |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 1362 | debug_shrink_set, "%llu\n"); |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 1363 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1364 | void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap) |
| 1365 | { |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 1366 | struct dentry *debug_file; |
| 1367 | |
Laura Abbott | f82ad60 | 2016-08-08 09:52:56 -0700 | [diff] [blame] | 1368 | if (!heap->ops->allocate || !heap->ops->free) |
Rebecca Schultz Zavin | 29ae6bc | 2013-12-13 14:23:43 -0800 | [diff] [blame] | 1369 | pr_err("%s: can not add heap with invalid ops struct.\n", |
| 1370 | __func__); |
| 1371 | |
Mitchel Humpherys | 95e53dd | 2015-01-08 17:24:27 -0800 | [diff] [blame] | 1372 | spin_lock_init(&heap->free_lock); |
| 1373 | heap->free_list_size = 0; |
| 1374 | |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 1375 | if (heap->flags & ION_HEAP_FLAG_DEFER_FREE) |
| 1376 | ion_heap_init_deferred_free(heap); |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 1377 | |
Colin Cross | b9daf0b | 2014-02-17 13:58:38 -0800 | [diff] [blame] | 1378 | if ((heap->flags & ION_HEAP_FLAG_DEFER_FREE) || heap->ops->shrink) |
| 1379 | ion_heap_init_shrinker(heap); |
| 1380 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1381 | heap->dev = dev; |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 1382 | down_write(&dev->lock); |
Sriram Raghunathan | 7e41617 | 2015-09-22 22:35:51 +0530 | [diff] [blame] | 1383 | /* |
| 1384 | * use negative heap->id to reverse the priority -- when traversing |
| 1385 | * the list later attempt higher id numbers first |
| 1386 | */ |
Rebecca Schultz Zavin | cd69488 | 2013-12-13 14:24:25 -0800 | [diff] [blame] | 1387 | plist_node_init(&heap->node, -heap->id); |
| 1388 | plist_add(&heap->node, &dev->heaps); |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 1389 | debug_file = debugfs_create_file(heap->name, 0664, |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 1390 | dev->heaps_debug_root, heap, |
| 1391 | &debug_heap_fops); |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 1392 | |
| 1393 | if (!debug_file) { |
| 1394 | char buf[256], *path; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1395 | |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 1396 | path = dentry_path(dev->heaps_debug_root, buf, 256); |
| 1397 | pr_err("Failed to create heap debugfs at %s/%s\n", |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 1398 | path, heap->name); |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 1399 | } |
| 1400 | |
Gioh Kim | aeb7fa7 | 2015-07-06 15:14:41 +0900 | [diff] [blame] | 1401 | if (heap->shrinker.count_objects && heap->shrinker.scan_objects) { |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 1402 | char debug_name[64]; |
| 1403 | |
| 1404 | snprintf(debug_name, 64, "%s_shrink", heap->name); |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 1405 | debug_file = debugfs_create_file( |
| 1406 | debug_name, 0644, dev->heaps_debug_root, heap, |
| 1407 | &debug_shrink_fops); |
| 1408 | if (!debug_file) { |
| 1409 | char buf[256], *path; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 1410 | |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 1411 | path = dentry_path(dev->heaps_debug_root, buf, 256); |
| 1412 | pr_err("Failed to create heap shrinker debugfs at %s/%s\n", |
Johanna Abrahamsson | 121ca0c | 2016-08-22 12:16:58 +0200 | [diff] [blame] | 1413 | path, debug_name); |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 1414 | } |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 1415 | } |
Gioh Kim | aeb7fa7 | 2015-07-06 15:14:41 +0900 | [diff] [blame] | 1416 | |
Laura Abbott | 02b2380 | 2016-09-07 11:49:59 -0700 | [diff] [blame] | 1417 | dev->heap_cnt++; |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 1418 | up_write(&dev->lock); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1419 | } |
Paul Gortmaker | 8c6c463 | 2015-10-13 16:46:53 -0400 | [diff] [blame] | 1420 | EXPORT_SYMBOL(ion_device_add_heap); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1421 | |
| 1422 | struct ion_device *ion_device_create(long (*custom_ioctl) |
| 1423 | (struct ion_client *client, |
| 1424 | unsigned int cmd, |
| 1425 | unsigned long arg)) |
| 1426 | { |
| 1427 | struct ion_device *idev; |
| 1428 | int ret; |
| 1429 | |
Ben Marsh | 411059f | 2016-03-28 19:26:19 +0200 | [diff] [blame] | 1430 | idev = kzalloc(sizeof(*idev), GFP_KERNEL); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1431 | if (!idev) |
| 1432 | return ERR_PTR(-ENOMEM); |
| 1433 | |
| 1434 | idev->dev.minor = MISC_DYNAMIC_MINOR; |
| 1435 | idev->dev.name = "ion"; |
| 1436 | idev->dev.fops = &ion_fops; |
| 1437 | idev->dev.parent = NULL; |
| 1438 | ret = misc_register(&idev->dev); |
| 1439 | if (ret) { |
| 1440 | pr_err("ion: failed to register misc device.\n"); |
Shailendra Verma | 283d930 | 2015-05-19 20:29:00 +0530 | [diff] [blame] | 1441 | kfree(idev); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1442 | return ERR_PTR(ret); |
| 1443 | } |
| 1444 | |
| 1445 | idev->debug_root = debugfs_create_dir("ion", NULL); |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 1446 | if (!idev->debug_root) { |
| 1447 | pr_err("ion: failed to create debugfs root directory.\n"); |
| 1448 | goto debugfs_done; |
| 1449 | } |
| 1450 | idev->heaps_debug_root = debugfs_create_dir("heaps", idev->debug_root); |
| 1451 | if (!idev->heaps_debug_root) { |
| 1452 | pr_err("ion: failed to create debugfs heaps directory.\n"); |
| 1453 | goto debugfs_done; |
| 1454 | } |
| 1455 | idev->clients_debug_root = debugfs_create_dir("clients", |
| 1456 | idev->debug_root); |
| 1457 | if (!idev->clients_debug_root) |
| 1458 | pr_err("ion: failed to create debugfs clients directory.\n"); |
| 1459 | |
| 1460 | debugfs_done: |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1461 | |
| 1462 | idev->custom_ioctl = custom_ioctl; |
| 1463 | idev->buffers = RB_ROOT; |
Rebecca Schultz Zavin | 8d7ab9a | 2013-12-13 14:24:16 -0800 | [diff] [blame] | 1464 | mutex_init(&idev->buffer_lock); |
| 1465 | init_rwsem(&idev->lock); |
Rebecca Schultz Zavin | cd69488 | 2013-12-13 14:24:25 -0800 | [diff] [blame] | 1466 | plist_head_init(&idev->heaps); |
Rebecca Schultz Zavin | b892bf7 | 2013-12-13 14:23:40 -0800 | [diff] [blame] | 1467 | idev->clients = RB_ROOT; |
Neil Zhang | 948c4db | 2016-01-26 17:39:06 +0800 | [diff] [blame] | 1468 | ion_root_client = &idev->clients; |
| 1469 | mutex_init(&debugfs_mutex); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1470 | return idev; |
| 1471 | } |
Paul Gortmaker | 8c6c463 | 2015-10-13 16:46:53 -0400 | [diff] [blame] | 1472 | EXPORT_SYMBOL(ion_device_create); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1473 | |
| 1474 | void ion_device_destroy(struct ion_device *dev) |
| 1475 | { |
| 1476 | misc_deregister(&dev->dev); |
Mitchel Humpherys | b08585fb | 2014-02-17 13:58:34 -0800 | [diff] [blame] | 1477 | debugfs_remove_recursive(dev->debug_root); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1478 | /* XXX need to free the heaps and clients ? */ |
| 1479 | kfree(dev); |
| 1480 | } |
Paul Gortmaker | 8c6c463 | 2015-10-13 16:46:53 -0400 | [diff] [blame] | 1481 | EXPORT_SYMBOL(ion_device_destroy); |