blob: ecb8f0c7f0253b7eef10e97e3e88bd7e25c28296 [file] [log] [blame]
Pawel Osciak3c18ff02010-10-11 10:58:53 -03001/*
2 * videobuf2-vmalloc.c - vmalloc memory allocator for videobuf2
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
Pawel Osciak95072082011-03-13 15:23:32 -03006 * Author: Pawel Osciak <pawel@osciak.com>
Pawel Osciak3c18ff02010-10-11 10:58:53 -03007 *
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.
11 */
12
Javier Martin570d2a42012-02-16 12:19:08 -030013#include <linux/io.h>
Pawel Osciak3c18ff02010-10-11 10:58:53 -030014#include <linux/module.h>
15#include <linux/mm.h>
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030016#include <linux/sched.h>
Pawel Osciak3c18ff02010-10-11 10:58:53 -030017#include <linux/slab.h>
18#include <linux/vmalloc.h>
19
20#include <media/videobuf2-core.h>
Nicolas THERYddb9fa22012-08-03 07:23:54 -030021#include <media/videobuf2-vmalloc.h>
Pawel Osciak3c18ff02010-10-11 10:58:53 -030022#include <media/videobuf2-memops.h>
23
24struct vb2_vmalloc_buf {
25 void *vaddr;
Jan Kara5a9e4de2015-07-13 11:55:48 -030026 struct frame_vector *vec;
Hans Verkuilcd474032014-11-18 09:50:58 -030027 enum dma_data_direction dma_dir;
Pawel Osciak3c18ff02010-10-11 10:58:53 -030028 unsigned long size;
29 atomic_t refcount;
30 struct vb2_vmarea_handler handler;
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -030031 struct dma_buf *dbuf;
Pawel Osciak3c18ff02010-10-11 10:58:53 -030032};
33
34static void vb2_vmalloc_put(void *buf_priv);
35
Hans Verkuild935c572014-11-18 09:50:59 -030036static void *vb2_vmalloc_alloc(void *alloc_ctx, unsigned long size,
37 enum dma_data_direction dma_dir, gfp_t gfp_flags)
Pawel Osciak3c18ff02010-10-11 10:58:53 -030038{
39 struct vb2_vmalloc_buf *buf;
40
Hans Verkuilb6ba2052013-03-01 15:44:20 -030041 buf = kzalloc(sizeof(*buf), GFP_KERNEL | gfp_flags);
Pawel Osciak3c18ff02010-10-11 10:58:53 -030042 if (!buf)
43 return NULL;
44
45 buf->size = size;
46 buf->vaddr = vmalloc_user(buf->size);
Hans Verkuild935c572014-11-18 09:50:59 -030047 buf->dma_dir = dma_dir;
Pawel Osciak3c18ff02010-10-11 10:58:53 -030048 buf->handler.refcount = &buf->refcount;
49 buf->handler.put = vb2_vmalloc_put;
50 buf->handler.arg = buf;
51
52 if (!buf->vaddr) {
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030053 pr_debug("vmalloc of size %ld failed\n", buf->size);
Pawel Osciak3c18ff02010-10-11 10:58:53 -030054 kfree(buf);
55 return NULL;
56 }
57
58 atomic_inc(&buf->refcount);
Pawel Osciak3c18ff02010-10-11 10:58:53 -030059 return buf;
60}
61
62static void vb2_vmalloc_put(void *buf_priv)
63{
64 struct vb2_vmalloc_buf *buf = buf_priv;
65
66 if (atomic_dec_and_test(&buf->refcount)) {
Pawel Osciak3c18ff02010-10-11 10:58:53 -030067 vfree(buf->vaddr);
68 kfree(buf);
69 }
70}
71
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030072static void *vb2_vmalloc_get_userptr(void *alloc_ctx, unsigned long vaddr,
Hans Verkuilcd474032014-11-18 09:50:58 -030073 unsigned long size,
74 enum dma_data_direction dma_dir)
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030075{
76 struct vb2_vmalloc_buf *buf;
Jan Kara5a9e4de2015-07-13 11:55:48 -030077 struct frame_vector *vec;
78 int n_pages, offset, i;
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030079
80 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
81 if (!buf)
82 return NULL;
83
Hans Verkuilcd474032014-11-18 09:50:58 -030084 buf->dma_dir = dma_dir;
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030085 offset = vaddr & ~PAGE_MASK;
86 buf->size = size;
Jan Kara5a9e4de2015-07-13 11:55:48 -030087 vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE);
88 if (IS_ERR(vec))
89 goto fail_pfnvec_create;
90 buf->vec = vec;
91 n_pages = frame_vector_count(vec);
92 if (frame_vector_to_pages(vec) < 0) {
93 unsigned long *nums = frame_vector_pfns(vec);
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030094
Jan Kara5a9e4de2015-07-13 11:55:48 -030095 /*
96 * We cannot get page pointers for these pfns. Check memory is
97 * physically contiguous and use direct mapping.
98 */
99 for (i = 1; i < n_pages; i++)
100 if (nums[i-1] + 1 != nums[i])
101 goto fail_map;
102 buf->vaddr = (__force void *)
103 ioremap_nocache(nums[0] << PAGE_SHIFT, size);
Javier Martin570d2a42012-02-16 12:19:08 -0300104 } else {
Jan Kara5a9e4de2015-07-13 11:55:48 -0300105 buf->vaddr = vm_map_ram(frame_vector_pages(vec), n_pages, -1,
Javier Martin570d2a42012-02-16 12:19:08 -0300106 PAGE_KERNEL);
Javier Martin570d2a42012-02-16 12:19:08 -0300107 }
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300108
Jan Kara5a9e4de2015-07-13 11:55:48 -0300109 if (!buf->vaddr)
110 goto fail_map;
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300111 buf->vaddr += offset;
112 return buf;
113
Jan Kara5a9e4de2015-07-13 11:55:48 -0300114fail_map:
115 vb2_destroy_framevec(vec);
116fail_pfnvec_create:
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300117 kfree(buf);
118
119 return NULL;
120}
121
122static void vb2_vmalloc_put_userptr(void *buf_priv)
123{
124 struct vb2_vmalloc_buf *buf = buf_priv;
125 unsigned long vaddr = (unsigned long)buf->vaddr & PAGE_MASK;
126 unsigned int i;
Jan Kara5a9e4de2015-07-13 11:55:48 -0300127 struct page **pages;
128 unsigned int n_pages;
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300129
Jan Kara5a9e4de2015-07-13 11:55:48 -0300130 if (!buf->vec->is_pfns) {
131 n_pages = frame_vector_count(buf->vec);
132 pages = frame_vector_pages(buf->vec);
Javier Martin570d2a42012-02-16 12:19:08 -0300133 if (vaddr)
Jan Kara5a9e4de2015-07-13 11:55:48 -0300134 vm_unmap_ram((void *)vaddr, n_pages);
135 if (buf->dma_dir == DMA_FROM_DEVICE)
136 for (i = 0; i < n_pages; i++)
137 set_page_dirty_lock(pages[i]);
Javier Martin570d2a42012-02-16 12:19:08 -0300138 } else {
Hans Verkuil7c424dd2014-12-13 08:52:54 -0300139 iounmap((__force void __iomem *)buf->vaddr);
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300140 }
Jan Kara5a9e4de2015-07-13 11:55:48 -0300141 vb2_destroy_framevec(buf->vec);
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300142 kfree(buf);
143}
144
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300145static void *vb2_vmalloc_vaddr(void *buf_priv)
146{
147 struct vb2_vmalloc_buf *buf = buf_priv;
148
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300149 if (!buf->vaddr) {
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300150 pr_err("Address of an unallocated plane requested "
151 "or cannot map user pointer\n");
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300152 return NULL;
153 }
154
155 return buf->vaddr;
156}
157
158static unsigned int vb2_vmalloc_num_users(void *buf_priv)
159{
160 struct vb2_vmalloc_buf *buf = buf_priv;
161 return atomic_read(&buf->refcount);
162}
163
164static int vb2_vmalloc_mmap(void *buf_priv, struct vm_area_struct *vma)
165{
166 struct vb2_vmalloc_buf *buf = buf_priv;
167 int ret;
168
169 if (!buf) {
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300170 pr_err("No memory to map\n");
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300171 return -EINVAL;
172 }
173
174 ret = remap_vmalloc_range(vma, buf->vaddr, 0);
175 if (ret) {
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300176 pr_err("Remapping vmalloc memory, error: %d\n", ret);
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300177 return ret;
178 }
179
180 /*
181 * Make sure that vm_areas for 2 buffers won't be merged together
182 */
183 vma->vm_flags |= VM_DONTEXPAND;
184
185 /*
186 * Use common vm_area operations to track buffer refcount.
187 */
188 vma->vm_private_data = &buf->handler;
189 vma->vm_ops = &vb2_common_vm_ops;
190
191 vma->vm_ops->open(vma);
192
193 return 0;
194}
195
Geert Uytterhoeven99f3cd52014-12-15 10:40:28 -0300196#ifdef CONFIG_HAS_DMA
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -0300197/*********************************************/
Hans Verkuil46506352014-11-18 09:51:05 -0300198/* DMABUF ops for exporters */
199/*********************************************/
200
201struct vb2_vmalloc_attachment {
202 struct sg_table sgt;
203 enum dma_data_direction dma_dir;
204};
205
206static int vb2_vmalloc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
207 struct dma_buf_attachment *dbuf_attach)
208{
209 struct vb2_vmalloc_attachment *attach;
210 struct vb2_vmalloc_buf *buf = dbuf->priv;
211 int num_pages = PAGE_ALIGN(buf->size) / PAGE_SIZE;
212 struct sg_table *sgt;
213 struct scatterlist *sg;
214 void *vaddr = buf->vaddr;
215 int ret;
216 int i;
217
218 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
219 if (!attach)
220 return -ENOMEM;
221
222 sgt = &attach->sgt;
223 ret = sg_alloc_table(sgt, num_pages, GFP_KERNEL);
224 if (ret) {
225 kfree(attach);
226 return ret;
227 }
228 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
229 struct page *page = vmalloc_to_page(vaddr);
230
231 if (!page) {
232 sg_free_table(sgt);
233 kfree(attach);
234 return -ENOMEM;
235 }
236 sg_set_page(sg, page, PAGE_SIZE, 0);
237 vaddr += PAGE_SIZE;
238 }
239
240 attach->dma_dir = DMA_NONE;
241 dbuf_attach->priv = attach;
242 return 0;
243}
244
245static void vb2_vmalloc_dmabuf_ops_detach(struct dma_buf *dbuf,
246 struct dma_buf_attachment *db_attach)
247{
248 struct vb2_vmalloc_attachment *attach = db_attach->priv;
249 struct sg_table *sgt;
250
251 if (!attach)
252 return;
253
254 sgt = &attach->sgt;
255
256 /* release the scatterlist cache */
257 if (attach->dma_dir != DMA_NONE)
258 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
259 attach->dma_dir);
260 sg_free_table(sgt);
261 kfree(attach);
262 db_attach->priv = NULL;
263}
264
265static struct sg_table *vb2_vmalloc_dmabuf_ops_map(
266 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
267{
268 struct vb2_vmalloc_attachment *attach = db_attach->priv;
269 /* stealing dmabuf mutex to serialize map/unmap operations */
270 struct mutex *lock = &db_attach->dmabuf->lock;
271 struct sg_table *sgt;
Hans Verkuil46506352014-11-18 09:51:05 -0300272
273 mutex_lock(lock);
274
275 sgt = &attach->sgt;
276 /* return previously mapped sg table */
277 if (attach->dma_dir == dma_dir) {
278 mutex_unlock(lock);
279 return sgt;
280 }
281
282 /* release any previous cache */
283 if (attach->dma_dir != DMA_NONE) {
284 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
285 attach->dma_dir);
286 attach->dma_dir = DMA_NONE;
287 }
288
289 /* mapping to the client with new direction */
Ricardo Ribaldaba81c6e2015-04-29 09:00:47 -0300290 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
291 dma_dir);
292 if (!sgt->nents) {
Hans Verkuil46506352014-11-18 09:51:05 -0300293 pr_err("failed to map scatterlist\n");
294 mutex_unlock(lock);
295 return ERR_PTR(-EIO);
296 }
297
298 attach->dma_dir = dma_dir;
299
300 mutex_unlock(lock);
301
302 return sgt;
303}
304
305static void vb2_vmalloc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
306 struct sg_table *sgt, enum dma_data_direction dma_dir)
307{
308 /* nothing to be done here */
309}
310
311static void vb2_vmalloc_dmabuf_ops_release(struct dma_buf *dbuf)
312{
313 /* drop reference obtained in vb2_vmalloc_get_dmabuf */
314 vb2_vmalloc_put(dbuf->priv);
315}
316
317static void *vb2_vmalloc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
318{
319 struct vb2_vmalloc_buf *buf = dbuf->priv;
320
321 return buf->vaddr + pgnum * PAGE_SIZE;
322}
323
324static void *vb2_vmalloc_dmabuf_ops_vmap(struct dma_buf *dbuf)
325{
326 struct vb2_vmalloc_buf *buf = dbuf->priv;
327
328 return buf->vaddr;
329}
330
331static int vb2_vmalloc_dmabuf_ops_mmap(struct dma_buf *dbuf,
332 struct vm_area_struct *vma)
333{
334 return vb2_vmalloc_mmap(dbuf->priv, vma);
335}
336
337static struct dma_buf_ops vb2_vmalloc_dmabuf_ops = {
338 .attach = vb2_vmalloc_dmabuf_ops_attach,
339 .detach = vb2_vmalloc_dmabuf_ops_detach,
340 .map_dma_buf = vb2_vmalloc_dmabuf_ops_map,
341 .unmap_dma_buf = vb2_vmalloc_dmabuf_ops_unmap,
342 .kmap = vb2_vmalloc_dmabuf_ops_kmap,
343 .kmap_atomic = vb2_vmalloc_dmabuf_ops_kmap,
344 .vmap = vb2_vmalloc_dmabuf_ops_vmap,
345 .mmap = vb2_vmalloc_dmabuf_ops_mmap,
346 .release = vb2_vmalloc_dmabuf_ops_release,
347};
348
349static struct dma_buf *vb2_vmalloc_get_dmabuf(void *buf_priv, unsigned long flags)
350{
351 struct vb2_vmalloc_buf *buf = buf_priv;
352 struct dma_buf *dbuf;
Sumit Semwald8fbe342015-01-23 12:53:43 +0530353 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
354
355 exp_info.ops = &vb2_vmalloc_dmabuf_ops;
356 exp_info.size = buf->size;
357 exp_info.flags = flags;
358 exp_info.priv = buf;
Hans Verkuil46506352014-11-18 09:51:05 -0300359
360 if (WARN_ON(!buf->vaddr))
361 return NULL;
362
Sumit Semwald8fbe342015-01-23 12:53:43 +0530363 dbuf = dma_buf_export(&exp_info);
Hans Verkuil46506352014-11-18 09:51:05 -0300364 if (IS_ERR(dbuf))
365 return NULL;
366
367 /* dmabuf keeps reference to vb2 buffer */
368 atomic_inc(&buf->refcount);
369
370 return dbuf;
371}
Geert Uytterhoeven99f3cd52014-12-15 10:40:28 -0300372#endif /* CONFIG_HAS_DMA */
373
Hans Verkuil46506352014-11-18 09:51:05 -0300374
375/*********************************************/
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -0300376/* callbacks for DMABUF buffers */
377/*********************************************/
378
379static int vb2_vmalloc_map_dmabuf(void *mem_priv)
380{
381 struct vb2_vmalloc_buf *buf = mem_priv;
382
383 buf->vaddr = dma_buf_vmap(buf->dbuf);
384
385 return buf->vaddr ? 0 : -EFAULT;
386}
387
388static void vb2_vmalloc_unmap_dmabuf(void *mem_priv)
389{
390 struct vb2_vmalloc_buf *buf = mem_priv;
391
392 dma_buf_vunmap(buf->dbuf, buf->vaddr);
393 buf->vaddr = NULL;
394}
395
396static void vb2_vmalloc_detach_dmabuf(void *mem_priv)
397{
398 struct vb2_vmalloc_buf *buf = mem_priv;
399
400 if (buf->vaddr)
401 dma_buf_vunmap(buf->dbuf, buf->vaddr);
402
403 kfree(buf);
404}
405
406static void *vb2_vmalloc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
Hans Verkuilcd474032014-11-18 09:50:58 -0300407 unsigned long size, enum dma_data_direction dma_dir)
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -0300408{
409 struct vb2_vmalloc_buf *buf;
410
411 if (dbuf->size < size)
412 return ERR_PTR(-EFAULT);
413
414 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
415 if (!buf)
416 return ERR_PTR(-ENOMEM);
417
418 buf->dbuf = dbuf;
Hans Verkuilcd474032014-11-18 09:50:58 -0300419 buf->dma_dir = dma_dir;
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -0300420 buf->size = size;
421
422 return buf;
423}
424
425
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300426const struct vb2_mem_ops vb2_vmalloc_memops = {
427 .alloc = vb2_vmalloc_alloc,
428 .put = vb2_vmalloc_put,
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300429 .get_userptr = vb2_vmalloc_get_userptr,
430 .put_userptr = vb2_vmalloc_put_userptr,
Geert Uytterhoeven99f3cd52014-12-15 10:40:28 -0300431#ifdef CONFIG_HAS_DMA
Hans Verkuil46506352014-11-18 09:51:05 -0300432 .get_dmabuf = vb2_vmalloc_get_dmabuf,
Geert Uytterhoeven99f3cd52014-12-15 10:40:28 -0300433#endif
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -0300434 .map_dmabuf = vb2_vmalloc_map_dmabuf,
435 .unmap_dmabuf = vb2_vmalloc_unmap_dmabuf,
436 .attach_dmabuf = vb2_vmalloc_attach_dmabuf,
437 .detach_dmabuf = vb2_vmalloc_detach_dmabuf,
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300438 .vaddr = vb2_vmalloc_vaddr,
439 .mmap = vb2_vmalloc_mmap,
440 .num_users = vb2_vmalloc_num_users,
441};
442EXPORT_SYMBOL_GPL(vb2_vmalloc_memops);
443
444MODULE_DESCRIPTION("vmalloc memory handling routines for videobuf2");
Pawel Osciak95072082011-03-13 15:23:32 -0300445MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300446MODULE_LICENSE("GPL");