summaryrefstree
path: root/kernel/drivers/vision/iva/iva_mem.c
blob: 769132c1ad1508584784bcf59b978ddaf7d88edb (plain)
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
/* iva_mem.c
 *
 * Copyright (C) 2016 Samsung Electronics Co.Ltd
 * Authors:
 *	Ilkwan Kim <ilkwan.kim@samsung.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 */

#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/time.h>
#include <linux/mm.h>
#include <linux/dma-buf.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <asm/cacheflush.h>

#include "iva_mem.h"
#include "iva_mem_sync.h"

#define CALL_SHOW_PROC_MAP_LIST
#ifdef CONFIG_ION_EXYNOS
#include <linux/exynos_ion.h>
#define REQ_ION_HEAP_ID		EXYNOS_ION_HEAP_SYSTEM_MASK
#else
#define REQ_ION_HEAP_ID		ZONE_BIT(ION_HEAP_TYPE_EXT)
extern struct ion_device *idev; /* CONFIG_ION_DUMMY */
#endif

//#define ENABLE_MAP_ATTACHMENT
static void __iva_mem_map_destroy(struct kref *kref)
{
	struct iva_mem_map 	*iva_map_node =
			container_of(kref, struct iva_mem_map, map_ref);
	struct dma_buf		*dmabuf;
	struct device 		*dev = iva_map_node->dev;

	dmabuf = iva_map_node->dmabuf;
	if (!dmabuf) {
		dev_err(dev, "%s() dmabuf is null, buf_fd(%d)\n",
		       __func__, iva_map_node->shared_fd);
	}

	dev_dbg(dev, "%s() buf_fd(%d) iova(0x%lx), size(0x%x, 0x%x), "
			"attachment(%p)\n", __func__,
				iva_map_node->shared_fd,
				iva_map_node->io_va, iva_map_node->req_size,
				iva_map_node->act_size,
				iva_map_node->attachment);
#ifdef CONFIG_ION_EXYNOS
	if (iva_map_node->attachment)
		ion_iovmm_unmap(iva_map_node->attachment, iva_map_node->io_va);
#endif
	if (iva_map_node->sg_table) {
		dma_buf_unmap_attachment(iva_map_node->attachment,
				iva_map_node->sg_table, DMA_BIDIRECTIONAL);
		iva_map_node->sg_table = NULL;
	}

	if (iva_map_node->attachment) {
		dma_buf_detach(dmabuf, iva_map_node->attachment);
		iva_map_node->attachment = NULL;
	}
}

static inline void iva_mem_map_get_refcnt(struct iva_mem_map *iva_map)
{
	kref_get(&iva_map->map_ref);
}

static inline int iva_mem_map_put_refcnt(struct iva_mem_map *iva_map)
{
	return kref_put(&iva_map->map_ref, __iva_mem_map_destroy);
}

int iva_mem_map_read_refcnt(struct iva_mem_map *iva_map)
{
	return atomic_read(&iva_map->map_ref.refcount);
}


void iva_mem_show_global_mapped_list_nolock(struct iva_dev_data *iva)
{
	struct device		*dev = iva->dev;
	struct list_head	*work_node;
	struct iva_mem_map	*iva_map_node;
	int			i = 0;

	dev_info(dev, "------------- show iva mapped list (%4d)----------\n",
			iva->map_nr);
	list_for_each(work_node, &iva->mem_map_list) {
		iva_map_node = list_entry(work_node, struct iva_mem_map, node);
		dev_info(dev, "[%d] buf_fd:%04d(%ld),\tflags:0x%x,\tio_va:0x%08lx,"
				"\tsize:0x%08x, 0x%08x,\tref_cnt:%d\n", i,
				iva_map_node->shared_fd,
				file_count(iva_map_node->dmabuf->file),
				iva_map_node->flags, iva_map_node->io_va,
				iva_map_node->req_size,
				iva_map_node->act_size,
				iva_mem_map_read_refcnt(iva_map_node));
		i++;
	}
	dev_info(dev, "---------------------------------------------------\n");

	return;
}


static void iva_mem_show_global_mapped_list(struct iva_dev_data *iva)
{
	mutex_lock(&iva->mem_map_lock);
	if (list_empty(&iva->mem_map_list)) {
		mutex_unlock(&iva->mem_map_lock);
		return;
	}

	iva_mem_show_global_mapped_list_nolock(iva);

	mutex_unlock(&iva->mem_map_lock);

	return;
}

