2 * drivers/video/tegra/nvmap/nvmap_dev.c
4 * User-space interface to nvmap
6 * Copyright (c) 2011-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 #include <linux/backing-dev.h>
24 #include <linux/bitmap.h>
25 #include <linux/debugfs.h>
26 #include <linux/delay.h>
27 #include <linux/kernel.h>
28 #include <linux/device.h>
29 #include <linux/oom.h>
30 #include <linux/platform_device.h>
31 #include <linux/seq_file.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <linux/uaccess.h>
35 #include <linux/vmalloc.h>
36 #include <linux/nvmap.h>
37 #include <linux/module.h>
38 #include <linux/resource.h>
39 #include <linux/security.h>
40 #include <linux/stat.h>
41 #include <linux/kthread.h>
43 #include <asm/cputype.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/nvmap.h>
48 #include "nvmap_priv.h"
49 #include "nvmap_ioctl.h"
51 #define NVMAP_CARVEOUT_KILLER_RETRY_TIME 100 /* msecs */
53 #ifdef CONFIG_NVMAP_CACHE_MAINT_BY_SET_WAYS
54 size_t cache_maint_inner_threshold = SZ_2M;
56 #ifdef CONFIG_NVMAP_OUTER_CACHE_MAINT_BY_SET_WAYS
57 size_t cache_maint_outer_threshold = SZ_1M;
60 struct nvmap_carveout_node {
61 unsigned int heap_bit;
62 struct nvmap_heap *carveout;
64 struct list_head clients;
65 spinlock_t clients_lock;
70 struct platform_device *nvmap_pdev;
71 EXPORT_SYMBOL(nvmap_pdev);
72 struct nvmap_device *nvmap_dev;
73 EXPORT_SYMBOL(nvmap_dev);
74 struct nvmap_stats nvmap_stats;
75 EXPORT_SYMBOL(nvmap_stats);
77 static struct backing_dev_info nvmap_bdi = {
79 .capabilities = (BDI_CAP_NO_ACCT_AND_WRITEBACK |
80 BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP),
83 static struct device_dma_parameters nvmap_dma_parameters = {
84 .max_segment_size = UINT_MAX,
87 static int nvmap_open(struct inode *inode, struct file *filp);
88 static int nvmap_release(struct inode *inode, struct file *filp);
89 static long nvmap_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
90 static int nvmap_map(struct file *filp, struct vm_area_struct *vma);
91 static void nvmap_vma_close(struct vm_area_struct *vma);
92 static int nvmap_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf);
94 static const struct file_operations nvmap_user_fops = {
97 .release = nvmap_release,
98 .unlocked_ioctl = nvmap_ioctl,
100 .compat_ioctl = nvmap_ioctl,
105 static struct vm_operations_struct nvmap_vma_ops = {
106 .open = nvmap_vma_open,
107 .close = nvmap_vma_close,
108 .fault = nvmap_vma_fault,
111 int is_nvmap_vma(struct vm_area_struct *vma)
113 return vma->vm_ops == &nvmap_vma_ops;
116 struct device *nvmap_client_to_device(struct nvmap_client *client)
120 return nvmap_dev->dev_user.this_device;
123 /* allocates a PTE for the caller's use; returns the PTE pointer or
124 * a negative errno. not safe from IRQs */
125 pte_t **nvmap_alloc_pte_irq(struct nvmap_device *dev, void **vaddr)
129 spin_lock(&dev->ptelock);
130 bit = find_next_zero_bit(dev->ptebits, NVMAP_NUM_PTES, dev->lastpte);
131 if (bit == NVMAP_NUM_PTES) {
132 bit = find_first_zero_bit(dev->ptebits, dev->lastpte);
133 if (bit == dev->lastpte)
134 bit = NVMAP_NUM_PTES;
137 if (bit == NVMAP_NUM_PTES) {
138 spin_unlock(&dev->ptelock);
139 return ERR_PTR(-ENOMEM);
143 set_bit(bit, dev->ptebits);
144 spin_unlock(&dev->ptelock);
146 *vaddr = dev->vm_rgn->addr + bit * PAGE_SIZE;
147 return &(dev->ptes[bit]);
150 /* allocates a PTE for the caller's use; returns the PTE pointer or
151 * a negative errno. must be called from sleepable contexts */
152 pte_t **nvmap_alloc_pte(struct nvmap_device *dev, void **vaddr)
156 ret = wait_event_interruptible(dev->pte_wait,
157 !IS_ERR(pte = nvmap_alloc_pte_irq(dev, vaddr)));
159 if (ret == -ERESTARTSYS)
160 return ERR_PTR(-EINTR);
166 void nvmap_free_pte(struct nvmap_device *dev, pte_t **pte)
169 unsigned int bit = pte - dev->ptes;
171 if (WARN_ON(bit >= NVMAP_NUM_PTES))
174 addr = (unsigned long)dev->vm_rgn->addr + bit * PAGE_SIZE;
175 set_pte_at(&init_mm, addr, *pte, 0);
177 spin_lock(&dev->ptelock);
178 clear_bit(bit, dev->ptebits);
179 spin_unlock(&dev->ptelock);
180 wake_up(&dev->pte_wait);
183 /* get pte for the virtual address */
184 pte_t **nvmap_vaddr_to_pte(struct nvmap_device *dev, unsigned long vaddr)
188 BUG_ON(vaddr < (unsigned long)dev->vm_rgn->addr);
189 bit = (vaddr - (unsigned long)dev->vm_rgn->addr) >> PAGE_SHIFT;
190 BUG_ON(bit >= NVMAP_NUM_PTES);
191 return &(dev->ptes[bit]);
195 * Verifies that the passed ID is a valid handle ID. Then the passed client's
196 * reference to the handle is returned.
198 * Note: to call this function make sure you own the client ref lock.
200 struct nvmap_handle_ref *__nvmap_validate_locked(struct nvmap_client *c,
201 struct nvmap_handle *h)
203 struct rb_node *n = c->handle_refs.rb_node;
206 struct nvmap_handle_ref *ref;
207 ref = rb_entry(n, struct nvmap_handle_ref, node);
208 if (ref->handle == h)
210 else if ((uintptr_t)h > (uintptr_t)ref->handle)
219 unsigned long nvmap_carveout_usage(struct nvmap_client *c,
220 struct nvmap_heap_block *b)
222 struct nvmap_heap *h = nvmap_block_to_heap(b);
223 struct nvmap_carveout_node *n;
226 for (i = 0; i < nvmap_dev->nr_carveouts; i++) {
227 n = &nvmap_dev->heaps[i];
228 if (n->carveout == h)
235 * This routine is used to flush the carveout memory from cache.
236 * Why cache flush is needed for carveout? Consider the case, where a piece of
237 * carveout is allocated as cached and released. After this, if the same memory is
238 * allocated for uncached request and the memory is not flushed out from cache.
239 * In this case, the client might pass this to H/W engine and it could start modify
240 * the memory. As this was cached earlier, it might have some portion of it in cache.
241 * During cpu request to read/write other memory, the cached portion of this memory
242 * might get flushed back to main memory and would cause corruptions, if it happens
243 * after H/W writes data to memory.
245 * But flushing out the memory blindly on each carveout allocation is redundant.
247 * In order to optimize the carveout buffer cache flushes, the following
250 * The whole Carveout is flushed out from cache during its initialization.
251 * During allocation, carveout buffers are not flused from cache.
252 * During deallocation, carveout buffers are flushed, if they were allocated as cached.
253 * if they were allocated as uncached/writecombined, no cache flush is needed.
254 * Just draining store buffers is enough.
256 int nvmap_flush_heap_block(struct nvmap_client *client,
257 struct nvmap_heap_block *block, size_t len, unsigned int prot)
262 phys_addr_t phys = block->base;
263 phys_addr_t end = block->base + len;
265 if (prot == NVMAP_HANDLE_UNCACHEABLE || prot == NVMAP_HANDLE_WRITE_COMBINE)
268 #ifdef CONFIG_NVMAP_CACHE_MAINT_BY_SET_WAYS
269 if (len >= cache_maint_inner_threshold) {
270 inner_flush_cache_all();
271 if (prot != NVMAP_HANDLE_INNER_CACHEABLE)
272 outer_flush_range(block->base, block->base + len);
277 pte = nvmap_alloc_pte(nvmap_dev, &addr);
281 kaddr = (uintptr_t)addr;
284 phys_addr_t next = (phys + PAGE_SIZE) & PAGE_MASK;
285 unsigned long pfn = __phys_to_pfn(phys);
286 void *base = (void *)kaddr + (phys & ~PAGE_MASK);
288 next = min(next, end);
289 set_pte_at(&init_mm, kaddr, *pte, pfn_pte(pfn, PG_PROT_KERNEL));
290 nvmap_flush_tlb_kernel_page(kaddr);
291 FLUSH_DCACHE_AREA(base, next - phys);
295 if (prot != NVMAP_HANDLE_INNER_CACHEABLE)
296 outer_flush_range(block->base, block->base + len);
298 nvmap_free_pte(nvmap_dev, pte);
305 struct nvmap_heap_block *do_nvmap_carveout_alloc(struct nvmap_client *client,
306 struct nvmap_handle *handle,
309 struct nvmap_carveout_node *co_heap;
310 struct nvmap_device *dev = nvmap_dev;
313 for (i = 0; i < dev->nr_carveouts; i++) {
314 struct nvmap_heap_block *block;
315 co_heap = &dev->heaps[i];
317 if (!(co_heap->heap_bit & type))
320 block = nvmap_heap_alloc(co_heap->carveout, handle);
327 struct nvmap_heap_block *nvmap_carveout_alloc(struct nvmap_client *client,
328 struct nvmap_handle *handle,
331 return do_nvmap_carveout_alloc(client, handle, type);
334 /* remove a handle from the device's tree of all handles; called
335 * when freeing handles. */
336 int nvmap_handle_remove(struct nvmap_device *dev, struct nvmap_handle *h)
338 spin_lock(&dev->handle_lock);
340 /* re-test inside the spinlock if the handle really has no clients;
341 * only remove the handle if it is unreferenced */
342 if (atomic_add_return(0, &h->ref) > 0) {
343 spin_unlock(&dev->handle_lock);
347 BUG_ON(atomic_read(&h->ref) < 0);
348 BUG_ON(atomic_read(&h->pin) != 0);
351 rb_erase(&h->node, &dev->handles);
353 spin_unlock(&dev->handle_lock);
357 /* adds a newly-created handle to the device master tree */
358 void nvmap_handle_add(struct nvmap_device *dev, struct nvmap_handle *h)
361 struct rb_node *parent = NULL;
363 spin_lock(&dev->handle_lock);
364 p = &dev->handles.rb_node;
366 struct nvmap_handle *b;
369 b = rb_entry(parent, struct nvmap_handle, node);
371 p = &parent->rb_right;
373 p = &parent->rb_left;
375 rb_link_node(&h->node, parent, p);
376 rb_insert_color(&h->node, &dev->handles);
378 spin_unlock(&dev->handle_lock);
381 struct nvmap_client *__nvmap_create_client(struct nvmap_device *dev,
384 struct nvmap_client *client;
385 struct task_struct *task;
390 client = kzalloc(sizeof(*client), GFP_KERNEL);
395 client->kernel_client = true;
396 client->handle_refs = RB_ROOT;
398 get_task_struct(current->group_leader);
399 task_lock(current->group_leader);
400 /* don't bother to store task struct for kernel threads,
401 they can't be killed anyway */
402 if (current->flags & PF_KTHREAD) {
403 put_task_struct(current->group_leader);
406 task = current->group_leader;
408 task_unlock(current->group_leader);
411 mutex_init(&client->ref_lock);
412 atomic_set(&client->count, 1);
414 mutex_lock(&dev->clients_lock);
415 list_add(&client->list, &dev->clients);
416 mutex_unlock(&dev->clients_lock);
420 static void destroy_client(struct nvmap_client *client)
427 mutex_lock(&nvmap_dev->clients_lock);
428 list_del(&client->list);
429 mutex_unlock(&nvmap_dev->clients_lock);
431 while ((n = rb_first(&client->handle_refs))) {
432 struct nvmap_handle_ref *ref;
435 ref = rb_entry(n, struct nvmap_handle_ref, node);
438 pins = atomic_read(&ref->pin);
443 if (ref->handle->owner == client) {
444 ref->handle->owner = NULL;
445 ref->handle->owner_ref = NULL;
448 dma_buf_put(ref->handle->dmabuf);
449 rb_erase(&ref->node, &client->handle_refs);
450 atomic_dec(&ref->handle->share_count);
452 dupes = atomic_read(&ref->dupes);
454 nvmap_handle_put(ref->handle);
460 put_task_struct(client->task);
465 struct nvmap_client *nvmap_client_get(struct nvmap_client *client)
467 if (!virt_addr_valid(client))
470 if (!atomic_add_unless(&client->count, 1, 0))
476 void nvmap_client_put(struct nvmap_client *client)
481 if (!atomic_dec_return(&client->count))
482 destroy_client(client);
485 static int nvmap_open(struct inode *inode, struct file *filp)
487 struct miscdevice *miscdev = filp->private_data;
488 struct nvmap_device *dev = dev_get_drvdata(miscdev->parent);
489 struct nvmap_client *priv;
491 __attribute__((unused)) struct rlimit old_rlim, new_rlim;
493 ret = nonseekable_open(inode, filp);
497 BUG_ON(dev != nvmap_dev);
498 priv = __nvmap_create_client(dev, "user");
501 trace_nvmap_open(priv, priv->name);
503 priv->kernel_client = false;
505 filp->f_mapping->backing_dev_info = &nvmap_bdi;
507 filp->private_data = priv;
511 static int nvmap_release(struct inode *inode, struct file *filp)
513 struct nvmap_client *priv = filp->private_data;
515 trace_nvmap_release(priv, priv->name);
516 nvmap_client_put(priv);
520 int __nvmap_map(struct nvmap_handle *h, struct vm_area_struct *vma)
522 struct nvmap_vma_priv *priv;
524 h = nvmap_handle_get(h);
528 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
533 vma->vm_flags |= VM_SHARED | VM_DONTEXPAND |
534 VM_DONTDUMP | VM_DONTCOPY |
535 (h->heap_pgalloc ? 0 : VM_PFNMAP);
536 vma->vm_ops = &nvmap_vma_ops;
537 BUG_ON(vma->vm_private_data != NULL);
538 vma->vm_private_data = priv;
539 vma->vm_page_prot = nvmap_pgprot(h, vma->vm_page_prot);
544 static int nvmap_map(struct file *filp, struct vm_area_struct *vma)
546 BUG_ON(vma->vm_private_data != NULL);
547 vma->vm_flags |= (VM_SHARED | VM_DONTEXPAND |
548 VM_DONTDUMP | VM_DONTCOPY);
549 vma->vm_ops = &nvmap_vma_ops;
553 static long nvmap_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
556 void __user *uarg = (void __user *)arg;
558 if (_IOC_TYPE(cmd) != NVMAP_IOC_MAGIC)
561 if (_IOC_NR(cmd) > NVMAP_IOC_MAXNR)
564 if (_IOC_DIR(cmd) & _IOC_READ)
565 err = !access_ok(VERIFY_WRITE, uarg, _IOC_SIZE(cmd));
566 if (_IOC_DIR(cmd) & _IOC_WRITE)
567 err = !access_ok(VERIFY_READ, uarg, _IOC_SIZE(cmd));
573 case NVMAP_IOC_CREATE:
574 case NVMAP_IOC_FROM_FD:
575 err = nvmap_ioctl_create(filp, cmd, uarg);
578 case NVMAP_IOC_FROM_ID:
579 case NVMAP_IOC_GET_ID:
580 pr_warn_once("nvmap: unsupported FROM_ID/GET_ID IOCTLs used.\n");
583 case NVMAP_IOC_GET_FD:
584 err = nvmap_ioctl_getfd(filp, uarg);
588 case NVMAP_IOC_PARAM_32:
589 err = nvmap_ioctl_get_param(filp, uarg, true);
593 case NVMAP_IOC_PARAM:
594 err = nvmap_ioctl_get_param(filp, uarg, false);
598 case NVMAP_IOC_UNPIN_MULT_32:
599 case NVMAP_IOC_PIN_MULT_32:
600 err = nvmap_ioctl_pinop(filp, cmd == NVMAP_IOC_PIN_MULT_32,
605 case NVMAP_IOC_UNPIN_MULT:
606 case NVMAP_IOC_PIN_MULT:
607 err = nvmap_ioctl_pinop(filp, cmd == NVMAP_IOC_PIN_MULT,
611 case NVMAP_IOC_ALLOC:
612 err = nvmap_ioctl_alloc(filp, uarg);
615 case NVMAP_IOC_ALLOC_KIND:
616 err = nvmap_ioctl_alloc_kind(filp, uarg);
620 err = nvmap_ioctl_free(filp, arg);
624 case NVMAP_IOC_MMAP_32:
625 err = nvmap_map_into_caller_ptr(filp, uarg, true);
630 err = nvmap_map_into_caller_ptr(filp, uarg, false);
634 case NVMAP_IOC_WRITE_32:
635 case NVMAP_IOC_READ_32:
636 err = nvmap_ioctl_rw_handle(filp, cmd == NVMAP_IOC_READ_32,
641 case NVMAP_IOC_WRITE:
643 err = nvmap_ioctl_rw_handle(filp, cmd == NVMAP_IOC_READ, uarg,
648 case NVMAP_IOC_CACHE_32:
649 err = nvmap_ioctl_cache_maint(filp, uarg, true);
653 case NVMAP_IOC_CACHE:
654 err = nvmap_ioctl_cache_maint(filp, uarg, false);
657 case NVMAP_IOC_CACHE_LIST:
658 case NVMAP_IOC_RESERVE:
659 err = nvmap_ioctl_cache_maint_list(filp, uarg,
660 cmd == NVMAP_IOC_RESERVE);
663 case NVMAP_IOC_SHARE:
664 err = nvmap_ioctl_share_dmabuf(filp, uarg);
673 /* to ensure that the backing store for the VMA isn't freed while a fork'd
674 * reference still exists, nvmap_vma_open increments the reference count on
675 * the handle, and nvmap_vma_close decrements it. alternatively, we could
676 * disallow copying of the vma, or behave like pmem and zap the pages. FIXME.
678 void nvmap_vma_open(struct vm_area_struct *vma)
680 struct nvmap_vma_priv *priv;
681 struct nvmap_handle *h;
682 struct nvmap_vma_list *vma_list, *tmp;
683 struct list_head *tmp_head = NULL;
684 pid_t current_pid = current->pid;
685 bool vma_pos_found = false;
687 priv = vma->vm_private_data;
689 BUG_ON(!priv->handle);
691 atomic_inc(&priv->count);
695 vma_list = kmalloc(sizeof(*vma_list), GFP_KERNEL);
697 mutex_lock(&h->lock);
700 /* insert vma into handle's vmas list in the increasing order of
703 list_for_each_entry(tmp, &h->vmas, list) {
704 BUG_ON(tmp->vma == vma);
706 if (!vma_pos_found && (current_pid == tmp->pid)) {
707 if (vma->vm_pgoff < tmp->vma->vm_pgoff) {
708 tmp_head = &tmp->list;
709 vma_pos_found = true;
711 tmp_head = tmp->list.next;
717 vma_list->pid = current_pid;
718 list_add_tail(&vma_list->list, tmp_head);
719 mutex_unlock(&h->lock);
721 WARN(1, "vma not tracked");
725 static void nvmap_vma_close(struct vm_area_struct *vma)
727 struct nvmap_vma_priv *priv = vma->vm_private_data;
728 struct nvmap_vma_list *vma_list;
729 struct nvmap_handle *h;
730 bool vma_found = false;
735 BUG_ON(!priv->handle);
738 mutex_lock(&h->lock);
739 list_for_each_entry(vma_list, &h->vmas, list) {
740 if (vma_list->vma != vma)
742 list_del(&vma_list->list);
748 mutex_unlock(&h->lock);
750 if (__atomic_add_unless(&priv->count, -1, 0) == 1) {
752 nvmap_handle_put(priv->handle);
753 vma->vm_private_data = NULL;
759 static int nvmap_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
762 struct nvmap_vma_priv *priv;
765 offs = (unsigned long)(vmf->virtual_address - vma->vm_start);
766 priv = vma->vm_private_data;
767 if (!priv || !priv->handle || !priv->handle->alloc)
768 return VM_FAULT_SIGBUS;
771 /* if the VMA was split for some reason, vm_pgoff will be the VMA's
772 * offset from the original VMA */
773 offs += (vma->vm_pgoff << PAGE_SHIFT);
775 if (offs >= priv->handle->size)
776 return VM_FAULT_SIGBUS;
778 if (!priv->handle->heap_pgalloc) {
780 BUG_ON(priv->handle->carveout->base & ~PAGE_MASK);
781 pfn = ((priv->handle->carveout->base + offs) >> PAGE_SHIFT);
782 if (!pfn_valid(pfn)) {
784 (unsigned long)vmf->virtual_address, pfn);
785 return VM_FAULT_NOPAGE;
787 /* CMA memory would get here */
788 page = pfn_to_page(pfn);
791 if (nvmap_page_reserved(priv->handle->pgalloc.pages[offs]))
792 return VM_FAULT_SIGBUS;
793 page = nvmap_to_page(priv->handle->pgalloc.pages[offs]);
794 nvmap_page_mkdirty(&priv->handle->pgalloc.pages[offs]);
800 return (page) ? 0 : VM_FAULT_SIGBUS;
803 #define DEBUGFS_OPEN_FOPS(name) \
804 static int nvmap_debug_##name##_open(struct inode *inode, \
807 return single_open(file, nvmap_debug_##name##_show, \
811 static const struct file_operations debug_##name##_fops = { \
812 .open = nvmap_debug_##name##_open, \
814 .llseek = seq_lseek, \
815 .release = single_release, \
818 #define K(x) (x >> 10)
820 static void client_stringify(struct nvmap_client *client, struct seq_file *s)
822 char task_comm[TASK_COMM_LEN];
824 seq_printf(s, "%-18s %18s %8u", client->name, "kernel", 0);
827 get_task_comm(task_comm, client->task);
828 seq_printf(s, "%-18s %18s %8u", client->name, task_comm,
832 static void allocations_stringify(struct nvmap_client *client,
833 struct seq_file *s, u32 heap_type)
837 nvmap_ref_lock(client);
838 n = rb_first(&client->handle_refs);
839 for (; n != NULL; n = rb_next(n)) {
840 struct nvmap_handle_ref *ref =
841 rb_entry(n, struct nvmap_handle_ref, node);
842 struct nvmap_handle *handle = ref->handle;
843 if (handle->alloc && handle->heap_type == heap_type) {
844 phys_addr_t base = heap_type == NVMAP_HEAP_IOVMM ? 0 :
845 (handle->carveout->base);
847 "%-18s %-18s %8llx %10zuK %8x %6u %6u %6u %6u %6u %6u\n",
849 (unsigned long long)base, K(handle->size),
851 atomic_read(&handle->ref),
852 atomic_read(&ref->dupes),
853 atomic_read(&ref->pin),
854 atomic_read(&handle->kmap_count),
855 atomic_read(&handle->umap_count),
856 atomic_read(&handle->share_count));
859 nvmap_ref_unlock(client);
862 /* compute the total amount of handle physical memory that is mapped
863 * into client's virtual address space. Remember that vmas list is
864 * sorted in ascending order of handle offsets.
865 * NOTE: This function should be called while holding handle's lock mutex.
867 static void nvmap_get_client_handle_mss(struct nvmap_client *client,
868 struct nvmap_handle *handle, u64 *total)
870 struct nvmap_vma_list *vma_list = NULL;
871 struct vm_area_struct *vma = NULL;
872 u64 end_offset = 0, vma_start_offset, vma_size;
873 int64_t overlap_size;
876 list_for_each_entry(vma_list, &handle->vmas, list) {
878 if (client->task->pid == vma_list->pid) {
880 vma_size = vma->vm_end - vma->vm_start;
882 vma_start_offset = vma->vm_pgoff << PAGE_SHIFT;
883 if (end_offset < vma_start_offset + vma_size) {
886 overlap_size = end_offset - vma_start_offset;
887 if (overlap_size > 0)
888 *total -= overlap_size;
889 end_offset = vma_start_offset + vma_size;
895 static void maps_stringify(struct nvmap_client *client,
896 struct seq_file *s, u32 heap_type)
899 struct nvmap_vma_list *vma_list = NULL;
900 struct vm_area_struct *vma = NULL;
901 u64 total_mapped_size, vma_size;
903 nvmap_ref_lock(client);
904 n = rb_first(&client->handle_refs);
905 for (; n != NULL; n = rb_next(n)) {
906 struct nvmap_handle_ref *ref =
907 rb_entry(n, struct nvmap_handle_ref, node);
908 struct nvmap_handle *handle = ref->handle;
909 if (handle->alloc && handle->heap_type == heap_type) {
910 phys_addr_t base = heap_type == NVMAP_HEAP_IOVMM ? 0 :
911 (handle->carveout->base);
913 "%-18s %-18s %8llx %10zuK %8x %6u %16p "
916 (unsigned long long)base, K(handle->size),
918 atomic_read(&handle->share_count),
921 mutex_lock(&handle->lock);
922 nvmap_get_client_handle_mss(client, handle,
924 seq_printf(s, "%6lluK\n", K(total_mapped_size));
926 list_for_each_entry(vma_list, &handle->vmas, list) {
928 if (vma_list->pid == client->task->pid) {
930 vma_size = vma->vm_end - vma->vm_start;
932 "%-18s %-18s %8s %11s %8s %6s %16s "
933 "%-12lx-%12lx %6lluK\n",
934 "", "", "", "", "", "", "",
935 vma->vm_start, vma->vm_end,
939 mutex_unlock(&handle->lock);
942 nvmap_ref_unlock(client);
945 static void nvmap_get_client_mss(struct nvmap_client *client,
946 u64 *total, u32 heap_type)
951 nvmap_ref_lock(client);
952 n = rb_first(&client->handle_refs);
953 for (; n != NULL; n = rb_next(n)) {
954 struct nvmap_handle_ref *ref =
955 rb_entry(n, struct nvmap_handle_ref, node);
956 struct nvmap_handle *handle = ref->handle;
957 if (handle->alloc && handle->heap_type == heap_type)
958 *total += handle->size /
959 atomic_read(&handle->share_count);
961 nvmap_ref_unlock(client);
964 static void nvmap_get_total_mss(u64 *pss, u64 *non_pss,
965 u64 *total, u32 heap_type)
969 struct nvmap_device *dev = nvmap_dev;
978 spin_lock(&dev->handle_lock);
979 n = rb_first(&dev->handles);
980 for (; n != NULL; n = rb_next(n)) {
981 struct nvmap_handle *h =
982 rb_entry(n, struct nvmap_handle, node);
984 if (!h || !h->alloc || h->heap_type != heap_type)
991 for (i = 0; i < h->size >> PAGE_SHIFT; i++) {
992 struct page *page = nvmap_to_page(h->pgalloc.pages[i]);
993 int mapcount = page_mapcount(page);
995 *non_pss += PAGE_SIZE;
1000 *pss = *total - *non_pss;
1001 spin_unlock(&dev->handle_lock);
1004 static int nvmap_debug_allocations_show(struct seq_file *s, void *unused)
1007 struct nvmap_client *client;
1008 u32 heap_type = (u32)(uintptr_t)s->private;
1010 mutex_lock(&nvmap_dev->clients_lock);
1011 seq_printf(s, "%-18s %18s %8s %11s\n",
1012 "CLIENT", "PROCESS", "PID", "SIZE");
1013 seq_printf(s, "%-18s %18s %8s %11s %8s %6s %6s %6s %6s %6s %6s %8s\n",
1014 "", "", "BASE", "SIZE", "FLAGS", "REFS",
1015 "DUPES", "PINS", "KMAPS", "UMAPS", "SHARE", "UID");
1016 list_for_each_entry(client, &nvmap_dev->clients, list) {
1018 client_stringify(client, s);
1019 nvmap_get_client_mss(client, &client_total, heap_type);
1020 seq_printf(s, " %10lluK\n", K(client_total));
1021 allocations_stringify(client, s, heap_type);
1024 mutex_unlock(&nvmap_dev->clients_lock);
1025 nvmap_get_total_mss(NULL, NULL, &total, heap_type);
1026 seq_printf(s, "%-18s %-18s %8s %10lluK\n", "total", "", "", K(total));
1030 DEBUGFS_OPEN_FOPS(allocations);
1032 static int nvmap_debug_maps_show(struct seq_file *s, void *unused)
1035 struct nvmap_client *client;
1036 u32 heap_type = (u32)(uintptr_t)s->private;
1038 mutex_lock(&nvmap_dev->clients_lock);
1039 seq_printf(s, "%-18s %18s %8s %11s\n",
1040 "CLIENT", "PROCESS", "PID", "SIZE");
1041 seq_printf(s, "%-18s %18s %8s %11s %8s %6s %9s %21s %18s\n",
1042 "", "", "BASE", "SIZE", "FLAGS", "SHARE", "UID",
1045 list_for_each_entry(client, &nvmap_dev->clients, list) {
1047 client_stringify(client, s);
1048 nvmap_get_client_mss(client, &client_total, heap_type);
1049 seq_printf(s, " %10lluK\n", K(client_total));
1050 maps_stringify(client, s, heap_type);
1053 mutex_unlock(&nvmap_dev->clients_lock);
1055 nvmap_get_total_mss(NULL, NULL, &total, heap_type);
1056 seq_printf(s, "%-18s %-18s %8s %10lluK\n", "total", "", "", K(total));
1060 DEBUGFS_OPEN_FOPS(maps);
1062 static int nvmap_debug_clients_show(struct seq_file *s, void *unused)
1065 struct nvmap_client *client;
1066 ulong heap_type = (ulong)s->private;
1068 mutex_lock(&nvmap_dev->clients_lock);
1069 seq_printf(s, "%-18s %18s %8s %11s\n",
1070 "CLIENT", "PROCESS", "PID", "SIZE");
1071 list_for_each_entry(client, &nvmap_dev->clients, list) {
1073 client_stringify(client, s);
1074 nvmap_get_client_mss(client, &client_total, heap_type);
1075 seq_printf(s, " %10lluK\n", K(client_total));
1077 mutex_unlock(&nvmap_dev->clients_lock);
1078 nvmap_get_total_mss(NULL, NULL, &total, heap_type);
1079 seq_printf(s, "%-18s %18s %8s %10lluK\n", "total", "", "", K(total));
1083 DEBUGFS_OPEN_FOPS(clients);
1085 #define PRINT_MEM_STATS_NOTE(x) \
1087 seq_printf(s, "Note: total memory is precise account of pages " \
1088 "allocated by NvMap.\nIt doesn't match with all clients " \
1089 "\"%s\" accumulated as shared memory\nis accounted in " \
1090 "full in each clients \"%s\" that shared memory.\n", #x, #x); \
1093 static int nvmap_debug_lru_allocations_show(struct seq_file *s, void *unused)
1095 struct nvmap_handle *h;
1096 int total_handles = 0, migratable_handles = 0;
1097 size_t total_size = 0, migratable_size = 0;
1099 seq_printf(s, "%-18s %18s %8s %11s %8s %6s %6s %6s %6s %6s %8s\n",
1100 "", "", "", "", "", "",
1101 "", "PINS", "KMAPS", "UMAPS", "UID");
1102 spin_lock(&nvmap_dev->lru_lock);
1103 list_for_each_entry(h, &nvmap_dev->lru_handles, lru) {
1105 total_size += h->size;
1106 if (!atomic_read(&h->pin) && !atomic_read(&h->kmap_count)) {
1107 migratable_handles++;
1108 migratable_size += h->size;
1110 seq_printf(s, "%-18s %18s %8s %10zuK %8s %6s %6s %6u %6u "
1111 "%6u %8p\n", "", "", "", K(h->size), "", "",
1112 "", atomic_read(&h->pin),
1113 atomic_read(&h->kmap_count),
1114 atomic_read(&h->umap_count),
1117 seq_printf(s, "total_handles = %d, migratable_handles = %d,"
1118 "total_size=%zuK, migratable_size=%zuK\n",
1119 total_handles, migratable_handles,
1120 K(total_size), K(migratable_size));
1121 spin_unlock(&nvmap_dev->lru_lock);
1122 PRINT_MEM_STATS_NOTE(SIZE);
1126 DEBUGFS_OPEN_FOPS(lru_allocations);
1128 static void nvmap_iovmm_get_client_mss(struct nvmap_client *client, u64 *pss,
1129 u64 *non_pss, u64 *total)
1134 *pss = *non_pss = *total = 0;
1135 nvmap_ref_lock(client);
1136 n = rb_first(&client->handle_refs);
1137 for (; n != NULL; n = rb_next(n)) {
1138 struct nvmap_handle_ref *ref =
1139 rb_entry(n, struct nvmap_handle_ref, node);
1140 struct nvmap_handle *h = ref->handle;
1142 if (!h || !h->alloc || !h->heap_pgalloc)
1145 for (i = 0; i < h->size >> PAGE_SHIFT; i++) {
1146 struct page *page = nvmap_to_page(h->pgalloc.pages[i]);
1147 int mapcount = page_mapcount(page);
1149 *non_pss += PAGE_SIZE;
1150 *total += PAGE_SIZE;
1152 *pss = *total - *non_pss;
1154 nvmap_ref_unlock(client);
1157 static int nvmap_debug_iovmm_procrank_show(struct seq_file *s, void *unused)
1159 u64 pss, non_pss, total;
1160 struct nvmap_client *client;
1161 struct nvmap_device *dev = s->private;
1162 u64 total_memory, total_pss, total_non_pss;
1164 mutex_lock(&dev->clients_lock);
1165 seq_printf(s, "%-18s %18s %8s %11s %11s %11s\n",
1166 "CLIENT", "PROCESS", "PID", "PSS", "NON-PSS", "TOTAL");
1167 list_for_each_entry(client, &dev->clients, list) {
1168 client_stringify(client, s);
1169 nvmap_iovmm_get_client_mss(client, &pss, &non_pss, &total);
1170 seq_printf(s, " %10lluK %10lluK %10lluK\n", K(pss),
1171 K(non_pss), K(total));
1173 mutex_unlock(&dev->clients_lock);
1175 nvmap_get_total_mss(&total_pss, &total_non_pss, &total_memory,
1177 seq_printf(s, "%-18s %18s %8s %10lluK %10lluK %10lluK\n",
1178 "total", "", "", K(total_pss),
1179 K(total_non_pss), K(total_memory));
1180 PRINT_MEM_STATS_NOTE(TOTAL);
1184 DEBUGFS_OPEN_FOPS(iovmm_procrank);
1186 ulong nvmap_iovmm_get_used_pages(void)
1190 nvmap_get_total_mss(NULL, NULL, &total, NVMAP_HEAP_IOVMM);
1191 return total >> PAGE_SHIFT;
1194 static int nvmap_stats_reset(void *data, u64 val)
1199 atomic64_set(&nvmap_stats.collect, 0);
1200 for (i = 0; i < NS_NUM; i++) {
1203 atomic64_set(&nvmap_stats.stats[i], 0);
1209 static int nvmap_stats_get(void *data, u64 *val)
1211 atomic64_t *ptr = data;
1213 *val = atomic64_read(ptr);
1217 static int nvmap_stats_set(void *data, u64 val)
1219 atomic64_t *ptr = data;
1221 atomic64_set(ptr, val);
1225 DEFINE_SIMPLE_ATTRIBUTE(reset_stats_fops, NULL, nvmap_stats_reset, "%llu\n");
1226 DEFINE_SIMPLE_ATTRIBUTE(stats_fops, nvmap_stats_get, nvmap_stats_set, "%llu\n");
1228 static void nvmap_stats_init(struct dentry *nvmap_debug_root)
1230 struct dentry *stats_root;
1232 #define CREATE_DF(x, y) \
1233 debugfs_create_file(#x, S_IRUGO, stats_root, &y, &stats_fops);
1235 stats_root = debugfs_create_dir("stats", nvmap_debug_root);
1236 if (!IS_ERR_OR_NULL(stats_root)) {
1237 CREATE_DF(alloc, nvmap_stats.stats[NS_ALLOC]);
1238 CREATE_DF(release, nvmap_stats.stats[NS_RELEASE]);
1239 CREATE_DF(ualloc, nvmap_stats.stats[NS_UALLOC]);
1240 CREATE_DF(urelease, nvmap_stats.stats[NS_URELEASE]);
1241 CREATE_DF(kalloc, nvmap_stats.stats[NS_KALLOC]);
1242 CREATE_DF(krelease, nvmap_stats.stats[NS_KRELEASE]);
1243 CREATE_DF(cflush_rq, nvmap_stats.stats[NS_CFLUSH_RQ]);
1244 CREATE_DF(cflush_done, nvmap_stats.stats[NS_CFLUSH_DONE]);
1245 CREATE_DF(ucflush_rq, nvmap_stats.stats[NS_UCFLUSH_RQ]);
1246 CREATE_DF(ucflush_done, nvmap_stats.stats[NS_UCFLUSH_DONE]);
1247 CREATE_DF(kcflush_rq, nvmap_stats.stats[NS_KCFLUSH_RQ]);
1248 CREATE_DF(kcflush_done, nvmap_stats.stats[NS_KCFLUSH_DONE]);
1249 CREATE_DF(total_memory, nvmap_stats.stats[NS_TOTAL]);
1251 debugfs_create_file("collect", S_IRUGO | S_IWUSR,
1252 stats_root, &nvmap_stats.collect, &stats_fops);
1253 debugfs_create_file("reset", S_IWUSR,
1254 stats_root, NULL, &reset_stats_fops);
1260 void nvmap_stats_inc(enum nvmap_stats_t stat, size_t size)
1262 if (atomic64_read(&nvmap_stats.collect) || stat == NS_TOTAL)
1263 atomic64_add(size, &nvmap_stats.stats[stat]);
1266 void nvmap_stats_dec(enum nvmap_stats_t stat, size_t size)
1268 if (atomic64_read(&nvmap_stats.collect) || stat == NS_TOTAL)
1269 atomic64_sub(size, &nvmap_stats.stats[stat]);
1272 u64 nvmap_stats_read(enum nvmap_stats_t stat)
1274 return atomic64_read(&nvmap_stats.stats[stat]);
1277 static int nvmap_probe(struct platform_device *pdev)
1279 struct nvmap_platform_data *plat = pdev->dev.platform_data;
1280 struct nvmap_device *dev;
1281 struct dentry *nvmap_debug_root;
1286 dev_err(&pdev->dev, "no platform data?\n");
1291 * The DMA mapping API uses these parameters to decide how to map the
1292 * passed buffers. If the maximum physical segment size is set to
1293 * smaller than the size of the buffer, then the buffer will be mapped
1294 * as separate IO virtual address ranges.
1296 pdev->dev.dma_parms = &nvmap_dma_parameters;
1298 if (WARN_ON(nvmap_dev != NULL)) {
1299 dev_err(&pdev->dev, "only one nvmap device may be present\n");
1303 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1305 dev_err(&pdev->dev, "out of memory for device\n");
1311 dev->dev_user.minor = MISC_DYNAMIC_MINOR;
1312 dev->dev_user.name = "nvmap";
1313 dev->dev_user.fops = &nvmap_user_fops;
1314 dev->dev_user.parent = &pdev->dev;
1316 dev->handles = RB_ROOT;
1318 init_waitqueue_head(&dev->pte_wait);
1320 #ifdef CONFIG_NVMAP_PAGE_POOLS
1321 e = nvmap_page_pool_init(dev);
1326 dev->vm_rgn = alloc_vm_area(NVMAP_NUM_PTES * PAGE_SIZE, NULL);
1329 dev_err(&pdev->dev, "couldn't allocate remapping region\n");
1333 spin_lock_init(&dev->ptelock);
1334 spin_lock_init(&dev->handle_lock);
1335 INIT_LIST_HEAD(&dev->clients);
1336 mutex_init(&dev->clients_lock);
1337 INIT_LIST_HEAD(&dev->lru_handles);
1338 spin_lock_init(&dev->lru_lock);
1340 for (i = 0; i < NVMAP_NUM_PTES; i++) {
1346 addr = (unsigned long)dev->vm_rgn->addr + (i * PAGE_SIZE);
1347 pgd = pgd_offset_k(addr);
1348 pud = pud_alloc(&init_mm, pgd, addr);
1351 dev_err(&pdev->dev, "couldn't allocate page tables\n");
1354 pmd = pmd_alloc(&init_mm, pud, addr);
1357 dev_err(&pdev->dev, "couldn't allocate page tables\n");
1360 dev->ptes[i] = pte_alloc_kernel(pmd, addr);
1361 if (!dev->ptes[i]) {
1363 dev_err(&pdev->dev, "couldn't allocate page tables\n");
1368 e = misc_register(&dev->dev_user);
1370 dev_err(&pdev->dev, "unable to register miscdevice %s\n",
1371 dev->dev_user.name);
1375 dev->nr_carveouts = 0;
1376 dev->heaps = kzalloc(sizeof(struct nvmap_carveout_node) *
1377 plat->nr_carveouts, GFP_KERNEL);
1380 dev_err(&pdev->dev, "couldn't allocate carveout memory\n");
1384 nvmap_debug_root = debugfs_create_dir("nvmap", NULL);
1385 if (IS_ERR_OR_NULL(nvmap_debug_root))
1386 dev_err(&pdev->dev, "couldn't create debug files\n");
1388 debugfs_create_u32("max_handle_count", S_IRUGO,
1389 nvmap_debug_root, &nvmap_max_handle_count);
1391 for (i = 0; i < plat->nr_carveouts; i++) {
1392 struct nvmap_carveout_node *node = &dev->heaps[dev->nr_carveouts];
1393 const struct nvmap_platform_carveout *co = &plat->carveouts[i];
1394 node->base = round_up(co->base, PAGE_SIZE);
1395 node->size = round_down(co->size -
1396 (node->base - co->base), PAGE_SIZE);
1400 node->carveout = nvmap_heap_create(
1401 dev->dev_user.this_device, co,
1402 node->base, node->size, node);
1404 if (!node->carveout) {
1406 dev_err(&pdev->dev, "couldn't create %s\n", co->name);
1409 node->index = dev->nr_carveouts;
1410 dev->nr_carveouts++;
1411 spin_lock_init(&node->clients_lock);
1412 INIT_LIST_HEAD(&node->clients);
1413 node->heap_bit = co->usage_mask;
1415 if (!IS_ERR_OR_NULL(nvmap_debug_root)) {
1416 struct dentry *heap_root =
1417 debugfs_create_dir(co->name, nvmap_debug_root);
1418 if (!IS_ERR_OR_NULL(heap_root)) {
1419 debugfs_create_file("clients", S_IRUGO,
1421 (void *)(uintptr_t)node->heap_bit,
1422 &debug_clients_fops);
1423 debugfs_create_file("allocations", S_IRUGO,
1425 (void *)(uintptr_t)node->heap_bit,
1426 &debug_allocations_fops);
1427 debugfs_create_file("maps", S_IRUGO,
1429 (void *)(uintptr_t)node->heap_bit,
1434 if (!IS_ERR_OR_NULL(nvmap_debug_root)) {
1435 struct dentry *iovmm_root =
1436 debugfs_create_dir("iovmm", nvmap_debug_root);
1437 if (!IS_ERR_OR_NULL(iovmm_root)) {
1438 debugfs_create_file("clients", S_IRUGO, iovmm_root,
1439 (void *)(uintptr_t)NVMAP_HEAP_IOVMM,
1440 &debug_clients_fops);
1441 debugfs_create_file("allocations", S_IRUGO, iovmm_root,
1442 (void *)(uintptr_t)NVMAP_HEAP_IOVMM,
1443 &debug_allocations_fops);
1444 debugfs_create_file("maps", S_IRUGO, iovmm_root,
1445 (void *)(uintptr_t)NVMAP_HEAP_IOVMM,
1447 debugfs_create_file("procrank", S_IRUGO, iovmm_root,
1448 dev, &debug_iovmm_procrank_fops);
1449 #ifdef CONFIG_NVMAP_PAGE_POOLS
1450 debugfs_create_u32("page_pool_available_pages",
1451 S_IRUGO, iovmm_root,
1453 #ifdef CONFIG_NVMAP_PAGE_POOL_DEBUG
1454 debugfs_create_u32("page_pool_alloc_ind",
1455 S_IRUGO, iovmm_root,
1457 debugfs_create_u32("page_pool_fill_ind",
1458 S_IRUGO, iovmm_root,
1460 debugfs_create_u64("page_pool_allocs",
1461 S_IRUGO, iovmm_root,
1463 debugfs_create_u64("page_pool_fills",
1464 S_IRUGO, iovmm_root,
1466 debugfs_create_u64("page_pool_hits",
1467 S_IRUGO, iovmm_root,
1469 debugfs_create_u64("page_pool_misses",
1470 S_IRUGO, iovmm_root,
1475 #ifdef CONFIG_NVMAP_CACHE_MAINT_BY_SET_WAYS
1476 debugfs_create_size_t("cache_maint_inner_threshold",
1479 &cache_maint_inner_threshold);
1482 if ((read_cpuid_id() >> 4 & 0xfff) == 0xc09)
1483 cache_maint_inner_threshold = SZ_32K;
1484 pr_info("nvmap:inner cache maint threshold=%zd",
1485 cache_maint_inner_threshold);
1487 #ifdef CONFIG_NVMAP_OUTER_CACHE_MAINT_BY_SET_WAYS
1488 debugfs_create_size_t("cache_maint_outer_threshold",
1491 &cache_maint_outer_threshold);
1492 pr_info("nvmap:outer cache maint threshold=%zd",
1493 cache_maint_outer_threshold);
1497 nvmap_stats_init(nvmap_debug_root);
1498 platform_set_drvdata(pdev, dev);
1502 nvmap_dmabuf_debugfs_init(nvmap_debug_root);
1503 e = nvmap_dmabuf_stash_init();
1509 for (i = 0; i < dev->nr_carveouts; i++) {
1510 struct nvmap_carveout_node *node = &dev->heaps[i];
1511 nvmap_heap_destroy(node->carveout);
1514 #ifdef CONFIG_NVMAP_PAGE_POOLS
1515 nvmap_page_pool_fini(nvmap_dev);
1518 if (dev->dev_user.minor != MISC_DYNAMIC_MINOR)
1519 misc_deregister(&dev->dev_user);
1521 free_vm_area(dev->vm_rgn);
1527 static int nvmap_remove(struct platform_device *pdev)
1529 struct nvmap_device *dev = platform_get_drvdata(pdev);
1531 struct nvmap_handle *h;
1534 misc_deregister(&dev->dev_user);
1536 while ((n = rb_first(&dev->handles))) {
1537 h = rb_entry(n, struct nvmap_handle, node);
1538 rb_erase(&h->node, &dev->handles);
1542 for (i = 0; i < dev->nr_carveouts; i++) {
1543 struct nvmap_carveout_node *node = &dev->heaps[i];
1544 nvmap_heap_destroy(node->carveout);
1548 free_vm_area(dev->vm_rgn);
1554 static int nvmap_suspend(struct platform_device *pdev, pm_message_t state)
1559 static int nvmap_resume(struct platform_device *pdev)
1564 static struct platform_driver nvmap_driver = {
1565 .probe = nvmap_probe,
1566 .remove = nvmap_remove,
1567 .suspend = nvmap_suspend,
1568 .resume = nvmap_resume,
1571 .name = "tegra-nvmap",
1572 .owner = THIS_MODULE,
1576 static int __init nvmap_init_driver(void)
1582 e = nvmap_heap_init();
1586 e = platform_driver_register(&nvmap_driver);
1588 nvmap_heap_deinit();
1595 fs_initcall(nvmap_init_driver);
1597 static void __exit nvmap_exit_driver(void)
1599 platform_driver_unregister(&nvmap_driver);
1600 nvmap_heap_deinit();
1603 module_exit(nvmap_exit_driver);