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
= 0;
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
);
69 block_size
= 1 << (PAGE_SHIFT
+ MAX_ORDER
<= 32 ?
70 PAGE_SHIFT
+ MAX_ORDER
- 1 : 31);
71 sgl_num_per_block
= block_size
/ sgl_size
;
72 block_num
= count
/ sgl_num_per_block
;
73 remain_sgl
= count
% sgl_num_per_block
;
75 if ((!remain_sgl
&& block_num
> HISI_ACC_MEM_BLOCK_NR
) ||
76 (remain_sgl
> 0 && block_num
> HISI_ACC_MEM_BLOCK_NR
- 1))
77 return ERR_PTR(-EINVAL
);
79 pool
= kzalloc(sizeof(*pool
), GFP_KERNEL
);
81 return ERR_PTR(-ENOMEM
);
82 block
= pool
->mem_block
;
84 for (i
= 0; i
< block_num
; i
++) {
85 block
[i
].sgl
= dma_alloc_coherent(dev
, block_size
,
91 block
[i
].size
= block_size
;
95 block
[i
].sgl
= dma_alloc_coherent(dev
, remain_sgl
* sgl_size
,
101 block
[i
].size
= remain_sgl
* sgl_size
;
104 pool
->sgl_num_per_block
= sgl_num_per_block
;
105 pool
->block_num
= remain_sgl
? block_num
+ 1 : block_num
;
107 pool
->sgl_size
= sgl_size
;
108 pool
->sge_nr
= sge_nr
;
113 for (j
= 0; j
< i
; j
++) {
114 dma_free_coherent(dev
, block_size
, block
[j
].sgl
,
116 memset(block
+ j
, 0, sizeof(*block
));
119 return ERR_PTR(-ENOMEM
);
121 EXPORT_SYMBOL_GPL(hisi_acc_create_sgl_pool
);
124 * hisi_acc_free_sgl_pool() - Free a hw sgl pool.
125 * @dev: The device which hw sgl pool belongs to.
126 * @pool: Pointer of pool.
128 * This function frees memory of a hw sgl pool.
130 void hisi_acc_free_sgl_pool(struct device
*dev
, struct hisi_acc_sgl_pool
*pool
)
132 struct mem_block
*block
;
138 block
= pool
->mem_block
;
140 for (i
= 0; i
< pool
->block_num
; i
++)
141 dma_free_coherent(dev
, block
[i
].size
, block
[i
].sgl
,
146 EXPORT_SYMBOL_GPL(hisi_acc_free_sgl_pool
);
148 static struct hisi_acc_hw_sgl
*acc_get_sgl(struct hisi_acc_sgl_pool
*pool
,
149 u32 index
, dma_addr_t
*hw_sgl_dma
)
151 struct mem_block
*block
;
152 u32 block_index
, offset
;
154 if (!pool
|| !hw_sgl_dma
|| index
>= pool
->count
)
155 return ERR_PTR(-EINVAL
);
157 block
= pool
->mem_block
;
158 block_index
= index
/ pool
->sgl_num_per_block
;
159 offset
= index
% pool
->sgl_num_per_block
;
161 *hw_sgl_dma
= block
[block_index
].sgl_dma
+ pool
->sgl_size
* offset
;
162 return (void *)block
[block_index
].sgl
+ pool
->sgl_size
* offset
;
165 static void sg_map_to_hw_sg(struct scatterlist
*sgl
,
166 struct acc_hw_sge
*hw_sge
)
168 hw_sge
->buf
= sg_dma_address(sgl
);
169 hw_sge
->len
= cpu_to_le32(sg_dma_len(sgl
));
172 static void inc_hw_sgl_sge(struct hisi_acc_hw_sgl
*hw_sgl
)
174 u16 var
= le16_to_cpu(hw_sgl
->entry_sum_in_sgl
);
177 hw_sgl
->entry_sum_in_sgl
= cpu_to_le16(var
);
180 static void update_hw_sgl_sum_sge(struct hisi_acc_hw_sgl
*hw_sgl
, u16 sum
)
182 hw_sgl
->entry_sum_in_chain
= cpu_to_le16(sum
);
186 * hisi_acc_sg_buf_map_to_hw_sgl - Map a scatterlist to a hw sgl.
187 * @dev: The device which hw sgl belongs to.
188 * @sgl: Scatterlist which will be mapped to hw sgl.
189 * @pool: Pool which hw sgl memory will be allocated in.
190 * @index: Index of hisi_acc_hw_sgl in pool.
191 * @hw_sgl_dma: The dma address of allocated hw sgl.
193 * This function builds hw sgl according input sgl, user can use hw_sgl_dma
194 * as src/dst in its BD. Only support single hw sgl currently.
196 struct hisi_acc_hw_sgl
*
197 hisi_acc_sg_buf_map_to_hw_sgl(struct device
*dev
,
198 struct scatterlist
*sgl
,
199 struct hisi_acc_sgl_pool
*pool
,
200 u32 index
, dma_addr_t
*hw_sgl_dma
)
202 struct hisi_acc_hw_sgl
*curr_hw_sgl
;
203 dma_addr_t curr_sgl_dma
= 0;
204 struct acc_hw_sge
*curr_hw_sge
;
205 struct scatterlist
*sg
;
206 int i
, sg_n
, sg_n_mapped
;
208 if (!dev
|| !sgl
|| !pool
|| !hw_sgl_dma
)
209 return ERR_PTR(-EINVAL
);
211 sg_n
= sg_nents(sgl
);
213 sg_n_mapped
= dma_map_sg(dev
, sgl
, sg_n
, DMA_BIDIRECTIONAL
);
215 return ERR_PTR(-EINVAL
);
217 if (sg_n_mapped
> pool
->sge_nr
) {
218 dma_unmap_sg(dev
, sgl
, sg_n
, DMA_BIDIRECTIONAL
);
219 return ERR_PTR(-EINVAL
);
222 curr_hw_sgl
= acc_get_sgl(pool
, index
, &curr_sgl_dma
);
223 if (IS_ERR(curr_hw_sgl
)) {
224 dma_unmap_sg(dev
, sgl
, sg_n
, DMA_BIDIRECTIONAL
);
225 return ERR_PTR(-ENOMEM
);
228 curr_hw_sgl
->entry_length_in_sgl
= cpu_to_le16(pool
->sge_nr
);
229 curr_hw_sge
= curr_hw_sgl
->sge_entries
;
231 for_each_sg(sgl
, sg
, sg_n_mapped
, i
) {
232 sg_map_to_hw_sg(sg
, curr_hw_sge
);
233 inc_hw_sgl_sge(curr_hw_sgl
);
237 update_hw_sgl_sum_sge(curr_hw_sgl
, pool
->sge_nr
);
238 *hw_sgl_dma
= curr_sgl_dma
;
242 EXPORT_SYMBOL_GPL(hisi_acc_sg_buf_map_to_hw_sgl
);
245 * hisi_acc_sg_buf_unmap() - Unmap allocated hw sgl.
246 * @dev: The device which hw sgl belongs to.
247 * @sgl: Related scatterlist.
248 * @hw_sgl: Virtual address of hw sgl.
250 * This function unmaps allocated hw sgl.
252 void hisi_acc_sg_buf_unmap(struct device
*dev
, struct scatterlist
*sgl
,
253 struct hisi_acc_hw_sgl
*hw_sgl
)
255 if (!dev
|| !sgl
|| !hw_sgl
)
258 dma_unmap_sg(dev
, sgl
, sg_nents(sgl
), DMA_BIDIRECTIONAL
);
260 hw_sgl
->entry_sum_in_chain
= 0;
261 hw_sgl
->entry_sum_in_sgl
= 0;
262 hw_sgl
->entry_length_in_sgl
= 0;
264 EXPORT_SYMBOL_GPL(hisi_acc_sg_buf_unmap
);