2 * drivers/video/tegra/nvmap/nvmap_handle.c
4 * Handle allocation and freeing routines for nvmap
6 * Copyright (c) 2009-2014, NVIDIA CORPORATION. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #define pr_fmt(fmt) "%s: " fmt, __func__
25 #include <linux/err.h>
26 #include <linux/kernel.h>
27 #include <linux/list.h>
29 #include <linux/rbtree.h>
30 #include <linux/dma-buf.h>
31 #include <linux/moduleparam.h>
32 #include <linux/nvmap.h>
33 #include <linux/tegra-soc.h>
35 #include <asm/pgtable.h>
37 #include <trace/events/nvmap.h>
39 #include "nvmap_priv.h"
40 #include "nvmap_ioctl.h"
43 u32 nvmap_max_handle_count;
45 static int zero_memory_set(const char *arg, const struct kernel_param *kp)
47 param_set_bool(arg, kp);
48 nvmap_page_pool_clear();
52 static struct kernel_param_ops zero_memory_ops = {
53 .get = param_get_bool,
54 .set = zero_memory_set,
57 module_param_cb(zero_memory, &zero_memory_ops, &zero_memory, 0644);
60 #define NVMAP_SECURE_HEAPS (NVMAP_HEAP_CARVEOUT_IRAM | NVMAP_HEAP_IOVMM | \
61 NVMAP_HEAP_CARVEOUT_VPR)
63 /* handles may be arbitrarily large (16+MiB), and any handle allocated from
64 * the kernel (i.e., not a carveout handle) includes its array of pages. to
65 * preserve kmalloc space, if the array of pages exceeds PAGELIST_VMALLOC_MIN,
66 * the array is allocated using vmalloc. */
67 #define PAGELIST_VMALLOC_MIN (PAGE_SIZE)
69 void *nvmap_altalloc(size_t len)
71 if (len > PAGELIST_VMALLOC_MIN)
74 return kmalloc(len, GFP_KERNEL);
77 void nvmap_altfree(void *ptr, size_t len)
82 if (len > PAGELIST_VMALLOC_MIN)
88 void _nvmap_handle_free(struct nvmap_handle *h)
90 unsigned int i, nr_page, page_index = 0;
91 #ifdef CONFIG_NVMAP_PAGE_POOLS
92 struct nvmap_page_pool *pool = NULL;
96 h->nvhost_priv_delete(h->nvhost_priv);
98 if (nvmap_handle_remove(h->dev, h) != 0)
104 nvmap_stats_inc(NS_RELEASE, h->size);
105 nvmap_stats_dec(NS_TOTAL, PAGE_ALIGN(h->orig_size));
106 if (!h->heap_pgalloc) {
107 nvmap_heap_free(h->carveout);
111 nr_page = DIV_ROUND_UP(h->size, PAGE_SIZE);
113 BUG_ON(h->size & ~PAGE_MASK);
114 BUG_ON(!h->pgalloc.pages);
116 #ifdef NVMAP_LAZY_VFREE
119 vm_unmap_ram(h->vaddr, h->size >> PAGE_SHIFT);
123 for (i = 0; i < nr_page; i++)
124 h->pgalloc.pages[i] = nvmap_to_page(h->pgalloc.pages[i]);
126 #ifdef CONFIG_NVMAP_PAGE_POOLS
128 pool = &nvmap_dev->pool;
130 while (page_index < nr_page) {
131 if (!nvmap_page_pool_fill(pool,
132 h->pgalloc.pages[page_index]))
140 for (i = page_index; i < nr_page; i++)
141 __free_page(h->pgalloc.pages[i]);
143 nvmap_altfree(h->pgalloc.pages, nr_page * sizeof(struct page *));
149 static struct page *nvmap_alloc_pages_exact(gfp_t gfp, size_t size)
151 struct page *page, *p, *e;
154 size = PAGE_ALIGN(size);
155 order = get_order(size);
156 page = alloc_pages(gfp, order);
161 split_page(page, order);
162 e = page + (1 << order);
163 for (p = page + (size >> PAGE_SHIFT); p < e; p++)
169 static int handle_page_alloc(struct nvmap_client *client,
170 struct nvmap_handle *h, bool contiguous)
173 size_t size = PAGE_ALIGN(h->size);
174 unsigned int nr_page = size >> PAGE_SHIFT;
176 unsigned int i = 0, page_index = 0;
178 #ifdef CONFIG_NVMAP_PAGE_POOLS
179 struct nvmap_page_pool *pool = NULL;
182 gfp_t gfp = GFP_NVMAP;
189 pages = nvmap_altalloc(nr_page * sizeof(*pages));
193 prot = nvmap_pgprot(h, PG_PROT_KERNEL);
197 page = nvmap_alloc_pages_exact(gfp, size);
201 for (i = 0; i < nr_page; i++)
202 pages[i] = nth_page(page, i);
205 #ifdef CONFIG_NVMAP_PAGE_POOLS
206 pool = &nvmap_dev->pool;
208 for (i = 0; i < nr_page; i++) {
209 /* Get pages from pool, if available. */
210 pages[i] = nvmap_page_pool_alloc(pool);
216 for (i = page_index; i < nr_page; i++) {
217 pages[i] = nvmap_alloc_pages_exact(gfp, PAGE_SIZE);
224 * Make sure any data in the caches is flushed out before
225 * passing these pages to userspace. otherwise, It can lead to
226 * corruption in pages that get mapped as something other than WB in
227 * userspace and leaked kernel data structures.
229 nvmap_flush_cache(pages, nr_page);
235 h->pgalloc.pages = pages;
236 h->pgalloc.contig = contiguous;
237 atomic_set(&h->pgalloc.ndirty, 0);
242 __free_page(pages[i]);
243 nvmap_altfree(pages, nr_page * sizeof(*pages));
248 static void alloc_handle(struct nvmap_client *client,
249 struct nvmap_handle *h, unsigned int type)
251 unsigned int carveout_mask = NVMAP_HEAP_CARVEOUT_MASK;
252 unsigned int iovmm_mask = NVMAP_HEAP_IOVMM;
254 BUG_ON(type & (type - 1));
256 #ifdef CONFIG_NVMAP_CONVERT_CARVEOUT_TO_IOVMM
257 /* Convert generic carveout requests to iovmm requests. */
258 carveout_mask &= ~NVMAP_HEAP_CARVEOUT_GENERIC;
259 iovmm_mask |= NVMAP_HEAP_CARVEOUT_GENERIC;
262 if (type & carveout_mask) {
263 struct nvmap_heap_block *b;
265 b = nvmap_carveout_alloc(client, h, type);
268 h->heap_pgalloc = false;
269 /* barrier to ensure all handle alloc data
270 * is visible before alloc is seen by other
276 } else if (type & iovmm_mask) {
279 ret = handle_page_alloc(client, h,
280 h->userflags & NVMAP_HANDLE_PHYS_CONTIG);
283 h->heap_type = NVMAP_HEAP_IOVMM;
284 h->heap_pgalloc = true;
290 /* small allocations will try to allocate from generic OS memory before
291 * any of the limited heaps, to increase the effective memory for graphics
292 * allocations, and to reduce fragmentation of the graphics heaps with
293 * sub-page splinters */
294 static const unsigned int heap_policy_small[] = {
295 NVMAP_HEAP_CARVEOUT_VPR,
296 NVMAP_HEAP_CARVEOUT_IRAM,
297 NVMAP_HEAP_CARVEOUT_MASK,
302 static const unsigned int heap_policy_large[] = {
303 NVMAP_HEAP_CARVEOUT_VPR,
304 NVMAP_HEAP_CARVEOUT_IRAM,
306 NVMAP_HEAP_CARVEOUT_MASK,
310 int nvmap_alloc_handle(struct nvmap_client *client,
311 struct nvmap_handle *h, unsigned int heap_mask,
316 const unsigned int *alloc_policy;
320 h = nvmap_handle_get(h);
330 nvmap_stats_inc(NS_TOTAL, PAGE_ALIGN(h->orig_size));
331 nvmap_stats_inc(NS_ALLOC, PAGE_ALIGN(h->size));
332 trace_nvmap_alloc_handle(client, h,
333 h->size, heap_mask, align, flags,
334 nvmap_stats_read(NS_TOTAL),
335 nvmap_stats_read(NS_ALLOC));
336 h->userflags = flags;
337 nr_page = ((h->size + PAGE_SIZE - 1) >> PAGE_SHIFT);
338 h->secure = !!(flags & NVMAP_HANDLE_SECURE);
339 h->flags = (flags & NVMAP_HANDLE_CACHE_FLAG);
340 h->align = max_t(size_t, align, L1_CACHE_BYTES);
342 h->map_resources = 0;
344 #ifndef CONFIG_TEGRA_IOVMM
345 /* convert iovmm requests to generic carveout. */
346 if (heap_mask & NVMAP_HEAP_IOVMM) {
347 heap_mask = (heap_mask & ~NVMAP_HEAP_IOVMM) |
348 NVMAP_HEAP_CARVEOUT_GENERIC;
351 /* secure allocations can only be served from secure heaps */
353 heap_mask &= NVMAP_SECURE_HEAPS;
360 alloc_policy = (nr_page == 1) ? heap_policy_small : heap_policy_large;
362 while (!h->alloc && *alloc_policy) {
363 unsigned int heap_type;
365 heap_type = *alloc_policy++;
366 heap_type &= heap_mask;
371 heap_mask &= ~heap_type;
373 while (heap_type && !h->alloc) {
376 /* iterate possible heaps MSB-to-LSB, since higher-
377 * priority carveouts will have higher usage masks */
378 heap = 1 << __fls(heap_type);
379 alloc_handle(client, h, heap);
386 if (client->kernel_client)
387 nvmap_stats_inc(NS_KALLOC, h->size);
389 nvmap_stats_inc(NS_UALLOC, h->size);
391 nvmap_stats_dec(NS_TOTAL, PAGE_ALIGN(h->orig_size));
392 nvmap_stats_dec(NS_ALLOC, PAGE_ALIGN(h->orig_size));
395 err = (h->alloc) ? 0 : err;
400 void nvmap_free_handle(struct nvmap_client *client,
401 struct nvmap_handle *handle)
403 struct nvmap_handle_ref *ref;
404 struct nvmap_handle *h;
407 nvmap_ref_lock(client);
409 ref = __nvmap_validate_locked(client, handle);
411 nvmap_ref_unlock(client);
415 trace_nvmap_free_handle(client, handle);
416 BUG_ON(!ref->handle);
419 if (atomic_dec_return(&ref->dupes)) {
420 nvmap_ref_unlock(client);
425 pins = atomic_read(&ref->pin);
426 rb_erase(&ref->node, &client->handle_refs);
427 client->handle_count--;
428 atomic_dec(&ref->handle->share_count);
430 nvmap_ref_unlock(client);
433 nvmap_debug(client, "%s freeing pinned handle %p\n",
434 current->group_leader->comm, h);
436 while (atomic_read(&ref->pin))
439 if (h->owner == client) {
444 dma_buf_put(ref->handle->dmabuf);
448 BUG_ON(!atomic_read(&h->ref));
451 EXPORT_SYMBOL(nvmap_free_handle);
453 void nvmap_free_handle_user_id(struct nvmap_client *client,
454 unsigned long user_id)
456 nvmap_free_handle(client, unmarshal_user_id(user_id));
459 static void add_handle_ref(struct nvmap_client *client,
460 struct nvmap_handle_ref *ref)
462 struct rb_node **p, *parent = NULL;
464 nvmap_ref_lock(client);
465 p = &client->handle_refs.rb_node;
467 struct nvmap_handle_ref *node;
469 node = rb_entry(parent, struct nvmap_handle_ref, node);
470 if (ref->handle > node->handle)
471 p = &parent->rb_right;
473 p = &parent->rb_left;
475 rb_link_node(&ref->node, parent, p);
476 rb_insert_color(&ref->node, &client->handle_refs);
477 client->handle_count++;
478 if (client->handle_count > nvmap_max_handle_count)
479 nvmap_max_handle_count = client->handle_count;
480 atomic_inc(&ref->handle->share_count);
481 nvmap_ref_unlock(client);
484 struct nvmap_handle_ref *nvmap_create_handle(struct nvmap_client *client,
487 void *err = ERR_PTR(-ENOMEM);
488 struct nvmap_handle *h;
489 struct nvmap_handle_ref *ref = NULL;
492 return ERR_PTR(-EINVAL);
495 return ERR_PTR(-EINVAL);
497 h = kzalloc(sizeof(*h), GFP_KERNEL);
499 return ERR_PTR(-ENOMEM);
501 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
505 atomic_set(&h->ref, 1);
506 atomic_set(&h->pin, 0);
511 h->size = h->orig_size = size;
512 h->flags = NVMAP_HANDLE_WRITE_COMBINE;
513 mutex_init(&h->lock);
514 INIT_LIST_HEAD(&h->vmas);
515 INIT_LIST_HEAD(&h->lru);
518 * This takes out 1 ref on the dambuf. This corresponds to the
519 * handle_ref that gets automatically made by nvmap_create_handle().
521 h->dmabuf = __nvmap_make_dmabuf(client, h);
522 if (IS_ERR(h->dmabuf)) {
524 goto make_dmabuf_fail;
528 * Pre-attach nvmap to this new dmabuf. This gets unattached during the
529 * dma_buf_release() operation.
531 h->attachment = dma_buf_attach(h->dmabuf, &nvmap_pdev->dev);
532 if (IS_ERR(h->attachment)) {
534 goto dma_buf_attach_fail;
537 nvmap_handle_add(nvmap_dev, h);
540 * Major assumption here: the dma_buf object that the handle contains
541 * is created with a ref count of 1.
543 atomic_set(&ref->dupes, 1);
545 atomic_set(&ref->pin, 0);
546 add_handle_ref(client, ref);
547 trace_nvmap_create_handle(client, client->name, h, size, ref);
551 dma_buf_put(h->dmabuf);
559 struct nvmap_handle_ref *nvmap_duplicate_handle(struct nvmap_client *client,
560 struct nvmap_handle *h, bool skip_val)
562 struct nvmap_handle_ref *ref = NULL;
565 /* on success, the reference count for the handle should be
566 * incremented, so the success paths will not call nvmap_handle_put */
567 h = nvmap_handle_get(h);
570 nvmap_debug(client, "%s duplicate handle failed\n",
571 current->group_leader->comm);
572 return ERR_PTR(-EPERM);
576 nvmap_err(client, "%s duplicating unallocated handle\n",
577 current->group_leader->comm);
579 return ERR_PTR(-EINVAL);
582 nvmap_ref_lock(client);
583 ref = __nvmap_validate_locked(client, h);
586 /* handle already duplicated in client; just increment
587 * the reference count rather than re-duplicating it */
588 atomic_inc(&ref->dupes);
589 nvmap_ref_unlock(client);
593 nvmap_ref_unlock(client);
595 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
598 return ERR_PTR(-ENOMEM);
601 atomic_set(&ref->dupes, 1);
603 atomic_set(&ref->pin, 0);
604 add_handle_ref(client, ref);
607 * Ref counting on the dma_bufs follows the creation and destruction of
608 * nvmap_handle_refs. That is every time a handle_ref is made the
609 * dma_buf ref count goes up and everytime a handle_ref is destroyed
610 * the dma_buf ref count goes down.
612 get_dma_buf(h->dmabuf);
614 trace_nvmap_duplicate_handle(client, h, ref);
618 struct nvmap_handle_ref *nvmap_create_handle_from_fd(
619 struct nvmap_client *client, int fd)
621 struct nvmap_handle *handle;
622 struct nvmap_handle_ref *ref;
626 handle = nvmap_get_id_from_dmabuf_fd(client, fd);
628 return ERR_CAST(handle);
629 ref = nvmap_duplicate_handle(client, handle, 1);
633 struct nvmap_handle *nvmap_duplicate_handle_id_ex(struct nvmap_client *client,
634 struct nvmap_handle *h)
636 struct nvmap_handle_ref *ref = nvmap_duplicate_handle(client, h, 0);
641 return __nvmap_ref_to_id(ref);
643 EXPORT_SYMBOL(nvmap_duplicate_handle_id_ex);
645 int nvmap_get_page_list_info(struct nvmap_client *client,
646 struct nvmap_handle *handle, u32 *size,
647 u32 *flags, u32 *nr_page, bool *contig)
649 struct nvmap_handle *h;
651 BUG_ON(!size || !flags || !nr_page || !contig);
658 h = nvmap_handle_get(handle);
661 nvmap_err(client, "%s query invalid handle %p\n",
662 current->group_leader->comm, handle);
666 if (!h->alloc || !h->heap_pgalloc) {
667 nvmap_err(client, "%s query unallocated handle %p\n",
668 current->group_leader->comm, handle);
674 *size = h->orig_size;
675 *nr_page = PAGE_ALIGN(h->size) >> PAGE_SHIFT;
676 *contig = h->pgalloc.contig;
681 EXPORT_SYMBOL(nvmap_get_page_list_info);
683 int nvmap_acquire_page_list(struct nvmap_client *client,
684 struct nvmap_handle *handle, struct page **pages,
687 struct nvmap_handle *h;
688 struct nvmap_handle_ref *ref;
694 h = nvmap_handle_get(handle);
697 nvmap_err(client, "%s query invalid handle %p\n",
698 current->group_leader->comm, handle);
702 if (!h->alloc || !h->heap_pgalloc) {
703 nvmap_err(client, "%s query unallocated handle %p\n",
704 current->group_leader->comm, handle);
709 BUG_ON(nr_page != PAGE_ALIGN(h->size) >> PAGE_SHIFT);
711 for (idx = 0; idx < nr_page; idx++)
712 pages[idx] = h->pgalloc.pages[idx];
714 nvmap_ref_lock(client);
715 ref = __nvmap_validate_locked(client, h);
717 __nvmap_pin(ref, &dummy);
718 nvmap_ref_unlock(client);
722 EXPORT_SYMBOL(nvmap_acquire_page_list);
724 int nvmap_release_page_list(struct nvmap_client *client,
725 struct nvmap_handle *handle)
727 struct nvmap_handle_ref *ref;
728 struct nvmap_handle *h = NULL;
732 nvmap_ref_lock(client);
734 ref = __nvmap_validate_locked(client, handle);
738 nvmap_ref_unlock(client);
747 EXPORT_SYMBOL(nvmap_release_page_list);
749 int __nvmap_get_handle_param(struct nvmap_client *client,
750 struct nvmap_handle *h, u32 param, u64 *result)
754 if (WARN_ON(!virt_addr_valid(h)))
758 case NVMAP_HANDLE_PARAM_SIZE:
759 *result = h->orig_size;
761 case NVMAP_HANDLE_PARAM_ALIGNMENT:
764 case NVMAP_HANDLE_PARAM_BASE:
765 if (!h->alloc || !atomic_read(&h->pin))
767 else if (!h->heap_pgalloc) {
768 mutex_lock(&h->lock);
769 *result = h->carveout->base;
770 mutex_unlock(&h->lock);
771 } else if (h->pgalloc.contig)
772 *result = page_to_phys(h->pgalloc.pages[0]);
773 else if (h->attachment->priv)
774 *result = sg_dma_address(
775 ((struct sg_table *)h->attachment->priv)->sgl);
779 case NVMAP_HANDLE_PARAM_HEAP:
782 else if (!h->heap_pgalloc) {
783 mutex_lock(&h->lock);
784 *result = nvmap_carveout_usage(client, h->carveout);
785 mutex_unlock(&h->lock);
787 *result = NVMAP_HEAP_IOVMM;
789 case NVMAP_HANDLE_PARAM_KIND:
792 case NVMAP_HANDLE_PARAM_COMPR:
793 /* ignored, to be removed */
802 int nvmap_get_handle_param(struct nvmap_client *client,
803 struct nvmap_handle_ref *ref, u32 param, u64 *result)
805 if (WARN_ON(!virt_addr_valid(ref)) ||
806 WARN_ON(!virt_addr_valid(client)) ||
810 return __nvmap_get_handle_param(client, ref->handle, param, result);