sgl.c 6.7 KB
Newer Older
1 2 3 4
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2019 HiSilicon Limited. */
#include <linux/dma-mapping.h>
#include <linux/module.h>
5
#include <linux/slab.h>
6
#include "qm.h"
7 8 9 10

#define HISI_ACC_SGL_SGE_NR_MIN		1
#define HISI_ACC_SGL_NR_MAX		256
#define HISI_ACC_SGL_ALIGN_SIZE		64
11
#define HISI_ACC_MEM_BLOCK_NR		5
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

struct acc_hw_sge {
	dma_addr_t buf;
	void *page_ctrl;
	__le32 len;
	__le32 pad;
	__le32 pad0;
	__le32 pad1;
};

/* use default sgl head size 64B */
struct hisi_acc_hw_sgl {
	dma_addr_t next_dma;
	__le16 entry_sum_in_chain;
	__le16 entry_sum_in_sgl;
	__le16 entry_length_in_sgl;
	__le16 pad0;
	__le64 pad1[5];
	struct hisi_acc_hw_sgl *next;
	struct acc_hw_sge sge_entries[];
} __aligned(1);

34
struct hisi_acc_sgl_pool {
35 36 37 38 39 40 41
	struct mem_block {
		struct hisi_acc_hw_sgl *sgl;
		dma_addr_t sgl_dma;
		size_t size;
	} mem_block[HISI_ACC_MEM_BLOCK_NR];
	u32 sgl_num_per_block;
	u32 block_num;
42 43 44 45 46
	u32 count;
	u32 sge_nr;
	size_t sgl_size;
};

47 48 49 50
/**
 * hisi_acc_create_sgl_pool() - Create a hw sgl pool.
 * @dev: The device which hw sgl pool belongs to.
 * @count: Count of hisi_acc_hw_sgl in pool.
51
 * @sge_nr: The count of sge in hw_sgl
52 53 54 55
 *
 * This function creates a hw sgl pool, after this user can get hw sgl memory
 * from it.
 */
56 57
struct hisi_acc_sgl_pool *hisi_acc_create_sgl_pool(struct device *dev,
						   u32 count, u32 sge_nr)
58
{
59
	u32 sgl_size, block_size, sgl_num_per_block, block_num, remain_sgl = 0;
60
	struct hisi_acc_sgl_pool *pool;
61 62
	struct mem_block *block;
	u32 i, j;
63

64 65
	if (!dev || !count || !sge_nr || sge_nr > HISI_ACC_SGL_SGE_NR_MAX)
		return ERR_PTR(-EINVAL);
66

67
	sgl_size = sizeof(struct acc_hw_sge) * sge_nr +
68
		   sizeof(struct hisi_acc_hw_sgl);
69 70
	block_size = 1 << (PAGE_SHIFT + MAX_ORDER <= 32 ?
			   PAGE_SHIFT + MAX_ORDER - 1 : 31);
71 72 73 74 75 76 77
	sgl_num_per_block = block_size / sgl_size;
	block_num = count / sgl_num_per_block;
	remain_sgl = count % sgl_num_per_block;

	if ((!remain_sgl && block_num > HISI_ACC_MEM_BLOCK_NR) ||
	    (remain_sgl > 0 && block_num > HISI_ACC_MEM_BLOCK_NR - 1))
		return ERR_PTR(-EINVAL);
78

79 80 81
	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
	if (!pool)
		return ERR_PTR(-ENOMEM);
82
	block = pool->mem_block;
83

84 85 86 87 88 89 90 91
	for (i = 0; i < block_num; i++) {
		block[i].sgl = dma_alloc_coherent(dev, block_size,
						  &block[i].sgl_dma,
						  GFP_KERNEL);
		if (!block[i].sgl)
			goto err_free_mem;

		block[i].size = block_size;
92
	}
93

94 95 96 97 98 99 100 101 102 103 104 105
	if (remain_sgl > 0) {
		block[i].sgl = dma_alloc_coherent(dev, remain_sgl * sgl_size,
						  &block[i].sgl_dma,
						  GFP_KERNEL);
		if (!block[i].sgl)
			goto err_free_mem;

		block[i].size = remain_sgl * sgl_size;
	}

	pool->sgl_num_per_block = sgl_num_per_block;
	pool->block_num = remain_sgl ? block_num + 1 : block_num;
106 107
	pool->count = count;
	pool->sgl_size = sgl_size;
108
	pool->sge_nr = sge_nr;
109

110
	return pool;
111 112 113 114 115 116 117 118 119

err_free_mem:
	for (j = 0; j < i; j++) {
		dma_free_coherent(dev, block_size, block[j].sgl,
				  block[j].sgl_dma);
		memset(block + j, 0, sizeof(*block));
	}
	kfree(pool);
	return ERR_PTR(-ENOMEM);
120 121 122 123 124 125 126 127 128 129 130 131
}
EXPORT_SYMBOL_GPL(hisi_acc_create_sgl_pool);

/**
 * hisi_acc_free_sgl_pool() - Free a hw sgl pool.
 * @dev: The device which hw sgl pool belongs to.
 * @pool: Pointer of pool.
 *
 * This function frees memory of a hw sgl pool.
 */
void hisi_acc_free_sgl_pool(struct device *dev, struct hisi_acc_sgl_pool *pool)
{
132 133 134
	struct mem_block *block;
	int i;

135 136 137
	if (!dev || !pool)
		return;

138 139 140 141 142 143
	block = pool->mem_block;

	for (i = 0; i < pool->block_num; i++)
		dma_free_coherent(dev, block[i].size, block[i].sgl,
				  block[i].sgl_dma);

144
	kfree(pool);
145 146 147
}
EXPORT_SYMBOL_GPL(hisi_acc_free_sgl_pool);

