blob: c2820a6e164dec114a6a062c071da7414428bf95 [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
Junghak Sungc1399902015-09-22 10:30:29 -030020#include <media/videobuf2-v4l2.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
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070036static void *vb2_vmalloc_alloc(struct device *dev, unsigned long attrs,
Hans Verkuild16e8322016-04-15 09:15:05 -030037 unsigned long size, enum dma_data_direction dma_dir,
38 gfp_t gfp_flags)
Pawel Osciak3c18ff02010-10-11 10:58:53 -030039{
40 struct vb2_vmalloc_buf *buf;
41
Hans Verkuilb6ba2052013-03-01 15:44:20 -030042 buf = kzalloc(sizeof(*buf), GFP_KERNEL | gfp_flags);
Pawel Osciak3c18ff02010-10-11 10:58:53 -030043 if (!buf)
44 return NULL;
45
46 buf->size = size;
47 buf->vaddr = vmalloc_user(buf->size);
Hans Verkuild935c572014-11-18 09:50:59 -030048 buf->dma_dir = dma_dir;
Pawel Osciak3c18ff02010-10-11 10:58:53 -030049 buf->handler.refcount = &buf->refcount;
50 buf->handler.put = vb2_vmalloc_put;
51 buf->handler.arg = buf;
52
53 if (!buf->vaddr) {
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030054 pr_debug("vmalloc of size %ld failed\n", buf->size);
Pawel Osciak3c18ff02010-10-11 10:58:53 -030055 kfree(buf);
56 return NULL;
57 }
58
59 atomic_inc(&buf->refcount);
Pawel Osciak3c18ff02010-10-11 10:58:53 -030060 return buf;
61}
62
63static void vb2_vmalloc_put(void *buf_priv)
64{
65 struct vb2_vmalloc_buf *buf = buf_priv;
66
67 if (atomic_dec_and_test(&buf->refcount)) {
Pawel Osciak3c18ff02010-10-11 10:58:53 -030068 vfree(buf->vaddr);
69 kfree(buf);
70 }
71}
72
Hans Verkuil36c0f8b2016-04-15 09:15:05 -030073static void *vb2_vmalloc_get_userptr(struct device *dev, unsigned long vaddr,
Hans Verkuilcd474032014-11-18 09:50:58 -030074 unsigned long size,
75 enum dma_data_direction dma_dir)
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030076{
77 struct vb2_vmalloc_buf *buf;
Jan Kara5a9e4de2015-07-13 11:55:48 -030078 struct frame_vector *vec;
79 int n_pages, offset, i;
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030080
81 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
82 if (!buf)
83 return NULL;
84
Hans Verkuilcd474032014-11-18 09:50:58 -030085 buf->dma_dir = dma_dir;
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030086 offset = vaddr & ~PAGE_MASK;
87 buf->size = size;
Jan Kara5a9e4de2015-07-13 11:55:48 -030088 vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE);
89 if (IS_ERR(vec))
90 goto fail_pfnvec_create;
91 buf->vec = vec;
92 n_pages = frame_vector_count(vec);
93 if (frame_vector_to_pages(vec) < 0) {
94 unsigned long *nums = frame_vector_pfns(vec);
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -030095
Jan Kara5a9e4de2015-07-13 11:55:48 -030096 /*
97 * We cannot get page pointers for these pfns. Check memory is
98 * physically contiguous and use direct mapping.
99 */
100 for (i = 1; i < n_pages; i++)
101 if (nums[i-1] + 1 != nums[i])
102 goto fail_map;
103 buf->vaddr = (__force void *)
104 ioremap_nocache(nums[0] << PAGE_SHIFT, size);
Javier Martin570d2a42012-02-16 12:19:08 -0300105 } else {
Jan Kara5a9e4de2015-07-13 11:55:48 -0300106 buf->vaddr = vm_map_ram(frame_vector_pages(vec), n_pages, -1,
Javier Martin570d2a42012-02-16 12:19:08 -0300107 PAGE_KERNEL);
Javier Martin570d2a42012-02-16 12:19:08 -0300108 }
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300109
Jan Kara5a9e4de2015-07-13 11:55:48 -0300110 if (!buf->vaddr)
111 goto fail_map;
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300112 buf->vaddr += offset;
113 return buf;
114
Jan Kara5a9e4de2015-07-13 11:55:48 -0300115fail_map:
116 vb2_destroy_framevec(vec);
117fail_pfnvec_create:
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300118 kfree(buf);
119
120 return NULL;
121}
122
123static void vb2_vmalloc_put_userptr(void *buf_priv)
124{
125 struct vb2_vmalloc_buf *buf = buf_priv;
126 unsigned long vaddr = (unsigned long)buf->vaddr & PAGE_MASK;
127 unsigned int i;
Jan Kara5a9e4de2015-07-13 11:55:48 -0300128 struct page **pages;
129 unsigned int n_pages;
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300130
Jan Kara5a9e4de2015-07-13 11:55:48 -0300131 if (!buf->vec->is_pfns) {
132 n_pages = frame_vector_count(buf->vec);
133 pages = frame_vector_pages(buf->vec);
Javier Martin570d2a42012-02-16 12:19:08 -0300134 if (vaddr)
Jan Kara5a9e4de2015-07-13 11:55:48 -0300135 vm_unmap_ram((void *)vaddr, n_pages);
136 if (buf->dma_dir == DMA_FROM_DEVICE)
137 for (i = 0; i < n_pages; i++)
138 set_page_dirty_lock(pages[i]);
Javier Martin570d2a42012-02-16 12:19:08 -0300139 } else {
Hans Verkuil7c424dd2014-12-13 08:52:54 -0300140 iounmap((__force void __iomem *)buf->vaddr);
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300141 }
Jan Kara5a9e4de2015-07-13 11:55:48 -0300142 vb2_destroy_framevec(buf->vec);
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300143 kfree(buf);
144}
145
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300146static void *vb2_vmalloc_vaddr(void *buf_priv)
147{
148 struct vb2_vmalloc_buf *buf = buf_priv;
149
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300150 if (!buf->vaddr) {
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300151 pr_err("Address of an unallocated plane requested "
152 "or cannot map user pointer\n");
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300153 return NULL;
154 }
155
156 return buf->vaddr;
157}
158
159static unsigned int vb2_vmalloc_num_users(void *buf_priv)
160{
161 struct vb2_vmalloc_buf *buf = buf_priv;
162 return atomic_read(&buf->refcount);
163}
164
165static int vb2_vmalloc_mmap(void *buf_priv, struct vm_area_struct *vma)
166{
167 struct vb2_vmalloc_buf *buf = buf_priv;
168 int ret;
169
170 if (!buf) {
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300171 pr_err("No memory to map\n");
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300172 return -EINVAL;
173 }
174
175 ret = remap_vmalloc_range(vma, buf->vaddr, 0);
176 if (ret) {
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300177 pr_err("Remapping vmalloc memory, error: %d\n", ret);
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300178 return ret;
179 }
180
181 /*
182 * Make sure that vm_areas for 2 buffers won't be merged together
183 */
184 vma->vm_flags |= VM_DONTEXPAND;
185
186 /*
187 * Use common vm_area operations to track buffer refcount.
188 */
189 vma->vm_private_data = &buf->handler;
190 vma->vm_ops = &vb2_common_vm_ops;
191
192 vma->vm_ops->open(vma);
193
194 return 0;
195}
196
Geert Uytterhoeven99f3cd52014-12-15 10:40:28 -0300197#ifdef CONFIG_HAS_DMA
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -0300198/*********************************************/
Hans Verkuil46506352014-11-18 09:51:05 -0300199/* DMABUF ops for exporters */
200/*********************************************/
201
202struct vb2_vmalloc_attachment {
203 struct sg_table sgt;
204 enum dma_data_direction dma_dir;
205};
206
207static int vb2_vmalloc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
208 struct dma_buf_attachment *dbuf_attach)
209{
210 struct vb2_vmalloc_attachment *attach;
211 struct vb2_vmalloc_buf *buf = dbuf->priv;
212 int num_pages = PAGE_ALIGN(buf->size) / PAGE_SIZE;
213 struct sg_table *sgt;
214 struct scatterlist *sg;
215 void *vaddr = buf->vaddr;
216 int ret;
217 int i;
218
219 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
220 if (!attach)
221 return -ENOMEM;
222
223 sgt = &attach->sgt;
224 ret = sg_alloc_table(sgt, num_pages, GFP_KERNEL);
225 if (ret) {
226 kfree(attach);
227 return ret;
228 }
229 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
230 struct page *page = vmalloc_to_page(vaddr);
231
232 if (!page) {
233 sg_free_table(sgt);
234 kfree(attach);
235 return -ENOMEM;
236 }
237 sg_set_page(sg, page, PAGE_SIZE, 0);
238 vaddr += PAGE_SIZE;
239 }
240
241 attach->dma_dir = DMA_NONE;
242 dbuf_attach->priv = attach;
243 return 0;
244}
245
246static void vb2_vmalloc_dmabuf_ops_detach(struct dma_buf *dbuf,
247 struct dma_buf_attachment *db_attach)
248{
249 struct vb2_vmalloc_attachment *attach = db_attach->priv;
250 struct sg_table *sgt;
251
252 if (!attach)
253 return;
254
255 sgt = &attach->sgt;
256
257 /* release the scatterlist cache */
258 if (attach->dma_dir != DMA_NONE)
259 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
260 attach->dma_dir);
261 sg_free_table(sgt);
262 kfree(attach);
263 db_attach->priv = NULL;
264}
265
266static struct sg_table *vb2_vmalloc_dmabuf_ops_map(
267 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
268{
269 struct vb2_vmalloc_attachment *attach = db_attach->priv;
270 /* stealing dmabuf mutex to serialize map/unmap operations */
271 struct mutex *lock = &db_attach->dmabuf->lock;
272 struct sg_table *sgt;
Hans Verkuil46506352014-11-18 09:51:05 -0300273
274 mutex_lock(lock);
275
276 sgt = &attach->sgt;
277 /* return previously mapped sg table */
278 if (attach->dma_dir == dma_dir) {
279 mutex_unlock(lock);
280 return sgt;
281 }
282
283 /* release any previous cache */
284 if (attach->dma_dir != DMA_NONE) {
285 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
286 attach->dma_dir);
287 attach->dma_dir = DMA_NONE;
288 }
289
290 /* mapping to the client with new direction */
Ricardo Ribaldaba81c6e2015-04-29 09:00:47 -0300291 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
292 dma_dir);
293 if (!sgt->nents) {
Hans Verkuil46506352014-11-18 09:51:05 -0300294 pr_err("failed to map scatterlist\n");
295 mutex_unlock(lock);
296 return ERR_PTR(-EIO);
297 }
298
299 attach->dma_dir = dma_dir;
300
301 mutex_unlock(lock);
302
303 return sgt;
304}
305
306static void vb2_vmalloc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
307 struct sg_table *sgt, enum dma_data_direction dma_dir)
308{
309 /* nothing to be done here */
310}
311
312static void vb2_vmalloc_dmabuf_ops_release(struct dma_buf *dbuf)
313{
314 /* drop reference obtained in vb2_vmalloc_get_dmabuf */
315 vb2_vmalloc_put(dbuf->priv);
316}
317
318static void *vb2_vmalloc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
319{
320 struct vb2_vmalloc_buf *buf = dbuf->priv;
321
322 return buf->vaddr + pgnum * PAGE_SIZE;
323}
324
325static void *vb2_vmalloc_dmabuf_ops_vmap(struct dma_buf *dbuf)
326{
327 struct vb2_vmalloc_buf *buf = dbuf->priv;
328
329 return buf->vaddr;
330}
331
332static int vb2_vmalloc_dmabuf_ops_mmap(struct dma_buf *dbuf,
333 struct vm_area_struct *vma)
334{
335 return vb2_vmalloc_mmap(dbuf->priv, vma);
336}
337
338static struct dma_buf_ops vb2_vmalloc_dmabuf_ops = {
339 .attach = vb2_vmalloc_dmabuf_ops_attach,
340 .detach = vb2_vmalloc_dmabuf_ops_detach,
341 .map_dma_buf = vb2_vmalloc_dmabuf_ops_map,
342 .unmap_dma_buf = vb2_vmalloc_dmabuf_ops_unmap,
343 .kmap = vb2_vmalloc_dmabuf_ops_kmap,
344 .kmap_atomic = vb2_vmalloc_dmabuf_ops_kmap,
345 .vmap = vb2_vmalloc_dmabuf_ops_vmap,
346 .mmap = vb2_vmalloc_dmabuf_ops_mmap,
347 .release = vb2_vmalloc_dmabuf_ops_release,
348};
349
350static struct dma_buf *vb2_vmalloc_get_dmabuf(void *buf_priv, unsigned long flags)
351{
352 struct vb2_vmalloc_buf *buf = buf_priv;
353 struct dma_buf *dbuf;
Sumit Semwald8fbe342015-01-23 12:53:43 +0530354 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
355
356 exp_info.ops = &vb2_vmalloc_dmabuf_ops;
357 exp_info.size = buf->size;
358 exp_info.flags = flags;
359 exp_info.priv = buf;
Hans Verkuil46506352014-11-18 09:51:05 -0300360
361 if (WARN_ON(!buf->vaddr))
362 return NULL;
363
Sumit Semwald8fbe342015-01-23 12:53:43 +0530364 dbuf = dma_buf_export(&exp_info);
Hans Verkuil46506352014-11-18 09:51:05 -0300365 if (IS_ERR(dbuf))
366 return NULL;
367
368 /* dmabuf keeps reference to vb2 buffer */
369 atomic_inc(&buf->refcount);
370
371 return dbuf;
372}
Geert Uytterhoeven99f3cd52014-12-15 10:40:28 -0300373#endif /* CONFIG_HAS_DMA */
374
Hans Verkuil46506352014-11-18 09:51:05 -0300375
376/*********************************************/
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -0300377/* callbacks for DMABUF buffers */
378/*********************************************/
379
380static int vb2_vmalloc_map_dmabuf(void *mem_priv)
381{
382 struct vb2_vmalloc_buf *buf = mem_priv;
383
384 buf->vaddr = dma_buf_vmap(buf->dbuf);
385
386 return buf->vaddr ? 0 : -EFAULT;
387}
388
389static void vb2_vmalloc_unmap_dmabuf(void *mem_priv)
390{
391 struct vb2_vmalloc_buf *buf = mem_priv;
392
393 dma_buf_vunmap(buf->dbuf, buf->vaddr);
394 buf->vaddr = NULL;
395}
396
397static void vb2_vmalloc_detach_dmabuf(void *mem_priv)
398{
399 struct vb2_vmalloc_buf *buf = mem_priv;
400
401 if (buf->vaddr)
402 dma_buf_vunmap(buf->dbuf, buf->vaddr);
403
404 kfree(buf);
405}
406
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300407static void *vb2_vmalloc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
Hans Verkuilcd474032014-11-18 09:50:58 -0300408 unsigned long size, enum dma_data_direction dma_dir)
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -0300409{
410 struct vb2_vmalloc_buf *buf;
411
412 if (dbuf->size < size)
413 return ERR_PTR(-EFAULT);
414
415 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
416 if (!buf)
417 return ERR_PTR(-ENOMEM);
418
419 buf->dbuf = dbuf;
Hans Verkuilcd474032014-11-18 09:50:58 -0300420 buf->dma_dir = dma_dir;
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -0300421 buf->size = size;
422
423 return buf;
424}
425
426
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300427const struct vb2_mem_ops vb2_vmalloc_memops = {
428 .alloc = vb2_vmalloc_alloc,
429 .put = vb2_vmalloc_put,
Andrzej Pietrasiewicz4419b8a2011-10-13 07:30:51 -0300430 .get_userptr = vb2_vmalloc_get_userptr,
431 .put_userptr = vb2_vmalloc_put_userptr,
Geert Uytterhoeven99f3cd52014-12-15 10:40:28 -0300432#ifdef CONFIG_HAS_DMA
Hans Verkuil46506352014-11-18 09:51:05 -0300433 .get_dmabuf = vb2_vmalloc_get_dmabuf,
Geert Uytterhoeven99f3cd52014-12-15 10:40:28 -0300434#endif
Tomasz Stanislawski89d2ee02012-06-14 10:37:46 -0300435 .map_dmabuf = vb2_vmalloc_map_dmabuf,
436 .unmap_dmabuf = vb2_vmalloc_unmap_dmabuf,
437 .attach_dmabuf = vb2_vmalloc_attach_dmabuf,
438 .detach_dmabuf = vb2_vmalloc_detach_dmabuf,
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300439 .vaddr = vb2_vmalloc_vaddr,
440 .mmap = vb2_vmalloc_mmap,
441 .num_users = vb2_vmalloc_num_users,
442};
443EXPORT_SYMBOL_GPL(vb2_vmalloc_memops);
444
445MODULE_DESCRIPTION("vmalloc memory handling routines for videobuf2");
Pawel Osciak95072082011-03-13 15:23:32 -0300446MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
Pawel Osciak3c18ff02010-10-11 10:58:53 -0300447MODULE_LICENSE("GPL");