static int __maybe_unused iva_mem_show_proc_mapped_list_nolock(
		struct iva_proc *proc)
{
	struct iva_mem_map	*iva_map_node;
	struct iva_dev_data	*iva = proc->iva_data;
	struct device		*dev = iva->dev;
	int			i, j = 0;

	if (hash_empty(proc->h_mem_map))
		return 0;

	/* non empty case */
	dev_info(dev, "--------------- show proc mapped list --------------\n");

	hash_for_each(proc->h_mem_map, i, iva_map_node, h_node) {
		dev_info(dev, "[%03d, %d] buf_fd:%04d(%ld),\tflags:0x%x,"
				"\tio_va:0x%08lx,"
				"\tsize:0x%08x, 0x%08x,\tref_cnt:%d\n", i, j,
				iva_map_node->shared_fd,
				file_count(iva_map_node->dmabuf->file),
				iva_map_node->flags, iva_map_node->io_va,
				iva_map_node->req_size,
				iva_map_node->act_size,
				iva_mem_map_read_refcnt(iva_map_node));
		j++;
	}
	dev_info(dev, "---------------------------------------------------\n");

	return j;
}

static struct iva_mem_map *iva_mem_alloc_map_node(struct iva_dev_data *iva)
{
	if (likely(iva->map_node_cache))
		return (struct iva_mem_map *) kmem_cache_alloc(
				iva->map_node_cache, GFP_KERNEL);
	else
		return (struct iva_mem_map *) devm_kmalloc(iva->dev,
				sizeof(struct iva_mem_map), GFP_KERNEL);
}

static void iva_mem_free_map_node(struct iva_dev_data *iva,
			struct iva_mem_map *map_node)
{
	if (likely(iva->map_node_cache))
		kmem_cache_free(iva->map_node_cache, (void *) map_node);
	else
		devm_kfree(iva->dev, map_node);
}


static struct iva_mem_map *iva_mem_find_proc_map_with_fd(struct iva_proc *proc,
			int shared_fd)
{
	struct iva_dev_data	*iva = proc->iva_data;
	struct iva_mem_map      *iva_map_node;

	mutex_lock(&iva->mem_map_lock);
	hash_for_each_possible(proc->h_mem_map, iva_map_node,
			h_node, shared_fd){
		if (iva_map_node->shared_fd == shared_fd) {
			mutex_unlock(&iva->mem_map_lock);
			return iva_map_node;
		}

	}
	mutex_unlock(&iva->mem_map_lock);
	return NULL;
}

