blob: b1838abb6d0056c38d7fefbce57b3bf2a154328b [file] [log] [blame]
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -03001/*
2 * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 *
6 * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
7 *
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
13#include <linux/module.h>
14#include <linux/mm.h>
15#include <linux/scatterlist.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19
20#include <media/videobuf2-core.h>
21#include <media/videobuf2-memops.h>
22#include <media/videobuf2-dma-sg.h>
23
Hans Verkuilffdc78e2013-03-02 05:12:08 -030024static int debug;
25module_param(debug, int, 0644);
26
27#define dprintk(level, fmt, arg...) \
28 do { \
29 if (debug >= level) \
30 printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
31 } while (0)
32
Hans Verkuil0c3a14c2014-11-18 09:51:01 -030033struct vb2_dma_sg_conf {
34 struct device *dev;
35};
36
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030037struct vb2_dma_sg_buf {
Hans Verkuil0c3a14c2014-11-18 09:51:01 -030038 struct device *dev;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030039 void *vaddr;
40 struct page **pages;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030041 int offset;
Hans Verkuilcd474032014-11-18 09:50:58 -030042 enum dma_data_direction dma_dir;
Ricardo Ribalda22301242013-08-02 10:20:00 -030043 struct sg_table sg_table;
Hans Verkuile078b792014-11-18 09:51:03 -030044 /*
45 * This will point to sg_table when used with the MMAP or USERPTR
46 * memory model, and to the dma_buf sglist when used with the
47 * DMABUF memory model.
48 */
49 struct sg_table *dma_sgt;
Ricardo Ribalda22301242013-08-02 10:20:00 -030050 size_t size;
51 unsigned int num_pages;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030052 atomic_t refcount;
53 struct vb2_vmarea_handler handler;
Ricardo Ribalda50ac9522013-11-26 09:58:44 -030054 struct vm_area_struct *vma;
Hans Verkuile078b792014-11-18 09:51:03 -030055
56 struct dma_buf_attachment *db_attach;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -030057};
58
59static void vb2_dma_sg_put(void *buf_priv);
60
Ricardo Ribaldadf237282013-08-02 10:19:59 -030061static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
62 gfp_t gfp_flags)
63{
64 unsigned int last_page = 0;
Ricardo Ribalda22301242013-08-02 10:20:00 -030065 int size = buf->size;
Ricardo Ribaldadf237282013-08-02 10:19:59 -030066
67 while (size > 0) {
68 struct page *pages;
69 int order;
70 int i;
71
72 order = get_order(size);
73 /* Dont over allocate*/
74 if ((PAGE_SIZE << order) > size)
75 order--;
76
77 pages = NULL;
78 while (!pages) {
79 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO |
80 __GFP_NOWARN | gfp_flags, order);
81 if (pages)
82 break;
83
84 if (order == 0) {
85 while (last_page--)
86 __free_page(buf->pages[last_page]);
87 return -ENOMEM;
88 }
89 order--;
90 }
91
92 split_page(pages, order);
Ricardo Ribalda22301242013-08-02 10:20:00 -030093 for (i = 0; i < (1 << order); i++)
94 buf->pages[last_page++] = &pages[i];
Ricardo Ribaldadf237282013-08-02 10:19:59 -030095
96 size -= PAGE_SIZE << order;
97 }
98
99 return 0;
100}
101
Hans Verkuild935c572014-11-18 09:50:59 -0300102static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size,
103 enum dma_data_direction dma_dir, gfp_t gfp_flags)
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300104{
Hans Verkuil0c3a14c2014-11-18 09:51:01 -0300105 struct vb2_dma_sg_conf *conf = alloc_ctx;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300106 struct vb2_dma_sg_buf *buf;
Hans Verkuild790b7e2014-11-24 08:50:31 -0300107 struct sg_table *sgt;
Ricardo Ribaldadf237282013-08-02 10:19:59 -0300108 int ret;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300109 int num_pages;
Hans Verkuil251a79f2014-11-18 09:51:08 -0300110 DEFINE_DMA_ATTRS(attrs);
111
112 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300113
Hans Verkuil0c3a14c2014-11-18 09:51:01 -0300114 if (WARN_ON(alloc_ctx == NULL))
115 return NULL;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300116 buf = kzalloc(sizeof *buf, GFP_KERNEL);
117 if (!buf)
118 return NULL;
119
120 buf->vaddr = NULL;
Hans Verkuild935c572014-11-18 09:50:59 -0300121 buf->dma_dir = dma_dir;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300122 buf->offset = 0;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300123 buf->size = size;
Mauro Carvalho Chehab7f841452013-04-19 07:18:01 -0300124 /* size is already page aligned */
Ricardo Ribalda22301242013-08-02 10:20:00 -0300125 buf->num_pages = size >> PAGE_SHIFT;
Hans Verkuile078b792014-11-18 09:51:03 -0300126 buf->dma_sgt = &buf->sg_table;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300127
Ricardo Ribalda22301242013-08-02 10:20:00 -0300128 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300129 GFP_KERNEL);
130 if (!buf->pages)
131 goto fail_pages_array_alloc;
132
Ricardo Ribaldadf237282013-08-02 10:19:59 -0300133 ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
134 if (ret)
135 goto fail_pages_alloc;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300136
Hans Verkuile078b792014-11-18 09:51:03 -0300137 ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
Hans Verkuil47bc59c2014-08-01 09:18:50 -0300138 buf->num_pages, 0, size, GFP_KERNEL);
Ricardo Ribalda22301242013-08-02 10:20:00 -0300139 if (ret)
140 goto fail_table_alloc;
141
Hans Verkuil0c3a14c2014-11-18 09:51:01 -0300142 /* Prevent the device from being released while the buffer is used */
143 buf->dev = get_device(conf->dev);
Hans Verkuild790b7e2014-11-24 08:50:31 -0300144
145 sgt = &buf->sg_table;
Hans Verkuil251a79f2014-11-18 09:51:08 -0300146 /*
147 * No need to sync to the device, this will happen later when the
148 * prepare() memop is called.
149 */
150 if (dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
151 buf->dma_dir, &attrs) == 0)
Hans Verkuild790b7e2014-11-24 08:50:31 -0300152 goto fail_map;
Hans Verkuild790b7e2014-11-24 08:50:31 -0300153
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300154 buf->handler.refcount = &buf->refcount;
155 buf->handler.put = vb2_dma_sg_put;
156 buf->handler.arg = buf;
157
158 atomic_inc(&buf->refcount);
159
Hans Verkuilffdc78e2013-03-02 05:12:08 -0300160 dprintk(1, "%s: Allocated buffer of %d pages\n",
Ricardo Ribalda22301242013-08-02 10:20:00 -0300161 __func__, buf->num_pages);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300162 return buf;
163
Hans Verkuild790b7e2014-11-24 08:50:31 -0300164fail_map:
165 put_device(buf->dev);
Hans Verkuile078b792014-11-18 09:51:03 -0300166 sg_free_table(buf->dma_sgt);
Ricardo Ribalda22301242013-08-02 10:20:00 -0300167fail_table_alloc:
168 num_pages = buf->num_pages;
169 while (num_pages--)
170 __free_page(buf->pages[num_pages]);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300171fail_pages_alloc:
Andrzej Pietrasiewicza9bb36a2011-01-28 09:42:51 -0300172 kfree(buf->pages);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300173fail_pages_array_alloc:
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300174 kfree(buf);
175 return NULL;
176}
177
178static void vb2_dma_sg_put(void *buf_priv)
179{
180 struct vb2_dma_sg_buf *buf = buf_priv;
Hans Verkuild790b7e2014-11-24 08:50:31 -0300181 struct sg_table *sgt = &buf->sg_table;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300182 int i = buf->num_pages;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300183
184 if (atomic_dec_and_test(&buf->refcount)) {
Hans Verkuil251a79f2014-11-18 09:51:08 -0300185 DEFINE_DMA_ATTRS(attrs);
186
187 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
Hans Verkuilffdc78e2013-03-02 05:12:08 -0300188 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
Ricardo Ribalda22301242013-08-02 10:20:00 -0300189 buf->num_pages);
Hans Verkuil251a79f2014-11-18 09:51:08 -0300190 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
191 buf->dma_dir, &attrs);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300192 if (buf->vaddr)
Ricardo Ribalda22301242013-08-02 10:20:00 -0300193 vm_unmap_ram(buf->vaddr, buf->num_pages);
Hans Verkuile078b792014-11-18 09:51:03 -0300194 sg_free_table(buf->dma_sgt);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300195 while (--i >= 0)
196 __free_page(buf->pages[i]);
197 kfree(buf->pages);
Hans Verkuil0c3a14c2014-11-18 09:51:01 -0300198 put_device(buf->dev);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300199 kfree(buf);
200 }
201}
202
Hans Verkuild790b7e2014-11-24 08:50:31 -0300203static void vb2_dma_sg_prepare(void *buf_priv)
204{
205 struct vb2_dma_sg_buf *buf = buf_priv;
Hans Verkuile078b792014-11-18 09:51:03 -0300206 struct sg_table *sgt = buf->dma_sgt;
207
208 /* DMABUF exporter will flush the cache for us */
209 if (buf->db_attach)
210 return;
Hans Verkuild790b7e2014-11-24 08:50:31 -0300211
212 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
213}
214
215static void vb2_dma_sg_finish(void *buf_priv)
216{
217 struct vb2_dma_sg_buf *buf = buf_priv;
Hans Verkuile078b792014-11-18 09:51:03 -0300218 struct sg_table *sgt = buf->dma_sgt;
219
220 /* DMABUF exporter will flush the cache for us */
221 if (buf->db_attach)
222 return;
Hans Verkuild790b7e2014-11-24 08:50:31 -0300223
224 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
225}
226
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300227static inline int vma_is_io(struct vm_area_struct *vma)
228{
229 return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
230}
231
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300232static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
Hans Verkuilcd474032014-11-18 09:50:58 -0300233 unsigned long size,
234 enum dma_data_direction dma_dir)
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300235{
Hans Verkuild790b7e2014-11-24 08:50:31 -0300236 struct vb2_dma_sg_conf *conf = alloc_ctx;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300237 struct vb2_dma_sg_buf *buf;
238 unsigned long first, last;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300239 int num_pages_from_user;
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300240 struct vm_area_struct *vma;
Hans Verkuild790b7e2014-11-24 08:50:31 -0300241 struct sg_table *sgt;
Hans Verkuil251a79f2014-11-18 09:51:08 -0300242 DEFINE_DMA_ATTRS(attrs);
243
244 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300245
246 buf = kzalloc(sizeof *buf, GFP_KERNEL);
247 if (!buf)
248 return NULL;
249
250 buf->vaddr = NULL;
Hans Verkuild790b7e2014-11-24 08:50:31 -0300251 buf->dev = conf->dev;
Hans Verkuilcd474032014-11-18 09:50:58 -0300252 buf->dma_dir = dma_dir;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300253 buf->offset = vaddr & ~PAGE_MASK;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300254 buf->size = size;
Hans Verkuile078b792014-11-18 09:51:03 -0300255 buf->dma_sgt = &buf->sg_table;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300256
257 first = (vaddr & PAGE_MASK) >> PAGE_SHIFT;
258 last = ((vaddr + size - 1) & PAGE_MASK) >> PAGE_SHIFT;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300259 buf->num_pages = last - first + 1;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300260
Ricardo Ribalda22301242013-08-02 10:20:00 -0300261 buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300262 GFP_KERNEL);
263 if (!buf->pages)
Geyslan G. Bem64c832a2013-11-20 18:02:52 -0300264 goto userptr_fail_alloc_pages;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300265
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300266 vma = find_vma(current->mm, vaddr);
267 if (!vma) {
268 dprintk(1, "no vma for address %lu\n", vaddr);
269 goto userptr_fail_find_vma;
270 }
271
272 if (vma->vm_end < vaddr + size) {
273 dprintk(1, "vma at %lu is too small for %lu bytes\n",
274 vaddr, size);
275 goto userptr_fail_find_vma;
276 }
277
278 buf->vma = vb2_get_vma(vma);
279 if (!buf->vma) {
280 dprintk(1, "failed to copy vma\n");
281 goto userptr_fail_find_vma;
282 }
283
284 if (vma_is_io(buf->vma)) {
285 for (num_pages_from_user = 0;
286 num_pages_from_user < buf->num_pages;
287 ++num_pages_from_user, vaddr += PAGE_SIZE) {
288 unsigned long pfn;
289
Ricardo Ribalda227ae222014-04-25 13:11:29 -0300290 if (follow_pfn(vma, vaddr, &pfn)) {
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300291 dprintk(1, "no page for address %lu\n", vaddr);
292 break;
293 }
294 buf->pages[num_pages_from_user] = pfn_to_page(pfn);
295 }
296 } else
297 num_pages_from_user = get_user_pages(current, current->mm,
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300298 vaddr & PAGE_MASK,
Ricardo Ribalda22301242013-08-02 10:20:00 -0300299 buf->num_pages,
Hans Verkuilcd474032014-11-18 09:50:58 -0300300 buf->dma_dir == DMA_FROM_DEVICE,
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300301 1, /* force */
302 buf->pages,
303 NULL);
Marek Szyprowskib037c0f2011-11-17 05:32:17 -0300304
Ricardo Ribalda22301242013-08-02 10:20:00 -0300305 if (num_pages_from_user != buf->num_pages)
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300306 goto userptr_fail_get_user_pages;
307
Hans Verkuile078b792014-11-18 09:51:03 -0300308 if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
Ricardo Ribalda22301242013-08-02 10:20:00 -0300309 buf->num_pages, buf->offset, size, 0))
310 goto userptr_fail_alloc_table_from_pages;
311
Hans Verkuild790b7e2014-11-24 08:50:31 -0300312 sgt = &buf->sg_table;
Hans Verkuil251a79f2014-11-18 09:51:08 -0300313 /*
314 * No need to sync to the device, this will happen later when the
315 * prepare() memop is called.
316 */
317 if (dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
318 buf->dma_dir, &attrs) == 0)
Hans Verkuild790b7e2014-11-24 08:50:31 -0300319 goto userptr_fail_map;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300320 return buf;
321
Hans Verkuild790b7e2014-11-24 08:50:31 -0300322userptr_fail_map:
323 sg_free_table(&buf->sg_table);
Ricardo Ribalda22301242013-08-02 10:20:00 -0300324userptr_fail_alloc_table_from_pages:
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300325userptr_fail_get_user_pages:
Hans Verkuilffdc78e2013-03-02 05:12:08 -0300326 dprintk(1, "get_user_pages requested/got: %d/%d]\n",
Ricardo Ribalda202dfbd2013-11-06 15:48:38 -0300327 buf->num_pages, num_pages_from_user);
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300328 if (!vma_is_io(buf->vma))
329 while (--num_pages_from_user >= 0)
330 put_page(buf->pages[num_pages_from_user]);
331 vb2_put_vma(buf->vma);
332userptr_fail_find_vma:
Andrzej Pietrasiewicza9bb36a2011-01-28 09:42:51 -0300333 kfree(buf->pages);
Geyslan G. Bem64c832a2013-11-20 18:02:52 -0300334userptr_fail_alloc_pages:
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300335 kfree(buf);
336 return NULL;
337}
338
339/*
340 * @put_userptr: inform the allocator that a USERPTR buffer will no longer
341 * be used
342 */
343static void vb2_dma_sg_put_userptr(void *buf_priv)
344{
345 struct vb2_dma_sg_buf *buf = buf_priv;
Hans Verkuild790b7e2014-11-24 08:50:31 -0300346 struct sg_table *sgt = &buf->sg_table;
Ricardo Ribalda22301242013-08-02 10:20:00 -0300347 int i = buf->num_pages;
Hans Verkuil251a79f2014-11-18 09:51:08 -0300348 DEFINE_DMA_ATTRS(attrs);
349
350 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300351
Hans Verkuilffdc78e2013-03-02 05:12:08 -0300352 dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
Ricardo Ribalda22301242013-08-02 10:20:00 -0300353 __func__, buf->num_pages);
Hans Verkuil251a79f2014-11-18 09:51:08 -0300354 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir, &attrs);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300355 if (buf->vaddr)
Ricardo Ribalda22301242013-08-02 10:20:00 -0300356 vm_unmap_ram(buf->vaddr, buf->num_pages);
Hans Verkuile078b792014-11-18 09:51:03 -0300357 sg_free_table(buf->dma_sgt);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300358 while (--i >= 0) {
Hans Verkuilcd474032014-11-18 09:50:58 -0300359 if (buf->dma_dir == DMA_FROM_DEVICE)
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300360 set_page_dirty_lock(buf->pages[i]);
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300361 if (!vma_is_io(buf->vma))
362 put_page(buf->pages[i]);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300363 }
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300364 kfree(buf->pages);
Ricardo Ribalda50ac9522013-11-26 09:58:44 -0300365 vb2_put_vma(buf->vma);
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300366 kfree(buf);
367}
368
369static void *vb2_dma_sg_vaddr(void *buf_priv)
370{
371 struct vb2_dma_sg_buf *buf = buf_priv;
372
373 BUG_ON(!buf);
374
Hans Verkuile078b792014-11-18 09:51:03 -0300375 if (!buf->vaddr) {
376 if (buf->db_attach)
377 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
378 else
379 buf->vaddr = vm_map_ram(buf->pages,
380 buf->num_pages, -1, PAGE_KERNEL);
381 }
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300382
383 /* add offset in case userptr is not page-aligned */
Hans Verkuile078b792014-11-18 09:51:03 -0300384 return buf->vaddr ? buf->vaddr + buf->offset : NULL;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300385}
386
387static unsigned int vb2_dma_sg_num_users(void *buf_priv)
388{
389 struct vb2_dma_sg_buf *buf = buf_priv;
390
391 return atomic_read(&buf->refcount);
392}
393
394static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
395{
396 struct vb2_dma_sg_buf *buf = buf_priv;
397 unsigned long uaddr = vma->vm_start;
398 unsigned long usize = vma->vm_end - vma->vm_start;
399 int i = 0;
400
401 if (!buf) {
402 printk(KERN_ERR "No memory to map\n");
403 return -EINVAL;
404 }
405
406 do {
407 int ret;
408
409 ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
410 if (ret) {
411 printk(KERN_ERR "Remapping memory, error: %d\n", ret);
412 return ret;
413 }
414
415 uaddr += PAGE_SIZE;
416 usize -= PAGE_SIZE;
417 } while (usize > 0);
418
419
420 /*
421 * Use common vm_area operations to track buffer refcount.
422 */
423 vma->vm_private_data = &buf->handler;
424 vma->vm_ops = &vb2_common_vm_ops;
425
426 vma->vm_ops->open(vma);
427
428 return 0;
429}
430
Hans Verkuile078b792014-11-18 09:51:03 -0300431/*********************************************/
Hans Verkuil041c7b62014-11-18 09:51:04 -0300432/* DMABUF ops for exporters */
433/*********************************************/
434
435struct vb2_dma_sg_attachment {
436 struct sg_table sgt;
437 enum dma_data_direction dma_dir;
438};
439
440static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
441 struct dma_buf_attachment *dbuf_attach)
442{
443 struct vb2_dma_sg_attachment *attach;
444 unsigned int i;
445 struct scatterlist *rd, *wr;
446 struct sg_table *sgt;
447 struct vb2_dma_sg_buf *buf = dbuf->priv;
448 int ret;
449
450 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
451 if (!attach)
452 return -ENOMEM;
453
454 sgt = &attach->sgt;
455 /* Copy the buf->base_sgt scatter list to the attachment, as we can't
456 * map the same scatter list to multiple attachments at the same time.
457 */
458 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
459 if (ret) {
460 kfree(attach);
461 return -ENOMEM;
462 }
463
464 rd = buf->dma_sgt->sgl;
465 wr = sgt->sgl;
466 for (i = 0; i < sgt->orig_nents; ++i) {
467 sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
468 rd = sg_next(rd);
469 wr = sg_next(wr);
470 }
471
472 attach->dma_dir = DMA_NONE;
473 dbuf_attach->priv = attach;
474
475 return 0;
476}
477
478static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
479 struct dma_buf_attachment *db_attach)
480{
481 struct vb2_dma_sg_attachment *attach = db_attach->priv;
482 struct sg_table *sgt;
483
484 if (!attach)
485 return;
486
487 sgt = &attach->sgt;
488
489 /* release the scatterlist cache */
490 if (attach->dma_dir != DMA_NONE)
491 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
492 attach->dma_dir);
493 sg_free_table(sgt);
494 kfree(attach);
495 db_attach->priv = NULL;
496}
497
498static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
499 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
500{
501 struct vb2_dma_sg_attachment *attach = db_attach->priv;
502 /* stealing dmabuf mutex to serialize map/unmap operations */
503 struct mutex *lock = &db_attach->dmabuf->lock;
504 struct sg_table *sgt;
505 int ret;
506
507 mutex_lock(lock);
508
509 sgt = &attach->sgt;
510 /* return previously mapped sg table */
511 if (attach->dma_dir == dma_dir) {
512 mutex_unlock(lock);
513 return sgt;
514 }
515
516 /* release any previous cache */
517 if (attach->dma_dir != DMA_NONE) {
518 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
519 attach->dma_dir);
520 attach->dma_dir = DMA_NONE;
521 }
522
523 /* mapping to the client with new direction */
524 ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dma_dir);
525 if (ret <= 0) {
526 pr_err("failed to map scatterlist\n");
527 mutex_unlock(lock);
528 return ERR_PTR(-EIO);
529 }
530
531 attach->dma_dir = dma_dir;
532
533 mutex_unlock(lock);
534
535 return sgt;
536}
537
538static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
539 struct sg_table *sgt, enum dma_data_direction dma_dir)
540{
541 /* nothing to be done here */
542}
543
544static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
545{
546 /* drop reference obtained in vb2_dma_sg_get_dmabuf */
547 vb2_dma_sg_put(dbuf->priv);
548}
549
550static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
551{
552 struct vb2_dma_sg_buf *buf = dbuf->priv;
553
554 return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
555}
556
557static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
558{
559 struct vb2_dma_sg_buf *buf = dbuf->priv;
560
561 return vb2_dma_sg_vaddr(buf);
562}
563
564static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
565 struct vm_area_struct *vma)
566{
567 return vb2_dma_sg_mmap(dbuf->priv, vma);
568}
569
570static struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
571 .attach = vb2_dma_sg_dmabuf_ops_attach,
572 .detach = vb2_dma_sg_dmabuf_ops_detach,
573 .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
574 .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
575 .kmap = vb2_dma_sg_dmabuf_ops_kmap,
576 .kmap_atomic = vb2_dma_sg_dmabuf_ops_kmap,
577 .vmap = vb2_dma_sg_dmabuf_ops_vmap,
578 .mmap = vb2_dma_sg_dmabuf_ops_mmap,
579 .release = vb2_dma_sg_dmabuf_ops_release,
580};
581
582static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
583{
584 struct vb2_dma_sg_buf *buf = buf_priv;
585 struct dma_buf *dbuf;
586
587 if (WARN_ON(!buf->dma_sgt))
588 return NULL;
589
590 dbuf = dma_buf_export(buf, &vb2_dma_sg_dmabuf_ops, buf->size, flags, NULL);
591 if (IS_ERR(dbuf))
592 return NULL;
593
594 /* dmabuf keeps reference to vb2 buffer */
595 atomic_inc(&buf->refcount);
596
597 return dbuf;
598}
599
600/*********************************************/
Hans Verkuile078b792014-11-18 09:51:03 -0300601/* callbacks for DMABUF buffers */
602/*********************************************/
603
604static int vb2_dma_sg_map_dmabuf(void *mem_priv)
605{
606 struct vb2_dma_sg_buf *buf = mem_priv;
607 struct sg_table *sgt;
608
609 if (WARN_ON(!buf->db_attach)) {
610 pr_err("trying to pin a non attached buffer\n");
611 return -EINVAL;
612 }
613
614 if (WARN_ON(buf->dma_sgt)) {
615 pr_err("dmabuf buffer is already pinned\n");
616 return 0;
617 }
618
619 /* get the associated scatterlist for this buffer */
620 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
621 if (IS_ERR(sgt)) {
622 pr_err("Error getting dmabuf scatterlist\n");
623 return -EINVAL;
624 }
625
626 buf->dma_sgt = sgt;
627 buf->vaddr = NULL;
628
629 return 0;
630}
631
632static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
633{
634 struct vb2_dma_sg_buf *buf = mem_priv;
635 struct sg_table *sgt = buf->dma_sgt;
636
637 if (WARN_ON(!buf->db_attach)) {
638 pr_err("trying to unpin a not attached buffer\n");
639 return;
640 }
641
642 if (WARN_ON(!sgt)) {
643 pr_err("dmabuf buffer is already unpinned\n");
644 return;
645 }
646
647 if (buf->vaddr) {
648 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
649 buf->vaddr = NULL;
650 }
651 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
652
653 buf->dma_sgt = NULL;
654}
655
656static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
657{
658 struct vb2_dma_sg_buf *buf = mem_priv;
659
660 /* if vb2 works correctly you should never detach mapped buffer */
661 if (WARN_ON(buf->dma_sgt))
662 vb2_dma_sg_unmap_dmabuf(buf);
663
664 /* detach this attachment */
665 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
666 kfree(buf);
667}
668
669static void *vb2_dma_sg_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
670 unsigned long size, enum dma_data_direction dma_dir)
671{
672 struct vb2_dma_sg_conf *conf = alloc_ctx;
673 struct vb2_dma_sg_buf *buf;
674 struct dma_buf_attachment *dba;
675
676 if (dbuf->size < size)
677 return ERR_PTR(-EFAULT);
678
679 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
680 if (!buf)
681 return ERR_PTR(-ENOMEM);
682
683 buf->dev = conf->dev;
684 /* create attachment for the dmabuf with the user device */
685 dba = dma_buf_attach(dbuf, buf->dev);
686 if (IS_ERR(dba)) {
687 pr_err("failed to attach dmabuf\n");
688 kfree(buf);
689 return dba;
690 }
691
692 buf->dma_dir = dma_dir;
693 buf->size = size;
694 buf->db_attach = dba;
695
696 return buf;
697}
698
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300699static void *vb2_dma_sg_cookie(void *buf_priv)
700{
701 struct vb2_dma_sg_buf *buf = buf_priv;
702
Hans Verkuile078b792014-11-18 09:51:03 -0300703 return buf->dma_sgt;
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300704}
705
706const struct vb2_mem_ops vb2_dma_sg_memops = {
707 .alloc = vb2_dma_sg_alloc,
708 .put = vb2_dma_sg_put,
709 .get_userptr = vb2_dma_sg_get_userptr,
710 .put_userptr = vb2_dma_sg_put_userptr,
Hans Verkuild790b7e2014-11-24 08:50:31 -0300711 .prepare = vb2_dma_sg_prepare,
712 .finish = vb2_dma_sg_finish,
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300713 .vaddr = vb2_dma_sg_vaddr,
714 .mmap = vb2_dma_sg_mmap,
715 .num_users = vb2_dma_sg_num_users,
Hans Verkuil041c7b62014-11-18 09:51:04 -0300716 .get_dmabuf = vb2_dma_sg_get_dmabuf,
Hans Verkuile078b792014-11-18 09:51:03 -0300717 .map_dmabuf = vb2_dma_sg_map_dmabuf,
718 .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
719 .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
720 .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300721 .cookie = vb2_dma_sg_cookie,
722};
723EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
724
Hans Verkuil0c3a14c2014-11-18 09:51:01 -0300725void *vb2_dma_sg_init_ctx(struct device *dev)
726{
727 struct vb2_dma_sg_conf *conf;
728
729 conf = kzalloc(sizeof(*conf), GFP_KERNEL);
730 if (!conf)
731 return ERR_PTR(-ENOMEM);
732
733 conf->dev = dev;
734
735 return conf;
736}
737EXPORT_SYMBOL_GPL(vb2_dma_sg_init_ctx);
738
739void vb2_dma_sg_cleanup_ctx(void *alloc_ctx)
740{
741 if (!IS_ERR_OR_NULL(alloc_ctx))
742 kfree(alloc_ctx);
743}
744EXPORT_SYMBOL_GPL(vb2_dma_sg_cleanup_ctx);
745
Andrzej Pietrasiewicz5ba3f752010-11-29 11:53:34 -0300746MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
747MODULE_AUTHOR("Andrzej Pietrasiewicz");
748MODULE_LICENSE("GPL");