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