1 /*
2  * videobuf2-memops.c - generic memory handling routines for videobuf2
3  *
4  * Copyright (C) 2010 Samsung Electronics
5  *
6  * Author: Pawel Osciak <pawel@osciak.com>
7  *	   Marek Szyprowski <m.szyprowski@samsung.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation.
12  */
13 
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mm.h>
19 #include <linux/sched.h>
20 #include <linux/file.h>
21 
22 #include <media/videobuf2-v4l2.h>
23 #include <media/videobuf2-memops.h>
24 
25 /**
26  * vb2_create_framevec() - map virtual addresses to pfns
27  * @start:	Virtual user address where we start mapping
28  * @length:	Length of a range to map
29  *
30  * This function allocates and fills in a vector with pfns corresponding to
31  * virtual address range passed in arguments. If pfns have corresponding pages,
32  * page references are also grabbed to pin pages in memory. The function
33  * returns pointer to the vector on success and error pointer in case of
34  * failure. Returned vector needs to be freed via vb2_destroy_pfnvec().
35  */
36 struct frame_vector *vb2_create_framevec(unsigned long start,
37 					 unsigned long length)
38 {
39 	int ret;
40 	unsigned long first, last;
41 	unsigned long nr;
42 	struct frame_vector *vec;
43 
44 	first = start >> PAGE_SHIFT;
45 	last = (start + length - 1) >> PAGE_SHIFT;
46 	nr = last - first + 1;
47 	vec = frame_vector_create(nr);
48 	if (!vec)
49 		return ERR_PTR(-ENOMEM);
50 	ret = get_vaddr_frames(start & PAGE_MASK, nr, vec);
51 	if (ret < 0)
52 		goto out_destroy;
53 	/* We accept only complete set of PFNs */
54 	if (ret != nr) {
55 		ret = -EFAULT;
56 		goto out_release;
57 	}
58 	return vec;
59 out_release:
60 	put_vaddr_frames(vec);
61 out_destroy:
62 	frame_vector_destroy(vec);
63 	return ERR_PTR(ret);
64 }
65 EXPORT_SYMBOL(vb2_create_framevec);
66 
67 /**
68  * vb2_destroy_framevec() - release vector of mapped pfns
69  * @vec:	vector of pfns / pages to release
70  *
71  * This releases references to all pages in the vector @vec (if corresponding
72  * pfns are backed by pages) and frees the passed vector.
73  */
74 void vb2_destroy_framevec(struct frame_vector *vec)
75 {
76 	put_vaddr_frames(vec);
77 	frame_vector_destroy(vec);
78 }
79 EXPORT_SYMBOL(vb2_destroy_framevec);
80 
81 /**
82  * vb2_common_vm_open() - increase refcount of the vma
83  * @vma:	virtual memory region for the mapping
84  *
85  * This function adds another user to the provided vma. It expects
86  * struct vb2_vmarea_handler pointer in vma->vm_private_data.
87  */
88 static void vb2_common_vm_open(struct vm_area_struct *vma)
89 {
90 	struct vb2_vmarea_handler *h = vma->vm_private_data;
91 
92 	pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n",
93 	       __func__, h, refcount_read(h->refcount), vma->vm_start,
94 	       vma->vm_end);
95 
96 	refcount_inc(h->refcount);
97 }
98 
99 /**
100  * vb2_common_vm_close() - decrease refcount of the vma
101  * @vma:	virtual memory region for the mapping
102  *
103  * This function releases the user from the provided vma. It expects
104  * struct vb2_vmarea_handler pointer in vma->vm_private_data.
105  */
106 static void vb2_common_vm_close(struct vm_area_struct *vma)
107 {
108 	struct vb2_vmarea_handler *h = vma->vm_private_data;
109 
110 	pr_debug("%s: %p, refcount: %d, vma: %08lx-%08lx\n",
111 	       __func__, h, refcount_read(h->refcount), vma->vm_start,
112 	       vma->vm_end);
113 
114 	h->put(h->arg);
115 }
116 
117 /*
118  * vb2_common_vm_ops - common vm_ops used for tracking refcount of mmapped
119  * video buffers
120  */
121 const struct vm_operations_struct vb2_common_vm_ops = {
122 	.open = vb2_common_vm_open,
123 	.close = vb2_common_vm_close,
124 };
125 EXPORT_SYMBOL_GPL(vb2_common_vm_ops);
126 
127 MODULE_DESCRIPTION("common memory handling routines for videobuf2");
128 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
129 MODULE_LICENSE("GPL");
130