static struct iva_mem_map *iva_mem_ion_alloc(struct iva_proc *proc,
			uint32_t size, uint32_t align, uint32_t cacheflag)
{
#define ZONE_BIT(zone)		(1 << zone)
	int			ion_shared_fd;
	struct iva_dev_data 	*iva = proc->iva_data;
	struct ion_client	*client = iva->ion_client;
	struct device		*dev = iva->dev;
	struct ion_handle	*handle;
	struct iva_mem_map 	*iva_map_node;
	struct dma_buf		*dmabuf;

	iva_map_node = iva_mem_alloc_map_node(iva);
	if (!iva_map_node) {
		dev_err(dev, "%s() fail to alloc mem for map.\n", __func__);
		return NULL;
	}

	handle = ion_alloc(client, size, align, REQ_ION_HEAP_ID, cacheflag);
	if (IS_ERR(handle)) {
		dev_err(dev, "%s() fail to alloc ion with id(0x%x), "
				"size(0x%x), align(0x%x), ret(%ld)\n",
				__func__, REQ_ION_HEAP_ID,
				size, align, PTR_ERR(handle));
		goto err_alloc_ion;
	}

	dmabuf = ion_share_dma_buf(client, handle);
	if (!dmabuf) {
		dev_err(dev, "%s() ion with dma_buf sharing failed.\n",
				__func__);
		goto err_dmabuf;
	}

	/* close() should be called for full release operation*/
	ion_shared_fd = dma_buf_fd(dmabuf, O_CLOEXEC);
	if (ion_shared_fd < 0) {
		dev_err(dev, "%s() ion with getting dma_buf_fd failed.\n",
				__func__);
		goto err_dmabuf_fd;
	}

	iva_map_node->shared_fd	= ion_shared_fd;
	iva_map_node->handle	= handle;
	iva_map_node->dmabuf	= dmabuf;
	/* increase file cnt. iva_mem_ion_free() required */
	get_dma_buf(dmabuf);
	iva_map_node->attachment= NULL;
	iva_map_node->sg_table	= NULL;
	iva_map_node->io_va	= 0x0;
	iva_map_node->req_size	= size;
	iva_map_node->act_size	= (uint32_t) dmabuf->size;

	/* data for managing */
	iva_map_node->flags	= 0x0;
	SET_IVA_ALLOC_TYPE(iva_map_node->flags, IVA_ALLOC_TYPE_ALLOCATED);
	SET_IVA_CACHE_TYPE(iva_map_node->flags, cacheflag);
	atomic_set(&iva_map_node->map_ref.refcount, 0);
	iva_map_node->dev	= dev;

	/* add to global list */
	mutex_lock(&iva->mem_map_lock);
	list_add(&iva_map_node->node, &iva->mem_map_list);
	iva->map_nr++;
	hash_add(proc->h_mem_map, &iva_map_node->h_node, ion_shared_fd);
	mutex_unlock(&iva->mem_map_lock);

	dev_dbg(dev, "%s() succeed : size(0x%x, 0x%x) align(0x%x) handle(%p) "
			"dma_buf(%p) with buf fd(%d, %ld)\n", __func__,
			iva_map_node->req_size, iva_map_node->act_size,
			align, handle, dmabuf,
			ion_shared_fd, file_count(dmabuf->file));

	return iva_map_node;

	/* handle.ref = 1, buffer.ref = 2(dma_buf, ion_buffer),
	 * buffer.handle_count = 1, dma_buf = exported, dma_buf.ref = 1
	 * dmabuf, ion_buffer_fd, iva_map_node are filled */
err_dmabuf_fd:
	dma_buf_put(dmabuf);
err_dmabuf:
	ion_free(client, handle);
err_alloc_ion:
	iva_mem_free_map_node(iva, iva_map_node);
	return NULL;
}


static int iva_mem_ion_free(struct iva_proc *proc,
			struct iva_mem_map *iva_map_node)
{
	int 			buf_fd = iva_map_node->shared_fd;
	struct iva_dev_data 	*iva = proc->iva_data;
	struct ion_client	*client = iva->ion_client;
	struct device		*dev = iva->dev;
	int			ref_cnt;

	/* in case that ion mem was allocated outside */
	if (!ALLOC_INSIDE(iva_map_node->flags)) {
		dev_warn(dev, "%s() fd(%d) alloced outside. flags(0x%x)\n",
				__func__, buf_fd, iva_map_node->flags);
		return -EINVAL;
	}

	/* still iova mapped */
	ref_cnt = iva_mem_map_read_refcnt(iva_map_node);
	if (ref_cnt) {
		iva_map_node->flags |= IVA_FREE_REQUESTED;
		dev_dbg(dev, "%s() fd(%d) still mapped, ref_cnt(0x%x), "
				"flags(0x%x)\n", __func__,
				buf_fd, ref_cnt, iva_map_node->flags);
		return 0;
	}

	dma_buf_put(iva_map_node->dmabuf);	/* ion_share_dma_buf */

	/* close() should be done in user space */
	ion_free(client, iva_map_node->handle); /* handle-1, ion_buffer-1 */

	mutex_lock(&iva->mem_map_lock);
	hash_del(&iva_map_node->h_node);
	list_del(&iva_map_node->node);
	iva->map_nr--;
	mutex_unlock(&iva->mem_map_lock);

	iva_mem_free_map_node(iva, iva_map_node);

	return 0;
}

