Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2021 HiSilicon Limited
3 : : */
4 : :
5 : : #include <inttypes.h>
6 : : #include <string.h>
7 : :
8 : : #include <bus_pci_driver.h>
9 : : #include <rte_cycles.h>
10 : : #include <rte_eal.h>
11 : : #include <rte_io.h>
12 : : #include <rte_log.h>
13 : : #include <rte_malloc.h>
14 : : #include <rte_memzone.h>
15 : : #include <rte_pci.h>
16 : : #include <rte_dmadev_pmd.h>
17 : :
18 : : #include "hisi_dmadev.h"
19 : :
20 [ - + ]: 251 : RTE_LOG_REGISTER_DEFAULT(hisi_dma_logtype, INFO);
21 : : #define RTE_LOGTYPE_HISI_DMA hisi_dma_logtype
22 : : #define HISI_DMA_LOG(level, ...) \
23 : : RTE_LOG_LINE_PREFIX(level, HISI_DMA, "%s(): ", __func__, __VA_ARGS__)
24 : : #define HISI_DMA_DEV_LOG(hw, level, ...) \
25 : : RTE_LOG_LINE_PREFIX(level, HISI_DMA, "%s %s(): ", \
26 : : (hw)->data->dev_name RTE_LOG_COMMA __func__, __VA_ARGS__)
27 : : #define HISI_DMA_DEBUG(hw, ...) \
28 : : HISI_DMA_DEV_LOG(hw, DEBUG, __VA_ARGS__)
29 : : #define HISI_DMA_INFO(hw, ...) \
30 : : HISI_DMA_DEV_LOG(hw, INFO, __VA_ARGS__)
31 : : #define HISI_DMA_WARN(hw, ...) \
32 : : HISI_DMA_DEV_LOG(hw, WARNING, __VA_ARGS__)
33 : : #define HISI_DMA_ERR(hw, ...) \
34 : : HISI_DMA_DEV_LOG(hw, ERR, __VA_ARGS__)
35 : :
36 : : static uint32_t
37 : : hisi_dma_queue_base(struct hisi_dma_dev *hw)
38 : : {
39 : : if (hw->reg_layout == HISI_DMA_REG_LAYOUT_HIP08)
40 : : return HISI_DMA_HIP08_QUEUE_BASE;
41 : : else
42 : : return 0;
43 : : }
44 : :
45 : : static volatile void *
46 : : hisi_dma_queue_regaddr(struct hisi_dma_dev *hw, uint32_t qoff)
47 : : {
48 : 0 : uint32_t off = hisi_dma_queue_base(hw) +
49 : 0 : hw->queue_id * HISI_DMA_QUEUE_REGION_SIZE + qoff;
50 : 0 : return (volatile void *)((char *)hw->io_base + off);
51 : : }
52 : :
53 : : static void
54 : : hisi_dma_write_reg(void *base, uint32_t off, uint32_t val)
55 : : {
56 : 0 : rte_write32(rte_cpu_to_le_32(val),
57 : : (volatile void *)((char *)base + off));
58 : : }
59 : :
60 : : static void
61 : : hisi_dma_write_dev(struct hisi_dma_dev *hw, uint32_t off, uint32_t val)
62 : : {
63 : 0 : hisi_dma_write_reg(hw->io_base, off, val);
64 : : }
65 : :
66 : : static void
67 : : hisi_dma_write_queue(struct hisi_dma_dev *hw, uint32_t qoff, uint32_t val)
68 : : {
69 : 0 : uint32_t off = hisi_dma_queue_base(hw) +
70 : 0 : hw->queue_id * HISI_DMA_QUEUE_REGION_SIZE + qoff;
71 : : hisi_dma_write_dev(hw, off, val);
72 : : }
73 : :
74 : : static uint32_t
75 : : hisi_dma_read_reg(void *base, uint32_t off)
76 : : {
77 : 0 : uint32_t val = rte_read32((volatile void *)((char *)base + off));
78 : : return rte_le_to_cpu_32(val);
79 : : }
80 : :
81 : : static uint32_t
82 : : hisi_dma_read_dev(struct hisi_dma_dev *hw, uint32_t off)
83 : : {
84 : 0 : return hisi_dma_read_reg(hw->io_base, off);
85 : : }
86 : :
87 : : static uint32_t
88 : : hisi_dma_read_queue(struct hisi_dma_dev *hw, uint32_t qoff)
89 : : {
90 : 0 : uint32_t off = hisi_dma_queue_base(hw) +
91 : 0 : hw->queue_id * HISI_DMA_QUEUE_REGION_SIZE + qoff;
92 : : return hisi_dma_read_dev(hw, off);
93 : : }
94 : :
95 : : static void
96 : : hisi_dma_update_bit(struct hisi_dma_dev *hw, uint32_t off, uint32_t pos,
97 : : bool set)
98 : : {
99 : : uint32_t tmp = hisi_dma_read_dev(hw, off);
100 : : uint32_t mask = 1u << pos;
101 : 0 : tmp = set ? tmp | mask : tmp & ~mask;
102 : : hisi_dma_write_dev(hw, off, tmp);
103 : 0 : }
104 : :
105 : : static void
106 : : hisi_dma_update_queue_bit(struct hisi_dma_dev *hw, uint32_t qoff, uint32_t pos,
107 : : bool set)
108 : : {
109 : : uint32_t tmp = hisi_dma_read_queue(hw, qoff);
110 : : uint32_t mask = 1u << pos;
111 : 0 : tmp = set ? tmp | mask : tmp & ~mask;
112 : : hisi_dma_write_queue(hw, qoff, tmp);
113 : : }
114 : :
115 : : static void
116 : : hisi_dma_update_queue_mbit(struct hisi_dma_dev *hw, uint32_t qoff,
117 : : uint32_t mask, bool set)
118 : : {
119 : : uint32_t tmp = hisi_dma_read_queue(hw, qoff);
120 : 0 : tmp = set ? tmp | mask : tmp & ~mask;
121 : : hisi_dma_write_queue(hw, qoff, tmp);
122 : 0 : }
123 : :
124 : : #define hisi_dma_poll_hw_state(hw, val, cond, sleep_us, timeout_us) __extension__ ({ \
125 : : uint32_t timeout = 0; \
126 : : while (timeout++ <= (timeout_us)) { \
127 : : (val) = hisi_dma_read_queue(hw, HISI_DMA_QUEUE_FSM_REG); \
128 : : if (cond) \
129 : : break; \
130 : : rte_delay_us(sleep_us); \
131 : : } \
132 : : (cond) ? 0 : -ETIME; \
133 : : })
134 : :
135 : : static int
136 : 0 : hisi_dma_reset_hw(struct hisi_dma_dev *hw)
137 : : {
138 : : #define POLL_SLEEP_US 100
139 : : #define POLL_TIMEOUT_US 10000
140 : :
141 : : uint32_t tmp;
142 : : int ret;
143 : :
144 : : hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL0_REG,
145 : : HISI_DMA_QUEUE_CTRL0_PAUSE_B, true);
146 : : hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL0_REG,
147 : : HISI_DMA_QUEUE_CTRL0_EN_B, false);
148 : :
149 [ # # # # : 0 : ret = hisi_dma_poll_hw_state(hw, tmp,
# # ]
150 : : FIELD_GET(HISI_DMA_QUEUE_FSM_STS_M, tmp) != HISI_DMA_STATE_RUN,
151 : : POLL_SLEEP_US, POLL_TIMEOUT_US);
152 : : if (ret) {
153 : 0 : HISI_DMA_ERR(hw, "disable dma timeout!");
154 : 0 : return ret;
155 : : }
156 : :
157 : : hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL1_REG,
158 : : HISI_DMA_QUEUE_CTRL1_RESET_B, true);
159 : : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_SQ_TAIL_REG, 0);
160 : : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_CQ_HEAD_REG, 0);
161 : : hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL0_REG,
162 : : HISI_DMA_QUEUE_CTRL0_PAUSE_B, false);
163 : :
164 [ # # # # : 0 : ret = hisi_dma_poll_hw_state(hw, tmp,
# # ]
165 : : FIELD_GET(HISI_DMA_QUEUE_FSM_STS_M, tmp) == HISI_DMA_STATE_IDLE,
166 : : POLL_SLEEP_US, POLL_TIMEOUT_US);
167 : : if (ret) {
168 : 0 : HISI_DMA_ERR(hw, "reset dma timeout!");
169 : 0 : return ret;
170 : : }
171 : :
172 : : return 0;
173 : : }
174 : :
175 : : static void
176 : 0 : hisi_dma_init_common(struct hisi_dma_dev *hw)
177 : : {
178 : 0 : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_SQ_BASE_L_REG,
179 : 0 : lower_32_bits(hw->sqe_iova));
180 : 0 : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_SQ_BASE_H_REG,
181 : 0 : upper_32_bits(hw->sqe_iova));
182 : 0 : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_CQ_BASE_L_REG,
183 : 0 : lower_32_bits(hw->cqe_iova));
184 : 0 : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_CQ_BASE_H_REG,
185 : 0 : upper_32_bits(hw->cqe_iova));
186 : 0 : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_SQ_DEPTH_REG,
187 : 0 : hw->sq_depth_mask);
188 : 0 : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_CQ_DEPTH_REG, hw->cq_depth - 1);
189 : : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_SQ_TAIL_REG, 0);
190 : : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_CQ_HEAD_REG, 0);
191 : : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_ERR_INT_NUM0_REG, 0);
192 : : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_ERR_INT_NUM1_REG, 0);
193 : : hisi_dma_write_queue(hw, HISI_DMA_QUEUE_ERR_INT_NUM2_REG, 0);
194 : 0 : }
195 : :
196 : : static void
197 : 0 : hisi_dma_init_hw(struct hisi_dma_dev *hw)
198 : : {
199 : 0 : hisi_dma_init_common(hw);
200 : :
201 [ # # ]: 0 : if (hw->reg_layout == HISI_DMA_REG_LAYOUT_HIP08) {
202 : : hisi_dma_write_queue(hw, HISI_DMA_HIP08_QUEUE_ERR_INT_NUM3_REG,
203 : : 0);
204 : : hisi_dma_write_queue(hw, HISI_DMA_HIP08_QUEUE_ERR_INT_NUM4_REG,
205 : : 0);
206 : : hisi_dma_write_queue(hw, HISI_DMA_HIP08_QUEUE_ERR_INT_NUM5_REG,
207 : : 0);
208 : : hisi_dma_write_queue(hw, HISI_DMA_HIP08_QUEUE_ERR_INT_NUM6_REG,
209 : : 0);
210 : : hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL0_REG,
211 : : HISI_DMA_HIP08_QUEUE_CTRL0_ERR_ABORT_B, false);
212 : : hisi_dma_update_queue_mbit(hw, HISI_DMA_QUEUE_INT_STATUS_REG,
213 : : HISI_DMA_HIP08_QUEUE_INT_MASK_M, true);
214 : : hisi_dma_update_queue_mbit(hw, HISI_DMA_QUEUE_INT_MASK_REG,
215 : : HISI_DMA_HIP08_QUEUE_INT_MASK_M, true);
216 : : }
217 : 0 : }
218 : :
219 : : static void
220 [ # # ]: 0 : hisi_dma_init_gbl(void *pci_bar, uint8_t revision)
221 : : {
222 : : struct hisi_dma_dev hw;
223 : :
224 : : memset(&hw, 0, sizeof(hw));
225 : 0 : hw.io_base = pci_bar;
226 : :
227 [ # # ]: 0 : if (revision == HISI_DMA_REVISION_HIP08B)
228 : : hisi_dma_update_bit(&hw, HISI_DMA_HIP08_MODE_REG,
229 : : HISI_DMA_HIP08_MODE_SEL_B, true);
230 : 0 : }
231 : :
232 : : static uint8_t
233 : : hisi_dma_reg_layout(uint8_t revision)
234 : : {
235 : 0 : if (revision == HISI_DMA_REVISION_HIP08B)
236 : : return HISI_DMA_REG_LAYOUT_HIP08;
237 : : else
238 : 0 : return HISI_DMA_REG_LAYOUT_INVALID;
239 : : }
240 : :
241 : : static void
242 : : hisi_dma_zero_iomem(struct hisi_dma_dev *hw)
243 : : {
244 : 0 : memset(hw->iomz->addr, 0, hw->iomz_sz);
245 : : }
246 : :
247 : : static int
248 : 0 : hisi_dma_alloc_iomem(struct hisi_dma_dev *hw, uint16_t ring_size,
249 : : const char *dev_name)
250 : : {
251 : 0 : uint32_t sq_size = sizeof(struct hisi_dma_sqe) * ring_size;
252 : 0 : uint32_t cq_size = sizeof(struct hisi_dma_cqe) *
253 : 0 : (ring_size + HISI_DMA_CQ_RESERVED);
254 : 0 : uint32_t status_size = sizeof(uint16_t) * ring_size;
255 : : char mz_name[RTE_MEMZONE_NAMESIZE];
256 : : const struct rte_memzone *iomz;
257 : : uint32_t total_size;
258 : :
259 : 0 : sq_size = RTE_CACHE_LINE_ROUNDUP(sq_size);
260 : 0 : cq_size = RTE_CACHE_LINE_ROUNDUP(cq_size);
261 : 0 : status_size = RTE_CACHE_LINE_ROUNDUP(status_size);
262 : 0 : total_size = sq_size + cq_size + status_size;
263 : :
264 : : (void)snprintf(mz_name, sizeof(mz_name), "hisi_dma:%s", dev_name);
265 : 0 : iomz = rte_memzone_reserve(mz_name, total_size, hw->data->numa_node,
266 : : RTE_MEMZONE_IOVA_CONTIG);
267 [ # # ]: 0 : if (iomz == NULL) {
268 : 0 : HISI_DMA_ERR(hw, "malloc %s iomem fail!", mz_name);
269 : 0 : return -ENOMEM;
270 : : }
271 : :
272 : 0 : hw->iomz = iomz;
273 : 0 : hw->iomz_sz = total_size;
274 : 0 : hw->sqe = iomz->addr;
275 : 0 : hw->cqe = (void *)((char *)iomz->addr + sq_size);
276 : 0 : hw->status = (void *)((char *)iomz->addr + sq_size + cq_size);
277 : 0 : hw->sqe_iova = iomz->iova;
278 : 0 : hw->cqe_iova = iomz->iova + sq_size;
279 : 0 : hw->sq_depth_mask = ring_size - 1;
280 : 0 : hw->cq_depth = ring_size + HISI_DMA_CQ_RESERVED;
281 : : hisi_dma_zero_iomem(hw);
282 : :
283 : 0 : return 0;
284 : : }
285 : :
286 : : static void
287 : : hisi_dma_free_iomem(struct hisi_dma_dev *hw)
288 : : {
289 : 0 : rte_memzone_free(hw->iomz);
290 : :
291 : 0 : hw->iomz = NULL;
292 : 0 : hw->sqe = NULL;
293 : 0 : hw->cqe = NULL;
294 : 0 : hw->status = NULL;
295 : 0 : hw->sqe_iova = 0;
296 : 0 : hw->cqe_iova = 0;
297 : 0 : hw->sq_depth_mask = 0;
298 : 0 : hw->cq_depth = 0;
299 : 0 : }
300 : :
301 : : static int
302 : 0 : hisi_dma_info_get(const struct rte_dma_dev *dev,
303 : : struct rte_dma_info *dev_info,
304 : : uint32_t info_sz)
305 : : {
306 : : RTE_SET_USED(dev);
307 : : RTE_SET_USED(info_sz);
308 : :
309 : 0 : dev_info->dev_capa = RTE_DMA_CAPA_MEM_TO_MEM |
310 : : RTE_DMA_CAPA_OPS_COPY;
311 : 0 : dev_info->max_vchans = 1;
312 : 0 : dev_info->max_desc = HISI_DMA_MAX_DESC_NUM;
313 : 0 : dev_info->min_desc = HISI_DMA_MIN_DESC_NUM;
314 : :
315 : 0 : return 0;
316 : : }
317 : :
318 : : static int
319 : 0 : hisi_dma_configure(struct rte_dma_dev *dev,
320 : : const struct rte_dma_conf *conf,
321 : : uint32_t conf_sz)
322 : : {
323 : : RTE_SET_USED(dev);
324 : : RTE_SET_USED(conf);
325 : : RTE_SET_USED(conf_sz);
326 : 0 : return 0;
327 : : }
328 : :
329 : : static int
330 : 0 : hisi_dma_vchan_setup(struct rte_dma_dev *dev, uint16_t vchan,
331 : : const struct rte_dma_vchan_conf *conf,
332 : : uint32_t conf_sz)
333 : : {
334 : 0 : struct hisi_dma_dev *hw = dev->data->dev_private;
335 : : int ret;
336 : :
337 : : RTE_SET_USED(vchan);
338 : : RTE_SET_USED(conf_sz);
339 : :
340 [ # # ]: 0 : if (!rte_is_power_of_2(conf->nb_desc)) {
341 : 0 : HISI_DMA_ERR(hw, "Number of desc must be power of 2!");
342 : 0 : return -EINVAL;
343 : : }
344 : :
345 : : hisi_dma_free_iomem(hw);
346 : 0 : ret = hisi_dma_alloc_iomem(hw, conf->nb_desc, dev->data->dev_name);
347 [ # # ]: 0 : if (ret)
348 : 0 : return ret;
349 : :
350 : : return 0;
351 : : }
352 : :
353 : : static int
354 : 0 : hisi_dma_start(struct rte_dma_dev *dev)
355 : : {
356 : 0 : struct hisi_dma_dev *hw = dev->data->dev_private;
357 : :
358 [ # # ]: 0 : if (hw->iomz == NULL) {
359 : 0 : HISI_DMA_ERR(hw, "Vchan was not setup, start fail!");
360 : 0 : return -EINVAL;
361 : : }
362 : :
363 : : /* Reset the dmadev to a known state, include:
364 : : * 1) zero iomem, also include status fields.
365 : : * 2) init hardware register.
366 : : * 3) init index values to zero.
367 : : * 4) init running statistics.
368 : : */
369 : : hisi_dma_zero_iomem(hw);
370 : 0 : hisi_dma_init_hw(hw);
371 : 0 : hw->ridx = 0;
372 : 0 : hw->cridx = 0;
373 : 0 : hw->sq_head = 0;
374 : 0 : hw->sq_tail = 0;
375 : 0 : hw->cq_sq_head = 0;
376 : 0 : hw->cq_head = 0;
377 : 0 : hw->cqs_completed = 0;
378 : 0 : hw->cqe_vld = 1;
379 : 0 : hw->submitted = 0;
380 : 0 : hw->completed = 0;
381 : 0 : hw->errors = 0;
382 : 0 : hw->qfulls = 0;
383 : :
384 : : hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL0_REG,
385 : : HISI_DMA_QUEUE_CTRL0_EN_B, true);
386 : :
387 : 0 : return 0;
388 : : }
389 : :
390 : : static int
391 : 0 : hisi_dma_stop(struct rte_dma_dev *dev)
392 : : {
393 : 0 : return hisi_dma_reset_hw(dev->data->dev_private);
394 : : }
395 : :
396 : : static int
397 : 0 : hisi_dma_close(struct rte_dma_dev *dev)
398 : : {
399 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
400 : : /* The dmadev already stopped */
401 : 0 : hisi_dma_free_iomem(dev->data->dev_private);
402 : : }
403 : 0 : return 0;
404 : : }
405 : :
406 : : static int
407 : 0 : hisi_dma_stats_get(const struct rte_dma_dev *dev, uint16_t vchan,
408 : : struct rte_dma_stats *stats,
409 : : uint32_t stats_sz)
410 : : {
411 : 0 : struct hisi_dma_dev *hw = dev->data->dev_private;
412 : :
413 : : RTE_SET_USED(vchan);
414 : : RTE_SET_USED(stats_sz);
415 : 0 : stats->submitted = hw->submitted;
416 : 0 : stats->completed = hw->completed;
417 : 0 : stats->errors = hw->errors;
418 : :
419 : 0 : return 0;
420 : : }
421 : :
422 : : static int
423 : 0 : hisi_dma_stats_reset(struct rte_dma_dev *dev, uint16_t vchan)
424 : : {
425 : 0 : struct hisi_dma_dev *hw = dev->data->dev_private;
426 : :
427 : : RTE_SET_USED(vchan);
428 : 0 : hw->submitted = 0;
429 : 0 : hw->completed = 0;
430 : 0 : hw->errors = 0;
431 : 0 : hw->qfulls = 0;
432 : :
433 : 0 : return 0;
434 : : }
435 : :
436 : : static int
437 : 0 : hisi_dma_vchan_status(const struct rte_dma_dev *dev, uint16_t vchan,
438 : : enum rte_dma_vchan_status *status)
439 : : {
440 : 0 : struct hisi_dma_dev *hw = dev->data->dev_private;
441 : : uint32_t val;
442 : :
443 : : RTE_SET_USED(vchan);
444 : :
445 : : val = hisi_dma_read_queue(hw, HISI_DMA_QUEUE_FSM_REG);
446 : : val = FIELD_GET(HISI_DMA_QUEUE_FSM_STS_M, val);
447 [ # # ]: 0 : if (val == HISI_DMA_STATE_RUN)
448 : 0 : *status = RTE_DMA_VCHAN_ACTIVE;
449 [ # # ]: 0 : else if (val == HISI_DMA_STATE_CPL)
450 : 0 : *status = RTE_DMA_VCHAN_IDLE;
451 : : else
452 : 0 : *status = RTE_DMA_VCHAN_HALTED_ERROR;
453 : :
454 : 0 : return 0;
455 : : }
456 : :
457 : : static void
458 : 0 : hisi_dma_dump_range(struct hisi_dma_dev *hw, FILE *f, uint32_t start,
459 : : uint32_t end)
460 : : {
461 : : #define DUMP_REGNUM_PER_LINE 4
462 : :
463 : : uint32_t cnt, i;
464 : :
465 : : cnt = 0;
466 [ # # ]: 0 : for (i = start; i <= end; i += sizeof(uint32_t)) {
467 [ # # ]: 0 : if (cnt % DUMP_REGNUM_PER_LINE == 0)
468 : : (void)fprintf(f, " [%4x]:", i);
469 : : (void)fprintf(f, " 0x%08x", hisi_dma_read_dev(hw, i));
470 : 0 : cnt++;
471 [ # # ]: 0 : if (cnt % DUMP_REGNUM_PER_LINE == 0)
472 : : (void)fprintf(f, "\n");
473 : : }
474 [ # # ]: 0 : if (cnt % DUMP_REGNUM_PER_LINE)
475 : : (void)fprintf(f, "\n");
476 : 0 : }
477 : :
478 : : static void
479 : 0 : hisi_dma_dump_common(struct hisi_dma_dev *hw, FILE *f)
480 : : {
481 : : struct {
482 : : uint8_t reg_layout;
483 : : uint32_t start;
484 : : uint32_t end;
485 : 0 : } reg_info[] = {
486 : : { HISI_DMA_REG_LAYOUT_HIP08,
487 : : HISI_DMA_HIP08_DUMP_START_REG,
488 : : HISI_DMA_HIP08_DUMP_END_REG },
489 : : };
490 : : uint32_t i;
491 : :
492 : : (void)fprintf(f, " common-register:\n");
493 [ # # ]: 0 : for (i = 0; i < RTE_DIM(reg_info); i++) {
494 [ # # ]: 0 : if (hw->reg_layout != reg_info[i].reg_layout)
495 : 0 : continue;
496 : 0 : hisi_dma_dump_range(hw, f, reg_info[i].start, reg_info[i].end);
497 : : }
498 : 0 : }
499 : :
500 : : static void
501 : 0 : hisi_dma_dump_read_queue(struct hisi_dma_dev *hw, uint32_t qoff,
502 : : char *buffer, int max_sz)
503 : : {
504 [ # # ]: 0 : memset(buffer, 0, max_sz);
505 : :
506 : : /* Address-related registers are not printed for security reasons. */
507 : 0 : if (qoff == HISI_DMA_QUEUE_SQ_BASE_L_REG ||
508 [ # # ]: 0 : qoff == HISI_DMA_QUEUE_SQ_BASE_H_REG ||
509 : 0 : qoff == HISI_DMA_QUEUE_CQ_BASE_L_REG ||
510 [ # # ]: 0 : qoff == HISI_DMA_QUEUE_CQ_BASE_H_REG) {
511 : : (void)snprintf(buffer, max_sz, "**********");
512 : 0 : return;
513 : : }
514 : :
515 : : (void)snprintf(buffer, max_sz, "0x%08x", hisi_dma_read_queue(hw, qoff));
516 : : }
517 : :
518 : : static void
519 : 0 : hisi_dma_dump_queue(struct hisi_dma_dev *hw, FILE *f)
520 : : {
521 : : #define REG_FMT_LEN 32
522 : 0 : char buf[REG_FMT_LEN] = { 0 };
523 : : uint32_t i;
524 : :
525 : : (void)fprintf(f, " queue-register:\n");
526 [ # # ]: 0 : for (i = 0; i < HISI_DMA_QUEUE_REGION_SIZE; ) {
527 : 0 : hisi_dma_dump_read_queue(hw, i, buf, sizeof(buf));
528 : : (void)fprintf(f, " [%2x]: %s", i, buf);
529 : 0 : i += sizeof(uint32_t);
530 : 0 : hisi_dma_dump_read_queue(hw, i, buf, sizeof(buf));
531 : : (void)fprintf(f, " %s", buf);
532 : 0 : i += sizeof(uint32_t);
533 : 0 : hisi_dma_dump_read_queue(hw, i, buf, sizeof(buf));
534 : : (void)fprintf(f, " %s", buf);
535 : 0 : i += sizeof(uint32_t);
536 : 0 : hisi_dma_dump_read_queue(hw, i, buf, sizeof(buf));
537 : : (void)fprintf(f, " %s\n", buf);
538 : 0 : i += sizeof(uint32_t);
539 : : }
540 : 0 : }
541 : :
542 : : static int
543 : 0 : hisi_dma_dump(const struct rte_dma_dev *dev, FILE *f)
544 : : {
545 : 0 : struct hisi_dma_dev *hw = dev->data->dev_private;
546 : :
547 : 0 : (void)fprintf(f,
548 : : " revision: 0x%x queue_id: %u ring_size: %u\n"
549 : : " ridx: %u cridx: %u\n"
550 : : " sq_head: %u sq_tail: %u cq_sq_head: %u\n"
551 : : " cq_head: %u cqs_completed: %u cqe_vld: %u\n"
552 : : " submitted: %" PRIu64 " completed: %" PRIu64 " errors: %"
553 : : PRIu64 " qfulls: %" PRIu64 "\n",
554 : 0 : hw->revision, hw->queue_id,
555 : 0 : hw->sq_depth_mask > 0 ? hw->sq_depth_mask + 1 : 0,
556 : 0 : hw->ridx, hw->cridx,
557 : 0 : hw->sq_head, hw->sq_tail, hw->cq_sq_head,
558 [ # # ]: 0 : hw->cq_head, hw->cqs_completed, hw->cqe_vld,
559 : : hw->submitted, hw->completed, hw->errors, hw->qfulls);
560 : 0 : hisi_dma_dump_queue(hw, f);
561 : 0 : hisi_dma_dump_common(hw, f);
562 : :
563 : 0 : return 0;
564 : : }
565 : :
566 : : static int
567 : 0 : hisi_dma_copy(void *dev_private, uint16_t vchan,
568 : : rte_iova_t src, rte_iova_t dst,
569 : : uint32_t length, uint64_t flags)
570 : : {
571 : : struct hisi_dma_dev *hw = dev_private;
572 : 0 : struct hisi_dma_sqe *sqe = &hw->sqe[hw->sq_tail];
573 : :
574 : : RTE_SET_USED(vchan);
575 : :
576 [ # # ]: 0 : if (((hw->sq_tail + 1) & hw->sq_depth_mask) == hw->sq_head) {
577 : 0 : hw->qfulls++;
578 : 0 : return -ENOSPC;
579 : : }
580 : :
581 : 0 : sqe->dw0 = rte_cpu_to_le_32(SQE_OPCODE_M2M);
582 : 0 : sqe->dw1 = 0;
583 : 0 : sqe->dw2 = 0;
584 : 0 : sqe->length = rte_cpu_to_le_32(length);
585 : 0 : sqe->src_addr = rte_cpu_to_le_64(src);
586 : 0 : sqe->dst_addr = rte_cpu_to_le_64(dst);
587 : 0 : hw->sq_tail = (hw->sq_tail + 1) & hw->sq_depth_mask;
588 : 0 : hw->submitted++;
589 : :
590 [ # # ]: 0 : if (flags & RTE_DMA_OP_FLAG_FENCE)
591 : 0 : sqe->dw0 |= rte_cpu_to_le_32(SQE_FENCE_FLAG);
592 [ # # ]: 0 : if (flags & RTE_DMA_OP_FLAG_SUBMIT)
593 : 0 : rte_write32(rte_cpu_to_le_32(hw->sq_tail), hw->sq_tail_reg);
594 : :
595 : 0 : return hw->ridx++;
596 : : }
597 : :
598 : : static int
599 : 0 : hisi_dma_submit(void *dev_private, uint16_t vchan)
600 : : {
601 : : struct hisi_dma_dev *hw = dev_private;
602 : :
603 : : RTE_SET_USED(vchan);
604 : 0 : rte_write32(rte_cpu_to_le_32(hw->sq_tail), hw->sq_tail_reg);
605 : :
606 : 0 : return 0;
607 : : }
608 : :
609 : : static inline void
610 : 0 : hisi_dma_scan_cq(struct hisi_dma_dev *hw)
611 : : {
612 : : volatile struct hisi_dma_cqe *cqe;
613 : 0 : uint16_t csq_head = hw->cq_sq_head;
614 : 0 : uint16_t cq_head = hw->cq_head;
615 : : uint16_t count = 0;
616 : : uint64_t misc;
617 : :
618 [ # # ]: 0 : while (count < hw->cq_depth) {
619 : 0 : cqe = &hw->cqe[cq_head];
620 : 0 : misc = cqe->misc;
621 : : misc = rte_le_to_cpu_64(misc);
622 [ # # ]: 0 : if (FIELD_GET(CQE_VALID_B, misc) != hw->cqe_vld)
623 : : break;
624 : :
625 : 0 : csq_head = FIELD_GET(CQE_SQ_HEAD_MASK, misc);
626 [ # # ]: 0 : if (unlikely(csq_head > hw->sq_depth_mask)) {
627 : : /**
628 : : * Defensive programming to prevent overflow of the
629 : : * status array indexed by csq_head. Only error logs
630 : : * are used for prompting.
631 : : */
632 : 0 : HISI_DMA_ERR(hw, "invalid csq_head:%u!", csq_head);
633 : : count = 0;
634 : : break;
635 : : }
636 [ # # ]: 0 : if (unlikely(misc & CQE_STATUS_MASK))
637 : 0 : hw->status[csq_head] = FIELD_GET(CQE_STATUS_MASK,
638 : : misc);
639 : :
640 : 0 : count++;
641 : 0 : cq_head++;
642 [ # # ]: 0 : if (cq_head == hw->cq_depth) {
643 : 0 : hw->cqe_vld = !hw->cqe_vld;
644 : : cq_head = 0;
645 : : }
646 : : }
647 : :
648 [ # # ]: 0 : if (count == 0)
649 : 0 : return;
650 : :
651 : 0 : hw->cq_head = cq_head;
652 : 0 : hw->cq_sq_head = (csq_head + 1) & hw->sq_depth_mask;
653 : 0 : hw->cqs_completed += count;
654 [ # # ]: 0 : if (hw->cqs_completed >= HISI_DMA_CQ_RESERVED) {
655 : 0 : rte_write32(rte_cpu_to_le_32(cq_head), hw->cq_head_reg);
656 : 0 : hw->cqs_completed = 0;
657 : : }
658 : : }
659 : :
660 : : static inline uint16_t
661 : : hisi_dma_calc_cpls(struct hisi_dma_dev *hw, const uint16_t nb_cpls)
662 : : {
663 : : uint16_t cpl_num;
664 : :
665 [ # # # # ]: 0 : if (hw->cq_sq_head >= hw->sq_head)
666 : 0 : cpl_num = hw->cq_sq_head - hw->sq_head;
667 : : else
668 : 0 : cpl_num = hw->sq_depth_mask + 1 - hw->sq_head + hw->cq_sq_head;
669 : :
670 : : if (cpl_num > nb_cpls)
671 : : cpl_num = nb_cpls;
672 : :
673 : : return cpl_num;
674 : : }
675 : :
676 : : static uint16_t
677 : 0 : hisi_dma_completed(void *dev_private,
678 : : uint16_t vchan, const uint16_t nb_cpls,
679 : : uint16_t *last_idx, bool *has_error)
680 : : {
681 : : struct hisi_dma_dev *hw = dev_private;
682 : 0 : uint16_t sq_head = hw->sq_head;
683 : : uint16_t cpl_num, i;
684 : :
685 : : RTE_SET_USED(vchan);
686 : 0 : hisi_dma_scan_cq(hw);
687 : :
688 : : cpl_num = hisi_dma_calc_cpls(hw, nb_cpls);
689 [ # # ]: 0 : for (i = 0; i < cpl_num; i++) {
690 [ # # ]: 0 : if (hw->status[sq_head]) {
691 : 0 : *has_error = true;
692 : 0 : break;
693 : : }
694 : 0 : sq_head = (sq_head + 1) & hw->sq_depth_mask;
695 : : }
696 : 0 : *last_idx = hw->cridx + i - 1;
697 [ # # ]: 0 : if (i > 0) {
698 : 0 : hw->cridx += i;
699 : 0 : hw->sq_head = sq_head;
700 : 0 : hw->completed += i;
701 : : }
702 : :
703 : 0 : return i;
704 : : }
705 : :
706 : : static enum rte_dma_status_code
707 : 0 : hisi_dma_convert_status(uint16_t status)
708 : : {
709 [ # # # # : 0 : switch (status) {
# # # # #
# ]
710 : : case HISI_DMA_STATUS_SUCCESS:
711 : : return RTE_DMA_STATUS_SUCCESSFUL;
712 : 0 : case HISI_DMA_STATUS_INVALID_OPCODE:
713 : 0 : return RTE_DMA_STATUS_INVALID_OPCODE;
714 : 0 : case HISI_DMA_STATUS_INVALID_LENGTH:
715 : 0 : return RTE_DMA_STATUS_INVALID_LENGTH;
716 : 0 : case HISI_DMA_STATUS_USER_ABORT:
717 : 0 : return RTE_DMA_STATUS_USER_ABORT;
718 : 0 : case HISI_DMA_STATUS_REMOTE_READ_ERROR:
719 : : case HISI_DMA_STATUS_AXI_READ_ERROR:
720 : 0 : return RTE_DMA_STATUS_BUS_READ_ERROR;
721 : 0 : case HISI_DMA_STATUS_AXI_WRITE_ERROR:
722 : 0 : return RTE_DMA_STATUS_BUS_WRITE_ERROR;
723 : 0 : case HISI_DMA_STATUS_DATA_POISON:
724 : : case HISI_DMA_STATUS_REMOTE_DATA_POISION:
725 : 0 : return RTE_DMA_STATUS_DATA_POISION;
726 : 0 : case HISI_DMA_STATUS_SQE_READ_ERROR:
727 : : case HISI_DMA_STATUS_SQE_READ_POISION:
728 : 0 : return RTE_DMA_STATUS_DESCRIPTOR_READ_ERROR;
729 : 0 : case HISI_DMA_STATUS_LINK_DOWN_ERROR:
730 : 0 : return RTE_DMA_STATUS_DEV_LINK_ERROR;
731 : 0 : default:
732 : 0 : return RTE_DMA_STATUS_ERROR_UNKNOWN;
733 : : }
734 : : }
735 : :
736 : : static uint16_t
737 : 0 : hisi_dma_completed_status(void *dev_private,
738 : : uint16_t vchan, const uint16_t nb_cpls,
739 : : uint16_t *last_idx, enum rte_dma_status_code *status)
740 : : {
741 : : struct hisi_dma_dev *hw = dev_private;
742 : 0 : uint16_t sq_head = hw->sq_head;
743 : : uint16_t cpl_num, i;
744 : :
745 : : RTE_SET_USED(vchan);
746 : 0 : hisi_dma_scan_cq(hw);
747 : :
748 : : cpl_num = hisi_dma_calc_cpls(hw, nb_cpls);
749 [ # # ]: 0 : for (i = 0; i < cpl_num; i++) {
750 : 0 : status[i] = hisi_dma_convert_status(hw->status[sq_head]);
751 : 0 : hw->errors += !!status[i];
752 : 0 : hw->status[sq_head] = HISI_DMA_STATUS_SUCCESS;
753 : 0 : sq_head = (sq_head + 1) & hw->sq_depth_mask;
754 : : }
755 : 0 : *last_idx = hw->cridx + cpl_num - 1;
756 [ # # ]: 0 : if (likely(cpl_num > 0)) {
757 : 0 : hw->cridx += cpl_num;
758 : 0 : hw->sq_head = sq_head;
759 : 0 : hw->completed += cpl_num;
760 : : }
761 : :
762 : 0 : return cpl_num;
763 : : }
764 : :
765 : : static uint16_t
766 : 0 : hisi_dma_burst_capacity(const void *dev_private, uint16_t vchan)
767 : : {
768 : : const struct hisi_dma_dev *hw = dev_private;
769 : 0 : uint16_t sq_head = hw->sq_head;
770 : 0 : uint16_t sq_tail = hw->sq_tail;
771 : :
772 : : RTE_SET_USED(vchan);
773 : :
774 [ # # ]: 0 : return (sq_tail >= sq_head) ? hw->sq_depth_mask - sq_tail + sq_head :
775 : 0 : sq_head - 1 - sq_tail;
776 : : }
777 : :
778 : : static void
779 : 0 : hisi_dma_gen_dev_name(const struct rte_pci_device *pci_dev,
780 : : uint8_t queue_id, char *dev_name, size_t size)
781 : : {
782 : 0 : char name[RTE_DEV_NAME_MAX_LEN] = { 0 };
783 : :
784 : : memset(dev_name, 0, size);
785 : 0 : rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
786 : 0 : (void)snprintf(dev_name, size, "%s-ch%u", name, queue_id);
787 : 0 : }
788 : :
789 : : /**
790 : : * Hardware queue state machine:
791 : : *
792 : : * ----------- dmadev_create ------------------
793 : : * | Unknown | ---------------> | IDLE |
794 : : * ----------- ------------------
795 : : * ^ |
796 : : * | |dev_start
797 : : * dev_stop| |
798 : : * | v
799 : : * ------------------
800 : : * | CPL |
801 : : * ------------------
802 : : * ^ |
803 : : * hardware | |
804 : : * completed all| |dev_submit
805 : : * descriptors | |
806 : : * | |
807 : : * ------------------
808 : : * | RUN |
809 : : * ------------------
810 : : *
811 : : */
812 : : static const struct rte_dma_dev_ops hisi_dmadev_ops = {
813 : : .dev_info_get = hisi_dma_info_get,
814 : : .dev_configure = hisi_dma_configure,
815 : : .dev_start = hisi_dma_start,
816 : : .dev_stop = hisi_dma_stop,
817 : : .dev_close = hisi_dma_close,
818 : : .vchan_setup = hisi_dma_vchan_setup,
819 : : .stats_get = hisi_dma_stats_get,
820 : : .stats_reset = hisi_dma_stats_reset,
821 : : .vchan_status = hisi_dma_vchan_status,
822 : : .dev_dump = hisi_dma_dump,
823 : : };
824 : :
825 : : static int
826 : 0 : hisi_dma_create(struct rte_pci_device *pci_dev, uint8_t queue_id,
827 : : uint8_t revision)
828 : : {
829 : : #define REG_PCI_BAR_INDEX 2
830 : :
831 : : char name[RTE_DEV_NAME_MAX_LEN];
832 : : struct rte_dma_dev *dev;
833 : : struct hisi_dma_dev *hw;
834 : : int ret;
835 : :
836 : 0 : hisi_dma_gen_dev_name(pci_dev, queue_id, name, sizeof(name));
837 : 0 : dev = rte_dma_pmd_allocate(name, pci_dev->device.numa_node,
838 : : sizeof(*hw));
839 [ # # ]: 0 : if (dev == NULL) {
840 : 0 : HISI_DMA_LOG(ERR, "%s allocate dmadev fail!", name);
841 : 0 : return -EINVAL;
842 : : }
843 : :
844 : 0 : dev->device = &pci_dev->device;
845 : 0 : dev->dev_ops = &hisi_dmadev_ops;
846 : 0 : dev->fp_obj->dev_private = dev->data->dev_private;
847 : 0 : dev->fp_obj->copy = hisi_dma_copy;
848 : 0 : dev->fp_obj->submit = hisi_dma_submit;
849 : 0 : dev->fp_obj->completed = hisi_dma_completed;
850 : 0 : dev->fp_obj->completed_status = hisi_dma_completed_status;
851 : 0 : dev->fp_obj->burst_capacity = hisi_dma_burst_capacity;
852 : :
853 : : hw = dev->data->dev_private;
854 : 0 : hw->data = dev->data;
855 [ # # ]: 0 : hw->revision = revision;
856 : 0 : hw->reg_layout = hisi_dma_reg_layout(revision);
857 : 0 : hw->io_base = pci_dev->mem_resource[REG_PCI_BAR_INDEX].addr;
858 : 0 : hw->queue_id = queue_id;
859 : 0 : hw->sq_tail_reg = hisi_dma_queue_regaddr(hw,
860 : : HISI_DMA_QUEUE_SQ_TAIL_REG);
861 : 0 : hw->cq_head_reg = hisi_dma_queue_regaddr(hw,
862 : : HISI_DMA_QUEUE_CQ_HEAD_REG);
863 : :
864 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
865 : 0 : ret = hisi_dma_reset_hw(hw);
866 [ # # ]: 0 : if (ret) {
867 : 0 : HISI_DMA_LOG(ERR, "%s init device fail!", name);
868 : 0 : (void)rte_dma_pmd_release(name);
869 : 0 : return -EIO;
870 : : }
871 : : }
872 : :
873 : 0 : dev->state = RTE_DMA_DEV_READY;
874 : 0 : HISI_DMA_LOG(DEBUG, "%s create dmadev success!", name);
875 : :
876 : 0 : return 0;
877 : : }
878 : :
879 : : static int
880 : 0 : hisi_dma_check_revision(struct rte_pci_device *pci_dev, const char *name,
881 : : uint8_t *out_revision)
882 : : {
883 : : uint8_t revision;
884 : : int ret;
885 : :
886 : 0 : ret = rte_pci_read_config(pci_dev, &revision, 1,
887 : : HISI_DMA_PCI_REVISION_ID_REG);
888 [ # # ]: 0 : if (ret != 1) {
889 : 0 : HISI_DMA_LOG(ERR, "%s read PCI revision failed!", name);
890 : 0 : return -EINVAL;
891 : : }
892 [ # # ]: 0 : if (hisi_dma_reg_layout(revision) == HISI_DMA_REG_LAYOUT_INVALID) {
893 : 0 : HISI_DMA_LOG(ERR, "%s revision: 0x%x not supported!",
894 : : name, revision);
895 : 0 : return -EINVAL;
896 : : }
897 : :
898 : 0 : *out_revision = revision;
899 : 0 : return 0;
900 : : }
901 : :
902 : : static int
903 : 0 : hisi_dma_probe(struct rte_pci_driver *pci_drv __rte_unused,
904 : : struct rte_pci_device *pci_dev)
905 : : {
906 : 0 : char name[RTE_DEV_NAME_MAX_LEN] = { 0 };
907 : : uint8_t revision;
908 : : uint8_t i;
909 : : int ret;
910 : :
911 : 0 : rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
912 : :
913 [ # # ]: 0 : if (pci_dev->mem_resource[2].addr == NULL) {
914 : 0 : HISI_DMA_LOG(ERR, "%s BAR2 is NULL!", name);
915 : 0 : return -ENODEV;
916 : : }
917 : :
918 : 0 : ret = hisi_dma_check_revision(pci_dev, name, &revision);
919 [ # # ]: 0 : if (ret)
920 : : return ret;
921 : 0 : HISI_DMA_LOG(DEBUG, "%s read PCI revision: 0x%x", name, revision);
922 : :
923 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
924 : 0 : hisi_dma_init_gbl(pci_dev->mem_resource[2].addr, revision);
925 : :
926 [ # # ]: 0 : for (i = 0; i < HISI_DMA_MAX_HW_QUEUES; i++) {
927 : 0 : ret = hisi_dma_create(pci_dev, i, revision);
928 [ # # ]: 0 : if (ret) {
929 : 0 : HISI_DMA_LOG(ERR, "%s create dmadev %u failed!",
930 : : name, i);
931 : 0 : break;
932 : : }
933 : : }
934 : :
935 : : return ret;
936 : : }
937 : :
938 : : static int
939 : 0 : hisi_dma_remove(struct rte_pci_device *pci_dev)
940 : : {
941 : : char name[RTE_DEV_NAME_MAX_LEN];
942 : : uint8_t i;
943 : : int ret;
944 : :
945 [ # # ]: 0 : for (i = 0; i < HISI_DMA_MAX_HW_QUEUES; i++) {
946 : 0 : hisi_dma_gen_dev_name(pci_dev, i, name, sizeof(name));
947 : 0 : ret = rte_dma_pmd_release(name);
948 [ # # ]: 0 : if (ret)
949 : 0 : return ret;
950 : : }
951 : :
952 : : return 0;
953 : : }
954 : :
955 : : static const struct rte_pci_id pci_id_hisi_dma_map[] = {
956 : : { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HISI_DMA_DEVICE_ID) },
957 : : { .vendor_id = 0, }, /* sentinel */
958 : : };
959 : :
960 : : static struct rte_pci_driver hisi_dma_pmd_drv = {
961 : : .id_table = pci_id_hisi_dma_map,
962 : : .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
963 : : .probe = hisi_dma_probe,
964 : : .remove = hisi_dma_remove,
965 : : };
966 : :
967 : 251 : RTE_PMD_REGISTER_PCI(dma_hisilicon, hisi_dma_pmd_drv);
968 : : RTE_PMD_REGISTER_PCI_TABLE(dma_hisilicon, pci_id_hisi_dma_map);
969 : : RTE_PMD_REGISTER_KMOD_DEP(dma_hisilicon, "vfio-pci");
|