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