static int iva_mem_get_ion_iova(struct iva_proc *proc,
			struct iva_mem_map *iva_map_node)
{
	int 			ret = 0;
	struct iva_dev_data	*iva = proc->iva_data;
	struct dma_buf		*dmabuf;
	struct sg_table		*sg_table = NULL;
	struct device		*dev = iva->dev;
	struct ion_handle 	*handle = NULL;
	struct dma_buf_attachment *attachment;
	int			map_ref_cnt;
#ifdef CONFIG_ION_EXYNOS
	dma_addr_t		io_va;
#else
	ion_phys_addr_t 	io_va;
	size_t			io_va_len;
#endif
	map_ref_cnt = iva_mem_map_read_refcnt(iva_map_node);
	if (map_ref_cnt) {	/* already mapped: ref_cnt > 0 */
		dev_dbg(dev, "%s() already mapped: "
			"fd(%d), iova(%lx), size(0x%x, 0x%x), ref_cnt(%d)",
			__func__, iva_map_node->shared_fd,
			iva_map_node->io_va,
			iva_map_node->req_size, iva_map_node->act_size,
			map_ref_cnt);
		iva_mem_map_get_refcnt(iva_map_node);
		return 0;
	}

	dmabuf = iva_map_node->dmabuf;
	if (!dmabuf) {
		dev_err(dev, "%s() fail to get dmabuf from list.\n",
				__func__);
		return -EINVAL;
	}

	handle = iva_map_node->handle;
	if (IS_ERR(handle)) {
		dev_err(dev, "%s() node has no handle, ret(%ld)\n",
				__func__, PTR_ERR(handle));
		return PTR_ERR(handle);
	}

	attachment = dma_buf_attach(dmabuf, dev);
	if (!attachment) {
		dev_err(dev, "%s() fail to attach dma buf, buf_fd(%d).\n",
				__func__, iva_map_node->shared_fd);
		goto err_attach;
	}

#ifdef ENABLE_MAP_ATTACHMENT	/* for showing */
	sg_table = dma_buf_map_attachment(attachment, DMA_BIDIRECTIONAL);
	if (!sg_table) {
		dev_err(dev, "%s() fail to map attachment(%p)\n",
				__func__, attachment);
		goto err_map_attach;
	}
#endif

	/* try to map io */
#ifdef CONFIG_ION_EXYNOS
	io_va = ion_iovmm_map(attachment, 0,
			iva_map_node->act_size, DMA_BIDIRECTIONAL, 0);
	if (IS_ERR_VALUE(io_va)) {
		ret = (int) io_va;
		dev_err(dev, "%s() fail to map iovm (%d)\n", __func__, ret);
		goto err_get_iova;
	}
#else
	/* CAUTION: io_va is filled with a physical addr */
	ret = ion_phys(iva->ion_client, handle, &io_va, &io_va_len);
	if (ret) {
		dev_err(dev, "%s() ion_phys() returns failure\n",
		       __func__);
		ret = -EFAULT;
		goto err_get_iova;
	}
#endif
	/* success */
	iva_map_node->io_va	= (unsigned long) io_va;
	iva_map_node->attachment= attachment;
	iva_map_node->sg_table	= sg_table;
	kref_init(&iva_map_node->map_ref);

	dev_dbg(dev, "%s() buf_fd(%d, %ld) attachment(%p) "
			"iova(0x%lx), size(0x%x, 0x%x), ref_cnt(%d)\n",
			__func__, iva_map_node->shared_fd,
			file_count(dmabuf->file), iva_map_node->attachment,
			iva_map_node->io_va,
			iva_map_node->req_size, iva_map_node->act_size,
			iva_mem_map_read_refcnt(iva_map_node));
	return 0;

err_get_iova:
#ifdef ENABLE_MAP_ATTACHMENT
	dma_buf_unmap_attachment(attachment,
				sg_table, DMA_BIDIRECTIONAL);
err_map_attach:
#endif
	dma_buf_detach(dmabuf, attachment);
err_attach:
	return ret;
}

static int iva_mem_put_ion_iova(struct iva_proc *proc,
		struct iva_mem_map *iva_map_node, bool forced_put)
{
	struct iva_dev_data	*iva = proc->iva_data;
	struct device		*dev = iva->dev;

	dev_dbg(dev, "%s() ref_cnt: %d, fd(%d, %ld)\n", __func__,
			iva_mem_map_read_refcnt(iva_map_node),
			iva_map_node->shared_fd,
			file_count(iva_map_node->dmabuf->file));

	if (forced_put) {
		/* forced to unmap iova */
		__iva_mem_map_destroy(&iva_map_node->map_ref);
		atomic_set(&iva_map_node->map_ref.refcount, 0);
		return 0;
	}

