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