aboutsummaryrefslogblamecommitdiffstats
path: root/drivers/media/video/videobuf2-ion.c
blob: 75185e54cf64f63a0045420fe45364aa2cf43911 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841








































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                       
/* linux/drivers/media/video/videobuf2-ion.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com/
 *
 * Implementation of Android ION memory allocator for videobuf2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/

#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/file.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/scatterlist.h>
#include <linux/dma-mapping.h>
#include <linux/ion.h>
#include <linux/syscalls.h>

#include <asm/cacheflush.h>

#include <media/videobuf2-ion.h>
#include <plat/iovmm.h>
#include <plat/cpu.h>

static int vb2_ion_debug;
module_param(vb2_ion_debug, int, 0644);
#define dbg(level, fmt, arg...)						\
	do {								\
		if (vb2_ion_debug >= level)				\
			printk(KERN_DEBUG "vb2_ion: " fmt, ## arg);	\
	} while (0)

#define SIZE_THRESHOLD SZ_1M

struct vb2_ion_conf {
	struct device		*dev;
	const char		*name;

	struct ion_client	*client;

	unsigned long		align;
	bool			contig;
	bool			sharable;
	bool			cacheable;
	bool			use_mmu;
	atomic_t		mmu_enable;

	spinlock_t		slock;
};

struct vb2_ion_buf {
	struct vm_area_struct		**vma;
	int				vma_count;
	struct vb2_ion_conf		*conf;
	struct vb2_vmarea_handler	handler;

	struct ion_handle		*handle;	/* Kernel space */

	dma_addr_t			kva;
	dma_addr_t			dva;
	unsigned long			size;

	struct scatterlist		*sg;
	int				nents;

	atomic_t			ref;

	bool				cacheable;
};

static void vb2_ion_put(void *buf_priv);

static struct ion_client *vb2_ion_init_ion(struct vb2_ion *ion,
					   struct vb2_drv *drv)
{
	struct ion_client *client;
	int ret;
	int mask = ION_HEAP_EXYNOS_MASK | ION_HEAP_EXYNOS_CONTIG_MASK |
						ION_HEAP_EXYNOS_USER_MASK;

	client = ion_client_create(ion_exynos, mask, ion->name);
	if (IS_ERR(client)) {
		pr_err("ion_client_create: ion_name(%s)\n", ion->name);
		return ERR_PTR(-EINVAL);
	}

	if (!drv->use_mmu)
		return client;

	ret = iovmm_setup(ion->dev);
	if (ret) {
		pr_err("iovmm_setup: ion_name(%s)\n", ion->name);
		ion_client_destroy(client);
		return ERR_PTR(-EINVAL);
	}

	return client;
}

static void vb2_ion_init_conf(struct vb2_ion_conf *conf,
			      struct ion_client *client,
			      struct vb2_ion *ion,
			      struct vb2_drv *drv)
{
	conf->dev		= ion->dev;
	conf->name		= ion->name;
	conf->client		= client;
	conf->contig		= ion->contig;
	conf->cacheable		= ion->cacheable;
	conf->align		= ion->align;
	conf->use_mmu		= drv->use_mmu;

	spin_lock_init(&conf->slock);
}

void *vb2_ion_init(struct vb2_ion *ion,
		   struct vb2_drv *drv)
{
	struct ion_client *client;
	struct vb2_ion_conf *conf;

	conf = kzalloc(sizeof *conf, GFP_KERNEL);
	if (!conf)
		return ERR_PTR(-ENOMEM);

	client = vb2_ion_init_ion(ion, drv);
	if (IS_ERR(client)) {
		kfree(conf);
		return ERR_PTR(-EINVAL);
	}

	vb2_ion_init_conf(conf, client, ion, drv);

	return conf;
}
EXPORT_SYMBOL_GPL(vb2_ion_init);

void vb2_ion_cleanup(void *alloc_ctx)
{
	struct vb2_ion_conf *conf = alloc_ctx;

	BUG_ON(!conf);

	if (conf->use_mmu) {
		if (atomic_read(&conf->mmu_enable)) {
			pr_warning("mmu_enable(%d)\n", atomic_read(&conf->mmu_enable));
			iovmm_deactivate(conf->dev);
		}

		iovmm_cleanup(conf->dev);
	}

	ion_client_destroy(conf->client);

	kfree(alloc_ctx);
}
EXPORT_SYMBOL_GPL(vb2_ion_cleanup);

