1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
3 #include <linux/dma-mapping.h>
4 #include <linux/module.h>
5 #include <linux/slab.h>
8 #define HISI_ACC_SGL_SGE_NR_MIN 1
9 #define HISI_ACC_SGL_NR_MAX 256
10 #define HISI_ACC_SGL_ALIGN_SIZE 64
11 #define HISI_ACC_MEM_BLOCK_NR 5
22 /* use default sgl head size 64B */
23 struct hisi_acc_hw_sgl {
25 __le16 entry_sum_in_chain;
26 __le16 entry_sum_in_sgl;
27 __le16 entry_length_in_sgl;
30 struct hisi_acc_hw_sgl *next;
31 struct acc_hw_sge sge_entries[];
34 struct hisi_acc_sgl_pool {
36 struct hisi_acc_hw_sgl *sgl;
39 } mem_block[HISI_ACC_MEM_BLOCK_NR];
40 u32 sgl_num_per_block;
48 * hisi_acc_create_sgl_pool() - Create a hw sgl pool.
49 * @dev: The device which hw sgl pool belongs to.
50 * @count: Count of hisi_acc_hw_sgl in pool.
51 * @sge_nr: The count of sge in hw_sgl
53 * This function creates a hw sgl pool, after this user can get hw sgl memory
56 struct hisi_acc_sgl_pool *hisi_acc_create_sgl_pool(struct device *dev,
57 u32 count, u32 sge_nr)
59 u32 sgl_size, block_size, sgl_num_per_block, block_num, remain_sgl;
60 struct hisi_acc_sgl_pool *pool;
61 struct mem_block *block;
64 if (!dev || !count || !sge_nr || sge_nr > HISI_ACC_SGL_SGE_NR_MAX)
65 return ERR_PTR(-EINVAL);
67 sgl_size = sizeof(struct acc_hw_sge) * sge_nr +
68 sizeof(struct hisi_acc_hw_sgl);
71 * the pool may allocate a block of memory of size PAGE_SIZE * 2^(MAX_ORDER - 1),
72 * block size may exceed 2^31 on ia64, so the max of block size is 2^31
74 block_size = 1 << (PAGE_SHIFT + MAX_ORDER <= 32 ?
75 PAGE_SHIFT + MAX_ORDER - 1 : 31);
76 sgl_num_per_block = block_size / sgl_size;
77 block_num = count / sgl_num_per_block;
78 remain_sgl = count % sgl_num_per_block;
80 if ((!remain_sgl && block_num > HISI_ACC_MEM_BLOCK_NR) ||
81 (remain_sgl > 0 && block_num > HISI_ACC_MEM_BLOCK_NR - 1))
82 return ERR_PTR(-EINVAL);
84 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
86 return ERR_PTR(-ENOMEM);
87 block = pool->mem_block;
89 for (i = 0; i < block_num; i++) {
90 block[i].sgl = dma_alloc_coherent(dev, block_size,
94 dev_err(dev, "Fail to allocate hw SG buffer!\n");
98 block[i].size = block_size;
101 if (remain_sgl > 0) {
102 block[i].sgl = dma_alloc_coherent(dev, remain_sgl * sgl_size,
106 dev_err(dev, "Fail to allocate remained hw SG buffer!\n");
110 block[i].size = remain_sgl * sgl_size;
113 pool->sgl_num_per_block = sgl_num_per_block;
114 pool->block_num = remain_sgl ? block_num + 1 : block_num;
116 pool->sgl_size = sgl_size;
117 pool->sge_nr = sge_nr;
122 for (j = 0; j < i; j++) {
123 dma_free_coherent(dev, block_size, block[j].sgl,
125 memset(block + j, 0, sizeof(*block));
128 return ERR_PTR(-ENOMEM);
130 EXPORT_SYMBOL_GPL(hisi_acc_create_sgl_pool);
133 * hisi_acc_free_sgl_pool() - Free a hw sgl pool.
134 * @dev: The device which hw sgl pool belongs to.
135 * @pool: Pointer of pool.
137 * This function frees memory of a hw sgl pool.
139 void hisi_acc_free_sgl_pool(struct device *dev, struct hisi_acc_sgl_pool *pool)
141 struct mem_block *block;
147 block = pool->mem_block;
149 for (i = 0; i < pool->block_num; i++)
150 dma_free_coherent(dev, block[i].size, block[i].sgl,
155 EXPORT_SYMBOL_GPL(hisi_acc_free_sgl_pool);
157 static struct hisi_acc_hw_sgl *acc_get_sgl(struct hisi_acc_sgl_pool *pool,
158 u32 index, dma_addr_t *hw_sgl_dma)
160 struct mem_block *block;
161 u32 block_index, offset;
163 if (!pool || !hw_sgl_dma || index >= pool->count)
164 return ERR_PTR(-EINVAL);
166 block = pool->mem_block;
167 block_index = index / pool->sgl_num_per_block;
168 offset = index % pool->sgl_num_per_block;
170 *hw_sgl_dma = block[block_index].sgl_dma + pool->sgl_size * offset;
171 return (void *)block[block_index].sgl + pool->sgl_size * offset;
174 static void sg_map_to_hw_sg(struct scatterlist *sgl,
175 struct acc_hw_sge *hw_sge)
177 hw_sge->buf = sg_dma_address(sgl);
178 hw_sge->len = cpu_to_le32(sg_dma_len(sgl));
179 hw_sge->page_ctrl = sg_virt(sgl);
182 static void inc_hw_sgl_sge(struct hisi_acc_hw_sgl *hw_sgl)
184 u16 var = le16_to_cpu(hw_sgl->entry_sum_in_sgl);
187 hw_sgl->entry_sum_in_sgl = cpu_to_le16(var);
190 static void update_hw_sgl_sum_sge(struct hisi_acc_hw_sgl *hw_sgl, u16 sum)
192 hw_sgl->entry_sum_in_chain = cpu_to_le16(sum);
195 static void clear_hw_sgl_sge(struct hisi_acc_hw_sgl *hw_sgl)
197 struct acc_hw_sge *hw_sge = hw_sgl->sge_entries;
200 for (i = 0; i < le16_to_cpu(hw_sgl->entry_sum_in_sgl); i++) {
201 hw_sge[i].page_ctrl = NULL;
208 * hisi_acc_sg_buf_map_to_hw_sgl - Map a scatterlist to a hw sgl.
209 * @dev: The device which hw sgl belongs to.
210 * @sgl: Scatterlist which will be mapped to hw sgl.
211 * @pool: Pool which hw sgl memory will be allocated in.
212 * @index: Index of hisi_acc_hw_sgl in pool.
213 * @hw_sgl_dma: The dma address of allocated hw sgl.
215 * This function builds hw sgl according input sgl, user can use hw_sgl_dma
216 * as src/dst in its BD. Only support single hw sgl currently.
218 struct hisi_acc_hw_sgl *
219 hisi_acc_sg_buf_map_to_hw_sgl(struct device *dev,
220 struct scatterlist *sgl,
221 struct hisi_acc_sgl_pool *pool,
222 u32 index, dma_addr_t *hw_sgl_dma)
224 struct hisi_acc_hw_sgl *curr_hw_sgl;
225 dma_addr_t curr_sgl_dma = 0;
226 struct acc_hw_sge *curr_hw_sge;
227 struct scatterlist *sg;
228 int i, sg_n, sg_n_mapped;
230 if (!dev || !sgl || !pool || !hw_sgl_dma)
231 return ERR_PTR(-EINVAL);
233 sg_n = sg_nents(sgl);
235 sg_n_mapped = dma_map_sg(dev, sgl, sg_n, DMA_BIDIRECTIONAL);
237 dev_err(dev, "DMA mapping for SG error!\n");
238 return ERR_PTR(-EINVAL);
241 if (sg_n_mapped > pool->sge_nr) {
242 dev_err(dev, "the number of entries in input scatterlist is bigger than SGL pool setting.\n");
243 return ERR_PTR(-EINVAL);
246 curr_hw_sgl = acc_get_sgl(pool, index, &curr_sgl_dma);
247 if (IS_ERR(curr_hw_sgl)) {
248 dev_err(dev, "Get SGL error!\n");
249 dma_unmap_sg(dev, sgl, sg_n, DMA_BIDIRECTIONAL);
250 return ERR_PTR(-ENOMEM);
253 curr_hw_sgl->entry_length_in_sgl = cpu_to_le16(pool->sge_nr);
254 curr_hw_sge = curr_hw_sgl->sge_entries;
256 for_each_sg(sgl, sg, sg_n_mapped, i) {
257 sg_map_to_hw_sg(sg, curr_hw_sge);
258 inc_hw_sgl_sge(curr_hw_sgl);
262 update_hw_sgl_sum_sge(curr_hw_sgl, pool->sge_nr);
263 *hw_sgl_dma = curr_sgl_dma;
267 EXPORT_SYMBOL_GPL(hisi_acc_sg_buf_map_to_hw_sgl);
270 * hisi_acc_sg_buf_unmap() - Unmap allocated hw sgl.
271 * @dev: The device which hw sgl belongs to.
272 * @sgl: Related scatterlist.
273 * @hw_sgl: Virtual address of hw sgl.
275 * This function unmaps allocated hw sgl.
277 void hisi_acc_sg_buf_unmap(struct device *dev, struct scatterlist *sgl,
278 struct hisi_acc_hw_sgl *hw_sgl)
280 if (!dev || !sgl || !hw_sgl)
283 dma_unmap_sg(dev, sgl, sg_nents(sgl), DMA_BIDIRECTIONAL);
284 clear_hw_sgl_sge(hw_sgl);
285 hw_sgl->entry_sum_in_chain = 0;
286 hw_sgl->entry_sum_in_sgl = 0;
287 hw_sgl->entry_length_in_sgl = 0;
289 EXPORT_SYMBOL_GPL(hisi_acc_sg_buf_unmap);