Commit | Line | Data |
---|---|---|
c30707be | 1 | /* |
38eeeb51 | 2 | |
c30707be RSZ |
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> | |
ab0c069a | 19 | #include <linux/err.h> |
c30707be | 20 | #include <linux/file.h> |
fe2faea7 | 21 | #include <linux/freezer.h> |
c30707be RSZ |
22 | #include <linux/fs.h> |
23 | #include <linux/anon_inodes.h> | |
fe2faea7 | 24 | #include <linux/kthread.h> |
c30707be | 25 | #include <linux/list.h> |
2991b7a0 | 26 | #include <linux/memblock.h> |
c30707be RSZ |
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> | |
c30707be RSZ |
32 | #include <linux/slab.h> |
33 | #include <linux/seq_file.h> | |
34 | #include <linux/uaccess.h> | |
c13bd1c4 | 35 | #include <linux/vmalloc.h> |
c30707be | 36 | #include <linux/debugfs.h> |
b892bf75 | 37 | #include <linux/dma-buf.h> |
47b40458 | 38 | #include <linux/idr.h> |
c30707be RSZ |
39 | |
40 | #include "ion.h" | |
41 | #include "ion_priv.h" | |
827c849e | 42 | #include "compat_ion.h" |
c30707be RSZ |
43 | |
44 | /** | |
45 | * struct ion_device - the metadata of the ion device node | |
46 | * @dev: the actual misc device | |
8d7ab9a9 RSZ |
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 | |
c30707be RSZ |
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; | |
8d7ab9a9 RSZ |
56 | struct mutex buffer_lock; |
57 | struct rw_semaphore lock; | |
cd69488c | 58 | struct plist_head heaps; |
51108985 DY |
59 | long (*custom_ioctl)(struct ion_client *client, unsigned int cmd, |
60 | unsigned long arg); | |
b892bf75 | 61 | struct rb_root clients; |
c30707be | 62 | struct dentry *debug_root; |
b08585fb MH |
63 | struct dentry *heaps_debug_root; |
64 | struct dentry *clients_debug_root; | |
c30707be RSZ |
65 | }; |
66 | ||
67 | /** | |
68 | * struct ion_client - a process/hw block local address space | |
c30707be RSZ |
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 | |
47b40458 | 72 | * @idr: an idr space for allocating handle ids |
c30707be | 73 | * @lock: lock protecting the tree of handles |
c30707be | 74 | * @name: used for debugging |
2803ac7b MH |
75 | * @display_name: used for debugging (unique version of @name) |
76 | * @display_serial: used for debugging (to make display_name unique) | |
c30707be RSZ |
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 { | |
c30707be RSZ |
84 | struct rb_node node; |
85 | struct ion_device *dev; | |
86 | struct rb_root handles; | |
47b40458 | 87 | struct idr idr; |
c30707be | 88 | struct mutex lock; |
c30707be | 89 | const char *name; |
2803ac7b MH |
90 | char *display_name; |
91 | int display_serial; | |
c30707be RSZ |
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 | |
47b40458 | 104 | * @id: client-unique id allocated by client->idr |
c30707be RSZ |
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; | |
47b40458 | 115 | int id; |
c30707be RSZ |
116 | }; |
117 | ||
13ba7805 RSZ |
118 | bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer) |
119 | { | |
e1d855b0 JS |
120 | return (buffer->flags & ION_FLAG_CACHED) && |
121 | !(buffer->flags & ION_FLAG_CACHED_NEEDS_SYNC); | |
13ba7805 RSZ |
122 | } |
123 | ||
45b17a80 RSZ |
124 | bool ion_buffer_cached(struct ion_buffer *buffer) |
125 | { | |
c13bd1c4 RSZ |
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)); | |
45b17a80 RSZ |
147 | } |
148 | ||
c30707be RSZ |
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; | |
29ae6bc7 | 183 | struct sg_table *table; |
a46b6b2d RSZ |
184 | struct scatterlist *sg; |
185 | int i, ret; | |
c30707be RSZ |
186 | |
187 | buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL); | |
188 | if (!buffer) | |
189 | return ERR_PTR(-ENOMEM); | |
190 | ||
191 | buffer->heap = heap; | |
13ba7805 | 192 | buffer->flags = flags; |
c30707be RSZ |
193 | kref_init(&buffer->ref); |
194 | ||
195 | ret = heap->ops->allocate(heap, buffer, len, align, flags); | |
fe2faea7 | 196 | |
c30707be | 197 | if (ret) { |
fe2faea7 RSZ |
198 | if (!(heap->flags & ION_HEAP_FLAG_DEFER_FREE)) |
199 | goto err2; | |
200 | ||
ea313b5f | 201 | ion_heap_freelist_drain(heap, 0); |
fe2faea7 RSZ |
202 | ret = heap->ops->allocate(heap, buffer, len, align, |
203 | flags); | |
204 | if (ret) | |
205 | goto err2; | |
c30707be | 206 | } |
29ae6bc7 | 207 | |
056be396 GH |
208 | buffer->dev = dev; |
209 | buffer->size = len; | |
210 | ||
56a7c185 | 211 | table = heap->ops->map_dma(heap, buffer); |
e1d855b0 JS |
212 | if (WARN_ONCE(table == NULL, |
213 | "heap->ops->map_dma should return ERR_PTR on error")) | |
9e907654 CC |
214 | table = ERR_PTR(-EINVAL); |
215 | if (IS_ERR(table)) { | |
29ae6bc7 RSZ |
216 | heap->ops->free(buffer); |
217 | kfree(buffer); | |
464a5028 | 218 | return ERR_CAST(table); |
29ae6bc7 RSZ |
219 | } |
220 | buffer->sg_table = table; | |
13ba7805 | 221 | if (ion_buffer_fault_user_mappings(buffer)) { |
c13bd1c4 RSZ |
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; | |
229 | goto err1; | |
230 | } | |
231 | ||
232 | for_each_sg(table->sgl, sg, table->nents, i) { | |
233 | struct page *page = sg_page(sg); | |
234 | ||
06e0dcae | 235 | for (j = 0; j < sg->length / PAGE_SIZE; j++) |
c13bd1c4 | 236 | buffer->pages[k++] = page++; |
56a7c185 | 237 | } |
29ae6bc7 | 238 | |
d3c0bced RSZ |
239 | if (ret) |
240 | goto err; | |
56a7c185 RSZ |
241 | } |
242 | ||
243 | buffer->dev = dev; | |
244 | buffer->size = len; | |
245 | INIT_LIST_HEAD(&buffer->vmas); | |
c30707be | 246 | mutex_init(&buffer->lock); |
a46b6b2d RSZ |
247 | /* this will set up dma addresses for the sglist -- it is not |
248 | technically correct as per the dma api -- a specific | |
249 | device isn't really taking ownership here. However, in practice on | |
250 | our systems the only dma_address space is physical addresses. | |
251 | Additionally, we can't afford the overhead of invalidating every | |
252 | allocation via dma_map_sg. The implicit contract here is that | |
bc47e7d9 | 253 | memory coming from the heaps is ready for dma, ie if it has a |
a46b6b2d RSZ |
254 | cached mapping that mapping has been invalidated */ |
255 | for_each_sg(buffer->sg_table->sgl, sg, buffer->sg_table->nents, i) | |
256 | sg_dma_address(sg) = sg_phys(sg); | |
8d7ab9a9 | 257 | mutex_lock(&dev->buffer_lock); |
c30707be | 258 | ion_buffer_add(dev, buffer); |
8d7ab9a9 | 259 | mutex_unlock(&dev->buffer_lock); |
c30707be | 260 | return buffer; |
d3c0bced RSZ |
261 | |
262 | err: | |
263 | heap->ops->unmap_dma(heap, buffer); | |
264 | heap->ops->free(buffer); | |
c13bd1c4 | 265 | err1: |
698f140d | 266 | vfree(buffer->pages); |
fe2faea7 | 267 | err2: |
d3c0bced RSZ |
268 | kfree(buffer); |
269 | return ERR_PTR(ret); | |
c30707be RSZ |
270 | } |
271 | ||
ea313b5f | 272 | void ion_buffer_destroy(struct ion_buffer *buffer) |
c30707be | 273 | { |
54ac0784 KC |
274 | if (WARN_ON(buffer->kmap_cnt > 0)) |
275 | buffer->heap->ops->unmap_kernel(buffer->heap, buffer); | |
29ae6bc7 | 276 | buffer->heap->ops->unmap_dma(buffer->heap, buffer); |
c30707be | 277 | buffer->heap->ops->free(buffer); |
698f140d | 278 | vfree(buffer->pages); |
c30707be RSZ |
279 | kfree(buffer); |
280 | } | |
281 | ||
ea313b5f | 282 | static void _ion_buffer_destroy(struct kref *kref) |
fe2faea7 RSZ |
283 | { |
284 | struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref); | |
285 | struct ion_heap *heap = buffer->heap; | |
286 | struct ion_device *dev = buffer->dev; | |
287 | ||
288 | mutex_lock(&dev->buffer_lock); | |
289 | rb_erase(&buffer->node, &dev->buffers); | |
290 | mutex_unlock(&dev->buffer_lock); | |
291 | ||
ea313b5f RSZ |
292 | if (heap->flags & ION_HEAP_FLAG_DEFER_FREE) |
293 | ion_heap_freelist_add(heap, buffer); | |
294 | else | |
295 | ion_buffer_destroy(buffer); | |
fe2faea7 RSZ |
296 | } |
297 | ||
c30707be RSZ |
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 | { | |
ea313b5f | 305 | return kref_put(&buffer->ref, _ion_buffer_destroy); |
c30707be RSZ |
306 | } |
307 | ||
5ad7bc3a RSZ |
308 | static void ion_buffer_add_to_handle(struct ion_buffer *buffer) |
309 | { | |
8d7ab9a9 | 310 | mutex_lock(&buffer->lock); |
5ad7bc3a | 311 | buffer->handle_count++; |
8d7ab9a9 | 312 | mutex_unlock(&buffer->lock); |
5ad7bc3a RSZ |
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 | */ | |
8d7ab9a9 | 326 | mutex_lock(&buffer->lock); |
5ad7bc3a RSZ |
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 | } | |
8d7ab9a9 | 336 | mutex_unlock(&buffer->lock); |
5ad7bc3a RSZ |
337 | } |
338 | ||
c30707be RSZ |
339 | static struct ion_handle *ion_handle_create(struct ion_client *client, |
340 | struct ion_buffer *buffer) | |
341 | { | |
342 | struct ion_handle *handle; | |
343 | ||
344 | handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL); | |
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); | |
5ad7bc3a | 351 | ion_buffer_add_to_handle(buffer); |
c30707be RSZ |
352 | handle->buffer = buffer; |
353 | ||
354 | return handle; | |
355 | } | |
356 | ||
b892bf75 RSZ |
357 | static void ion_handle_kmap_put(struct ion_handle *); |
358 | ||
c30707be RSZ |
359 | static void ion_handle_destroy(struct kref *kref) |
360 | { | |
361 | struct ion_handle *handle = container_of(kref, struct ion_handle, ref); | |
b892bf75 RSZ |
362 | struct ion_client *client = handle->client; |
363 | struct ion_buffer *buffer = handle->buffer; | |
364 | ||
b892bf75 | 365 | mutex_lock(&buffer->lock); |
2900cd76 | 366 | while (handle->kmap_cnt) |
b892bf75 RSZ |
367 | ion_handle_kmap_put(handle); |
368 | mutex_unlock(&buffer->lock); | |
369 | ||
47b40458 | 370 | idr_remove(&client->idr, handle->id); |
c30707be | 371 | if (!RB_EMPTY_NODE(&handle->node)) |
b892bf75 | 372 | rb_erase(&handle->node, &client->handles); |
b892bf75 | 373 | |
5ad7bc3a | 374 | ion_buffer_remove_from_handle(buffer); |
b892bf75 | 375 | ion_buffer_put(buffer); |
5ad7bc3a | 376 | |
c30707be RSZ |
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 | ||
390 | static int ion_handle_put(struct ion_handle *handle) | |
391 | { | |
83271f62 CC |
392 | struct ion_client *client = handle->client; |
393 | int ret; | |
394 | ||
395 | mutex_lock(&client->lock); | |
396 | ret = kref_put(&handle->ref, ion_handle_destroy); | |
397 | mutex_unlock(&client->lock); | |
398 | ||
399 | return ret; | |
c30707be RSZ |
400 | } |
401 | ||
402 | static struct ion_handle *ion_handle_lookup(struct ion_client *client, | |
403 | struct ion_buffer *buffer) | |
404 | { | |
e1cf3682 CC |
405 | struct rb_node *n = client->handles.rb_node; |
406 | ||
407 | while (n) { | |
408 | struct ion_handle *entry = rb_entry(n, struct ion_handle, node); | |
10f62861 | 409 | |
e1cf3682 CC |
410 | if (buffer < entry->buffer) |
411 | n = n->rb_left; | |
412 | else if (buffer > entry->buffer) | |
413 | n = n->rb_right; | |
414 | else | |
415 | return entry; | |
c30707be | 416 | } |
9e907654 | 417 | return ERR_PTR(-EINVAL); |
c30707be RSZ |
418 | } |
419 | ||
83271f62 CC |
420 | static struct ion_handle *ion_handle_get_by_id(struct ion_client *client, |
421 | int id) | |
47b40458 | 422 | { |
83271f62 CC |
423 | struct ion_handle *handle; |
424 | ||
425 | mutex_lock(&client->lock); | |
426 | handle = idr_find(&client->idr, id); | |
427 | if (handle) | |
428 | ion_handle_get(handle); | |
429 | mutex_unlock(&client->lock); | |
430 | ||
431 | return handle ? handle : ERR_PTR(-EINVAL); | |
47b40458 CC |
432 | } |
433 | ||
e1d855b0 JS |
434 | static bool ion_handle_validate(struct ion_client *client, |
435 | struct ion_handle *handle) | |
c30707be | 436 | { |
83271f62 | 437 | WARN_ON(!mutex_is_locked(&client->lock)); |
51108985 | 438 | return idr_find(&client->idr, handle->id) == handle; |
c30707be RSZ |
439 | } |
440 | ||
47b40458 | 441 | static int ion_handle_add(struct ion_client *client, struct ion_handle *handle) |
c30707be | 442 | { |
b26661d1 | 443 | int id; |
c30707be RSZ |
444 | struct rb_node **p = &client->handles.rb_node; |
445 | struct rb_node *parent = NULL; | |
446 | struct ion_handle *entry; | |
447 | ||
b26661d1 CC |
448 | id = idr_alloc(&client->idr, handle, 1, 0, GFP_KERNEL); |
449 | if (id < 0) | |
450 | return id; | |
47b40458 | 451 | |
b26661d1 | 452 | handle->id = id; |
47b40458 | 453 | |
c30707be RSZ |
454 | while (*p) { |
455 | parent = *p; | |
456 | entry = rb_entry(parent, struct ion_handle, node); | |
457 | ||
e1cf3682 | 458 | if (handle->buffer < entry->buffer) |
c30707be | 459 | p = &(*p)->rb_left; |
e1cf3682 | 460 | else if (handle->buffer > entry->buffer) |
c30707be RSZ |
461 | p = &(*p)->rb_right; |
462 | else | |
463 | WARN(1, "%s: buffer already found.", __func__); | |
464 | } | |
465 | ||
466 | rb_link_node(&handle->node, parent, p); | |
467 | rb_insert_color(&handle->node, &client->handles); | |
47b40458 CC |
468 | |
469 | return 0; | |
c30707be RSZ |
470 | } |
471 | ||
472 | struct ion_handle *ion_alloc(struct ion_client *client, size_t len, | |
38eeeb51 | 473 | size_t align, unsigned int heap_id_mask, |
56a7c185 | 474 | unsigned int flags) |
c30707be | 475 | { |
c30707be RSZ |
476 | struct ion_handle *handle; |
477 | struct ion_device *dev = client->dev; | |
478 | struct ion_buffer *buffer = NULL; | |
cd69488c | 479 | struct ion_heap *heap; |
47b40458 | 480 | int ret; |
c30707be | 481 | |
e61fc915 | 482 | pr_debug("%s: len %zu align %zu heap_id_mask %u flags %x\n", __func__, |
38eeeb51 | 483 | len, align, heap_id_mask, flags); |
c30707be RSZ |
484 | /* |
485 | * traverse the list of heaps available in this system in priority | |
486 | * order. If the heap type is supported by the client, and matches the | |
487 | * request of the caller allocate from it. Repeat until allocate has | |
488 | * succeeded or all heaps have been tried | |
489 | */ | |
54ac0784 KC |
490 | len = PAGE_ALIGN(len); |
491 | ||
a14baf71 CC |
492 | if (!len) |
493 | return ERR_PTR(-EINVAL); | |
494 | ||
8d7ab9a9 | 495 | down_read(&dev->lock); |
cd69488c | 496 | plist_for_each_entry(heap, &dev->heaps, node) { |
38eeeb51 RSZ |
497 | /* if the caller didn't specify this heap id */ |
498 | if (!((1 << heap->id) & heap_id_mask)) | |
c30707be RSZ |
499 | continue; |
500 | buffer = ion_buffer_create(heap, dev, len, align, flags); | |
9e907654 | 501 | if (!IS_ERR(buffer)) |
c30707be RSZ |
502 | break; |
503 | } | |
8d7ab9a9 | 504 | up_read(&dev->lock); |
c30707be | 505 | |
54ac0784 KC |
506 | if (buffer == NULL) |
507 | return ERR_PTR(-ENODEV); | |
508 | ||
509 | if (IS_ERR(buffer)) | |
464a5028 | 510 | return ERR_CAST(buffer); |
c30707be RSZ |
511 | |
512 | handle = ion_handle_create(client, buffer); | |
513 | ||
c30707be RSZ |
514 | /* |
515 | * ion_buffer_create will create a buffer with a ref_cnt of 1, | |
516 | * and ion_handle_create will take a second reference, drop one here | |
517 | */ | |
518 | ion_buffer_put(buffer); | |
519 | ||
47b40458 CC |
520 | if (IS_ERR(handle)) |
521 | return handle; | |
c30707be | 522 | |
47b40458 CC |
523 | mutex_lock(&client->lock); |
524 | ret = ion_handle_add(client, handle); | |
83271f62 | 525 | mutex_unlock(&client->lock); |
47b40458 CC |
526 | if (ret) { |
527 | ion_handle_put(handle); | |
528 | handle = ERR_PTR(ret); | |
529 | } | |
29ae6bc7 | 530 | |
c30707be RSZ |
531 | return handle; |
532 | } | |
ee4c8aa9 | 533 | EXPORT_SYMBOL(ion_alloc); |
c30707be RSZ |
534 | |
535 | void ion_free(struct ion_client *client, struct ion_handle *handle) | |
536 | { | |
537 | bool valid_handle; | |
538 | ||
539 | BUG_ON(client != handle->client); | |
540 | ||
541 | mutex_lock(&client->lock); | |
542 | valid_handle = ion_handle_validate(client, handle); | |
c30707be RSZ |
543 | |
544 | if (!valid_handle) { | |
a9bb075d | 545 | WARN(1, "%s: invalid handle passed to free.\n", __func__); |
37bdbf00 | 546 | mutex_unlock(&client->lock); |
c30707be RSZ |
547 | return; |
548 | } | |
0e9c03a5 | 549 | mutex_unlock(&client->lock); |
83271f62 | 550 | ion_handle_put(handle); |
c30707be | 551 | } |
ee4c8aa9 | 552 | EXPORT_SYMBOL(ion_free); |
c30707be | 553 | |
c30707be RSZ |
554 | int ion_phys(struct ion_client *client, struct ion_handle *handle, |
555 | ion_phys_addr_t *addr, size_t *len) | |
556 | { | |
557 | struct ion_buffer *buffer; | |
558 | int ret; | |
559 | ||
560 | mutex_lock(&client->lock); | |
561 | if (!ion_handle_validate(client, handle)) { | |
562 | mutex_unlock(&client->lock); | |
563 | return -EINVAL; | |
564 | } | |
565 | ||
566 | buffer = handle->buffer; | |
567 | ||
568 | if (!buffer->heap->ops->phys) { | |
d9954896 MH |
569 | pr_err("%s: ion_phys is not implemented by this heap (name=%s, type=%d).\n", |
570 | __func__, buffer->heap->name, buffer->heap->type); | |
c30707be RSZ |
571 | mutex_unlock(&client->lock); |
572 | return -ENODEV; | |
573 | } | |
574 | mutex_unlock(&client->lock); | |
575 | ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len); | |
576 | return ret; | |
577 | } | |
ee4c8aa9 | 578 | EXPORT_SYMBOL(ion_phys); |
c30707be | 579 | |
0f34faf8 RSZ |
580 | static void *ion_buffer_kmap_get(struct ion_buffer *buffer) |
581 | { | |
582 | void *vaddr; | |
583 | ||
584 | if (buffer->kmap_cnt) { | |
585 | buffer->kmap_cnt++; | |
586 | return buffer->vaddr; | |
587 | } | |
588 | vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer); | |
e1d855b0 JS |
589 | if (WARN_ONCE(vaddr == NULL, |
590 | "heap->ops->map_kernel should return ERR_PTR on error")) | |
9e907654 CC |
591 | return ERR_PTR(-EINVAL); |
592 | if (IS_ERR(vaddr)) | |
0f34faf8 RSZ |
593 | return vaddr; |
594 | buffer->vaddr = vaddr; | |
595 | buffer->kmap_cnt++; | |
596 | return vaddr; | |
597 | } | |
598 | ||
b892bf75 | 599 | static void *ion_handle_kmap_get(struct ion_handle *handle) |
c30707be | 600 | { |
b892bf75 | 601 | struct ion_buffer *buffer = handle->buffer; |
c30707be RSZ |
602 | void *vaddr; |
603 | ||
b892bf75 RSZ |
604 | if (handle->kmap_cnt) { |
605 | handle->kmap_cnt++; | |
606 | return buffer->vaddr; | |
c30707be | 607 | } |
0f34faf8 | 608 | vaddr = ion_buffer_kmap_get(buffer); |
9e907654 | 609 | if (IS_ERR(vaddr)) |
b892bf75 | 610 | return vaddr; |
b892bf75 | 611 | handle->kmap_cnt++; |
b892bf75 RSZ |
612 | return vaddr; |
613 | } | |
c30707be | 614 | |
0f34faf8 RSZ |
615 | static void ion_buffer_kmap_put(struct ion_buffer *buffer) |
616 | { | |
617 | buffer->kmap_cnt--; | |
618 | if (!buffer->kmap_cnt) { | |
619 | buffer->heap->ops->unmap_kernel(buffer->heap, buffer); | |
620 | buffer->vaddr = NULL; | |
621 | } | |
622 | } | |
623 | ||
b892bf75 RSZ |
624 | static void ion_handle_kmap_put(struct ion_handle *handle) |
625 | { | |
626 | struct ion_buffer *buffer = handle->buffer; | |
627 | ||
22f6b978 MH |
628 | if (!handle->kmap_cnt) { |
629 | WARN(1, "%s: Double unmap detected! bailing...\n", __func__); | |
630 | return; | |
631 | } | |
b892bf75 RSZ |
632 | handle->kmap_cnt--; |
633 | if (!handle->kmap_cnt) | |
0f34faf8 | 634 | ion_buffer_kmap_put(buffer); |
c30707be RSZ |
635 | } |
636 | ||
b892bf75 | 637 | void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle) |
c30707be RSZ |
638 | { |
639 | struct ion_buffer *buffer; | |
b892bf75 | 640 | void *vaddr; |
c30707be RSZ |
641 | |
642 | mutex_lock(&client->lock); | |
643 | if (!ion_handle_validate(client, handle)) { | |
b892bf75 | 644 | pr_err("%s: invalid handle passed to map_kernel.\n", |
c30707be RSZ |
645 | __func__); |
646 | mutex_unlock(&client->lock); | |
647 | return ERR_PTR(-EINVAL); | |
648 | } | |
b892bf75 | 649 | |
c30707be | 650 | buffer = handle->buffer; |
c30707be | 651 | |
b892bf75 | 652 | if (!handle->buffer->heap->ops->map_kernel) { |
c30707be RSZ |
653 | pr_err("%s: map_kernel is not implemented by this heap.\n", |
654 | __func__); | |
c30707be RSZ |
655 | mutex_unlock(&client->lock); |
656 | return ERR_PTR(-ENODEV); | |
657 | } | |
c30707be | 658 | |
c30707be | 659 | mutex_lock(&buffer->lock); |
b892bf75 | 660 | vaddr = ion_handle_kmap_get(handle); |
c30707be RSZ |
661 | mutex_unlock(&buffer->lock); |
662 | mutex_unlock(&client->lock); | |
b892bf75 | 663 | return vaddr; |
c30707be | 664 | } |
ee4c8aa9 | 665 | EXPORT_SYMBOL(ion_map_kernel); |
c30707be | 666 | |
b892bf75 | 667 | void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle) |
c30707be RSZ |
668 | { |
669 | struct ion_buffer *buffer; | |
670 | ||
671 | mutex_lock(&client->lock); | |
672 | buffer = handle->buffer; | |
673 | mutex_lock(&buffer->lock); | |
b892bf75 | 674 | ion_handle_kmap_put(handle); |
c30707be RSZ |
675 | mutex_unlock(&buffer->lock); |
676 | mutex_unlock(&client->lock); | |
677 | } | |
ee4c8aa9 | 678 | EXPORT_SYMBOL(ion_unmap_kernel); |
c30707be | 679 | |
c30707be RSZ |
680 | static int ion_debug_client_show(struct seq_file *s, void *unused) |
681 | { | |
682 | struct ion_client *client = s->private; | |
683 | struct rb_node *n; | |
38eeeb51 | 684 | size_t sizes[ION_NUM_HEAP_IDS] = {0}; |
f63958d8 | 685 | const char *names[ION_NUM_HEAP_IDS] = {NULL}; |
c30707be RSZ |
686 | int i; |
687 | ||
688 | mutex_lock(&client->lock); | |
689 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { | |
690 | struct ion_handle *handle = rb_entry(n, struct ion_handle, | |
691 | node); | |
38eeeb51 | 692 | unsigned int id = handle->buffer->heap->id; |
c30707be | 693 | |
38eeeb51 RSZ |
694 | if (!names[id]) |
695 | names[id] = handle->buffer->heap->name; | |
696 | sizes[id] += handle->buffer->size; | |
c30707be RSZ |
697 | } |
698 | mutex_unlock(&client->lock); | |
699 | ||
700 | seq_printf(s, "%16.16s: %16.16s\n", "heap_name", "size_in_bytes"); | |
38eeeb51 | 701 | for (i = 0; i < ION_NUM_HEAP_IDS; i++) { |
c30707be RSZ |
702 | if (!names[i]) |
703 | continue; | |
e61fc915 | 704 | seq_printf(s, "%16.16s: %16zu\n", names[i], sizes[i]); |
c30707be RSZ |
705 | } |
706 | return 0; | |
707 | } | |
708 | ||
709 | static int ion_debug_client_open(struct inode *inode, struct file *file) | |
710 | { | |
711 | return single_open(file, ion_debug_client_show, inode->i_private); | |
712 | } | |
713 | ||
714 | static const struct file_operations debug_client_fops = { | |
715 | .open = ion_debug_client_open, | |
716 | .read = seq_read, | |
717 | .llseek = seq_lseek, | |
718 | .release = single_release, | |
719 | }; | |
720 | ||
2803ac7b MH |
721 | static int ion_get_client_serial(const struct rb_root *root, |
722 | const unsigned char *name) | |
723 | { | |
724 | int serial = -1; | |
725 | struct rb_node *node; | |
10f62861 | 726 | |
2803ac7b MH |
727 | for (node = rb_first(root); node; node = rb_next(node)) { |
728 | struct ion_client *client = rb_entry(node, struct ion_client, | |
729 | node); | |
10f62861 | 730 | |
2803ac7b MH |
731 | if (strcmp(client->name, name)) |
732 | continue; | |
733 | serial = max(serial, client->display_serial); | |
734 | } | |
735 | return serial + 1; | |
736 | } | |
737 | ||
c30707be | 738 | struct ion_client *ion_client_create(struct ion_device *dev, |
c30707be RSZ |
739 | const char *name) |
740 | { | |
741 | struct ion_client *client; | |
742 | struct task_struct *task; | |
743 | struct rb_node **p; | |
744 | struct rb_node *parent = NULL; | |
745 | struct ion_client *entry; | |
c30707be RSZ |
746 | pid_t pid; |
747 | ||
2803ac7b MH |
748 | if (!name) { |
749 | pr_err("%s: Name cannot be null\n", __func__); | |
750 | return ERR_PTR(-EINVAL); | |
751 | } | |
752 | ||
c30707be RSZ |
753 | get_task_struct(current->group_leader); |
754 | task_lock(current->group_leader); | |
755 | pid = task_pid_nr(current->group_leader); | |
756 | /* don't bother to store task struct for kernel threads, | |
757 | they can't be killed anyway */ | |
758 | if (current->group_leader->flags & PF_KTHREAD) { | |
759 | put_task_struct(current->group_leader); | |
760 | task = NULL; | |
761 | } else { | |
762 | task = current->group_leader; | |
763 | } | |
764 | task_unlock(current->group_leader); | |
765 | ||
c30707be | 766 | client = kzalloc(sizeof(struct ion_client), GFP_KERNEL); |
ae5cbf4a MH |
767 | if (!client) |
768 | goto err_put_task_struct; | |
c30707be RSZ |
769 | |
770 | client->dev = dev; | |
771 | client->handles = RB_ROOT; | |
47b40458 | 772 | idr_init(&client->idr); |
c30707be | 773 | mutex_init(&client->lock); |
c30707be RSZ |
774 | client->task = task; |
775 | client->pid = pid; | |
ae5cbf4a MH |
776 | client->name = kstrdup(name, GFP_KERNEL); |
777 | if (!client->name) | |
778 | goto err_free_client; | |
c30707be | 779 | |
8d7ab9a9 | 780 | down_write(&dev->lock); |
2803ac7b MH |
781 | client->display_serial = ion_get_client_serial(&dev->clients, name); |
782 | client->display_name = kasprintf( | |
783 | GFP_KERNEL, "%s-%d", name, client->display_serial); | |
784 | if (!client->display_name) { | |
785 | up_write(&dev->lock); | |
786 | goto err_free_client_name; | |
787 | } | |
b892bf75 RSZ |
788 | p = &dev->clients.rb_node; |
789 | while (*p) { | |
790 | parent = *p; | |
791 | entry = rb_entry(parent, struct ion_client, node); | |
792 | ||
793 | if (client < entry) | |
794 | p = &(*p)->rb_left; | |
795 | else if (client > entry) | |
796 | p = &(*p)->rb_right; | |
c30707be | 797 | } |
b892bf75 RSZ |
798 | rb_link_node(&client->node, parent, p); |
799 | rb_insert_color(&client->node, &dev->clients); | |
c30707be | 800 | |
2803ac7b | 801 | client->debug_root = debugfs_create_file(client->display_name, 0664, |
b08585fb MH |
802 | dev->clients_debug_root, |
803 | client, &debug_client_fops); | |
804 | if (!client->debug_root) { | |
805 | char buf[256], *path; | |
04e14356 | 806 | |
b08585fb MH |
807 | path = dentry_path(dev->clients_debug_root, buf, 256); |
808 | pr_err("Failed to create client debugfs at %s/%s\n", | |
2803ac7b | 809 | path, client->display_name); |
b08585fb MH |
810 | } |
811 | ||
8d7ab9a9 | 812 | up_write(&dev->lock); |
c30707be RSZ |
813 | |
814 | return client; | |
ae5cbf4a | 815 | |
2803ac7b MH |
816 | err_free_client_name: |
817 | kfree(client->name); | |
ae5cbf4a MH |
818 | err_free_client: |
819 | kfree(client); | |
820 | err_put_task_struct: | |
821 | if (task) | |
822 | put_task_struct(current->group_leader); | |
823 | return ERR_PTR(-ENOMEM); | |
c30707be | 824 | } |
9122fe86 | 825 | EXPORT_SYMBOL(ion_client_create); |
c30707be | 826 | |
b892bf75 | 827 | void ion_client_destroy(struct ion_client *client) |
c30707be | 828 | { |
c30707be RSZ |
829 | struct ion_device *dev = client->dev; |
830 | struct rb_node *n; | |
831 | ||
832 | pr_debug("%s: %d\n", __func__, __LINE__); | |
833 | while ((n = rb_first(&client->handles))) { | |
834 | struct ion_handle *handle = rb_entry(n, struct ion_handle, | |
835 | node); | |
836 | ion_handle_destroy(&handle->ref); | |
837 | } | |
47b40458 | 838 | |
47b40458 CC |
839 | idr_destroy(&client->idr); |
840 | ||
8d7ab9a9 | 841 | down_write(&dev->lock); |
b892bf75 | 842 | if (client->task) |
c30707be | 843 | put_task_struct(client->task); |
b892bf75 | 844 | rb_erase(&client->node, &dev->clients); |
c30707be | 845 | debugfs_remove_recursive(client->debug_root); |
8d7ab9a9 | 846 | up_write(&dev->lock); |
c30707be | 847 | |
2803ac7b | 848 | kfree(client->display_name); |
ae5cbf4a | 849 | kfree(client->name); |
c30707be RSZ |
850 | kfree(client); |
851 | } | |
ee4c8aa9 | 852 | EXPORT_SYMBOL(ion_client_destroy); |
c30707be | 853 | |
ce1f147a RSZ |
854 | struct sg_table *ion_sg_table(struct ion_client *client, |
855 | struct ion_handle *handle) | |
c30707be | 856 | { |
29ae6bc7 | 857 | struct ion_buffer *buffer; |
b892bf75 | 858 | struct sg_table *table; |
c30707be | 859 | |
29ae6bc7 RSZ |
860 | mutex_lock(&client->lock); |
861 | if (!ion_handle_validate(client, handle)) { | |
862 | pr_err("%s: invalid handle passed to map_dma.\n", | |
b892bf75 | 863 | __func__); |
29ae6bc7 RSZ |
864 | mutex_unlock(&client->lock); |
865 | return ERR_PTR(-EINVAL); | |
54ac0784 | 866 | } |
29ae6bc7 RSZ |
867 | buffer = handle->buffer; |
868 | table = buffer->sg_table; | |
869 | mutex_unlock(&client->lock); | |
b892bf75 | 870 | return table; |
c30707be | 871 | } |
ee4c8aa9 | 872 | EXPORT_SYMBOL(ion_sg_table); |
c30707be | 873 | |
56a7c185 RSZ |
874 | static void ion_buffer_sync_for_device(struct ion_buffer *buffer, |
875 | struct device *dev, | |
876 | enum dma_data_direction direction); | |
877 | ||
29ae6bc7 RSZ |
878 | static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment, |
879 | enum dma_data_direction direction) | |
c30707be | 880 | { |
b892bf75 RSZ |
881 | struct dma_buf *dmabuf = attachment->dmabuf; |
882 | struct ion_buffer *buffer = dmabuf->priv; | |
c30707be | 883 | |
0b9ec1cf | 884 | ion_buffer_sync_for_device(buffer, attachment->dev, direction); |
29ae6bc7 RSZ |
885 | return buffer->sg_table; |
886 | } | |
887 | ||
888 | static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment, | |
889 | struct sg_table *table, | |
890 | enum dma_data_direction direction) | |
891 | { | |
c30707be RSZ |
892 | } |
893 | ||
e946b209 CC |
894 | void ion_pages_sync_for_device(struct device *dev, struct page *page, |
895 | size_t size, enum dma_data_direction dir) | |
896 | { | |
897 | struct scatterlist sg; | |
898 | ||
899 | sg_init_table(&sg, 1); | |
900 | sg_set_page(&sg, page, size, 0); | |
901 | /* | |
902 | * This is not correct - sg_dma_address needs a dma_addr_t that is valid | |
8e4ec4fe | 903 | * for the targeted device, but this works on the currently targeted |
e946b209 CC |
904 | * hardware. |
905 | */ | |
906 | sg_dma_address(&sg) = page_to_phys(page); | |
907 | dma_sync_sg_for_device(dev, &sg, 1, dir); | |
908 | } | |
909 | ||
56a7c185 RSZ |
910 | struct ion_vma_list { |
911 | struct list_head list; | |
912 | struct vm_area_struct *vma; | |
913 | }; | |
914 | ||
915 | static void ion_buffer_sync_for_device(struct ion_buffer *buffer, | |
916 | struct device *dev, | |
917 | enum dma_data_direction dir) | |
918 | { | |
56a7c185 | 919 | struct ion_vma_list *vma_list; |
c13bd1c4 RSZ |
920 | int pages = PAGE_ALIGN(buffer->size) / PAGE_SIZE; |
921 | int i; | |
56a7c185 RSZ |
922 | |
923 | pr_debug("%s: syncing for device %s\n", __func__, | |
924 | dev ? dev_name(dev) : "null"); | |
0b9ec1cf | 925 | |
13ba7805 | 926 | if (!ion_buffer_fault_user_mappings(buffer)) |
0b9ec1cf RSZ |
927 | return; |
928 | ||
56a7c185 | 929 | mutex_lock(&buffer->lock); |
c13bd1c4 RSZ |
930 | for (i = 0; i < pages; i++) { |
931 | struct page *page = buffer->pages[i]; | |
932 | ||
933 | if (ion_buffer_page_is_dirty(page)) | |
e946b209 CC |
934 | ion_pages_sync_for_device(dev, ion_buffer_page(page), |
935 | PAGE_SIZE, dir); | |
936 | ||
c13bd1c4 | 937 | ion_buffer_page_clean(buffer->pages + i); |
56a7c185 RSZ |
938 | } |
939 | list_for_each_entry(vma_list, &buffer->vmas, list) { | |
940 | struct vm_area_struct *vma = vma_list->vma; | |
941 | ||
942 | zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, | |
943 | NULL); | |
944 | } | |
945 | mutex_unlock(&buffer->lock); | |
946 | } | |
947 | ||
f63958d8 | 948 | static int ion_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
56a7c185 RSZ |
949 | { |
950 | struct ion_buffer *buffer = vma->vm_private_data; | |
462be0c6 | 951 | unsigned long pfn; |
c13bd1c4 | 952 | int ret; |
56a7c185 RSZ |
953 | |
954 | mutex_lock(&buffer->lock); | |
c13bd1c4 | 955 | ion_buffer_page_dirty(buffer->pages + vmf->pgoff); |
c13bd1c4 | 956 | BUG_ON(!buffer->pages || !buffer->pages[vmf->pgoff]); |
462be0c6 CC |
957 | |
958 | pfn = page_to_pfn(ion_buffer_page(buffer->pages[vmf->pgoff])); | |
959 | ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); | |
56a7c185 | 960 | mutex_unlock(&buffer->lock); |
c13bd1c4 RSZ |
961 | if (ret) |
962 | return VM_FAULT_ERROR; | |
963 | ||
56a7c185 RSZ |
964 | return VM_FAULT_NOPAGE; |
965 | } | |
966 | ||
967 | static void ion_vm_open(struct vm_area_struct *vma) | |
968 | { | |
969 | struct ion_buffer *buffer = vma->vm_private_data; | |
970 | struct ion_vma_list *vma_list; | |
971 | ||
972 | vma_list = kmalloc(sizeof(struct ion_vma_list), GFP_KERNEL); | |
973 | if (!vma_list) | |
974 | return; | |
975 | vma_list->vma = vma; | |
976 | mutex_lock(&buffer->lock); | |
977 | list_add(&vma_list->list, &buffer->vmas); | |
978 | mutex_unlock(&buffer->lock); | |
979 | pr_debug("%s: adding %p\n", __func__, vma); | |
980 | } | |
981 | ||
982 | static void ion_vm_close(struct vm_area_struct *vma) | |
983 | { | |
984 | struct ion_buffer *buffer = vma->vm_private_data; | |
985 | struct ion_vma_list *vma_list, *tmp; | |
986 | ||
987 | pr_debug("%s\n", __func__); | |
988 | mutex_lock(&buffer->lock); | |
989 | list_for_each_entry_safe(vma_list, tmp, &buffer->vmas, list) { | |
990 | if (vma_list->vma != vma) | |
991 | continue; | |
992 | list_del(&vma_list->list); | |
993 | kfree(vma_list); | |
994 | pr_debug("%s: deleting %p\n", __func__, vma); | |
995 | break; | |
996 | } | |
997 | mutex_unlock(&buffer->lock); | |
998 | } | |
999 | ||
f63958d8 | 1000 | static struct vm_operations_struct ion_vma_ops = { |
56a7c185 RSZ |
1001 | .open = ion_vm_open, |
1002 | .close = ion_vm_close, | |
1003 | .fault = ion_vm_fault, | |
1004 | }; | |
1005 | ||
b892bf75 | 1006 | static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) |
c30707be | 1007 | { |
b892bf75 | 1008 | struct ion_buffer *buffer = dmabuf->priv; |
56a7c185 | 1009 | int ret = 0; |
c30707be | 1010 | |
b892bf75 | 1011 | if (!buffer->heap->ops->map_user) { |
7287bb52 IM |
1012 | pr_err("%s: this heap does not define a method for mapping to userspace\n", |
1013 | __func__); | |
b892bf75 | 1014 | return -EINVAL; |
c30707be RSZ |
1015 | } |
1016 | ||
13ba7805 | 1017 | if (ion_buffer_fault_user_mappings(buffer)) { |
462be0c6 CC |
1018 | vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | |
1019 | VM_DONTDUMP; | |
56a7c185 RSZ |
1020 | vma->vm_private_data = buffer; |
1021 | vma->vm_ops = &ion_vma_ops; | |
1022 | ion_vm_open(vma); | |
856661d5 | 1023 | return 0; |
56a7c185 | 1024 | } |
b892bf75 | 1025 | |
856661d5 RSZ |
1026 | if (!(buffer->flags & ION_FLAG_CACHED)) |
1027 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); | |
1028 | ||
1029 | mutex_lock(&buffer->lock); | |
1030 | /* now map it to userspace */ | |
1031 | ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma); | |
1032 | mutex_unlock(&buffer->lock); | |
1033 | ||
b892bf75 | 1034 | if (ret) |
c30707be RSZ |
1035 | pr_err("%s: failure mapping buffer to userspace\n", |
1036 | __func__); | |
c30707be | 1037 | |
c30707be RSZ |
1038 | return ret; |
1039 | } | |
1040 | ||
b892bf75 RSZ |
1041 | static void ion_dma_buf_release(struct dma_buf *dmabuf) |
1042 | { | |
1043 | struct ion_buffer *buffer = dmabuf->priv; | |
10f62861 | 1044 | |
b892bf75 RSZ |
1045 | ion_buffer_put(buffer); |
1046 | } | |
c30707be | 1047 | |
b892bf75 | 1048 | static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset) |
c30707be | 1049 | { |
0f34faf8 | 1050 | struct ion_buffer *buffer = dmabuf->priv; |
10f62861 | 1051 | |
12edf53d | 1052 | return buffer->vaddr + offset * PAGE_SIZE; |
b892bf75 | 1053 | } |
c30707be | 1054 | |
b892bf75 RSZ |
1055 | static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset, |
1056 | void *ptr) | |
1057 | { | |
b892bf75 RSZ |
1058 | } |
1059 | ||
0f34faf8 RSZ |
1060 | static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, |
1061 | size_t len, | |
1062 | enum dma_data_direction direction) | |
b892bf75 | 1063 | { |
0f34faf8 RSZ |
1064 | struct ion_buffer *buffer = dmabuf->priv; |
1065 | void *vaddr; | |
1066 | ||
1067 | if (!buffer->heap->ops->map_kernel) { | |
1068 | pr_err("%s: map kernel is not implemented by this heap.\n", | |
1069 | __func__); | |
1070 | return -ENODEV; | |
1071 | } | |
1072 | ||
1073 | mutex_lock(&buffer->lock); | |
1074 | vaddr = ion_buffer_kmap_get(buffer); | |
1075 | mutex_unlock(&buffer->lock); | |
ab0c069a | 1076 | return PTR_ERR_OR_ZERO(vaddr); |
b892bf75 RSZ |
1077 | } |
1078 | ||
0f34faf8 RSZ |
1079 | static void ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, |
1080 | size_t len, | |
1081 | enum dma_data_direction direction) | |
b892bf75 | 1082 | { |
0f34faf8 | 1083 | struct ion_buffer *buffer = dmabuf->priv; |
c30707be | 1084 | |
0f34faf8 RSZ |
1085 | mutex_lock(&buffer->lock); |
1086 | ion_buffer_kmap_put(buffer); | |
1087 | mutex_unlock(&buffer->lock); | |
1088 | } | |
c30707be | 1089 | |
f63958d8 | 1090 | static struct dma_buf_ops dma_buf_ops = { |
b892bf75 RSZ |
1091 | .map_dma_buf = ion_map_dma_buf, |
1092 | .unmap_dma_buf = ion_unmap_dma_buf, | |
1093 | .mmap = ion_mmap, | |
1094 | .release = ion_dma_buf_release, | |
0f34faf8 RSZ |
1095 | .begin_cpu_access = ion_dma_buf_begin_cpu_access, |
1096 | .end_cpu_access = ion_dma_buf_end_cpu_access, | |
1097 | .kmap_atomic = ion_dma_buf_kmap, | |
1098 | .kunmap_atomic = ion_dma_buf_kunmap, | |
b892bf75 RSZ |
1099 | .kmap = ion_dma_buf_kmap, |
1100 | .kunmap = ion_dma_buf_kunmap, | |
1101 | }; | |
1102 | ||
22ba4322 JM |
1103 | struct dma_buf *ion_share_dma_buf(struct ion_client *client, |
1104 | struct ion_handle *handle) | |
b892bf75 | 1105 | { |
5605b188 | 1106 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
b892bf75 RSZ |
1107 | struct ion_buffer *buffer; |
1108 | struct dma_buf *dmabuf; | |
1109 | bool valid_handle; | |
d8fbe341 | 1110 | |
b892bf75 RSZ |
1111 | mutex_lock(&client->lock); |
1112 | valid_handle = ion_handle_validate(client, handle); | |
b892bf75 | 1113 | if (!valid_handle) { |
a9bb075d | 1114 | WARN(1, "%s: invalid handle passed to share.\n", __func__); |
83271f62 | 1115 | mutex_unlock(&client->lock); |
22ba4322 | 1116 | return ERR_PTR(-EINVAL); |
b892bf75 | 1117 | } |
b892bf75 RSZ |
1118 | buffer = handle->buffer; |
1119 | ion_buffer_get(buffer); | |
83271f62 CC |
1120 | mutex_unlock(&client->lock); |
1121 | ||
72449cb4 SS |
1122 | exp_info.ops = &dma_buf_ops; |
1123 | exp_info.size = buffer->size; | |
1124 | exp_info.flags = O_RDWR; | |
1125 | exp_info.priv = buffer; | |
1126 | ||
d8fbe341 | 1127 | dmabuf = dma_buf_export(&exp_info); |
b892bf75 RSZ |
1128 | if (IS_ERR(dmabuf)) { |
1129 | ion_buffer_put(buffer); | |
22ba4322 | 1130 | return dmabuf; |
b892bf75 | 1131 | } |
22ba4322 JM |
1132 | |
1133 | return dmabuf; | |
1134 | } | |
1135 | EXPORT_SYMBOL(ion_share_dma_buf); | |
1136 | ||
1137 | int ion_share_dma_buf_fd(struct ion_client *client, struct ion_handle *handle) | |
1138 | { | |
1139 | struct dma_buf *dmabuf; | |
1140 | int fd; | |
1141 | ||
1142 | dmabuf = ion_share_dma_buf(client, handle); | |
1143 | if (IS_ERR(dmabuf)) | |
1144 | return PTR_ERR(dmabuf); | |
1145 | ||
b892bf75 | 1146 | fd = dma_buf_fd(dmabuf, O_CLOEXEC); |
55808b8d | 1147 | if (fd < 0) |
b892bf75 | 1148 | dma_buf_put(dmabuf); |
55808b8d | 1149 | |
c30707be | 1150 | return fd; |
b892bf75 | 1151 | } |
22ba4322 | 1152 | EXPORT_SYMBOL(ion_share_dma_buf_fd); |
c30707be | 1153 | |
b892bf75 RSZ |
1154 | struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd) |
1155 | { | |
1156 | struct dma_buf *dmabuf; | |
1157 | struct ion_buffer *buffer; | |
1158 | struct ion_handle *handle; | |
47b40458 | 1159 | int ret; |
b892bf75 RSZ |
1160 | |
1161 | dmabuf = dma_buf_get(fd); | |
9e907654 | 1162 | if (IS_ERR(dmabuf)) |
464a5028 | 1163 | return ERR_CAST(dmabuf); |
b892bf75 RSZ |
1164 | /* if this memory came from ion */ |
1165 | ||
1166 | if (dmabuf->ops != &dma_buf_ops) { | |
1167 | pr_err("%s: can not import dmabuf from another exporter\n", | |
1168 | __func__); | |
1169 | dma_buf_put(dmabuf); | |
1170 | return ERR_PTR(-EINVAL); | |
1171 | } | |
1172 | buffer = dmabuf->priv; | |
1173 | ||
1174 | mutex_lock(&client->lock); | |
1175 | /* if a handle exists for this buffer just take a reference to it */ | |
1176 | handle = ion_handle_lookup(client, buffer); | |
9e907654 | 1177 | if (!IS_ERR(handle)) { |
b892bf75 | 1178 | ion_handle_get(handle); |
83271f62 | 1179 | mutex_unlock(&client->lock); |
b892bf75 RSZ |
1180 | goto end; |
1181 | } | |
83271f62 CC |
1182 | mutex_unlock(&client->lock); |
1183 | ||
b892bf75 | 1184 | handle = ion_handle_create(client, buffer); |
9e907654 | 1185 | if (IS_ERR(handle)) |
b892bf75 | 1186 | goto end; |
83271f62 CC |
1187 | |
1188 | mutex_lock(&client->lock); | |
47b40458 | 1189 | ret = ion_handle_add(client, handle); |
83271f62 | 1190 | mutex_unlock(&client->lock); |
47b40458 CC |
1191 | if (ret) { |
1192 | ion_handle_put(handle); | |
1193 | handle = ERR_PTR(ret); | |
1194 | } | |
83271f62 | 1195 | |
b892bf75 | 1196 | end: |
b892bf75 RSZ |
1197 | dma_buf_put(dmabuf); |
1198 | return handle; | |
c30707be | 1199 | } |
ee4c8aa9 | 1200 | EXPORT_SYMBOL(ion_import_dma_buf); |
c30707be | 1201 | |
0b9ec1cf RSZ |
1202 | static int ion_sync_for_device(struct ion_client *client, int fd) |
1203 | { | |
1204 | struct dma_buf *dmabuf; | |
1205 | struct ion_buffer *buffer; | |
1206 | ||
1207 | dmabuf = dma_buf_get(fd); | |
9e907654 | 1208 | if (IS_ERR(dmabuf)) |
0b9ec1cf RSZ |
1209 | return PTR_ERR(dmabuf); |
1210 | ||
1211 | /* if this memory came from ion */ | |
1212 | if (dmabuf->ops != &dma_buf_ops) { | |
1213 | pr_err("%s: can not sync dmabuf from another exporter\n", | |
1214 | __func__); | |
1215 | dma_buf_put(dmabuf); | |
1216 | return -EINVAL; | |
1217 | } | |
1218 | buffer = dmabuf->priv; | |
856661d5 RSZ |
1219 | |
1220 | dma_sync_sg_for_device(NULL, buffer->sg_table->sgl, | |
1221 | buffer->sg_table->nents, DMA_BIDIRECTIONAL); | |
0b9ec1cf RSZ |
1222 | dma_buf_put(dmabuf); |
1223 | return 0; | |
1224 | } | |
1225 | ||
db866e3d CC |
1226 | /* fix up the cases where the ioctl direction bits are incorrect */ |
1227 | static unsigned int ion_ioctl_dir(unsigned int cmd) | |
1228 | { | |
1229 | switch (cmd) { | |
1230 | case ION_IOC_SYNC: | |
1231 | case ION_IOC_FREE: | |
1232 | case ION_IOC_CUSTOM: | |
1233 | return _IOC_WRITE; | |
1234 | default: | |
1235 | return _IOC_DIR(cmd); | |
1236 | } | |
1237 | } | |
1238 | ||
c30707be RSZ |
1239 | static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
1240 | { | |
1241 | struct ion_client *client = filp->private_data; | |
db866e3d CC |
1242 | struct ion_device *dev = client->dev; |
1243 | struct ion_handle *cleanup_handle = NULL; | |
1244 | int ret = 0; | |
1245 | unsigned int dir; | |
1246 | ||
1247 | union { | |
1248 | struct ion_fd_data fd; | |
1249 | struct ion_allocation_data allocation; | |
1250 | struct ion_handle_data handle; | |
1251 | struct ion_custom_data custom; | |
1252 | } data; | |
1253 | ||
1254 | dir = ion_ioctl_dir(cmd); | |
1255 | ||
1256 | if (_IOC_SIZE(cmd) > sizeof(data)) | |
1257 | return -EINVAL; | |
1258 | ||
1259 | if (dir & _IOC_WRITE) | |
1260 | if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd))) | |
1261 | return -EFAULT; | |
c30707be RSZ |
1262 | |
1263 | switch (cmd) { | |
1264 | case ION_IOC_ALLOC: | |
1265 | { | |
47b40458 | 1266 | struct ion_handle *handle; |
c30707be | 1267 | |
db866e3d CC |
1268 | handle = ion_alloc(client, data.allocation.len, |
1269 | data.allocation.align, | |
1270 | data.allocation.heap_id_mask, | |
1271 | data.allocation.flags); | |
47b40458 CC |
1272 | if (IS_ERR(handle)) |
1273 | return PTR_ERR(handle); | |
1274 | ||
db866e3d | 1275 | data.allocation.handle = handle->id; |
54ac0784 | 1276 | |
db866e3d | 1277 | cleanup_handle = handle; |
c30707be RSZ |
1278 | break; |
1279 | } | |
1280 | case ION_IOC_FREE: | |
1281 | { | |
47b40458 | 1282 | struct ion_handle *handle; |
c30707be | 1283 | |
db866e3d | 1284 | handle = ion_handle_get_by_id(client, data.handle.handle); |
83271f62 CC |
1285 | if (IS_ERR(handle)) |
1286 | return PTR_ERR(handle); | |
47b40458 | 1287 | ion_free(client, handle); |
83271f62 | 1288 | ion_handle_put(handle); |
c30707be RSZ |
1289 | break; |
1290 | } | |
c30707be | 1291 | case ION_IOC_SHARE: |
df0f6c76 | 1292 | case ION_IOC_MAP: |
c30707be | 1293 | { |
47b40458 | 1294 | struct ion_handle *handle; |
c30707be | 1295 | |
db866e3d | 1296 | handle = ion_handle_get_by_id(client, data.handle.handle); |
83271f62 CC |
1297 | if (IS_ERR(handle)) |
1298 | return PTR_ERR(handle); | |
db866e3d | 1299 | data.fd.fd = ion_share_dma_buf_fd(client, handle); |
83271f62 | 1300 | ion_handle_put(handle); |
db866e3d CC |
1301 | if (data.fd.fd < 0) |
1302 | ret = data.fd.fd; | |
c30707be RSZ |
1303 | break; |
1304 | } | |
1305 | case ION_IOC_IMPORT: | |
1306 | { | |
47b40458 | 1307 | struct ion_handle *handle; |
10f62861 | 1308 | |
db866e3d | 1309 | handle = ion_import_dma_buf(client, data.fd.fd); |
47b40458 CC |
1310 | if (IS_ERR(handle)) |
1311 | ret = PTR_ERR(handle); | |
1312 | else | |
db866e3d | 1313 | data.handle.handle = handle->id; |
c30707be RSZ |
1314 | break; |
1315 | } | |
0b9ec1cf RSZ |
1316 | case ION_IOC_SYNC: |
1317 | { | |
db866e3d | 1318 | ret = ion_sync_for_device(client, data.fd.fd); |
0b9ec1cf RSZ |
1319 | break; |
1320 | } | |
c30707be RSZ |
1321 | case ION_IOC_CUSTOM: |
1322 | { | |
c30707be RSZ |
1323 | if (!dev->custom_ioctl) |
1324 | return -ENOTTY; | |
db866e3d CC |
1325 | ret = dev->custom_ioctl(client, data.custom.cmd, |
1326 | data.custom.arg); | |
1327 | break; | |
c30707be RSZ |
1328 | } |
1329 | default: | |
1330 | return -ENOTTY; | |
1331 | } | |
db866e3d CC |
1332 | |
1333 | if (dir & _IOC_READ) { | |
1334 | if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd))) { | |
1335 | if (cleanup_handle) | |
1336 | ion_free(client, cleanup_handle); | |
1337 | return -EFAULT; | |
1338 | } | |
1339 | } | |
1340 | return ret; | |
c30707be RSZ |
1341 | } |
1342 | ||
1343 | static int ion_release(struct inode *inode, struct file *file) | |
1344 | { | |
1345 | struct ion_client *client = file->private_data; | |
1346 | ||
1347 | pr_debug("%s: %d\n", __func__, __LINE__); | |
b892bf75 | 1348 | ion_client_destroy(client); |
c30707be RSZ |
1349 | return 0; |
1350 | } | |
1351 | ||
1352 | static int ion_open(struct inode *inode, struct file *file) | |
1353 | { | |
1354 | struct miscdevice *miscdev = file->private_data; | |
1355 | struct ion_device *dev = container_of(miscdev, struct ion_device, dev); | |
1356 | struct ion_client *client; | |
483ed03f | 1357 | char debug_name[64]; |
c30707be RSZ |
1358 | |
1359 | pr_debug("%s: %d\n", __func__, __LINE__); | |
483ed03f LA |
1360 | snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader)); |
1361 | client = ion_client_create(dev, debug_name); | |
9e907654 | 1362 | if (IS_ERR(client)) |
c30707be RSZ |
1363 | return PTR_ERR(client); |
1364 | file->private_data = client; | |
1365 | ||
1366 | return 0; | |
1367 | } | |
1368 | ||
1369 | static const struct file_operations ion_fops = { | |
1370 | .owner = THIS_MODULE, | |
1371 | .open = ion_open, | |
1372 | .release = ion_release, | |
1373 | .unlocked_ioctl = ion_ioctl, | |
827c849e | 1374 | .compat_ioctl = compat_ion_ioctl, |
c30707be RSZ |
1375 | }; |
1376 | ||
1377 | static size_t ion_debug_heap_total(struct ion_client *client, | |
2bb9f503 | 1378 | unsigned int id) |
c30707be RSZ |
1379 | { |
1380 | size_t size = 0; | |
1381 | struct rb_node *n; | |
1382 | ||
1383 | mutex_lock(&client->lock); | |
1384 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { | |
1385 | struct ion_handle *handle = rb_entry(n, | |
1386 | struct ion_handle, | |
1387 | node); | |
2bb9f503 | 1388 | if (handle->buffer->heap->id == id) |
c30707be RSZ |
1389 | size += handle->buffer->size; |
1390 | } | |
1391 | mutex_unlock(&client->lock); | |
1392 | return size; | |
1393 | } | |
1394 | ||
1395 | static int ion_debug_heap_show(struct seq_file *s, void *unused) | |
1396 | { | |
1397 | struct ion_heap *heap = s->private; | |
1398 | struct ion_device *dev = heap->dev; | |
1399 | struct rb_node *n; | |
5ad7bc3a RSZ |
1400 | size_t total_size = 0; |
1401 | size_t total_orphaned_size = 0; | |
c30707be | 1402 | |
b5693964 | 1403 | seq_printf(s, "%16s %16s %16s\n", "client", "pid", "size"); |
164ad86d | 1404 | seq_puts(s, "----------------------------------------------------\n"); |
c30707be | 1405 | |
b892bf75 | 1406 | for (n = rb_first(&dev->clients); n; n = rb_next(n)) { |
c30707be RSZ |
1407 | struct ion_client *client = rb_entry(n, struct ion_client, |
1408 | node); | |
2bb9f503 | 1409 | size_t size = ion_debug_heap_total(client, heap->id); |
10f62861 | 1410 | |
c30707be RSZ |
1411 | if (!size) |
1412 | continue; | |
b892bf75 RSZ |
1413 | if (client->task) { |
1414 | char task_comm[TASK_COMM_LEN]; | |
1415 | ||
1416 | get_task_comm(task_comm, client->task); | |
b5693964 | 1417 | seq_printf(s, "%16s %16u %16zu\n", task_comm, |
b892bf75 RSZ |
1418 | client->pid, size); |
1419 | } else { | |
b5693964 | 1420 | seq_printf(s, "%16s %16u %16zu\n", client->name, |
b892bf75 RSZ |
1421 | client->pid, size); |
1422 | } | |
c30707be | 1423 | } |
164ad86d IM |
1424 | seq_puts(s, "----------------------------------------------------\n"); |
1425 | seq_puts(s, "orphaned allocations (info is from last known client):\n"); | |
8d7ab9a9 | 1426 | mutex_lock(&dev->buffer_lock); |
5ad7bc3a RSZ |
1427 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
1428 | struct ion_buffer *buffer = rb_entry(n, struct ion_buffer, | |
1429 | node); | |
2bb9f503 | 1430 | if (buffer->heap->id != heap->id) |
45b17a80 RSZ |
1431 | continue; |
1432 | total_size += buffer->size; | |
5ad7bc3a | 1433 | if (!buffer->handle_count) { |
b5693964 | 1434 | seq_printf(s, "%16s %16u %16zu %d %d\n", |
e61fc915 CC |
1435 | buffer->task_comm, buffer->pid, |
1436 | buffer->size, buffer->kmap_cnt, | |
092c354b | 1437 | atomic_read(&buffer->ref.refcount)); |
5ad7bc3a RSZ |
1438 | total_orphaned_size += buffer->size; |
1439 | } | |
1440 | } | |
8d7ab9a9 | 1441 | mutex_unlock(&dev->buffer_lock); |
164ad86d | 1442 | seq_puts(s, "----------------------------------------------------\n"); |
b5693964 | 1443 | seq_printf(s, "%16s %16zu\n", "total orphaned", |
5ad7bc3a | 1444 | total_orphaned_size); |
b5693964 | 1445 | seq_printf(s, "%16s %16zu\n", "total ", total_size); |
2540c73a | 1446 | if (heap->flags & ION_HEAP_FLAG_DEFER_FREE) |
b5693964 | 1447 | seq_printf(s, "%16s %16zu\n", "deferred free", |
2540c73a | 1448 | heap->free_list_size); |
164ad86d | 1449 | seq_puts(s, "----------------------------------------------------\n"); |
45b17a80 RSZ |
1450 | |
1451 | if (heap->debug_show) | |
1452 | heap->debug_show(heap, s, unused); | |
5ad7bc3a | 1453 | |
c30707be RSZ |
1454 | return 0; |
1455 | } | |
1456 | ||
1457 | static int ion_debug_heap_open(struct inode *inode, struct file *file) | |
1458 | { | |
1459 | return single_open(file, ion_debug_heap_show, inode->i_private); | |
1460 | } | |
1461 | ||
1462 | static const struct file_operations debug_heap_fops = { | |
1463 | .open = ion_debug_heap_open, | |
1464 | .read = seq_read, | |
1465 | .llseek = seq_lseek, | |
1466 | .release = single_release, | |
1467 | }; | |
1468 | ||
ea313b5f | 1469 | static int debug_shrink_set(void *data, u64 val) |
fe2faea7 | 1470 | { |
e1d855b0 JS |
1471 | struct ion_heap *heap = data; |
1472 | struct shrink_control sc; | |
1473 | int objs; | |
fe2faea7 | 1474 | |
e1d855b0 | 1475 | sc.gfp_mask = -1; |
aeb7fa7b | 1476 | sc.nr_to_scan = val; |
fe2faea7 | 1477 | |
aeb7fa7b GK |
1478 | if (!val) { |
1479 | objs = heap->shrinker.count_objects(&heap->shrinker, &sc); | |
1480 | sc.nr_to_scan = objs; | |
1481 | } | |
fe2faea7 | 1482 | |
aeb7fa7b | 1483 | heap->shrinker.scan_objects(&heap->shrinker, &sc); |
e1d855b0 | 1484 | return 0; |
fe2faea7 RSZ |
1485 | } |
1486 | ||
ea313b5f | 1487 | static int debug_shrink_get(void *data, u64 *val) |
fe2faea7 | 1488 | { |
e1d855b0 JS |
1489 | struct ion_heap *heap = data; |
1490 | struct shrink_control sc; | |
1491 | int objs; | |
fe2faea7 | 1492 | |
e1d855b0 JS |
1493 | sc.gfp_mask = -1; |
1494 | sc.nr_to_scan = 0; | |
fe2faea7 | 1495 | |
aeb7fa7b | 1496 | objs = heap->shrinker.count_objects(&heap->shrinker, &sc); |
e1d855b0 JS |
1497 | *val = objs; |
1498 | return 0; | |
fe2faea7 RSZ |
1499 | } |
1500 | ||
ea313b5f | 1501 | DEFINE_SIMPLE_ATTRIBUTE(debug_shrink_fops, debug_shrink_get, |
e1d855b0 | 1502 | debug_shrink_set, "%llu\n"); |
ea313b5f | 1503 | |
c30707be RSZ |
1504 | void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap) |
1505 | { | |
b08585fb MH |
1506 | struct dentry *debug_file; |
1507 | ||
29ae6bc7 RSZ |
1508 | if (!heap->ops->allocate || !heap->ops->free || !heap->ops->map_dma || |
1509 | !heap->ops->unmap_dma) | |
1510 | pr_err("%s: can not add heap with invalid ops struct.\n", | |
1511 | __func__); | |
1512 | ||
95e53ddd MH |
1513 | spin_lock_init(&heap->free_lock); |
1514 | heap->free_list_size = 0; | |
1515 | ||
ea313b5f RSZ |
1516 | if (heap->flags & ION_HEAP_FLAG_DEFER_FREE) |
1517 | ion_heap_init_deferred_free(heap); | |
fe2faea7 | 1518 | |
b9daf0b6 CC |
1519 | if ((heap->flags & ION_HEAP_FLAG_DEFER_FREE) || heap->ops->shrink) |
1520 | ion_heap_init_shrinker(heap); | |
1521 | ||
c30707be | 1522 | heap->dev = dev; |
8d7ab9a9 | 1523 | down_write(&dev->lock); |
cd69488c RSZ |
1524 | /* use negative heap->id to reverse the priority -- when traversing |
1525 | the list later attempt higher id numbers first */ | |
1526 | plist_node_init(&heap->node, -heap->id); | |
1527 | plist_add(&heap->node, &dev->heaps); | |
b08585fb MH |
1528 | debug_file = debugfs_create_file(heap->name, 0664, |
1529 | dev->heaps_debug_root, heap, | |
1530 | &debug_heap_fops); | |
1531 | ||
1532 | if (!debug_file) { | |
1533 | char buf[256], *path; | |
10f62861 | 1534 | |
b08585fb MH |
1535 | path = dentry_path(dev->heaps_debug_root, buf, 256); |
1536 | pr_err("Failed to create heap debugfs at %s/%s\n", | |
1537 | path, heap->name); | |
1538 | } | |
1539 | ||
aeb7fa7b | 1540 | if (heap->shrinker.count_objects && heap->shrinker.scan_objects) { |
ea313b5f RSZ |
1541 | char debug_name[64]; |
1542 | ||
1543 | snprintf(debug_name, 64, "%s_shrink", heap->name); | |
b08585fb MH |
1544 | debug_file = debugfs_create_file( |
1545 | debug_name, 0644, dev->heaps_debug_root, heap, | |
1546 | &debug_shrink_fops); | |
1547 | if (!debug_file) { | |
1548 | char buf[256], *path; | |
10f62861 | 1549 | |
b08585fb MH |
1550 | path = dentry_path(dev->heaps_debug_root, buf, 256); |
1551 | pr_err("Failed to create heap shrinker debugfs at %s/%s\n", | |
1552 | path, debug_name); | |
1553 | } | |
ea313b5f | 1554 | } |
aeb7fa7b | 1555 | |
8d7ab9a9 | 1556 | up_write(&dev->lock); |
c30707be RSZ |
1557 | } |
1558 | ||
1559 | struct ion_device *ion_device_create(long (*custom_ioctl) | |
1560 | (struct ion_client *client, | |
1561 | unsigned int cmd, | |
1562 | unsigned long arg)) | |
1563 | { | |
1564 | struct ion_device *idev; | |
1565 | int ret; | |
1566 | ||
1567 | idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL); | |
1568 | if (!idev) | |
1569 | return ERR_PTR(-ENOMEM); | |
1570 | ||
1571 | idev->dev.minor = MISC_DYNAMIC_MINOR; | |
1572 | idev->dev.name = "ion"; | |
1573 | idev->dev.fops = &ion_fops; | |
1574 | idev->dev.parent = NULL; | |
1575 | ret = misc_register(&idev->dev); | |
1576 | if (ret) { | |
1577 | pr_err("ion: failed to register misc device.\n"); | |
283d9304 | 1578 | kfree(idev); |
c30707be RSZ |
1579 | return ERR_PTR(ret); |
1580 | } | |
1581 | ||
1582 | idev->debug_root = debugfs_create_dir("ion", NULL); | |
b08585fb MH |
1583 | if (!idev->debug_root) { |
1584 | pr_err("ion: failed to create debugfs root directory.\n"); | |
1585 | goto debugfs_done; | |
1586 | } | |
1587 | idev->heaps_debug_root = debugfs_create_dir("heaps", idev->debug_root); | |
1588 | if (!idev->heaps_debug_root) { | |
1589 | pr_err("ion: failed to create debugfs heaps directory.\n"); | |
1590 | goto debugfs_done; | |
1591 | } | |
1592 | idev->clients_debug_root = debugfs_create_dir("clients", | |
1593 | idev->debug_root); | |
1594 | if (!idev->clients_debug_root) | |
1595 | pr_err("ion: failed to create debugfs clients directory.\n"); | |
1596 | ||
1597 | debugfs_done: | |
c30707be RSZ |
1598 | |
1599 | idev->custom_ioctl = custom_ioctl; | |
1600 | idev->buffers = RB_ROOT; | |
8d7ab9a9 RSZ |
1601 | mutex_init(&idev->buffer_lock); |
1602 | init_rwsem(&idev->lock); | |
cd69488c | 1603 | plist_head_init(&idev->heaps); |
b892bf75 | 1604 | idev->clients = RB_ROOT; |
c30707be RSZ |
1605 | return idev; |
1606 | } | |
1607 | ||
1608 | void ion_device_destroy(struct ion_device *dev) | |
1609 | { | |
1610 | misc_deregister(&dev->dev); | |
b08585fb | 1611 | debugfs_remove_recursive(dev->debug_root); |
c30707be RSZ |
1612 | /* XXX need to free the heaps and clients ? */ |
1613 | kfree(dev); | |
1614 | } | |
2991b7a0 RSZ |
1615 | |
1616 | void __init ion_reserve(struct ion_platform_data *data) | |
1617 | { | |
fa9bba55 | 1618 | int i; |
2991b7a0 RSZ |
1619 | |
1620 | for (i = 0; i < data->nr; i++) { | |
1621 | if (data->heaps[i].size == 0) | |
1622 | continue; | |
fa9bba55 RSZ |
1623 | |
1624 | if (data->heaps[i].base == 0) { | |
1625 | phys_addr_t paddr; | |
10f62861 | 1626 | |
fa9bba55 RSZ |
1627 | paddr = memblock_alloc_base(data->heaps[i].size, |
1628 | data->heaps[i].align, | |
1629 | MEMBLOCK_ALLOC_ANYWHERE); | |
1630 | if (!paddr) { | |
51108985 | 1631 | pr_err("%s: error allocating memblock for heap %d\n", |
fa9bba55 RSZ |
1632 | __func__, i); |
1633 | continue; | |
1634 | } | |
1635 | data->heaps[i].base = paddr; | |
1636 | } else { | |
1637 | int ret = memblock_reserve(data->heaps[i].base, | |
1638 | data->heaps[i].size); | |
1639 | if (ret) | |
e61fc915 | 1640 | pr_err("memblock reserve of %zx@%lx failed\n", |
fa9bba55 RSZ |
1641 | data->heaps[i].size, |
1642 | data->heaps[i].base); | |
1643 | } | |
e61fc915 | 1644 | pr_info("%s: %s reserved base %lx size %zu\n", __func__, |
fa9bba55 RSZ |
1645 | data->heaps[i].name, |
1646 | data->heaps[i].base, | |
1647 | data->heaps[i].size); | |
2991b7a0 RSZ |
1648 | } |
1649 | } |