void **vb2_ion_init_multi(unsigned int num_planes,
			  struct vb2_ion *ion,
			  struct vb2_drv *drv)
{
	struct ion_client *client;
	struct vb2_ion_conf *conf;
	void **alloc_ctxes;
	int i;

	/* allocate structure of alloc_ctxes */
	alloc_ctxes = kzalloc((sizeof *alloc_ctxes + sizeof *conf) * num_planes,
			      GFP_KERNEL);

	if (!alloc_ctxes)
		return ERR_PTR(-ENOMEM);

	client = vb2_ion_init_ion(ion, drv);
	if (IS_ERR(client)) {
		kfree(alloc_ctxes);
		return ERR_PTR(-EINVAL);
	}

	conf = (void *)(alloc_ctxes + num_planes);
	for (i = 0; i < num_planes; ++i, ++conf) {
		alloc_ctxes[i] = conf;
		vb2_ion_init_conf(conf, client, ion, drv);
	}

	return alloc_ctxes;
}
EXPORT_SYMBOL_GPL(vb2_ion_init_multi);

void vb2_ion_cleanup_multi(void **alloc_ctxes)
{
	struct vb2_ion_conf *conf = alloc_ctxes[0];

	BUG_ON(!conf);

	if (conf->use_mmu) {
		if (atomic_read(&conf->mmu_enable)) {
			pr_warning("mmu_enable(%d)\n", atomic_read(&conf->mmu_enable));
			iovmm_deactivate(conf->dev);
		}

		iovmm_cleanup(conf->dev);
	}

	ion_client_destroy(conf->client);

	kfree(alloc_ctxes);
}
EXPORT_SYMBOL_GPL(vb2_ion_cleanup_multi);

static void *vb2_ion_alloc(void *alloc_ctx, unsigned long size)
{
	struct vb2_ion_conf	*conf = alloc_ctx;
	struct vb2_ion_buf	*buf;
	struct scatterlist	*sg;
	size_t	len;
	u32 heap = 0;
	int ret = 0;

	buf = kzalloc(sizeof *buf, GFP_KERNEL);
	if (!buf) {
		pr_err("no memory for vb2_ion_conf\n");
		return ERR_PTR(-ENOMEM);
	}

	/* Set vb2_ion_buf */
	buf->conf = conf;
	buf->size = size;
	buf->cacheable = conf->cacheable;

	/* Allocate: physical memory */
	if (conf->contig)
		heap = ION_HEAP_EXYNOS_CONTIG_MASK;
	else
		heap = ION_HEAP_EXYNOS_MASK;

	buf->handle = ion_alloc(conf->client, size, conf->align, heap);
	if (IS_ERR(buf->handle)) {
		pr_err("ion_alloc of size %ld\n", size);
		ret = -ENOMEM;
		goto err_alloc;
	}

	/* Getting scatterlist */
	buf->sg = ion_map_dma(conf->client, buf->handle);
	if (IS_ERR(buf->sg)) {
		pr_err("ion_map_dma conf->name(%s)\n", conf->name);
		ret = -ENOMEM;
		goto err_map_dma;
	}
	dbg(6, "PA(0x%x), SIZE(%x)\n", buf->sg->dma_address, buf->sg->length);

	sg = buf->sg;
	do {
		buf->nents++;
	} while ((sg = sg_next(sg)));
	dbg(6, "buf->nents(0x%x)\n", buf->nents);

	/* Map DVA */
	if (conf->use_mmu) {
		buf->dva = iovmm_map(conf->dev, buf->sg, 0, size);
		if (!buf->dva) {
			pr_err("iovmm_map: conf->name(%s)\n", conf->name);
			goto err_ion_map_dva;
		}
		dbg(6, "DVA(0x%x)\n", buf->dva);
	} else {
		ret = ion_phys(conf->client, buf->handle,
			       (unsigned long *)&buf->dva, &len);
		if (ret) {
			pr_err("ion_phys: conf->name(%s)\n", conf->name);
			goto err_ion_map_dva;
		}
	}

	/* Set struct vb2_vmarea_handler */
	buf->handler.refcount = &buf->ref;
	buf->handler.put = vb2_ion_put;
	buf->handler.arg = buf;

	atomic_inc(&buf->ref);

	return buf;

err_ion_map_dva:
	ion_unmap_dma(conf->client, buf->handle);

err_map_dma:
	ion_free(conf->client, buf->handle);

err_alloc:
	kfree(buf);

	return ERR_PTR(ret);
}

