1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2021 HiSilicon Limited
3 */
4
5 #include <inttypes.h>
6
7 #include <rte_bus_vdev.h>
8 #include <rte_cycles.h>
9 #include <rte_eal.h>
10 #include <rte_kvargs.h>
11 #include <rte_lcore.h>
12 #include <rte_log.h>
13 #include <rte_malloc.h>
14 #include <rte_memcpy.h>
15
16 #include <rte_dmadev_pmd.h>
17
18 #include "skeleton_dmadev.h"
19
20 RTE_LOG_REGISTER_DEFAULT(skeldma_logtype, INFO);
21 #define SKELDMA_LOG(level, fmt, args...) \
22 rte_log(RTE_LOG_ ## level, skeldma_logtype, "%s(): " fmt "\n", \
23 __func__, ##args)
24
25 /* Count of instances, currently only 1 is supported. */
26 static uint16_t skeldma_count;
27
28 static int
skeldma_info_get(const struct rte_dma_dev * dev,struct rte_dma_info * dev_info,uint32_t info_sz)29 skeldma_info_get(const struct rte_dma_dev *dev, struct rte_dma_info *dev_info,
30 uint32_t info_sz)
31 {
32 #define SKELDMA_MAX_DESC 8192
33 #define SKELDMA_MIN_DESC 32
34
35 RTE_SET_USED(dev);
36 RTE_SET_USED(info_sz);
37
38 dev_info->dev_capa = RTE_DMA_CAPA_MEM_TO_MEM |
39 RTE_DMA_CAPA_SVA |
40 RTE_DMA_CAPA_OPS_COPY;
41 dev_info->max_vchans = 1;
42 dev_info->max_desc = SKELDMA_MAX_DESC;
43 dev_info->min_desc = SKELDMA_MIN_DESC;
44
45 return 0;
46 }
47
48 static int
skeldma_configure(struct rte_dma_dev * dev,const struct rte_dma_conf * conf,uint32_t conf_sz)49 skeldma_configure(struct rte_dma_dev *dev, const struct rte_dma_conf *conf,
50 uint32_t conf_sz)
51 {
52 RTE_SET_USED(dev);
53 RTE_SET_USED(conf);
54 RTE_SET_USED(conf_sz);
55 return 0;
56 }
57
58 static void *
cpucopy_thread(void * param)59 cpucopy_thread(void *param)
60 {
61 #define SLEEP_THRESHOLD 10000
62 #define SLEEP_US_VAL 10
63
64 struct rte_dma_dev *dev = param;
65 struct skeldma_hw *hw = dev->data->dev_private;
66 struct skeldma_desc *desc = NULL;
67 int ret;
68
69 while (!hw->exit_flag) {
70 ret = rte_ring_dequeue(hw->desc_running, (void **)&desc);
71 if (ret) {
72 hw->zero_req_count++;
73 if (hw->zero_req_count == 0)
74 hw->zero_req_count = SLEEP_THRESHOLD;
75 if (hw->zero_req_count >= SLEEP_THRESHOLD)
76 rte_delay_us_sleep(SLEEP_US_VAL);
77 continue;
78 }
79
80 hw->zero_req_count = 0;
81 rte_memcpy(desc->dst, desc->src, desc->len);
82 __atomic_add_fetch(&hw->completed_count, 1, __ATOMIC_RELEASE);
83 (void)rte_ring_enqueue(hw->desc_completed, (void *)desc);
84 }
85
86 return NULL;
87 }
88
89 static void
fflush_ring(struct skeldma_hw * hw,struct rte_ring * ring)90 fflush_ring(struct skeldma_hw *hw, struct rte_ring *ring)
91 {
92 struct skeldma_desc *desc = NULL;
93 while (rte_ring_count(ring) > 0) {
94 (void)rte_ring_dequeue(ring, (void **)&desc);
95 (void)rte_ring_enqueue(hw->desc_empty, (void *)desc);
96 }
97 }
98
99 static int
skeldma_start(struct rte_dma_dev * dev)100 skeldma_start(struct rte_dma_dev *dev)
101 {
102 struct skeldma_hw *hw = dev->data->dev_private;
103 rte_cpuset_t cpuset;
104 int ret;
105
106 if (hw->desc_mem == NULL) {
107 SKELDMA_LOG(ERR, "Vchan was not setup, start fail!");
108 return -EINVAL;
109 }
110
111 /* Reset the dmadev to a known state, include:
112 * 1) fflush pending/running/completed ring to empty ring.
113 * 2) init ring idx to zero.
114 * 3) init running statistics.
115 * 4) mark cpucopy task exit_flag to false.
116 */
117 fflush_ring(hw, hw->desc_pending);
118 fflush_ring(hw, hw->desc_running);
119 fflush_ring(hw, hw->desc_completed);
120 hw->ridx = 0;
121 hw->submitted_count = 0;
122 hw->zero_req_count = 0;
123 hw->completed_count = 0;
124 hw->exit_flag = false;
125
126 rte_mb();
127
128 ret = rte_ctrl_thread_create(&hw->thread, "dma_skeleton", NULL,
129 cpucopy_thread, dev);
130 if (ret) {
131 SKELDMA_LOG(ERR, "Start cpucopy thread fail!");
132 return -EINVAL;
133 }
134
135 if (hw->lcore_id != -1) {
136 cpuset = rte_lcore_cpuset(hw->lcore_id);
137 ret = pthread_setaffinity_np(hw->thread, sizeof(cpuset),
138 &cpuset);
139 if (ret)
140 SKELDMA_LOG(WARNING,
141 "Set thread affinity lcore = %d fail!",
142 hw->lcore_id);
143 }
144
145 return 0;
146 }
147
148 static int
skeldma_stop(struct rte_dma_dev * dev)149 skeldma_stop(struct rte_dma_dev *dev)
150 {
151 struct skeldma_hw *hw = dev->data->dev_private;
152
153 hw->exit_flag = true;
154 rte_delay_ms(1);
155
156 (void)pthread_cancel(hw->thread);
157 pthread_join(hw->thread, NULL);
158
159 return 0;
160 }
161
162 static int
vchan_setup(struct skeldma_hw * hw,uint16_t nb_desc)163 vchan_setup(struct skeldma_hw *hw, uint16_t nb_desc)
164 {
165 struct skeldma_desc *desc;
166 struct rte_ring *empty;
167 struct rte_ring *pending;
168 struct rte_ring *running;
169 struct rte_ring *completed;
170 uint16_t i;
171
172 desc = rte_zmalloc_socket("dma_skeleton_desc",
173 nb_desc * sizeof(struct skeldma_desc),
174 RTE_CACHE_LINE_SIZE, hw->socket_id);
175 if (desc == NULL) {
176 SKELDMA_LOG(ERR, "Malloc dma skeleton desc fail!");
177 return -ENOMEM;
178 }
179
180 empty = rte_ring_create("dma_skeleton_desc_empty", nb_desc,
181 hw->socket_id, RING_F_SP_ENQ | RING_F_SC_DEQ);
182 pending = rte_ring_create("dma_skeleton_desc_pending", nb_desc,
183 hw->socket_id, RING_F_SP_ENQ | RING_F_SC_DEQ);
184 running = rte_ring_create("dma_skeleton_desc_running", nb_desc,
185 hw->socket_id, RING_F_SP_ENQ | RING_F_SC_DEQ);
186 completed = rte_ring_create("dma_skeleton_desc_completed", nb_desc,
187 hw->socket_id, RING_F_SP_ENQ | RING_F_SC_DEQ);
188 if (empty == NULL || pending == NULL || running == NULL ||
189 completed == NULL) {
190 SKELDMA_LOG(ERR, "Create dma skeleton desc ring fail!");
191 rte_ring_free(empty);
192 rte_ring_free(pending);
193 rte_ring_free(running);
194 rte_ring_free(completed);
195 rte_free(desc);
196 return -ENOMEM;
197 }
198
199 /* The real usable ring size is *count-1* instead of *count* to
200 * differentiate a free ring from an empty ring.
201 * @see rte_ring_create
202 */
203 for (i = 0; i < nb_desc - 1; i++)
204 (void)rte_ring_enqueue(empty, (void *)(desc + i));
205
206 hw->desc_mem = desc;
207 hw->desc_empty = empty;
208 hw->desc_pending = pending;
209 hw->desc_running = running;
210 hw->desc_completed = completed;
211
212 return 0;
213 }
214
215 static void
vchan_release(struct skeldma_hw * hw)216 vchan_release(struct skeldma_hw *hw)
217 {
218 if (hw->desc_mem == NULL)
219 return;
220
221 rte_free(hw->desc_mem);
222 hw->desc_mem = NULL;
223 rte_ring_free(hw->desc_empty);
224 hw->desc_empty = NULL;
225 rte_ring_free(hw->desc_pending);
226 hw->desc_pending = NULL;
227 rte_ring_free(hw->desc_running);
228 hw->desc_running = NULL;
229 rte_ring_free(hw->desc_completed);
230 hw->desc_completed = NULL;
231 }
232
233 static int
skeldma_close(struct rte_dma_dev * dev)234 skeldma_close(struct rte_dma_dev *dev)
235 {
236 /* The device already stopped */
237 vchan_release(dev->data->dev_private);
238 return 0;
239 }
240
241 static int
skeldma_vchan_setup(struct rte_dma_dev * dev,uint16_t vchan,const struct rte_dma_vchan_conf * conf,uint32_t conf_sz)242 skeldma_vchan_setup(struct rte_dma_dev *dev, uint16_t vchan,
243 const struct rte_dma_vchan_conf *conf,
244 uint32_t conf_sz)
245 {
246 struct skeldma_hw *hw = dev->data->dev_private;
247
248 RTE_SET_USED(vchan);
249 RTE_SET_USED(conf_sz);
250
251 if (!rte_is_power_of_2(conf->nb_desc)) {
252 SKELDMA_LOG(ERR, "Number of desc must be power of 2!");
253 return -EINVAL;
254 }
255
256 vchan_release(hw);
257 return vchan_setup(hw, conf->nb_desc);
258 }
259
260 static int
skeldma_vchan_status(const struct rte_dma_dev * dev,uint16_t vchan,enum rte_dma_vchan_status * status)261 skeldma_vchan_status(const struct rte_dma_dev *dev,
262 uint16_t vchan, enum rte_dma_vchan_status *status)
263 {
264 struct skeldma_hw *hw = dev->data->dev_private;
265
266 RTE_SET_USED(vchan);
267
268 *status = RTE_DMA_VCHAN_IDLE;
269 if (hw->submitted_count != __atomic_load_n(&hw->completed_count, __ATOMIC_ACQUIRE)
270 || hw->zero_req_count == 0)
271 *status = RTE_DMA_VCHAN_ACTIVE;
272 return 0;
273 }
274
275 static int
skeldma_stats_get(const struct rte_dma_dev * dev,uint16_t vchan,struct rte_dma_stats * stats,uint32_t stats_sz)276 skeldma_stats_get(const struct rte_dma_dev *dev, uint16_t vchan,
277 struct rte_dma_stats *stats, uint32_t stats_sz)
278 {
279 struct skeldma_hw *hw = dev->data->dev_private;
280
281 RTE_SET_USED(vchan);
282 RTE_SET_USED(stats_sz);
283
284 stats->submitted = hw->submitted_count;
285 stats->completed = hw->completed_count;
286 stats->errors = 0;
287
288 return 0;
289 }
290
291 static int
skeldma_stats_reset(struct rte_dma_dev * dev,uint16_t vchan)292 skeldma_stats_reset(struct rte_dma_dev *dev, uint16_t vchan)
293 {
294 struct skeldma_hw *hw = dev->data->dev_private;
295
296 RTE_SET_USED(vchan);
297
298 hw->submitted_count = 0;
299 hw->completed_count = 0;
300
301 return 0;
302 }
303
304 static int
skeldma_dump(const struct rte_dma_dev * dev,FILE * f)305 skeldma_dump(const struct rte_dma_dev *dev, FILE *f)
306 {
307 #define GET_RING_COUNT(ring) ((ring) ? (rte_ring_count(ring)) : 0)
308
309 struct skeldma_hw *hw = dev->data->dev_private;
310
311 (void)fprintf(f,
312 " lcore_id: %d\n"
313 " socket_id: %d\n"
314 " desc_empty_ring_count: %u\n"
315 " desc_pending_ring_count: %u\n"
316 " desc_running_ring_count: %u\n"
317 " desc_completed_ring_count: %u\n",
318 hw->lcore_id, hw->socket_id,
319 GET_RING_COUNT(hw->desc_empty),
320 GET_RING_COUNT(hw->desc_pending),
321 GET_RING_COUNT(hw->desc_running),
322 GET_RING_COUNT(hw->desc_completed));
323 (void)fprintf(f,
324 " next_ring_idx: %u\n"
325 " submitted_count: %" PRIu64 "\n"
326 " completed_count: %" PRIu64 "\n",
327 hw->ridx, hw->submitted_count, hw->completed_count);
328
329 return 0;
330 }
331
332 static inline void
submit(struct skeldma_hw * hw,struct skeldma_desc * desc)333 submit(struct skeldma_hw *hw, struct skeldma_desc *desc)
334 {
335 uint16_t count = rte_ring_count(hw->desc_pending);
336 struct skeldma_desc *pend_desc = NULL;
337
338 while (count > 0) {
339 (void)rte_ring_dequeue(hw->desc_pending, (void **)&pend_desc);
340 (void)rte_ring_enqueue(hw->desc_running, (void *)pend_desc);
341 count--;
342 }
343
344 if (desc)
345 (void)rte_ring_enqueue(hw->desc_running, (void *)desc);
346 }
347
348 static int
skeldma_copy(void * dev_private,uint16_t vchan,rte_iova_t src,rte_iova_t dst,uint32_t length,uint64_t flags)349 skeldma_copy(void *dev_private, uint16_t vchan,
350 rte_iova_t src, rte_iova_t dst,
351 uint32_t length, uint64_t flags)
352 {
353 struct skeldma_hw *hw = dev_private;
354 struct skeldma_desc *desc;
355 int ret;
356
357 RTE_SET_USED(vchan);
358 RTE_SET_USED(flags);
359
360 ret = rte_ring_dequeue(hw->desc_empty, (void **)&desc);
361 if (ret)
362 return -ENOSPC;
363 desc->src = (void *)(uintptr_t)src;
364 desc->dst = (void *)(uintptr_t)dst;
365 desc->len = length;
366 desc->ridx = hw->ridx;
367 if (flags & RTE_DMA_OP_FLAG_SUBMIT)
368 submit(hw, desc);
369 else
370 (void)rte_ring_enqueue(hw->desc_pending, (void *)desc);
371 hw->submitted_count++;
372
373 return hw->ridx++;
374 }
375
376 static int
skeldma_submit(void * dev_private,uint16_t vchan)377 skeldma_submit(void *dev_private, uint16_t vchan)
378 {
379 struct skeldma_hw *hw = dev_private;
380 RTE_SET_USED(vchan);
381 submit(hw, NULL);
382 return 0;
383 }
384
385 static uint16_t
skeldma_completed(void * dev_private,uint16_t vchan,const uint16_t nb_cpls,uint16_t * last_idx,bool * has_error)386 skeldma_completed(void *dev_private,
387 uint16_t vchan, const uint16_t nb_cpls,
388 uint16_t *last_idx, bool *has_error)
389 {
390 struct skeldma_hw *hw = dev_private;
391 struct skeldma_desc *desc = NULL;
392 uint16_t index = 0;
393 uint16_t count;
394
395 RTE_SET_USED(vchan);
396 RTE_SET_USED(has_error);
397
398 count = RTE_MIN(nb_cpls, rte_ring_count(hw->desc_completed));
399 while (index < count) {
400 (void)rte_ring_dequeue(hw->desc_completed, (void **)&desc);
401 if (index == count - 1)
402 *last_idx = desc->ridx;
403 index++;
404 (void)rte_ring_enqueue(hw->desc_empty, (void *)desc);
405 }
406
407 return count;
408 }
409
410 static uint16_t
skeldma_completed_status(void * dev_private,uint16_t vchan,const uint16_t nb_cpls,uint16_t * last_idx,enum rte_dma_status_code * status)411 skeldma_completed_status(void *dev_private,
412 uint16_t vchan, const uint16_t nb_cpls,
413 uint16_t *last_idx, enum rte_dma_status_code *status)
414 {
415 struct skeldma_hw *hw = dev_private;
416 struct skeldma_desc *desc = NULL;
417 uint16_t index = 0;
418 uint16_t count;
419
420 RTE_SET_USED(vchan);
421
422 count = RTE_MIN(nb_cpls, rte_ring_count(hw->desc_completed));
423 while (index < count) {
424 (void)rte_ring_dequeue(hw->desc_completed, (void **)&desc);
425 if (index == count - 1)
426 *last_idx = desc->ridx;
427 status[index++] = RTE_DMA_STATUS_SUCCESSFUL;
428 (void)rte_ring_enqueue(hw->desc_empty, (void *)desc);
429 }
430
431 return count;
432 }
433
434 static uint16_t
skeldma_burst_capacity(const void * dev_private,uint16_t vchan)435 skeldma_burst_capacity(const void *dev_private, uint16_t vchan)
436 {
437 const struct skeldma_hw *hw = dev_private;
438
439 RTE_SET_USED(vchan);
440 return rte_ring_count(hw->desc_empty);
441 }
442
443 static const struct rte_dma_dev_ops skeldma_ops = {
444 .dev_info_get = skeldma_info_get,
445 .dev_configure = skeldma_configure,
446 .dev_start = skeldma_start,
447 .dev_stop = skeldma_stop,
448 .dev_close = skeldma_close,
449
450 .vchan_setup = skeldma_vchan_setup,
451 .vchan_status = skeldma_vchan_status,
452
453 .stats_get = skeldma_stats_get,
454 .stats_reset = skeldma_stats_reset,
455
456 .dev_dump = skeldma_dump,
457 };
458
459 static int
skeldma_create(const char * name,struct rte_vdev_device * vdev,int lcore_id)460 skeldma_create(const char *name, struct rte_vdev_device *vdev, int lcore_id)
461 {
462 struct rte_dma_dev *dev;
463 struct skeldma_hw *hw;
464 int socket_id;
465
466 socket_id = (lcore_id < 0) ? rte_socket_id() :
467 rte_lcore_to_socket_id(lcore_id);
468 dev = rte_dma_pmd_allocate(name, socket_id, sizeof(struct skeldma_hw));
469 if (dev == NULL) {
470 SKELDMA_LOG(ERR, "Unable to allocate dmadev: %s", name);
471 return -EINVAL;
472 }
473
474 dev->device = &vdev->device;
475 dev->dev_ops = &skeldma_ops;
476 dev->fp_obj->dev_private = dev->data->dev_private;
477 dev->fp_obj->copy = skeldma_copy;
478 dev->fp_obj->submit = skeldma_submit;
479 dev->fp_obj->completed = skeldma_completed;
480 dev->fp_obj->completed_status = skeldma_completed_status;
481 dev->fp_obj->burst_capacity = skeldma_burst_capacity;
482
483 hw = dev->data->dev_private;
484 hw->lcore_id = lcore_id;
485 hw->socket_id = socket_id;
486
487 dev->state = RTE_DMA_DEV_READY;
488
489 return dev->data->dev_id;
490 }
491
492 static int
skeldma_destroy(const char * name)493 skeldma_destroy(const char *name)
494 {
495 return rte_dma_pmd_release(name);
496 }
497
498 static int
skeldma_parse_lcore(const char * key __rte_unused,const char * value,void * opaque)499 skeldma_parse_lcore(const char *key __rte_unused,
500 const char *value,
501 void *opaque)
502 {
503 int lcore_id = atoi(value);
504 if (lcore_id >= 0 && lcore_id < RTE_MAX_LCORE)
505 *(int *)opaque = lcore_id;
506 return 0;
507 }
508
509 static void
skeldma_parse_vdev_args(struct rte_vdev_device * vdev,int * lcore_id)510 skeldma_parse_vdev_args(struct rte_vdev_device *vdev, int *lcore_id)
511 {
512 static const char *const args[] = {
513 SKELDMA_ARG_LCORE,
514 NULL
515 };
516
517 struct rte_kvargs *kvlist;
518 const char *params;
519
520 params = rte_vdev_device_args(vdev);
521 if (params == NULL || params[0] == '\0')
522 return;
523
524 kvlist = rte_kvargs_parse(params, args);
525 if (!kvlist)
526 return;
527
528 (void)rte_kvargs_process(kvlist, SKELDMA_ARG_LCORE,
529 skeldma_parse_lcore, lcore_id);
530 SKELDMA_LOG(INFO, "Parse lcore_id = %d", *lcore_id);
531
532 rte_kvargs_free(kvlist);
533 }
534
535 static int
skeldma_probe(struct rte_vdev_device * vdev)536 skeldma_probe(struct rte_vdev_device *vdev)
537 {
538 const char *name;
539 int lcore_id = -1;
540 int ret;
541
542 name = rte_vdev_device_name(vdev);
543 if (name == NULL)
544 return -EINVAL;
545
546 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
547 SKELDMA_LOG(ERR, "Multiple process not supported for %s", name);
548 return -EINVAL;
549 }
550
551 /* More than one instance is not supported */
552 if (skeldma_count > 0) {
553 SKELDMA_LOG(ERR, "Multiple instance not supported for %s",
554 name);
555 return -EINVAL;
556 }
557
558 skeldma_parse_vdev_args(vdev, &lcore_id);
559
560 ret = skeldma_create(name, vdev, lcore_id);
561 if (ret >= 0) {
562 SKELDMA_LOG(INFO, "Create %s dmadev with lcore-id %d",
563 name, lcore_id);
564 skeldma_count = 1;
565 }
566
567 return ret < 0 ? ret : 0;
568 }
569
570 static int
skeldma_remove(struct rte_vdev_device * vdev)571 skeldma_remove(struct rte_vdev_device *vdev)
572 {
573 const char *name;
574 int ret;
575
576 name = rte_vdev_device_name(vdev);
577 if (name == NULL)
578 return -1;
579
580 ret = skeldma_destroy(name);
581 if (!ret) {
582 skeldma_count = 0;
583 SKELDMA_LOG(INFO, "Remove %s dmadev", name);
584 }
585
586 return ret;
587 }
588
589 static struct rte_vdev_driver skeldma_pmd_drv = {
590 .probe = skeldma_probe,
591 .remove = skeldma_remove,
592 .drv_flags = RTE_VDEV_DRV_NEED_IOVA_AS_VA,
593 };
594
595 RTE_PMD_REGISTER_VDEV(dma_skeleton, skeldma_pmd_drv);
596 RTE_PMD_REGISTER_PARAM_STRING(dma_skeleton,
597 SKELDMA_ARG_LCORE "=<uint16> ");
598