	/* decrease ref_cnt of iva_map_node and release */
	if (iva_mem_map_put_refcnt(iva_map_node)) {
		/* removed iova map successfully */
		dev_dbg(dev, "%s() fd(%d, %ld) iova unmaped, "
				"flags(0x%x)\n",
				__func__,
				iva_map_node->shared_fd,
				file_count(iva_map_node->dmabuf->file),
				iva_map_node->flags);
		return 0;
	}

	/* return ref count */
	return iva_mem_map_read_refcnt(iva_map_node);
}

static int iva_mem_ion_sync_for_cpu(struct iva_dev_data *iva,
		struct iva_mem_map *iva_map_node)
{
	struct device		*dev = iva->dev;
	struct dma_buf		*dmabuf;

	dmabuf = iva_map_node->dmabuf;
	if (!dmabuf) {
		dev_err(dev, "%s() dmabuf is null, buf_fd(%d)\n",
		       __func__, iva_map_node->shared_fd);
		return -EINVAL;
	}

	mutex_lock(&iva->mem_map_lock);
#ifdef CONFIG_ION_EXYNOS
	exynos_ion_sync_dmabuf_for_cpu(dev, dmabuf,
			iva_map_node->act_size,	DMA_BIDIRECTIONAL);

#else
	iva_ion_sync_sg_for_cpu(dev, iva, iva_map_node);

#endif
	mutex_unlock(&iva->mem_map_lock);
	return 0;
}

static int iva_mem_ion_sync_for_device(struct iva_dev_data *iva,
		struct iva_mem_map *iva_map_node)
{
	struct device		*dev = iva->dev;
	struct dma_buf		*dmabuf = NULL;

	if (iva_map_node) {
		dmabuf = iva_map_node->dmabuf;
		if (!dmabuf) {
			dev_err(dev, "%s() dmabuf is null, buf_fd(%d)\n",
		       			__func__, iva_map_node->shared_fd);
			return -EINVAL;
		}

		mutex_lock(&iva->mem_map_lock);
	#ifdef CONFIG_ION_EXYNOS
		exynos_ion_flush_dmabuf_for_device(dev,
				dmabuf, iva_map_node->act_size);
	#else
		iva_ion_sync_sg_for_device(dev, iva, iva_map_node);
	#endif
		mutex_unlock(&iva->mem_map_lock);
	} else {/* else: sync_all */
	#ifdef CONFIG_ION_EXYNOS
		flush_all_cpu_caches();
	#else
		flush_cache_all();
	#endif
	}
	return 0;
}

/* import the buffer created outside */
static struct iva_mem_map *iva_mem_import_ion_buf(struct iva_proc *proc,
		int import_fd)
{
	struct iva_dev_data 	*iva = proc->iva_data;
	struct device		*dev = iva->dev;
	struct iva_mem_map	*iva_map_node;
	struct dma_buf		*dmabuf;
	struct ion_handle	*handle;

	iva_map_node = iva_mem_alloc_map_node(iva);
	if (!iva_map_node) {
		dev_err(dev, "%s() fail to alloc mem for map.\n",
				__func__);
		return NULL;
	}

	dmabuf = dma_buf_get(import_fd);
	if (IS_ERR_OR_NULL(dmabuf)) {
		dev_err(dev, "%s() fail to get dmabuf from "
				"fd(%d)\n", __func__, import_fd);
		goto err_buf_get;
	}

	/* create handle in space of our client */
	handle = ion_import_dma_buf(iva->ion_client, import_fd);
	if (IS_ERR(handle)) {
		dev_err(dev, "%s() fail to import handle with fd"
				"(%d), ret(%ld)\n",
				__func__, import_fd,
				PTR_ERR(handle));
		goto err_buf_import;
	}

	iva_map_node->shared_fd = import_fd;
	iva_map_node->handle	= handle;
	iva_map_node->dmabuf	= dmabuf;
	iva_map_node->attachment= NULL;
	iva_map_node->sg_table	= NULL;
	iva_map_node->io_va	= 0x0;
	iva_map_node->req_size	= 0;	/* not alloced inside */
	iva_map_node->act_size	= (uint32_t) dmabuf->size;

	iva_map_node->flags	= 0x0;
	SET_IVA_ALLOC_TYPE(iva_map_node->flags, IVA_ALLOC_TYPE_IMPORTED);
	atomic_set(&iva_map_node->map_ref.refcount, 0);
	iva_map_node->dev	= dev;

	mutex_lock(&iva->mem_map_lock);
	list_add(&iva_map_node->node, &iva->mem_map_list);
	iva->map_nr++;
	hash_add(proc->h_mem_map, &iva_map_node->h_node, import_fd);
	mutex_unlock(&iva->mem_map_lock);

	return iva_map_node;

err_buf_import:
	dma_buf_put(dmabuf);
err_buf_get:
	iva_mem_free_map_node(iva, iva_map_node);
	return NULL;
}