static void vb2_ion_put(void *buf_priv)
{
	struct vb2_ion_buf *buf = buf_priv;
	struct vb2_ion_conf *conf = buf->conf;

	dbg(6, "released: buf_refcnt(%d)\n", atomic_read(&buf->ref) - 1);

	if (atomic_dec_and_test(&buf->ref)) {
		if (conf->use_mmu)
			iovmm_unmap(conf->dev, buf->dva);

		ion_unmap_dma(conf->client, buf->handle);

		if (buf->kva)
			ion_unmap_kernel(conf->client, buf->handle);

		ion_free(conf->client, buf->handle);

		kfree(buf);
	}
}

/**
 * _vb2_ion_get_vma() - lock userspace mapped memory
 * @vaddr:	starting virtual address of the area to be verified
 * @size:	size of the area
 * @res_vma:	will return locked copy of struct vm_area for the given area
 *
 * This function will go through memory area of size @size mapped at @vaddr
 * If they are contiguous the virtual memory area is locked and a @res_vma is
 * filled with the copy and @res_pa set to the physical address of the buffer.
 *
 * Returns 0 on success.
 */
static struct vm_area_struct **_vb2_ion_get_vma(unsigned long vaddr,
					unsigned long size, int *vma_num)
{
	struct mm_struct *mm = current->mm;
	struct vm_area_struct *vma, *vma0;
	struct vm_area_struct **vmas;
	unsigned long prev_end = 0;
	unsigned long end;
	int i;

	end = vaddr + size;

	down_read(&mm->mmap_sem);
	vma0 = find_vma(mm, vaddr);
	if (!vma0) {
		vmas = ERR_PTR(-EINVAL);
		goto done;
	}

	for (*vma_num = 1, vma = vma0->vm_next, prev_end = vma0->vm_end;
		vma && (end > vma->vm_start) && (prev_end == vma->vm_start);
				prev_end = vma->vm_end, vma = vma->vm_next) {
		*vma_num += 1;
	}

	if (prev_end < end) {
		vmas = ERR_PTR(-EINVAL);
		goto done;
	}

	vmas = kmalloc(sizeof(*vmas) * *vma_num, GFP_KERNEL);
	if (!vmas) {
		vmas = ERR_PTR(-ENOMEM);
		goto done;
	}

	for (i = 0; i < *vma_num; i++, vma0 = vma0->vm_next) {
		vmas[i] = vb2_get_vma(vma0);
		if (!vmas[i])
			break;
	}

	if (i < *vma_num) {
		while (i-- > 0)
			vb2_put_vma(vmas[i]);

		kfree(vmas);
		vmas = ERR_PTR(-ENOMEM);
	}

done:
	up_read(&mm->mmap_sem);
	return vmas;
}

