1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/media/platform/samsung/s5p-mfc/s5p_mfc_opr.c
4  *
5  * Samsung MFC (Multi Function Codec - FIMV) driver
6  * This file contains hw related functions.
7  *
8  * Kamil Debski, Copyright (c) 2012 Samsung Electronics Co., Ltd.
9  * http://www.samsung.com/
10  */
11 
12 #include "s5p_mfc_debug.h"
13 #include "s5p_mfc_opr.h"
14 #include "s5p_mfc_opr_v5.h"
15 #include "s5p_mfc_opr_v6.h"
16 
17 static struct s5p_mfc_hw_ops *s5p_mfc_ops;
18 
19 void s5p_mfc_init_hw_ops(struct s5p_mfc_dev *dev)
20 {
21 	if (IS_MFCV6_PLUS(dev)) {
22 		s5p_mfc_ops = s5p_mfc_init_hw_ops_v6();
23 		dev->warn_start = S5P_FIMV_ERR_WARNINGS_START_V6;
24 	} else {
25 		s5p_mfc_ops = s5p_mfc_init_hw_ops_v5();
26 		dev->warn_start = S5P_FIMV_ERR_WARNINGS_START;
27 	}
28 	dev->mfc_ops = s5p_mfc_ops;
29 }
30 
31 void s5p_mfc_init_regs(struct s5p_mfc_dev *dev)
32 {
33 	if (IS_MFCV6_PLUS(dev))
34 		dev->mfc_regs = s5p_mfc_init_regs_v6_plus(dev);
35 }
36 
37 int s5p_mfc_alloc_priv_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx,
38 			   struct s5p_mfc_priv_buf *b)
39 {
40 	unsigned int bits = dev->mem_size >> PAGE_SHIFT;
41 	unsigned int count = b->size >> PAGE_SHIFT;
42 	unsigned int align = (SZ_64K >> PAGE_SHIFT) - 1;
43 	unsigned int start, offset;
44 
45 	mfc_debug(3, "Allocating priv: %zu\n", b->size);
46 
47 	if (dev->mem_virt) {
48 		start = bitmap_find_next_zero_area(dev->mem_bitmap, bits, 0, count, align);
49 		if (start > bits)
50 			goto no_mem;
51 
52 		bitmap_set(dev->mem_bitmap, start, count);
53 		offset = start << PAGE_SHIFT;
54 		b->virt = dev->mem_virt + offset;
55 		b->dma = dev->mem_base + offset;
56 	} else {
57 		struct device *mem_dev = dev->mem_dev[mem_ctx];
58 		dma_addr_t base = dev->dma_base[mem_ctx];
59 
60 		b->ctx = mem_ctx;
61 		b->virt = dma_alloc_coherent(mem_dev, b->size, &b->dma, GFP_KERNEL);
62 		if (!b->virt)
63 			goto no_mem;
64 		if (b->dma < base) {
65 			mfc_err("Invalid memory configuration - buffer (%pad) is below base memory address(%pad)\n",
66 				&b->dma, &base);
67 			dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
68 			return -ENOMEM;
69 		}
70 	}
71 
72 	mfc_debug(3, "Allocated addr %p %pad\n", b->virt, &b->dma);
73 	return 0;
74 no_mem:
75 	mfc_err("Allocating private buffer of size %zu failed\n", b->size);
76 	return -ENOMEM;
77 }
78 
79 int s5p_mfc_alloc_generic_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx,
80 			   struct s5p_mfc_priv_buf *b)
81 {
82 	struct device *mem_dev = dev->mem_dev[mem_ctx];
83 
84 	mfc_debug(3, "Allocating generic buf: %zu\n", b->size);
85 
86 	b->ctx = mem_ctx;
87 	b->virt = dma_alloc_coherent(mem_dev, b->size, &b->dma, GFP_KERNEL);
88 	if (!b->virt)
89 		goto no_mem;
90 
91 	mfc_debug(3, "Allocated addr %p %pad\n", b->virt, &b->dma);
92 	return 0;
93 no_mem:
94 	mfc_err("Allocating generic buffer of size %zu failed\n", b->size);
95 	return -ENOMEM;
96 }
97 
98 void s5p_mfc_release_priv_buf(struct s5p_mfc_dev *dev,
99 			      struct s5p_mfc_priv_buf *b)
100 {
101 	if (dev->mem_virt) {
102 		unsigned int start = (b->dma - dev->mem_base) >> PAGE_SHIFT;
103 		unsigned int count = b->size >> PAGE_SHIFT;
104 
105 		bitmap_clear(dev->mem_bitmap, start, count);
106 	} else {
107 		struct device *mem_dev = dev->mem_dev[b->ctx];
108 
109 		dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
110 	}
111 	b->virt = NULL;
112 	b->dma = 0;
113 	b->size = 0;
114 }
115 
116 void s5p_mfc_release_generic_buf(struct s5p_mfc_dev *dev,
117 			      struct s5p_mfc_priv_buf *b)
118 {
119 	struct device *mem_dev = dev->mem_dev[b->ctx];
120 	dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
121 	b->virt = NULL;
122 	b->dma = 0;
123 	b->size = 0;
124 }
125