static void iva_mem_cancel_imported_ion_buf(struct iva_proc *proc,
		struct iva_mem_map *iva_map_node)
{
	struct iva_dev_data	*iva = proc->iva_data;
	struct device		*dev = iva->dev;
	int			ref_cnt;

	ref_cnt = iva_mem_map_read_refcnt(iva_map_node);
	if (ref_cnt) {
		dev_dbg(dev, "%s() fd(%d) still mapped, ref_cnt(0x%x), "
				"flags(0x%x)\n", __func__,
				iva_map_node->shared_fd, ref_cnt,
				iva_map_node->flags);
		return;
	}

	dev_dbg(dev, "%s() will release mem with outside buf_fd(%d)\n",
			__func__, iva_map_node->shared_fd);
	if (iva_map_node->dmabuf)	/* ion_import_dma_buf */
		dma_buf_put(iva_map_node->dmabuf);

	ion_free(iva->ion_client, iva_map_node->handle);/* destroy handle */

	mutex_lock(&iva->mem_map_lock);
	hash_del(&iva_map_node->h_node);
	list_del(&iva_map_node->node);		/* pop shared node from list */
	iva->map_nr--;
	mutex_unlock(&iva->mem_map_lock);

	iva_mem_free_map_node(iva, iva_map_node);
	return;
}

static int iva_mem_ion_alloc_param(struct iva_proc *proc,
			struct iva_ion_param *ion_param)
{
#define ZONE_BIT(zone)		(1 << zone)
	struct iva_dev_data	*iva = proc->iva_data;
	struct device		*dev = iva->dev;
	struct iva_mem_map 	*iva_map_node;

	iva_map_node = iva_mem_ion_alloc(proc,
			ion_param->size, ion_param->align, ion_param->cacheflag);
	if (!iva_map_node) {
		dev_err(dev, "%s() fail to alloc ion/ion fd\n", __func__);
		return -ENOMEM;
	}

	ion_param->shared_fd	= iva_map_node->shared_fd;
	ion_param->iova		= (unsigned int) iva_map_node->io_va;
	return 0;

}

static int iva_mem_ion_free_param(struct iva_proc *proc,
			struct iva_ion_param *ion_param)
{
	int 			ret = 0;
	struct iva_mem_map 	*iva_map_node;
	struct iva_dev_data	*iva = proc->iva_data;
	struct device		*dev = iva->dev;

	mutex_lock(&proc->proc_mem_lock);
	iva_map_node = iva_mem_find_proc_map_with_fd(proc, ion_param->shared_fd);
	if (!iva_map_node) {
		mutex_unlock(&proc->proc_mem_lock);
		dev_err(dev, "%s() shared_fd(%d) is not found in list !!!\n",
				__func__, ion_param->shared_fd);
		return -EINVAL;
	}

	ret = iva_mem_ion_free(proc, iva_map_node);
	if (ret) {
		dev_warn(dev, "%s() shared_fd(%d) alloced outside.\n",
				__func__, ion_param->shared_fd);
		mutex_unlock(&proc->proc_mem_lock);
		return -EINVAL;
	}
	mutex_unlock(&proc->proc_mem_lock);
	return 0;
}