static void *vb2_ion_get_userptr(void *alloc_ctx, unsigned long vaddr,
				 unsigned long size, int write)
{
	struct vb2_ion_conf *conf = alloc_ctx;
	struct vb2_ion_buf *buf = NULL;
	size_t len;
	int ret = 0;
	bool malloced = false;
	struct scatterlist *sg;
	off_t offset;

	/* Create vb2_ion_buf */
	buf = kzalloc(sizeof *buf, GFP_KERNEL);
	if (!buf) {
		pr_err("kzalloc failed\n");
		return ERR_PTR(-ENOMEM);
	}

	/* Getting handle, client from DVA */
	buf->handle = ion_import_uva(conf->client, vaddr, &offset);
	if (IS_ERR(buf->handle)) {
		if ((PTR_ERR(buf->handle) == -ENXIO) && conf->use_mmu) {
			int flags = ION_HEAP_EXYNOS_USER_MASK;

			if (write)
				flags |= ION_EXYNOS_WRITE_MASK;

			buf->handle = ion_exynos_get_user_pages(conf->client,
							vaddr, size, flags);
			if (IS_ERR(buf->handle))
				ret = PTR_ERR(buf->handle);
		} else {
			ret = -EINVAL;
		}

		if (ret) {
			pr_err("%s: Failed to retrieving non-ion user buffer @ "
				"0x%lx (size:0x%lx, dev:%s, errno %ld)\n",
				__func__, vaddr, size, dev_name(conf->dev),
					PTR_ERR(buf->handle));
			goto err_import_uva;
		}

		malloced = true;
		offset = 0;
	}

	/* TODO: Need to check whether already DVA is created or not */

	buf->sg = ion_map_dma(conf->client, buf->handle);
	if (IS_ERR(buf->sg)) {
		ret = -ENOMEM;
		goto err_map_dma;
	}
	dbg(6, "PA(0x%x) size(%x)\n", buf->sg->dma_address, buf->sg->length);

	sg = buf->sg;
	do {
		buf->nents++;
	} while ((sg = sg_next(sg)));

	/* Map DVA */
	if (conf->use_mmu) {
		buf->dva = iovmm_map(conf->dev, buf->sg, offset, size);
		if (!buf->dva) {
			pr_err("iovmm_map: conf->name(%s)\n", conf->name);
			goto err_ion_map_dva;
		}
		dbg(6, "DVA(0x%x)\n", buf->dva);
	} else {
		ret = ion_phys(conf->client, buf->handle,
			       (unsigned long *)&buf->dva, &len);
		if (ret) {
			pr_err("ion_phys: conf->name(%s)\n", conf->name);
			goto err_ion_map_dva;
		}

		buf->dva += offset;
	}

	/* Set vb2_ion_buf */
	buf->vma = _vb2_ion_get_vma(vaddr, size, &buf->vma_count);
	if (IS_ERR(buf->vma)) {
		pr_err("Failed acquiring VMA 0x%08lx\n", vaddr);

		if (conf->use_mmu)
			iovmm_unmap(conf->dev, buf->dva);

		goto err_get_vma;
	}

	buf->conf = conf;
	buf->size = size;
	buf->cacheable = conf->cacheable;

	return buf;

err_get_vma:	/* fall through */
err_ion_map_dva:
	ion_unmap_dma(conf->client, buf->handle);

err_map_dma:
	ion_free(conf->client, buf->handle);

err_import_uva:
	kfree(buf);

	return ERR_PTR(ret);
}

static void vb2_ion_put_userptr(void *mem_priv)
{
	struct vb2_ion_buf *buf = mem_priv;
	struct vb2_ion_conf *conf = buf->conf;
	int i;

	if (!buf) {
		pr_err("No buffer to put\n");
		return;
	}

	/* Unmap DVA, KVA */
	if (conf->use_mmu)
		iovmm_unmap(conf->dev, buf->dva);

	ion_unmap_dma(conf->client, buf->handle);
	if (buf->kva)
		ion_unmap_kernel(conf->client, buf->handle);

	ion_free(conf->client, buf->handle);

	for (i = 0; i < buf->vma_count; i++)
		vb2_put_vma(buf->vma[i]);
	kfree(buf->vma);

	kfree(buf);
}

static void *vb2_ion_cookie(void *buf_priv)
{
	struct vb2_ion_buf *buf = buf_priv;

	if (!buf) {
		pr_err("failed to get buffer\n");
		return NULL;
	}

	return (void *)buf->dva;
}

static void *vb2_ion_vaddr(void *buf_priv)
{
	struct vb2_ion_buf *buf = buf_priv;
	struct vb2_ion_conf *conf = buf->conf;

	if (!buf) {
		pr_err("failed to get buffer\n");
		return NULL;
	}

	if (!buf->kva) {
		buf->kva = (dma_addr_t)ion_map_kernel(conf->client, buf->handle);
		if (IS_ERR(ERR_PTR(buf->kva))) {
			pr_err("ion_map_kernel handle(%x)\n",
				(u32)buf->handle);
			return NULL;
		}
	}

	return (void *)buf->kva;
}

static unsigned int vb2_ion_num_users(void *buf_priv)
{
	struct vb2_ion_buf *buf = buf_priv;

	return atomic_read(&buf->ref);
}

