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