1 /*
2  * videobuf2-dma-contig.c - DMA contig memory allocator for videobuf2
3  *
4  * Copyright (C) 2010 Samsung Electronics
5  *
6  * Author: Pawel Osciak <pawel@osciak.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/dma-buf.h>
14 #include <linux/module.h>
15 #include <linux/refcount.h>
16 #include <linux/scatterlist.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/dma-mapping.h>
20 
21 #include <media/videobuf2-v4l2.h>
22 #include <media/videobuf2-dma-contig.h>
23 #include <media/videobuf2-memops.h>
24 
25 struct vb2_dc_buf {
26 	struct device			*dev;
27 	void				*vaddr;
28 	unsigned long			size;
29 	void				*cookie;
30 	dma_addr_t			dma_addr;
31 	unsigned long			attrs;
32 	enum dma_data_direction		dma_dir;
33 	struct sg_table			*dma_sgt;
34 	struct frame_vector		*vec;
35 
36 	/* MMAP related */
37 	struct vb2_vmarea_handler	handler;
38 	refcount_t			refcount;
39 	struct sg_table			*sgt_base;
40 
41 	/* DMABUF related */
42 	struct dma_buf_attachment	*db_attach;
43 };
44 
45 static inline bool vb2_dc_buffer_consistent(unsigned long attr)
46 {
47 	return !(attr & DMA_ATTR_NON_CONSISTENT);
48 }
49 
50 /*********************************************/
51 /*        scatterlist table functions        */
52 /*********************************************/
53 
54 static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
55 {
56 	struct scatterlist *s;
57 	dma_addr_t expected = sg_dma_address(sgt->sgl);
58 	unsigned int i;
59 	unsigned long size = 0;
60 
61 	for_each_sg(sgt->sgl, s, sgt->nents, i) {
62 		if (sg_dma_address(s) != expected)
63 			break;
64 		expected = sg_dma_address(s) + sg_dma_len(s);
65 		size += sg_dma_len(s);
66 	}
67 	return size;
68 }
69 
70 /*********************************************/
71 /*         callbacks for all buffers         */
72 /*********************************************/
73 
74 static void *vb2_dc_cookie(void *buf_priv)
75 {
76 	struct vb2_dc_buf *buf = buf_priv;
77 
78 	return &buf->dma_addr;
79 }
80 
81 static void *vb2_dc_vaddr(void *buf_priv)
82 {
83 	struct vb2_dc_buf *buf = buf_priv;
84 	struct dma_buf_map map;
85 	int ret;
86 
87 	if (!buf->vaddr && buf->db_attach) {
88 		ret = dma_buf_vmap(buf->db_attach->dmabuf, &map);
89 		buf->vaddr = ret ? NULL : map.vaddr;
90 	}
91 
92 	return buf->vaddr;
93 }
94 
95 static unsigned int vb2_dc_num_users(void *buf_priv)
96 {
97 	struct vb2_dc_buf *buf = buf_priv;
98 
99 	return refcount_read(&buf->refcount);
100 }
101 
102 static void vb2_dc_prepare(void *buf_priv)
103 {
104 	struct vb2_dc_buf *buf = buf_priv;
105 	struct sg_table *sgt = buf->dma_sgt;
106 
107 	if (!sgt)
108 		return;
109 
110 	dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
111 			       buf->dma_dir);
112 }
113 
114 static void vb2_dc_finish(void *buf_priv)
115 {
116 	struct vb2_dc_buf *buf = buf_priv;
117 	struct sg_table *sgt = buf->dma_sgt;
118 
119 	if (!sgt)
120 		return;
121 
122 	dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
123 }
124 
125 /*********************************************/
126 /*        callbacks for MMAP buffers         */
127 /*********************************************/
128 
129 static void vb2_dc_put(void *buf_priv)
130 {
131 	struct vb2_dc_buf *buf = buf_priv;
132 
133 	if (!refcount_dec_and_test(&buf->refcount))
134 		return;
135 
136 	if (buf->sgt_base) {
137 		sg_free_table(buf->sgt_base);
138 		kfree(buf->sgt_base);
139 	}
140 	dma_free_attrs(buf->dev, buf->size, buf->cookie, buf->dma_addr,
141 		       buf->attrs);
142 	put_device(buf->dev);
143 	kfree(buf);
144 }
145 
146 static void *vb2_dc_alloc(struct device *dev, unsigned long attrs,
147 			  unsigned long size, enum dma_data_direction dma_dir,
148 			  gfp_t gfp_flags)
149 {
150 	struct vb2_dc_buf *buf;
151 
152 	if (WARN_ON(!dev))
153 		return ERR_PTR(-EINVAL);
154 
155 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
156 	if (!buf)
157 		return ERR_PTR(-ENOMEM);
158 
159 	buf->attrs = attrs;
160 	buf->cookie = dma_alloc_attrs(dev, size, &buf->dma_addr,
161 					GFP_KERNEL | gfp_flags, buf->attrs);
162 	if (!buf->cookie) {
163 		dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
164 		kfree(buf);
165 		return ERR_PTR(-ENOMEM);
166 	}
167 
168 	if ((buf->attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0)
169 		buf->vaddr = buf->cookie;
170 
171 	/* Prevent the device from being released while the buffer is used */
172 	buf->dev = get_device(dev);
173 	buf->size = size;
174 	buf->dma_dir = dma_dir;
175 
176 	buf->handler.refcount = &buf->refcount;
177 	buf->handler.put = vb2_dc_put;
178 	buf->handler.arg = buf;
179 
180 	refcount_set(&buf->refcount, 1);
181 
182 	return buf;
183 }
184 
185 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
186 {
187 	struct vb2_dc_buf *buf = buf_priv;
188 	int ret;
189 
190 	if (!buf) {
191 		printk(KERN_ERR "No buffer to map\n");
192 		return -EINVAL;
193 	}
194 
195 	ret = dma_mmap_attrs(buf->dev, vma, buf->cookie,
196 		buf->dma_addr, buf->size, buf->attrs);
197 
198 	if (ret) {
199 		pr_err("Remapping memory failed, error: %d\n", ret);
200 		return ret;
201 	}
202 
203 	vma->vm_flags		|= VM_DONTEXPAND | VM_DONTDUMP;
204 	vma->vm_private_data	= &buf->handler;
205 	vma->vm_ops		= &vb2_common_vm_ops;
206 
207 	vma->vm_ops->open(vma);
208 
209 	pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
210 		__func__, (unsigned long)buf->dma_addr, vma->vm_start,
211 		buf->size);
212 
213 	return 0;
214 }
215 
216 /*********************************************/
217 /*         DMABUF ops for exporters          */
218 /*********************************************/
219 
220 struct vb2_dc_attachment {
221 	struct sg_table sgt;
222 	enum dma_data_direction dma_dir;
223 };
224 
225 static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf,
226 	struct dma_buf_attachment *dbuf_attach)
227 {
228 	struct vb2_dc_attachment *attach;
229 	unsigned int i;
230 	struct scatterlist *rd, *wr;
231 	struct sg_table *sgt;
232 	struct vb2_dc_buf *buf = dbuf->priv;
233 	int ret;
234 
235 	attach = kzalloc(sizeof(*attach), GFP_KERNEL);
236 	if (!attach)
237 		return -ENOMEM;
238 
239 	sgt = &attach->sgt;
240 	/* Copy the buf->base_sgt scatter list to the attachment, as we can't
241 	 * map the same scatter list to multiple attachments at the same time.
242 	 */
243 	ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
244 	if (ret) {
245 		kfree(attach);
246 		return -ENOMEM;
247 	}
248 
249 	rd = buf->sgt_base->sgl;
250 	wr = sgt->sgl;
251 	for (i = 0; i < sgt->orig_nents; ++i) {
252 		sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
253 		rd = sg_next(rd);
254 		wr = sg_next(wr);
255 	}
256 
257 	attach->dma_dir = DMA_NONE;
258 	dbuf_attach->priv = attach;
259 
260 	return 0;
261 }
262 
263 static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
264 	struct dma_buf_attachment *db_attach)
265 {
266 	struct vb2_dc_attachment *attach = db_attach->priv;
267 	struct sg_table *sgt;
268 
269 	if (!attach)
270 		return;
271 
272 	sgt = &attach->sgt;
273 
274 	/* release the scatterlist cache */
275 	if (attach->dma_dir != DMA_NONE)
276 		/*
277 		 * Cache sync can be skipped here, as the vb2_dc memory is
278 		 * allocated from device coherent memory, which means the
279 		 * memory locations do not require any explicit cache
280 		 * maintenance prior or after being used by the device.
281 		 */
282 		dma_unmap_sg_attrs(db_attach->dev, sgt->sgl, sgt->orig_nents,
283 				   attach->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
284 	sg_free_table(sgt);
285 	kfree(attach);
286 	db_attach->priv = NULL;
287 }
288 
289 static struct sg_table *vb2_dc_dmabuf_ops_map(
290 	struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
291 {
292 	struct vb2_dc_attachment *attach = db_attach->priv;
293 	/* stealing dmabuf mutex to serialize map/unmap operations */
294 	struct mutex *lock = &db_attach->dmabuf->lock;
295 	struct sg_table *sgt;
296 
297 	mutex_lock(lock);
298 
299 	sgt = &attach->sgt;
300 	/* return previously mapped sg table */
301 	if (attach->dma_dir == dma_dir) {
302 		mutex_unlock(lock);
303 		return sgt;
304 	}
305 
306 	/* release any previous cache */
307 	if (attach->dma_dir != DMA_NONE) {
308 		dma_unmap_sg_attrs(db_attach->dev, sgt->sgl, sgt->orig_nents,
309 				   attach->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
310 		attach->dma_dir = DMA_NONE;
311 	}
312 
313 	/*
314 	 * mapping to the client with new direction, no cache sync
315 	 * required see comment in vb2_dc_dmabuf_ops_detach()
316 	 */
317 	sgt->nents = dma_map_sg_attrs(db_attach->dev, sgt->sgl, sgt->orig_nents,
318 				      dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
319 	if (!sgt->nents) {
320 		pr_err("failed to map scatterlist\n");
321 		mutex_unlock(lock);
322 		return ERR_PTR(-EIO);
323 	}
324 
325 	attach->dma_dir = dma_dir;
326 
327 	mutex_unlock(lock);
328 
329 	return sgt;
330 }
331 
332 static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
333 	struct sg_table *sgt, enum dma_data_direction dma_dir)
334 {
335 	/* nothing to be done here */
336 }
337 
338 static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
339 {
340 	/* drop reference obtained in vb2_dc_get_dmabuf */
341 	vb2_dc_put(dbuf->priv);
342 }
343 
344 static int
345 vb2_dc_dmabuf_ops_begin_cpu_access(struct dma_buf *dbuf,
346 				   enum dma_data_direction direction)
347 {
348 	struct vb2_dc_buf *buf = dbuf->priv;
349 	struct sg_table *sgt = buf->dma_sgt;
350 
351 	if (vb2_dc_buffer_consistent(buf->attrs))
352 		return 0;
353 
354 	dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
355 	return 0;
356 }
357 
358 static int
359 vb2_dc_dmabuf_ops_end_cpu_access(struct dma_buf *dbuf,
360 				 enum dma_data_direction direction)
361 {
362 	struct vb2_dc_buf *buf = dbuf->priv;
363 	struct sg_table *sgt = buf->dma_sgt;
364 
365 	if (vb2_dc_buffer_consistent(buf->attrs))
366 		return 0;
367 
368 	dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
369 	return 0;
370 }
371 
372 static int vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf, struct dma_buf_map *map)
373 {
374 	struct vb2_dc_buf *buf = dbuf->priv;
375 
376 	dma_buf_map_set_vaddr(map, buf->vaddr);
377 
378 	return 0;
379 }
380 
381 static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
382 	struct vm_area_struct *vma)
383 {
384 	return vb2_dc_mmap(dbuf->priv, vma);
385 }
386 
387 static const struct dma_buf_ops vb2_dc_dmabuf_ops = {
388 	.attach = vb2_dc_dmabuf_ops_attach,
389 	.detach = vb2_dc_dmabuf_ops_detach,
390 	.map_dma_buf = vb2_dc_dmabuf_ops_map,
391 	.unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
392 	.begin_cpu_access = vb2_dc_dmabuf_ops_begin_cpu_access,
393 	.end_cpu_access = vb2_dc_dmabuf_ops_end_cpu_access,
394 	.vmap = vb2_dc_dmabuf_ops_vmap,
395 	.mmap = vb2_dc_dmabuf_ops_mmap,
396 	.release = vb2_dc_dmabuf_ops_release,
397 };
398 
399 static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
400 {
401 	int ret;
402 	struct sg_table *sgt;
403 
404 	sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
405 	if (!sgt) {
406 		dev_err(buf->dev, "failed to alloc sg table\n");
407 		return NULL;
408 	}
409 
410 	ret = dma_get_sgtable_attrs(buf->dev, sgt, buf->cookie, buf->dma_addr,
411 		buf->size, buf->attrs);
412 	if (ret < 0) {
413 		dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
414 		kfree(sgt);
415 		return NULL;
416 	}
417 
418 	return sgt;
419 }
420 
421 static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
422 {
423 	struct vb2_dc_buf *buf = buf_priv;
424 	struct dma_buf *dbuf;
425 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
426 
427 	exp_info.ops = &vb2_dc_dmabuf_ops;
428 	exp_info.size = buf->size;
429 	exp_info.flags = flags;
430 	exp_info.priv = buf;
431 
432 	if (!buf->sgt_base)
433 		buf->sgt_base = vb2_dc_get_base_sgt(buf);
434 
435 	if (WARN_ON(!buf->sgt_base))
436 		return NULL;
437 
438 	dbuf = dma_buf_export(&exp_info);
439 	if (IS_ERR(dbuf))
440 		return NULL;
441 
442 	/* dmabuf keeps reference to vb2 buffer */
443 	refcount_inc(&buf->refcount);
444 
445 	return dbuf;
446 }
447 
448 /*********************************************/
449 /*       callbacks for USERPTR buffers       */
450 /*********************************************/
451 
452 static void vb2_dc_put_userptr(void *buf_priv)
453 {
454 	struct vb2_dc_buf *buf = buf_priv;
455 	struct sg_table *sgt = buf->dma_sgt;
456 	int i;
457 	struct page **pages;
458 
459 	if (sgt) {
460 		/*
461 		 * No need to sync to CPU, it's already synced to the CPU
462 		 * since the finish() memop will have been called before this.
463 		 */
464 		dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
465 				   buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
466 		pages = frame_vector_pages(buf->vec);
467 		/* sgt should exist only if vector contains pages... */
468 		BUG_ON(IS_ERR(pages));
469 		if (buf->dma_dir == DMA_FROM_DEVICE ||
470 		    buf->dma_dir == DMA_BIDIRECTIONAL)
471 			for (i = 0; i < frame_vector_count(buf->vec); i++)
472 				set_page_dirty_lock(pages[i]);
473 		sg_free_table(sgt);
474 		kfree(sgt);
475 	} else {
476 		dma_unmap_resource(buf->dev, buf->dma_addr, buf->size,
477 				   buf->dma_dir, 0);
478 	}
479 	vb2_destroy_framevec(buf->vec);
480 	kfree(buf);
481 }
482 
483 static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr,
484 	unsigned long size, enum dma_data_direction dma_dir)
485 {
486 	struct vb2_dc_buf *buf;
487 	struct frame_vector *vec;
488 	unsigned int offset;
489 	int n_pages, i;
490 	int ret = 0;
491 	struct sg_table *sgt;
492 	unsigned long contig_size;
493 	unsigned long dma_align = dma_get_cache_alignment();
494 
495 	/* Only cache aligned DMA transfers are reliable */
496 	if (!IS_ALIGNED(vaddr | size, dma_align)) {
497 		pr_debug("user data must be aligned to %lu bytes\n", dma_align);
498 		return ERR_PTR(-EINVAL);
499 	}
500 
501 	if (!size) {
502 		pr_debug("size is zero\n");
503 		return ERR_PTR(-EINVAL);
504 	}
505 
506 	if (WARN_ON(!dev))
507 		return ERR_PTR(-EINVAL);
508 
509 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
510 	if (!buf)
511 		return ERR_PTR(-ENOMEM);
512 
513 	buf->dev = dev;
514 	buf->dma_dir = dma_dir;
515 
516 	offset = lower_32_bits(offset_in_page(vaddr));
517 	vec = vb2_create_framevec(vaddr, size);
518 	if (IS_ERR(vec)) {
519 		ret = PTR_ERR(vec);
520 		goto fail_buf;
521 	}
522 	buf->vec = vec;
523 	n_pages = frame_vector_count(vec);
524 	ret = frame_vector_to_pages(vec);
525 	if (ret < 0) {
526 		unsigned long *nums = frame_vector_pfns(vec);
527 
528 		/*
529 		 * Failed to convert to pages... Check the memory is physically
530 		 * contiguous and use direct mapping
531 		 */
532 		for (i = 1; i < n_pages; i++)
533 			if (nums[i-1] + 1 != nums[i])
534 				goto fail_pfnvec;
535 		buf->dma_addr = dma_map_resource(buf->dev,
536 				__pfn_to_phys(nums[0]), size, buf->dma_dir, 0);
537 		if (dma_mapping_error(buf->dev, buf->dma_addr)) {
538 			ret = -ENOMEM;
539 			goto fail_pfnvec;
540 		}
541 		goto out;
542 	}
543 
544 	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
545 	if (!sgt) {
546 		pr_err("failed to allocate sg table\n");
547 		ret = -ENOMEM;
548 		goto fail_pfnvec;
549 	}
550 
551 	ret = sg_alloc_table_from_pages(sgt, frame_vector_pages(vec), n_pages,
552 		offset, size, GFP_KERNEL);
553 	if (ret) {
554 		pr_err("failed to initialize sg table\n");
555 		goto fail_sgt;
556 	}
557 
558 	/*
559 	 * No need to sync to the device, this will happen later when the
560 	 * prepare() memop is called.
561 	 */
562 	sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
563 				      buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
564 	if (sgt->nents <= 0) {
565 		pr_err("failed to map scatterlist\n");
566 		ret = -EIO;
567 		goto fail_sgt_init;
568 	}
569 
570 	contig_size = vb2_dc_get_contiguous_size(sgt);
571 	if (contig_size < size) {
572 		pr_err("contiguous mapping is too small %lu/%lu\n",
573 			contig_size, size);
574 		ret = -EFAULT;
575 		goto fail_map_sg;
576 	}
577 
578 	buf->dma_addr = sg_dma_address(sgt->sgl);
579 	buf->dma_sgt = sgt;
580 out:
581 	buf->size = size;
582 
583 	return buf;
584 
585 fail_map_sg:
586 	dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
587 			   buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
588 
589 fail_sgt_init:
590 	sg_free_table(sgt);
591 
592 fail_sgt:
593 	kfree(sgt);
594 
595 fail_pfnvec:
596 	vb2_destroy_framevec(vec);
597 
598 fail_buf:
599 	kfree(buf);
600 
601 	return ERR_PTR(ret);
602 }
603 
604 /*********************************************/
605 /*       callbacks for DMABUF buffers        */
606 /*********************************************/
607 
608 static int vb2_dc_map_dmabuf(void *mem_priv)
609 {
610 	struct vb2_dc_buf *buf = mem_priv;
611 	struct sg_table *sgt;
612 	unsigned long contig_size;
613 
614 	if (WARN_ON(!buf->db_attach)) {
615 		pr_err("trying to pin a non attached buffer\n");
616 		return -EINVAL;
617 	}
618 
619 	if (WARN_ON(buf->dma_sgt)) {
620 		pr_err("dmabuf buffer is already pinned\n");
621 		return 0;
622 	}
623 
624 	/* get the associated scatterlist for this buffer */
625 	sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
626 	if (IS_ERR(sgt)) {
627 		pr_err("Error getting dmabuf scatterlist\n");
628 		return -EINVAL;
629 	}
630 
631 	/* checking if dmabuf is big enough to store contiguous chunk */
632 	contig_size = vb2_dc_get_contiguous_size(sgt);
633 	if (contig_size < buf->size) {
634 		pr_err("contiguous chunk is too small %lu/%lu\n",
635 		       contig_size, buf->size);
636 		dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
637 		return -EFAULT;
638 	}
639 
640 	buf->dma_addr = sg_dma_address(sgt->sgl);
641 	buf->dma_sgt = sgt;
642 	buf->vaddr = NULL;
643 
644 	return 0;
645 }
646 
647 static void vb2_dc_unmap_dmabuf(void *mem_priv)
648 {
649 	struct vb2_dc_buf *buf = mem_priv;
650 	struct sg_table *sgt = buf->dma_sgt;
651 	struct dma_buf_map map = DMA_BUF_MAP_INIT_VADDR(buf->vaddr);
652 
653 	if (WARN_ON(!buf->db_attach)) {
654 		pr_err("trying to unpin a not attached buffer\n");
655 		return;
656 	}
657 
658 	if (WARN_ON(!sgt)) {
659 		pr_err("dmabuf buffer is already unpinned\n");
660 		return;
661 	}
662 
663 	if (buf->vaddr) {
664 		dma_buf_vunmap(buf->db_attach->dmabuf, &map);
665 		buf->vaddr = NULL;
666 	}
667 	dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
668 
669 	buf->dma_addr = 0;
670 	buf->dma_sgt = NULL;
671 }
672 
673 static void vb2_dc_detach_dmabuf(void *mem_priv)
674 {
675 	struct vb2_dc_buf *buf = mem_priv;
676 
677 	/* if vb2 works correctly you should never detach mapped buffer */
678 	if (WARN_ON(buf->dma_addr))
679 		vb2_dc_unmap_dmabuf(buf);
680 
681 	/* detach this attachment */
682 	dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
683 	kfree(buf);
684 }
685 
686 static void *vb2_dc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
687 	unsigned long size, enum dma_data_direction dma_dir)
688 {
689 	struct vb2_dc_buf *buf;
690 	struct dma_buf_attachment *dba;
691 
692 	if (dbuf->size < size)
693 		return ERR_PTR(-EFAULT);
694 
695 	if (WARN_ON(!dev))
696 		return ERR_PTR(-EINVAL);
697 
698 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
699 	if (!buf)
700 		return ERR_PTR(-ENOMEM);
701 
702 	buf->dev = dev;
703 	/* create attachment for the dmabuf with the user device */
704 	dba = dma_buf_attach(dbuf, buf->dev);
705 	if (IS_ERR(dba)) {
706 		pr_err("failed to attach dmabuf\n");
707 		kfree(buf);
708 		return dba;
709 	}
710 
711 	buf->dma_dir = dma_dir;
712 	buf->size = size;
713 	buf->db_attach = dba;
714 
715 	return buf;
716 }
717 
718 /*********************************************/
719 /*       DMA CONTIG exported functions       */
720 /*********************************************/
721 
722 const struct vb2_mem_ops vb2_dma_contig_memops = {
723 	.alloc		= vb2_dc_alloc,
724 	.put		= vb2_dc_put,
725 	.get_dmabuf	= vb2_dc_get_dmabuf,
726 	.cookie		= vb2_dc_cookie,
727 	.vaddr		= vb2_dc_vaddr,
728 	.mmap		= vb2_dc_mmap,
729 	.get_userptr	= vb2_dc_get_userptr,
730 	.put_userptr	= vb2_dc_put_userptr,
731 	.prepare	= vb2_dc_prepare,
732 	.finish		= vb2_dc_finish,
733 	.map_dmabuf	= vb2_dc_map_dmabuf,
734 	.unmap_dmabuf	= vb2_dc_unmap_dmabuf,
735 	.attach_dmabuf	= vb2_dc_attach_dmabuf,
736 	.detach_dmabuf	= vb2_dc_detach_dmabuf,
737 	.num_users	= vb2_dc_num_users,
738 };
739 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
740 
741 /**
742  * vb2_dma_contig_set_max_seg_size() - configure DMA max segment size
743  * @dev:	device for configuring DMA parameters
744  * @size:	size of DMA max segment size to set
745  *
746  * To allow mapping the scatter-list into a single chunk in the DMA
747  * address space, the device is required to have the DMA max segment
748  * size parameter set to a value larger than the buffer size. Otherwise,
749  * the DMA-mapping subsystem will split the mapping into max segment
750  * size chunks. This function sets the DMA max segment size
751  * parameter to let DMA-mapping map a buffer as a single chunk in DMA
752  * address space.
753  * This code assumes that the DMA-mapping subsystem will merge all
754  * scatterlist segments if this is really possible (for example when
755  * an IOMMU is available and enabled).
756  * Ideally, this parameter should be set by the generic bus code, but it
757  * is left with the default 64KiB value due to historical litmiations in
758  * other subsystems (like limited USB host drivers) and there no good
759  * place to set it to the proper value.
760  * This function should be called from the drivers, which are known to
761  * operate on platforms with IOMMU and provide access to shared buffers
762  * (either USERPTR or DMABUF). This should be done before initializing
763  * videobuf2 queue.
764  */
765 int vb2_dma_contig_set_max_seg_size(struct device *dev, unsigned int size)
766 {
767 	if (!dev->dma_parms) {
768 		dev_err(dev, "Failed to set max_seg_size: dma_parms is NULL\n");
769 		return -ENODEV;
770 	}
771 	if (dma_get_max_seg_size(dev) < size)
772 		return dma_set_max_seg_size(dev, size);
773 
774 	return 0;
775 }
776 EXPORT_SYMBOL_GPL(vb2_dma_contig_set_max_seg_size);
777 
778 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
779 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
780 MODULE_LICENSE("GPL");
781