/**
 * _vb2_ion_mmap_pfn_range() - map physical pages(vcm) to userspace
 * @vma:	virtual memory region for the mapping
 * @sg:		scatterlist to be mapped
 * @nents:	number of scatterlist to be mapped
 * @size:	size of the memory to be mapped
 * @vm_ops:	vm operations to be assigned to the created area
 * @priv:	private data to be associated with the area
 *
 * Returns 0 on success.
 */
static int _vb2_ion_mmap_pfn_range(struct vm_area_struct *vma,
				   struct scatterlist *sg,
				   int nents,
				   unsigned long size,
				   const struct vm_operations_struct *vm_ops,
				   void *priv)
{
	struct scatterlist *s;
	dma_addr_t addr;
	size_t len;
	unsigned long org_vm_start = vma->vm_start;
	int vma_size = vma->vm_end - vma->vm_start;
	resource_size_t remap_size;
	int mapped_size = 0;
	int remap_break = 0;
	int ret, i = 0;

	for_each_sg(sg, s, nents, i) {
		addr = sg_phys(s);
		len = sg_dma_len(s);
		if ((mapped_size + len) > vma_size) {
			remap_size = vma_size - mapped_size;
			remap_break = 1;
		} else {
			remap_size = len;
		}

		ret = remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
				      remap_size, vma->vm_page_prot);
		if (ret) {
			pr_err("Remapping failed, error: %d\n", ret);
			return ret;
		}

		dbg(6, "%dth page vaddr(0x%08x), paddr(0x%08x),	size(0x%08x)\n",
			i++, (u32)vma->vm_start, addr, len);

		mapped_size += remap_size;
		vma->vm_start += len;

		if (remap_break)
			break;
	}

	WARN_ON(size > mapped_size);

	/* re-assign initial start address */
	vma->vm_start		= org_vm_start;
	vma->vm_flags		|= VM_DONTEXPAND | VM_RESERVED;
	vma->vm_private_data	= priv;
	vma->vm_ops		= vm_ops;

	vma->vm_ops->open(vma);

	return 0;
}

static int vb2_ion_mmap(void *buf_priv, struct vm_area_struct *vma)
{
	struct vb2_ion_buf *buf = buf_priv;

	if (!buf) {
		pr_err("No buffer to map\n");
		return -EINVAL;
	}

	if (!buf->cacheable)
		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);

	return _vb2_ion_mmap_pfn_range(vma, buf->sg, buf->nents, buf->size,
				&vb2_common_vm_ops, &buf->handler);
}

const struct vb2_mem_ops vb2_ion_memops = {
	.alloc		= vb2_ion_alloc,
	.put		= vb2_ion_put,
	.cookie		= vb2_ion_cookie,
	.vaddr		= vb2_ion_vaddr,
	.mmap		= vb2_ion_mmap,
	.get_userptr	= vb2_ion_get_userptr,
	.put_userptr	= vb2_ion_put_userptr,
	.num_users	= vb2_ion_num_users,
};
EXPORT_SYMBOL_GPL(vb2_ion_memops);


void vb2_ion_set_sharable(void *alloc_ctx, bool sharable)
{
	((struct vb2_ion_conf *)alloc_ctx)->sharable = sharable;
}

void vb2_ion_set_cacheable(void *alloc_ctx, bool cacheable)
{
	((struct vb2_ion_conf *)alloc_ctx)->cacheable = cacheable;
}

bool vb2_ion_get_cacheable(void *alloc_ctx)
{
	return ((struct vb2_ion_conf *)alloc_ctx)->cacheable;
}

#if 0
int vb2_ion_cache_flush(struct vb2_buffer *vb, u32 num_planes)
{
	struct vb2_ion_conf *conf;
	struct vb2_ion_buf *buf;
	int i, ret;

	for (i = 0; i < num_planes; i++) {
		buf = vb->planes[i].mem_priv;
		conf = buf->conf;

		if (!buf->cacheable) {
			pr_warning("This is non-cacheable buffer allocator\n");
			return -EINVAL;
		}

		ret = dma_map_sg(conf->dev, buf->sg, buf->nents, DMA_TO_DEVICE);
		if (ret) {
			pr_err("flush sg cnt(%d)\n", ret);
			return -EINVAL;
		}
	}

	return 0;
}
#else
static void _vb2_ion_cache_flush_all(void)
{
	flush_cache_all();	/* L1 */
	smp_call_function((void (*)(void *))__cpuc_flush_kern_all, NULL, 1);
	outer_flush_all();	/* L2 */
}