void iva_mem_force_to_free_proc_mapped_list(struct iva_proc *proc)
{
	struct hlist_node	*tmp;
	struct iva_mem_map	*iva_map_node;
	struct iva_dev_data	*iva = proc->iva_data;
	int			i;
#ifdef CALL_SHOW_PROC_MAP_LIST
	struct device		*dev = iva->dev;
	int			j = 0;
#else
	int			ret;

	ret = iva_mem_show_proc_mapped_list_nolock(proc);
	if (ret == 0)	/* nothing to do */
		return;
#endif

	hash_for_each_safe(proc->h_mem_map, i, tmp, iva_map_node, h_node) {
	#ifdef CALL_SHOW_PROC_MAP_LIST
		dev_info(dev, "FORCE FREE [%3d, %3d] fd:%04d(%ld) flags(0x%x) "
				"io_va(0x%08lx) "
				"size(0x%08x, 0x%08x) ref_cnt(%d)\n", i, j,
				iva_map_node->shared_fd,
				file_count(iva_map_node->dmabuf->file),
				iva_map_node->flags, iva_map_node->io_va,
				iva_map_node->req_size,
				iva_map_node->act_size,
				iva_mem_map_read_refcnt(iva_map_node));
	#endif
		/* mapping : get_iova? */
		if (iva_mem_map_read_refcnt(iva_map_node) > 0)
			iva_mem_put_ion_iova(proc, iva_map_node, true);

		/* release memory also iva_mem_map */
		if (ALLOC_INSIDE(iva_map_node->flags))
			iva_mem_ion_free(proc, iva_map_node);
		else
			iva_mem_cancel_imported_ion_buf(proc, iva_map_node);
	#ifdef CALL_SHOW_PROC_MAP_LIST
		j++;
	#endif
	}

	return;
}

static int iva_mem_get_ion_iova_param(struct iva_proc *proc,
			struct iva_ion_param *ion_param)
{
	int	ret;
	struct iva_mem_map	*iva_map_node;
	bool			new_map = false;	/* from outside */

	iva_map_node = iva_mem_find_proc_map_with_fd(proc, ion_param->shared_fd);
	if (!iva_map_node) {	/* buf fd from outside */
		iva_map_node = iva_mem_import_ion_buf(proc, ion_param->shared_fd);
		if (!iva_map_node)
			return -EINVAL;
		new_map = true;
	}

	ret = iva_mem_get_ion_iova(proc, iva_map_node);
	if (ret < 0) {
		if (new_map)
			iva_mem_cancel_imported_ion_buf(proc, iva_map_node);

		return ret;
	}

	ion_param->iova = (unsigned int) iva_map_node->io_va;
	ion_param->size = iva_map_node->act_size;

	return 0;
}

static int iva_mem_put_ion_iova_param(struct iva_proc *proc,
 			struct iva_ion_param *ion_param)
{
	int	ret;
	struct iva_mem_map	*iva_map_node;
	struct iva_dev_data 	*iva = proc->iva_data;
	struct device		*dev = iva->dev;

	iva_map_node = iva_mem_find_proc_map_with_fd(proc, ion_param->shared_fd);
	if (!iva_map_node) {
		dev_warn(dev, "%s() fail to get map node with buf fd(%d)\n",
				__func__, ion_param->shared_fd);
		return -EINVAL;
	}

	ret = iva_mem_put_ion_iova(proc, iva_map_node, false);
	if (!ret) {	/* success to iova unmap */
		if (ALLOC_OUTSIDE(iva_map_node->flags))	{
			/* if exported fd */
			iva_mem_cancel_imported_ion_buf(proc, iva_map_node);
		} else if (FREE_REQUESTED(iva_map_node->flags)) {
			/* already free is called and inside allocation */
			iva_mem_ion_free(proc, iva_map_node);
		}
	}

	return ret;
}

static int iva_mem_ion_sync_for_cpu_param(struct iva_proc *proc,
		struct iva_ion_param *ion_param)
{
	int	ret;
	struct iva_mem_map	*iva_map_node;
	struct iva_dev_data 	*iva = proc->iva_data;
	struct device		*dev = iva->dev;

	iva_map_node = iva_mem_find_proc_map_with_fd(proc, ion_param->shared_fd);
	if (!iva_map_node) {
		dev_warn(dev, "%s() fail to get map node with buf fd(%d)\n",
				__func__, ion_param->shared_fd);
		return -EINVAL;
	}

	ret = iva_mem_ion_sync_for_cpu(proc->iva_data, iva_map_node);

	return ret;
}

static int iva_mem_ion_sync_for_device_param(struct iva_proc *proc,
		struct iva_ion_param *ion_param)
{
	struct iva_mem_map	*iva_map_node;
	struct iva_dev_data 	*iva = proc->iva_data;
	struct device		*dev = iva->dev;