Z
Zhou Wang 已提交
148 149
static struct hisi_acc_hw_sgl *acc_get_sgl(struct hisi_acc_sgl_pool *pool,
					   u32 index, dma_addr_t *hw_sgl_dma)
150
{
151 152 153 154
	struct mem_block *block;
	u32 block_index, offset;

	if (!pool || !hw_sgl_dma || index >= pool->count)
155 156
		return ERR_PTR(-EINVAL);

157 158 159 160 161 162
	block = pool->mem_block;
	block_index = index / pool->sgl_num_per_block;
	offset = index % pool->sgl_num_per_block;

	*hw_sgl_dma = block[block_index].sgl_dma + pool->sgl_size * offset;
	return (void *)block[block_index].sgl + pool->sgl_size * offset;
163 164 165 166 167
}

static void sg_map_to_hw_sg(struct scatterlist *sgl,
			    struct acc_hw_sge *hw_sge)
{
168 169
	hw_sge->buf = sg_dma_address(sgl);
	hw_sge->len = cpu_to_le32(sg_dma_len(sgl));
170 171 172 173
}

static void inc_hw_sgl_sge(struct hisi_acc_hw_sgl *hw_sgl)
{
Z
Zhou Wang 已提交
174 175 176 177
	u16 var = le16_to_cpu(hw_sgl->entry_sum_in_sgl);

	var++;
	hw_sgl->entry_sum_in_sgl = cpu_to_le16(var);
178 179 180 181
}

static void update_hw_sgl_sum_sge(struct hisi_acc_hw_sgl *hw_sgl, u16 sum)
{
Z
Zhou Wang 已提交
182
	hw_sgl->entry_sum_in_chain = cpu_to_le16(sum);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
}

/**
 * hisi_acc_sg_buf_map_to_hw_sgl - Map a scatterlist to a hw sgl.
 * @dev: The device which hw sgl belongs to.
 * @sgl: Scatterlist which will be mapped to hw sgl.
 * @pool: Pool which hw sgl memory will be allocated in.
 * @index: Index of hisi_acc_hw_sgl in pool.
 * @hw_sgl_dma: The dma address of allocated hw sgl.
 *
 * This function builds hw sgl according input sgl, user can use hw_sgl_dma
 * as src/dst in its BD. Only support single hw sgl currently.
 */
struct hisi_acc_hw_sgl *
hisi_acc_sg_buf_map_to_hw_sgl(struct device *dev,
			      struct scatterlist *sgl,
			      struct hisi_acc_sgl_pool *pool,
			      u32 index, dma_addr_t *hw_sgl_dma)
{
	struct hisi_acc_hw_sgl *curr_hw_sgl;
203
	dma_addr_t curr_sgl_dma = 0;
204 205
	struct acc_hw_sge *curr_hw_sge;
	struct scatterlist *sg;
206
	int i, sg_n, sg_n_mapped;
207

Z
Zhou Wang 已提交
208 209 210 211
	if (!dev || !sgl || !pool || !hw_sgl_dma)
		return ERR_PTR(-EINVAL);

	sg_n = sg_nents(sgl);
212 213 214

	sg_n_mapped = dma_map_sg(dev, sgl, sg_n, DMA_BIDIRECTIONAL);
	if (!sg_n_mapped)
215 216
		return ERR_PTR(-EINVAL);

217 218
	if (sg_n_mapped > pool->sge_nr) {
		dma_unmap_sg(dev, sgl, sg_n, DMA_BIDIRECTIONAL);
219
		return ERR_PTR(-EINVAL);
220
	}
221 222

	curr_hw_sgl = acc_get_sgl(pool, index, &curr_sgl_dma);
Z
Zhou Wang 已提交
223 224 225 226
	if (IS_ERR(curr_hw_sgl)) {
		dma_unmap_sg(dev, sgl, sg_n, DMA_BIDIRECTIONAL);
		return ERR_PTR(-ENOMEM);

227
	}
Z
Zhou Wang 已提交
228
	curr_hw_sgl->entry_length_in_sgl = cpu_to_le16(pool->sge_nr);
229 230
	curr_hw_sge = curr_hw_sgl->sge_entries;

231
	for_each_sg(sgl, sg, sg_n_mapped, i) {
232 233 234 235 236
		sg_map_to_hw_sg(sg, curr_hw_sge);
		inc_hw_sgl_sge(curr_hw_sgl);
		curr_hw_sge++;
	}

237
	update_hw_sgl_sum_sge(curr_hw_sgl, pool->sge_nr);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	*hw_sgl_dma = curr_sgl_dma;

	return curr_hw_sgl;
}
EXPORT_SYMBOL_GPL(hisi_acc_sg_buf_map_to_hw_sgl);

/**
 * hisi_acc_sg_buf_unmap() - Unmap allocated hw sgl.
 * @dev: The device which hw sgl belongs to.
 * @sgl: Related scatterlist.
 * @hw_sgl: Virtual address of hw sgl.
 *
 * This function unmaps allocated hw sgl.
 */
void hisi_acc_sg_buf_unmap(struct device *dev, struct scatterlist *sgl,
			   struct hisi_acc_hw_sgl *hw_sgl)
{
Z
Zhou Wang 已提交
255 256 257
	if (!dev || !sgl || !hw_sgl)
		return;

258 259 260 261 262 263 264
	dma_unmap_sg(dev, sgl, sg_nents(sgl), DMA_BIDIRECTIONAL);

	hw_sgl->entry_sum_in_chain = 0;
	hw_sgl->entry_sum_in_sgl = 0;
	hw_sgl->entry_length_in_sgl = 0;
}
EXPORT_SYMBOL_GPL(hisi_acc_sg_buf_unmap);