static void _vb2_ion_cache_flush_range(struct vb2_ion_buf *buf,
				       unsigned long size)
{
	struct scatterlist *s;
	phys_addr_t start, end;
	int i;

	/* sequentially traversal phys */
	if (size > SZ_64K ) {
		flush_cache_all();	/* L1 */
		smp_call_function((void (*)(void *))__cpuc_flush_kern_all, NULL, 1);

		for_each_sg(buf->sg, s, buf->nents, i) {
			start = sg_phys(s);
			end = start + sg_dma_len(s) - 1;

			outer_flush_range(start, end);	/* L2 */
		}
	} else {
		dma_sync_sg_for_device(buf->conf->dev, buf->sg, buf->nents,
							DMA_BIDIRECTIONAL);
		dma_sync_sg_for_cpu(buf->conf->dev, buf->sg, buf->nents,
							DMA_BIDIRECTIONAL);
	}
}


int vb2_ion_cache_flush(struct vb2_buffer *vb, u32 num_planes)
{
	struct vb2_ion_conf *conf;
	struct vb2_ion_buf *buf;
	unsigned long size = 0;
	int i;

	for (i = 0; i < num_planes; i++) {
		buf = vb->planes[i].mem_priv;
		conf = buf->conf;

		if (!buf->cacheable) {
			pr_warning("This is non-cacheable buffer allocator\n");
			return -EINVAL;
		}

		size += buf->size;
	}

	if (size > (unsigned long)SIZE_THRESHOLD) {
		_vb2_ion_cache_flush_all();
	} else {
		for (i = 0; i < num_planes; i++) {
			buf = vb->planes[i].mem_priv;
			_vb2_ion_cache_flush_range(buf, size);
		}
	}

	return 0;
}
#endif

int vb2_ion_cache_inv(struct vb2_buffer *vb, u32 num_planes)
{
	struct vb2_ion_conf *conf;
	struct vb2_ion_buf *buf;
	int i;

	for (i = 0; i < num_planes; i++) {
		buf = vb->planes[i].mem_priv;
		conf = buf->conf;
		if (!buf->cacheable) {
			pr_warning("This is non-cacheable buffer allocator\n");
			return -EINVAL;
		}

		dma_unmap_sg(conf->dev, buf->sg, buf->nents, DMA_FROM_DEVICE);
	}

	return 0;
}

void vb2_ion_suspend(void *alloc_ctx)
{
	struct vb2_ion_conf *conf = alloc_ctx;
	unsigned long flags;

	if (!conf->use_mmu)
		return;

	spin_lock_irqsave(&conf->slock, flags);
	if (!atomic_read(&conf->mmu_enable)) {
		pr_warning("Already suspend: device(%x)\n", (u32)conf->dev);
		return;
	}

	atomic_dec(&conf->mmu_enable);
	iovmm_deactivate(conf->dev);
	spin_unlock_irqrestore(&conf->slock, flags);

}

void vb2_ion_resume(void *alloc_ctx)
{
	struct vb2_ion_conf *conf = alloc_ctx;
	int ret;
	unsigned long flags;

	if (!conf->use_mmu)
		return;

	spin_lock_irqsave(&conf->slock, flags);
	if (atomic_read(&conf->mmu_enable)) {
		pr_warning("Already resume: device(%x)\n", (u32)conf->dev);
		return;
	}

	atomic_inc(&conf->mmu_enable);
	ret = iovmm_activate(conf->dev);
	if (ret) {
		pr_err("iovmm_activate: dev(%x)\n", (u32)conf->dev);
		atomic_dec(&conf->mmu_enable);
	}
	spin_unlock_irqrestore(&conf->slock, flags);
}

MODULE_AUTHOR("Jonghun,	Han <jonghun.han@samsung.com>");
MODULE_DESCRIPTION("Android ION allocator handling routines for videobuf2");
MODULE_LICENSE("GPL");