	if (ion_param->shared_fd < 0)
		return -EINVAL;
	else if (ion_param->shared_fd == 0)	/* special purpose */
		iva_map_node = NULL;
	else {
		iva_map_node = iva_mem_find_proc_map_with_fd(proc,
				ion_param->shared_fd);
		if (!iva_map_node) {
			dev_warn(dev, "%s() fail to get map node with "
					"buf fd(%d)\n",	__func__,
					ion_param->shared_fd);
			return -EINVAL;
		}
	}

	return iva_mem_ion_sync_for_device(proc->iva_data, iva_map_node);
}

long iva_mem_ioctl(struct iva_proc *proc, unsigned int cmd, void __user *p)
{
	struct iva_ion_param	iva_param;
	struct device		*dev = proc->iva_data->dev;
	long			ret;

	/* copy from user routine */
	if (_IOC_DIR(cmd) & _IOC_WRITE) {
		ret = copy_from_user(&iva_param, p, sizeof(iva_param));
		if (ret) {
			dev_err(dev, "%s() fail to copy_from_user, %d\n",
					__func__, (int) ret);
			return ret;
		}
	}

	switch (cmd) {
	case IVA_ION_GET_IOVA:
		ret = iva_mem_get_ion_iova_param(proc, &iva_param);
		break;

	case IVA_ION_PUT_IOVA:
		ret = iva_mem_put_ion_iova_param(proc, &iva_param);
		break;

	case IVA_ION_ALLOC:
		ret = iva_mem_ion_alloc_param(proc, &iva_param);
		break;

	case IVA_ION_FREE: /* This may not be used */
		ret = iva_mem_ion_free_param(proc, &iva_param);
		break;

	case IVA_ION_SYNC_FOR_CPU:
		ret = iva_mem_ion_sync_for_cpu_param(proc, &iva_param);
		break;

	case IVA_ION_SYNC_FOR_DEVICE:
		ret = iva_mem_ion_sync_for_device_param(proc, &iva_param);
		break;

	default:
		return -ENOTTY;
	}

	/* copy to user routine */
	if (ret >= 0 && _IOC_DIR(cmd) & _IOC_READ) {
		ret = copy_to_user((void __user *)p,
				&iva_param, sizeof(iva_param));
		if (ret) {
			dev_err(dev, "%s() fail to copy to user, %d\n",
				__func__, (int) ret);
		}
	}

	return ret;
}


int iva_mem_create_ion_client(struct iva_dev_data *iva)
{
#define ION_IVA_CLIENT_NAME	"iva_ion"
	struct device		*dev = iva->dev;
	struct ion_client	*client;

	/* initialize internal data structure */
	mutex_init(&iva->mem_map_lock);
	/* these are not unnecessary, indeeed */
	mutex_lock(&iva->mem_map_lock);
	INIT_LIST_HEAD(&iva->mem_map_list);
	iva->map_nr = 0;
	mutex_unlock(&iva->mem_map_lock);
#ifdef CONFIG_ION_EXYNOS
	client = exynos_ion_client_create(ION_IVA_CLIENT_NAME);
#else
	iva->ion_dev = idev;
	client = ion_client_create(iva->ion_dev, ION_IVA_CLIENT_NAME);
#endif
	if (IS_ERR(client)) {
		dev_err(dev, "%s(%d) ion client creation is failed..\n",
				__func__, __LINE__);
		return -EINVAL;
	}

	dev_dbg(dev, "%s(%d) ion device is created with client %p\n",
				__func__, __LINE__, client);
	iva->ion_client = client;

	/* panic if kmem cache is not created */
	iva->map_node_cache = kmem_cache_create("iva_mem",
				sizeof(struct iva_mem_map),
				0, SLAB_PANIC, NULL);
	return 0;
}

int iva_mem_destroy_ion_client(struct iva_dev_data *iva)
{
	iva_mem_show_global_mapped_list(iva);

	kmem_cache_destroy(iva->map_node_cache);

	if (iva->ion_client) {
		ion_client_destroy(iva->ion_client);
		iva->ion_client = NULL;
	}
#ifndef CONFIG_ION_EXYNOS
	iva->ion_dev = NULL;
#endif

	mutex_destroy(&iva->mem_map_lock);
	return 0;
}