xref: /linux/drivers/media/common/videobuf2/videobuf2-dma-sg.c (revision 37744feebc086908fd89760650f458ab19071750)
1 /*
2  * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
3  *
4  * Copyright (C) 2010 Samsung Electronics
5  *
6  * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.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/refcount.h>
16 #include <linux/scatterlist.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 
21 #include <media/videobuf2-v4l2.h>
22 #include <media/videobuf2-memops.h>
23 #include <media/videobuf2-dma-sg.h>
24 
25 static int debug;
26 module_param(debug, int, 0644);
27 
28 #define dprintk(level, fmt, arg...)					\
29 	do {								\
30 		if (debug >= level)					\
31 			printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg);	\
32 	} while (0)
33 
34 struct vb2_dma_sg_buf {
35 	struct device			*dev;
36 	void				*vaddr;
37 	struct page			**pages;
38 	struct frame_vector		*vec;
39 	int				offset;
40 	enum dma_data_direction		dma_dir;
41 	struct sg_table			sg_table;
42 	/*
43 	 * This will point to sg_table when used with the MMAP or USERPTR
44 	 * memory model, and to the dma_buf sglist when used with the
45 	 * DMABUF memory model.
46 	 */
47 	struct sg_table			*dma_sgt;
48 	size_t				size;
49 	unsigned int			num_pages;
50 	refcount_t			refcount;
51 	struct vb2_vmarea_handler	handler;
52 
53 	struct dma_buf_attachment	*db_attach;
54 };
55 
56 static void vb2_dma_sg_put(void *buf_priv);
57 
58 static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
59 		gfp_t gfp_flags)
60 {
61 	unsigned int last_page = 0;
62 	unsigned long size = buf->size;
63 
64 	while (size > 0) {
65 		struct page *pages;
66 		int order;
67 		int i;
68 
69 		order = get_order(size);
70 		/* Don't over allocate*/
71 		if ((PAGE_SIZE << order) > size)
72 			order--;
73 
74 		pages = NULL;
75 		while (!pages) {
76 			pages = alloc_pages(GFP_KERNEL | __GFP_ZERO |
77 					__GFP_NOWARN | gfp_flags, order);
78 			if (pages)
79 				break;
80 
81 			if (order == 0) {
82 				while (last_page--)
83 					__free_page(buf->pages[last_page]);
84 				return -ENOMEM;
85 			}
86 			order--;
87 		}
88 
89 		split_page(pages, order);
90 		for (i = 0; i < (1 << order); i++)
91 			buf->pages[last_page++] = &pages[i];
92 
93 		size -= PAGE_SIZE << order;
94 	}
95 
96 	return 0;
97 }
98 
99 static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs,
100 			      unsigned long size, enum dma_data_direction dma_dir,
101 			      gfp_t gfp_flags)
102 {
103 	struct vb2_dma_sg_buf *buf;
104 	struct sg_table *sgt;
105 	int ret;
106 	int num_pages;
107 
108 	if (WARN_ON(!dev))
109 		return ERR_PTR(-EINVAL);
110 
111 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
112 	if (!buf)
113 		return ERR_PTR(-ENOMEM);
114 
115 	buf->vaddr = NULL;
116 	buf->dma_dir = dma_dir;
117 	buf->offset = 0;
118 	buf->size = size;
119 	/* size is already page aligned */
120 	buf->num_pages = size >> PAGE_SHIFT;
121 	buf->dma_sgt = &buf->sg_table;
122 
123 	buf->pages = kvmalloc_array(buf->num_pages, sizeof(struct page *),
124 				    GFP_KERNEL | __GFP_ZERO);
125 	if (!buf->pages)
126 		goto fail_pages_array_alloc;
127 
128 	ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags);
129 	if (ret)
130 		goto fail_pages_alloc;
131 
132 	ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
133 			buf->num_pages, 0, size, GFP_KERNEL);
134 	if (ret)
135 		goto fail_table_alloc;
136 
137 	/* Prevent the device from being released while the buffer is used */
138 	buf->dev = get_device(dev);
139 
140 	sgt = &buf->sg_table;
141 	/*
142 	 * No need to sync to the device, this will happen later when the
143 	 * prepare() memop is called.
144 	 */
145 	sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
146 				      buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
147 	if (!sgt->nents)
148 		goto fail_map;
149 
150 	buf->handler.refcount = &buf->refcount;
151 	buf->handler.put = vb2_dma_sg_put;
152 	buf->handler.arg = buf;
153 
154 	refcount_set(&buf->refcount, 1);
155 
156 	dprintk(1, "%s: Allocated buffer of %d pages\n",
157 		__func__, buf->num_pages);
158 	return buf;
159 
160 fail_map:
161 	put_device(buf->dev);
162 	sg_free_table(buf->dma_sgt);
163 fail_table_alloc:
164 	num_pages = buf->num_pages;
165 	while (num_pages--)
166 		__free_page(buf->pages[num_pages]);
167 fail_pages_alloc:
168 	kvfree(buf->pages);
169 fail_pages_array_alloc:
170 	kfree(buf);
171 	return ERR_PTR(-ENOMEM);
172 }
173 
174 static void vb2_dma_sg_put(void *buf_priv)
175 {
176 	struct vb2_dma_sg_buf *buf = buf_priv;
177 	struct sg_table *sgt = &buf->sg_table;
178 	int i = buf->num_pages;
179 
180 	if (refcount_dec_and_test(&buf->refcount)) {
181 		dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
182 			buf->num_pages);
183 		dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
184 				   buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
185 		if (buf->vaddr)
186 			vm_unmap_ram(buf->vaddr, buf->num_pages);
187 		sg_free_table(buf->dma_sgt);
188 		while (--i >= 0)
189 			__free_page(buf->pages[i]);
190 		kvfree(buf->pages);
191 		put_device(buf->dev);
192 		kfree(buf);
193 	}
194 }
195 
196 static void vb2_dma_sg_prepare(void *buf_priv)
197 {
198 	struct vb2_dma_sg_buf *buf = buf_priv;
199 	struct sg_table *sgt = buf->dma_sgt;
200 
201 	/* DMABUF exporter will flush the cache for us */
202 	if (buf->db_attach)
203 		return;
204 
205 	dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
206 			       buf->dma_dir);
207 }
208 
209 static void vb2_dma_sg_finish(void *buf_priv)
210 {
211 	struct vb2_dma_sg_buf *buf = buf_priv;
212 	struct sg_table *sgt = buf->dma_sgt;
213 
214 	/* DMABUF exporter will flush the cache for us */
215 	if (buf->db_attach)
216 		return;
217 
218 	dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
219 }
220 
221 static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
222 				    unsigned long size,
223 				    enum dma_data_direction dma_dir)
224 {
225 	struct vb2_dma_sg_buf *buf;
226 	struct sg_table *sgt;
227 	struct frame_vector *vec;
228 
229 	if (WARN_ON(!dev))
230 		return ERR_PTR(-EINVAL);
231 
232 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
233 	if (!buf)
234 		return ERR_PTR(-ENOMEM);
235 
236 	buf->vaddr = NULL;
237 	buf->dev = dev;
238 	buf->dma_dir = dma_dir;
239 	buf->offset = vaddr & ~PAGE_MASK;
240 	buf->size = size;
241 	buf->dma_sgt = &buf->sg_table;
242 	vec = vb2_create_framevec(vaddr, size);
243 	if (IS_ERR(vec))
244 		goto userptr_fail_pfnvec;
245 	buf->vec = vec;
246 
247 	buf->pages = frame_vector_pages(vec);
248 	if (IS_ERR(buf->pages))
249 		goto userptr_fail_sgtable;
250 	buf->num_pages = frame_vector_count(vec);
251 
252 	if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
253 			buf->num_pages, buf->offset, size, 0))
254 		goto userptr_fail_sgtable;
255 
256 	sgt = &buf->sg_table;
257 	/*
258 	 * No need to sync to the device, this will happen later when the
259 	 * prepare() memop is called.
260 	 */
261 	sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
262 				      buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
263 	if (!sgt->nents)
264 		goto userptr_fail_map;
265 
266 	return buf;
267 
268 userptr_fail_map:
269 	sg_free_table(&buf->sg_table);
270 userptr_fail_sgtable:
271 	vb2_destroy_framevec(vec);
272 userptr_fail_pfnvec:
273 	kfree(buf);
274 	return ERR_PTR(-ENOMEM);
275 }
276 
277 /*
278  * @put_userptr: inform the allocator that a USERPTR buffer will no longer
279  *		 be used
280  */
281 static void vb2_dma_sg_put_userptr(void *buf_priv)
282 {
283 	struct vb2_dma_sg_buf *buf = buf_priv;
284 	struct sg_table *sgt = &buf->sg_table;
285 	int i = buf->num_pages;
286 
287 	dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
288 	       __func__, buf->num_pages);
289 	dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir,
290 			   DMA_ATTR_SKIP_CPU_SYNC);
291 	if (buf->vaddr)
292 		vm_unmap_ram(buf->vaddr, buf->num_pages);
293 	sg_free_table(buf->dma_sgt);
294 	if (buf->dma_dir == DMA_FROM_DEVICE ||
295 	    buf->dma_dir == DMA_BIDIRECTIONAL)
296 		while (--i >= 0)
297 			set_page_dirty_lock(buf->pages[i]);
298 	vb2_destroy_framevec(buf->vec);
299 	kfree(buf);
300 }
301 
302 static void *vb2_dma_sg_vaddr(void *buf_priv)
303 {
304 	struct vb2_dma_sg_buf *buf = buf_priv;
305 
306 	BUG_ON(!buf);
307 
308 	if (!buf->vaddr) {
309 		if (buf->db_attach)
310 			buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
311 		else
312 			buf->vaddr = vm_map_ram(buf->pages,
313 					buf->num_pages, -1, PAGE_KERNEL);
314 	}
315 
316 	/* add offset in case userptr is not page-aligned */
317 	return buf->vaddr ? buf->vaddr + buf->offset : NULL;
318 }
319 
320 static unsigned int vb2_dma_sg_num_users(void *buf_priv)
321 {
322 	struct vb2_dma_sg_buf *buf = buf_priv;
323 
324 	return refcount_read(&buf->refcount);
325 }
326 
327 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
328 {
329 	struct vb2_dma_sg_buf *buf = buf_priv;
330 	int err;
331 
332 	if (!buf) {
333 		printk(KERN_ERR "No memory to map\n");
334 		return -EINVAL;
335 	}
336 
337 	err = vm_map_pages(vma, buf->pages, buf->num_pages);
338 	if (err) {
339 		printk(KERN_ERR "Remapping memory, error: %d\n", err);
340 		return err;
341 	}
342 
343 	/*
344 	 * Use common vm_area operations to track buffer refcount.
345 	 */
346 	vma->vm_private_data	= &buf->handler;
347 	vma->vm_ops		= &vb2_common_vm_ops;
348 
349 	vma->vm_ops->open(vma);
350 
351 	return 0;
352 }
353 
354 /*********************************************/
355 /*         DMABUF ops for exporters          */
356 /*********************************************/
357 
358 struct vb2_dma_sg_attachment {
359 	struct sg_table sgt;
360 	enum dma_data_direction dma_dir;
361 };
362 
363 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf,
364 	struct dma_buf_attachment *dbuf_attach)
365 {
366 	struct vb2_dma_sg_attachment *attach;
367 	unsigned int i;
368 	struct scatterlist *rd, *wr;
369 	struct sg_table *sgt;
370 	struct vb2_dma_sg_buf *buf = dbuf->priv;
371 	int ret;
372 
373 	attach = kzalloc(sizeof(*attach), GFP_KERNEL);
374 	if (!attach)
375 		return -ENOMEM;
376 
377 	sgt = &attach->sgt;
378 	/* Copy the buf->base_sgt scatter list to the attachment, as we can't
379 	 * map the same scatter list to multiple attachments at the same time.
380 	 */
381 	ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
382 	if (ret) {
383 		kfree(attach);
384 		return -ENOMEM;
385 	}
386 
387 	rd = buf->dma_sgt->sgl;
388 	wr = sgt->sgl;
389 	for (i = 0; i < sgt->orig_nents; ++i) {
390 		sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
391 		rd = sg_next(rd);
392 		wr = sg_next(wr);
393 	}
394 
395 	attach->dma_dir = DMA_NONE;
396 	dbuf_attach->priv = attach;
397 
398 	return 0;
399 }
400 
401 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
402 	struct dma_buf_attachment *db_attach)
403 {
404 	struct vb2_dma_sg_attachment *attach = db_attach->priv;
405 	struct sg_table *sgt;
406 
407 	if (!attach)
408 		return;
409 
410 	sgt = &attach->sgt;
411 
412 	/* release the scatterlist cache */
413 	if (attach->dma_dir != DMA_NONE)
414 		dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
415 			attach->dma_dir);
416 	sg_free_table(sgt);
417 	kfree(attach);
418 	db_attach->priv = NULL;
419 }
420 
421 static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
422 	struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
423 {
424 	struct vb2_dma_sg_attachment *attach = db_attach->priv;
425 	/* stealing dmabuf mutex to serialize map/unmap operations */
426 	struct mutex *lock = &db_attach->dmabuf->lock;
427 	struct sg_table *sgt;
428 
429 	mutex_lock(lock);
430 
431 	sgt = &attach->sgt;
432 	/* return previously mapped sg table */
433 	if (attach->dma_dir == dma_dir) {
434 		mutex_unlock(lock);
435 		return sgt;
436 	}
437 
438 	/* release any previous cache */
439 	if (attach->dma_dir != DMA_NONE) {
440 		dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
441 			attach->dma_dir);
442 		attach->dma_dir = DMA_NONE;
443 	}
444 
445 	/* mapping to the client with new direction */
446 	sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
447 				dma_dir);
448 	if (!sgt->nents) {
449 		pr_err("failed to map scatterlist\n");
450 		mutex_unlock(lock);
451 		return ERR_PTR(-EIO);
452 	}
453 
454 	attach->dma_dir = dma_dir;
455 
456 	mutex_unlock(lock);
457 
458 	return sgt;
459 }
460 
461 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
462 	struct sg_table *sgt, enum dma_data_direction dma_dir)
463 {
464 	/* nothing to be done here */
465 }
466 
467 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
468 {
469 	/* drop reference obtained in vb2_dma_sg_get_dmabuf */
470 	vb2_dma_sg_put(dbuf->priv);
471 }
472 
473 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
474 {
475 	struct vb2_dma_sg_buf *buf = dbuf->priv;
476 
477 	return vb2_dma_sg_vaddr(buf);
478 }
479 
480 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
481 	struct vm_area_struct *vma)
482 {
483 	return vb2_dma_sg_mmap(dbuf->priv, vma);
484 }
485 
486 static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
487 	.attach = vb2_dma_sg_dmabuf_ops_attach,
488 	.detach = vb2_dma_sg_dmabuf_ops_detach,
489 	.map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
490 	.unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
491 	.vmap = vb2_dma_sg_dmabuf_ops_vmap,
492 	.mmap = vb2_dma_sg_dmabuf_ops_mmap,
493 	.release = vb2_dma_sg_dmabuf_ops_release,
494 };
495 
496 static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
497 {
498 	struct vb2_dma_sg_buf *buf = buf_priv;
499 	struct dma_buf *dbuf;
500 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
501 
502 	exp_info.ops = &vb2_dma_sg_dmabuf_ops;
503 	exp_info.size = buf->size;
504 	exp_info.flags = flags;
505 	exp_info.priv = buf;
506 
507 	if (WARN_ON(!buf->dma_sgt))
508 		return NULL;
509 
510 	dbuf = dma_buf_export(&exp_info);
511 	if (IS_ERR(dbuf))
512 		return NULL;
513 
514 	/* dmabuf keeps reference to vb2 buffer */
515 	refcount_inc(&buf->refcount);
516 
517 	return dbuf;
518 }
519 
520 /*********************************************/
521 /*       callbacks for DMABUF buffers        */
522 /*********************************************/
523 
524 static int vb2_dma_sg_map_dmabuf(void *mem_priv)
525 {
526 	struct vb2_dma_sg_buf *buf = mem_priv;
527 	struct sg_table *sgt;
528 
529 	if (WARN_ON(!buf->db_attach)) {
530 		pr_err("trying to pin a non attached buffer\n");
531 		return -EINVAL;
532 	}
533 
534 	if (WARN_ON(buf->dma_sgt)) {
535 		pr_err("dmabuf buffer is already pinned\n");
536 		return 0;
537 	}
538 
539 	/* get the associated scatterlist for this buffer */
540 	sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
541 	if (IS_ERR(sgt)) {
542 		pr_err("Error getting dmabuf scatterlist\n");
543 		return -EINVAL;
544 	}
545 
546 	buf->dma_sgt = sgt;
547 	buf->vaddr = NULL;
548 
549 	return 0;
550 }
551 
552 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
553 {
554 	struct vb2_dma_sg_buf *buf = mem_priv;
555 	struct sg_table *sgt = buf->dma_sgt;
556 
557 	if (WARN_ON(!buf->db_attach)) {
558 		pr_err("trying to unpin a not attached buffer\n");
559 		return;
560 	}
561 
562 	if (WARN_ON(!sgt)) {
563 		pr_err("dmabuf buffer is already unpinned\n");
564 		return;
565 	}
566 
567 	if (buf->vaddr) {
568 		dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
569 		buf->vaddr = NULL;
570 	}
571 	dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
572 
573 	buf->dma_sgt = NULL;
574 }
575 
576 static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
577 {
578 	struct vb2_dma_sg_buf *buf = mem_priv;
579 
580 	/* if vb2 works correctly you should never detach mapped buffer */
581 	if (WARN_ON(buf->dma_sgt))
582 		vb2_dma_sg_unmap_dmabuf(buf);
583 
584 	/* detach this attachment */
585 	dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
586 	kfree(buf);
587 }
588 
589 static void *vb2_dma_sg_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
590 	unsigned long size, enum dma_data_direction dma_dir)
591 {
592 	struct vb2_dma_sg_buf *buf;
593 	struct dma_buf_attachment *dba;
594 
595 	if (WARN_ON(!dev))
596 		return ERR_PTR(-EINVAL);
597 
598 	if (dbuf->size < size)
599 		return ERR_PTR(-EFAULT);
600 
601 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
602 	if (!buf)
603 		return ERR_PTR(-ENOMEM);
604 
605 	buf->dev = dev;
606 	/* create attachment for the dmabuf with the user device */
607 	dba = dma_buf_attach(dbuf, buf->dev);
608 	if (IS_ERR(dba)) {
609 		pr_err("failed to attach dmabuf\n");
610 		kfree(buf);
611 		return dba;
612 	}
613 
614 	buf->dma_dir = dma_dir;
615 	buf->size = size;
616 	buf->db_attach = dba;
617 
618 	return buf;
619 }
620 
621 static void *vb2_dma_sg_cookie(void *buf_priv)
622 {
623 	struct vb2_dma_sg_buf *buf = buf_priv;
624 
625 	return buf->dma_sgt;
626 }
627 
628 const struct vb2_mem_ops vb2_dma_sg_memops = {
629 	.alloc		= vb2_dma_sg_alloc,
630 	.put		= vb2_dma_sg_put,
631 	.get_userptr	= vb2_dma_sg_get_userptr,
632 	.put_userptr	= vb2_dma_sg_put_userptr,
633 	.prepare	= vb2_dma_sg_prepare,
634 	.finish		= vb2_dma_sg_finish,
635 	.vaddr		= vb2_dma_sg_vaddr,
636 	.mmap		= vb2_dma_sg_mmap,
637 	.num_users	= vb2_dma_sg_num_users,
638 	.get_dmabuf	= vb2_dma_sg_get_dmabuf,
639 	.map_dmabuf	= vb2_dma_sg_map_dmabuf,
640 	.unmap_dmabuf	= vb2_dma_sg_unmap_dmabuf,
641 	.attach_dmabuf	= vb2_dma_sg_attach_dmabuf,
642 	.detach_dmabuf	= vb2_dma_sg_detach_dmabuf,
643 	.cookie		= vb2_dma_sg_cookie,
644 };
645 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
646 
647 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
648 MODULE_AUTHOR("Andrzej Pietrasiewicz");
649 MODULE_LICENSE("GPL");
650