1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2021 Intel Corporation
7 #include <rte_malloc.h>
8 #include <rte_common.h>
10 #include <rte_prefetch.h>
12 #include "idxd_internal.h"
14 #define IDXD_PMD_NAME_STR "dmadev_idxd"
16 /* systems with DSA all support AVX2 so allow our data-path functions to
17 * always use at least that instruction set
20 #define __use_avx2 __attribute__((target("avx2")))
26 static __rte_always_inline rte_iova_t
27 __desc_idx_to_iova(struct idxd_dmadev *idxd, uint16_t n)
29 return idxd->desc_iova + (n * sizeof(struct idxd_hw_desc));
33 static __rte_always_inline void
34 __idxd_movdir64b(volatile void *dst, const struct idxd_hw_desc *src)
36 asm volatile (".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
38 : "a" (dst), "d" (src)
43 static __rte_always_inline void
44 __submit(struct idxd_dmadev *idxd)
46 rte_prefetch1(&idxd->batch_comp_ring[idxd->batch_idx_read]);
48 if (idxd->batch_size == 0)
51 /* write completion to batch comp ring */
52 rte_iova_t comp_addr = idxd->batch_iova +
53 (idxd->batch_idx_write * sizeof(struct idxd_completion));
55 if (idxd->batch_size == 1) {
56 /* submit batch directly */
57 struct idxd_hw_desc desc =
58 idxd->desc_ring[idxd->batch_start & idxd->desc_ring_mask];
59 desc.completion = comp_addr;
60 desc.op_flags |= IDXD_FLAG_REQUEST_COMPLETION;
61 _mm_sfence(); /* fence before writing desc to device */
62 __idxd_movdir64b(idxd->portal, &desc);
64 const struct idxd_hw_desc batch_desc = {
65 .op_flags = (idxd_op_batch << IDXD_CMD_OP_SHIFT) |
66 IDXD_FLAG_COMPLETION_ADDR_VALID |
67 IDXD_FLAG_REQUEST_COMPLETION,
68 .desc_addr = __desc_idx_to_iova(idxd,
69 idxd->batch_start & idxd->desc_ring_mask),
70 .completion = comp_addr,
71 .size = idxd->batch_size,
73 _mm_sfence(); /* fence before writing desc to device */
74 __idxd_movdir64b(idxd->portal, &batch_desc);
77 if (++idxd->batch_idx_write > idxd->max_batches)
78 idxd->batch_idx_write = 0;
80 idxd->stats.submitted += idxd->batch_size;
82 idxd->batch_start += idxd->batch_size;
84 idxd->batch_idx_ring[idxd->batch_idx_write] = idxd->batch_start;
85 _mm256_store_si256((void *)&idxd->batch_comp_ring[idxd->batch_idx_write],
86 _mm256_setzero_si256());
90 static __rte_always_inline int
91 __idxd_write_desc(struct idxd_dmadev *idxd,
92 const uint32_t op_flags,
98 uint16_t mask = idxd->desc_ring_mask;
99 uint16_t job_id = idxd->batch_start + idxd->batch_size;
100 /* we never wrap batches, so we only mask the start and allow start+size to overflow */
101 uint16_t write_idx = (idxd->batch_start & mask) + idxd->batch_size;
103 /* first check batch ring space then desc ring space */
104 if ((idxd->batch_idx_read == 0 && idxd->batch_idx_write == idxd->max_batches) ||
105 idxd->batch_idx_write + 1 == idxd->batch_idx_read)
107 if (((write_idx + 1) & mask) == (idxd->ids_returned & mask))
110 /* write desc. Note: descriptors don't wrap, but the completion address does */
111 const uint64_t op_flags64 = (uint64_t)(op_flags | IDXD_FLAG_COMPLETION_ADDR_VALID) << 32;
112 const uint64_t comp_addr = __desc_idx_to_iova(idxd, write_idx & mask);
113 _mm256_store_si256((void *)&idxd->desc_ring[write_idx],
114 _mm256_set_epi64x(dst, src, comp_addr, op_flags64));
115 _mm256_store_si256((void *)&idxd->desc_ring[write_idx].size,
116 _mm256_set_epi64x(0, 0, 0, size));
120 rte_prefetch0_write(&idxd->desc_ring[write_idx + 1]);
122 if (flags & RTE_DMA_OP_FLAG_SUBMIT)
130 idxd_enqueue_copy(void *dev_private, uint16_t qid __rte_unused, rte_iova_t src,
131 rte_iova_t dst, unsigned int length, uint64_t flags)
133 /* we can take advantage of the fact that the fence flag in dmadev and DSA are the same,
134 * but check it at compile time to be sure.
136 RTE_BUILD_BUG_ON(RTE_DMA_OP_FLAG_FENCE != IDXD_FLAG_FENCE);
137 uint32_t memmove = (idxd_op_memmove << IDXD_CMD_OP_SHIFT) |
138 IDXD_FLAG_CACHE_CONTROL | (flags & IDXD_FLAG_FENCE);
139 return __idxd_write_desc(dev_private, memmove, src, dst, length,
145 idxd_enqueue_fill(void *dev_private, uint16_t qid __rte_unused, uint64_t pattern,
146 rte_iova_t dst, unsigned int length, uint64_t flags)
148 uint32_t fill = (idxd_op_fill << IDXD_CMD_OP_SHIFT) |
149 IDXD_FLAG_CACHE_CONTROL | (flags & IDXD_FLAG_FENCE);
150 return __idxd_write_desc(dev_private, fill, pattern, dst, length,
156 idxd_submit(void *dev_private, uint16_t qid __rte_unused)
158 __submit(dev_private);
163 static enum rte_dma_status_code
164 get_comp_status(struct idxd_completion *c)
166 uint8_t st = c->status;
168 /* successful descriptors are not written back normally */
169 case IDXD_COMP_STATUS_INCOMPLETE:
170 case IDXD_COMP_STATUS_SUCCESS:
171 return RTE_DMA_STATUS_SUCCESSFUL;
172 case IDXD_COMP_STATUS_INVALID_OPCODE:
173 return RTE_DMA_STATUS_INVALID_OPCODE;
174 case IDXD_COMP_STATUS_INVALID_SIZE:
175 return RTE_DMA_STATUS_INVALID_LENGTH;
176 case IDXD_COMP_STATUS_SKIPPED:
177 return RTE_DMA_STATUS_NOT_ATTEMPTED;
179 return RTE_DMA_STATUS_ERROR_UNKNOWN;
185 idxd_vchan_status(const struct rte_dma_dev *dev, uint16_t vchan __rte_unused,
186 enum rte_dma_vchan_status *status)
188 struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
189 uint16_t last_batch_write = idxd->batch_idx_write == 0 ? idxd->max_batches :
190 idxd->batch_idx_write - 1;
191 uint8_t bstatus = (idxd->batch_comp_ring[last_batch_write].status != 0);
193 /* An IDXD device will always be either active or idle.
194 * RTE_DMA_VCHAN_HALTED_ERROR is therefore not supported by IDXD.
196 *status = bstatus ? RTE_DMA_VCHAN_IDLE : RTE_DMA_VCHAN_ACTIVE;
202 static __rte_always_inline int
203 batch_ok(struct idxd_dmadev *idxd, uint16_t max_ops, enum rte_dma_status_code *status)
211 /* first check if there are any unreturned handles from last time */
212 if (idxd->ids_avail != idxd->ids_returned) {
213 ret = RTE_MIN((uint16_t)(idxd->ids_avail - idxd->ids_returned), max_ops);
214 idxd->ids_returned += ret;
216 memset(status, RTE_DMA_STATUS_SUCCESSFUL, ret * sizeof(*status));
220 if (idxd->batch_idx_read == idxd->batch_idx_write)
223 bstatus = idxd->batch_comp_ring[idxd->batch_idx_read].status;
224 /* now check if next batch is complete and successful */
225 if (bstatus == IDXD_COMP_STATUS_SUCCESS) {
226 /* since the batch idx ring stores the start of each batch, pre-increment to lookup
227 * start of next batch.
229 if (++idxd->batch_idx_read > idxd->max_batches)
230 idxd->batch_idx_read = 0;
231 idxd->ids_avail = idxd->batch_idx_ring[idxd->batch_idx_read];
233 ret = RTE_MIN((uint16_t)(idxd->ids_avail - idxd->ids_returned), max_ops);
234 idxd->ids_returned += ret;
236 memset(status, RTE_DMA_STATUS_SUCCESSFUL, ret * sizeof(*status));
239 /* check if batch is incomplete */
240 else if (bstatus == IDXD_COMP_STATUS_INCOMPLETE)
243 return -1; /* error case */
247 static inline uint16_t
248 batch_completed(struct idxd_dmadev *idxd, uint16_t max_ops, bool *has_error)
251 uint16_t b_start, b_end, next_batch;
253 int ret = batch_ok(idxd, max_ops, NULL);
257 /* ERROR case, not successful, not incomplete */
258 /* Get the batch size, and special case size 1.
259 * once we identify the actual failure job, return other jobs, then update
260 * the batch ring indexes to make it look like the first job of the batch has failed.
261 * Subsequent calls here will always return zero packets, and the error must be cleared by
262 * calling the completed_status() function.
264 next_batch = (idxd->batch_idx_read + 1);
265 if (next_batch > idxd->max_batches)
267 b_start = idxd->batch_idx_ring[idxd->batch_idx_read];
268 b_end = idxd->batch_idx_ring[next_batch];
270 if (b_end - b_start == 1) { /* not a batch */
275 for (i = b_start; i < b_end; i++) {
276 struct idxd_completion *c = (void *)&idxd->desc_ring[i & idxd->desc_ring_mask];
277 if (c->status > IDXD_COMP_STATUS_SUCCESS) /* ignore incomplete(0) and success(1) */
280 ret = RTE_MIN((uint16_t)(i - idxd->ids_returned), max_ops);
282 *has_error = true; /* we got up to the point of error */
283 idxd->ids_avail = idxd->ids_returned += ret;
285 /* to ensure we can call twice and just return 0, set start of batch to where we finished */
286 idxd->batch_comp_ring[idxd->batch_idx_read].completed_size -= ret;
287 idxd->batch_idx_ring[idxd->batch_idx_read] += ret;
288 if (idxd->batch_idx_ring[next_batch] - idxd->batch_idx_ring[idxd->batch_idx_read] == 1) {
289 /* copy over the descriptor status to the batch ring as if no batch */
290 uint16_t d_idx = idxd->batch_idx_ring[idxd->batch_idx_read] & idxd->desc_ring_mask;
291 struct idxd_completion *desc_comp = (void *)&idxd->desc_ring[d_idx];
292 idxd->batch_comp_ring[idxd->batch_idx_read].status = desc_comp->status;
300 batch_completed_status(struct idxd_dmadev *idxd, uint16_t max_ops, enum rte_dma_status_code *status)
304 int ret = batch_ok(idxd, max_ops, status);
308 /* ERROR case, not successful, not incomplete */
309 /* Get the batch size, and special case size 1.
311 next_batch = (idxd->batch_idx_read + 1);
312 if (next_batch > idxd->max_batches)
314 const uint16_t b_start = idxd->batch_idx_ring[idxd->batch_idx_read];
315 const uint16_t b_end = idxd->batch_idx_ring[next_batch];
316 const uint16_t b_len = b_end - b_start;
317 if (b_len == 1) {/* not a batch */
318 *status = get_comp_status(&idxd->batch_comp_ring[idxd->batch_idx_read]);
319 if (status != RTE_DMA_STATUS_SUCCESSFUL)
320 idxd->stats.errors++;
322 idxd->ids_returned++;
323 idxd->batch_idx_read = next_batch;
327 /* not a single-element batch, need to process more.
329 * 1. max_ops >= batch_size - can fit everything, simple case
330 * - loop through completed ops and then add on any not-attempted ones
331 * 2. max_ops < batch_size - can't fit everything, more complex case
332 * - loop through completed/incomplete and stop when hit max_ops
333 * - adjust the batch descriptor to update where we stopped, with appropriate bcount
334 * - if bcount is to be exactly 1, update the batch descriptor as it will be treated as
335 * non-batch next time.
337 const uint16_t bcount = idxd->batch_comp_ring[idxd->batch_idx_read].completed_size;
338 for (ret = 0; ret < b_len && ret < max_ops; ret++) {
339 struct idxd_completion *c = (void *)
340 &idxd->desc_ring[(b_start + ret) & idxd->desc_ring_mask];
341 status[ret] = (ret < bcount) ? get_comp_status(c) : RTE_DMA_STATUS_NOT_ATTEMPTED;
342 if (status[ret] != RTE_DMA_STATUS_SUCCESSFUL)
343 idxd->stats.errors++;
345 idxd->ids_avail = idxd->ids_returned += ret;
349 idxd->batch_idx_read = next_batch;
353 /* set up for next time, update existing batch descriptor & start idx at batch_idx_read */
354 idxd->batch_idx_ring[idxd->batch_idx_read] += ret;
356 /* we have only incomplete ones - set batch completed size to 0 */
357 struct idxd_completion *comp = &idxd->batch_comp_ring[idxd->batch_idx_read];
358 comp->completed_size = 0;
359 /* if there is only one descriptor left, job skipped so set flag appropriately */
360 if (b_len - ret == 1)
361 comp->status = IDXD_COMP_STATUS_SKIPPED;
363 struct idxd_completion *comp = &idxd->batch_comp_ring[idxd->batch_idx_read];
364 comp->completed_size -= ret;
365 /* if there is only one descriptor left, copy status info straight to desc */
366 if (comp->completed_size == 1) {
367 struct idxd_completion *c = (void *)
368 &idxd->desc_ring[(b_start + ret) & idxd->desc_ring_mask];
369 comp->status = c->status;
370 /* individual descs can be ok without writeback, but not batches */
371 if (comp->status == IDXD_COMP_STATUS_INCOMPLETE)
372 comp->status = IDXD_COMP_STATUS_SUCCESS;
373 } else if (bcount == b_len) {
374 /* check if we still have an error, and clear flag if not */
376 for (i = b_start + ret; i < b_end; i++) {
377 struct idxd_completion *c = (void *)
378 &idxd->desc_ring[i & idxd->desc_ring_mask];
379 if (c->status > IDXD_COMP_STATUS_SUCCESS)
382 if (i == b_end) /* no errors */
383 comp->status = IDXD_COMP_STATUS_SUCCESS;
392 idxd_completed(void *dev_private, uint16_t qid __rte_unused, uint16_t max_ops,
393 uint16_t *last_idx, bool *has_error)
395 struct idxd_dmadev *idxd = dev_private;
396 uint16_t batch, ret = 0;
399 batch = batch_completed(idxd, max_ops - ret, has_error);
401 } while (batch > 0 && *has_error == false);
403 idxd->stats.completed += ret;
404 *last_idx = idxd->ids_returned - 1;
410 idxd_completed_status(void *dev_private, uint16_t qid __rte_unused, uint16_t max_ops,
411 uint16_t *last_idx, enum rte_dma_status_code *status)
413 struct idxd_dmadev *idxd = dev_private;
414 uint16_t batch, ret = 0;
417 batch = batch_completed_status(idxd, max_ops - ret, &status[ret]);
421 idxd->stats.completed += ret;
422 *last_idx = idxd->ids_returned - 1;
427 idxd_dump(const struct rte_dma_dev *dev, FILE *f)
429 struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
432 fprintf(f, "== IDXD Private Data ==\n");
433 fprintf(f, " Portal: %p\n", idxd->portal);
434 fprintf(f, " Config: { ring_size: %u }\n",
436 fprintf(f, " Batch ring (sz = %u, max_batches = %u):\n\t",
437 idxd->max_batches + 1, idxd->max_batches);
438 for (i = 0; i <= idxd->max_batches; i++) {
439 fprintf(f, " %u ", idxd->batch_idx_ring[i]);
440 if (i == idxd->batch_idx_read && i == idxd->batch_idx_write)
441 fprintf(f, "[rd ptr, wr ptr] ");
442 else if (i == idxd->batch_idx_read)
443 fprintf(f, "[rd ptr] ");
444 else if (i == idxd->batch_idx_write)
445 fprintf(f, "[wr ptr] ");
446 if (i == idxd->max_batches)
450 fprintf(f, " Curr batch: start = %u, size = %u\n", idxd->batch_start, idxd->batch_size);
451 fprintf(f, " IDS: avail = %u, returned: %u\n", idxd->ids_avail, idxd->ids_returned);
456 idxd_stats_get(const struct rte_dma_dev *dev, uint16_t vchan __rte_unused,
457 struct rte_dma_stats *stats, uint32_t stats_sz)
459 struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
460 if (stats_sz < sizeof(*stats))
462 *stats = idxd->stats;
467 idxd_stats_reset(struct rte_dma_dev *dev, uint16_t vchan __rte_unused)
469 struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
470 idxd->stats = (struct rte_dma_stats){0};
475 idxd_info_get(const struct rte_dma_dev *dev, struct rte_dma_info *info, uint32_t size)
477 struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
479 if (size < sizeof(*info))
482 *info = (struct rte_dma_info) {
483 .dev_capa = RTE_DMA_CAPA_MEM_TO_MEM | RTE_DMA_CAPA_HANDLES_ERRORS |
484 RTE_DMA_CAPA_OPS_COPY | RTE_DMA_CAPA_OPS_FILL,
489 if (idxd->sva_support)
490 info->dev_capa |= RTE_DMA_CAPA_SVA;
495 idxd_burst_capacity(const void *dev_private, uint16_t vchan __rte_unused)
497 const struct idxd_dmadev *idxd = dev_private;
498 uint16_t write_idx = idxd->batch_start + idxd->batch_size;
501 /* Check for space in the batch ring */
502 if ((idxd->batch_idx_read == 0 && idxd->batch_idx_write == idxd->max_batches) ||
503 idxd->batch_idx_write + 1 == idxd->batch_idx_read)
506 /* Subtract and mask to get in correct range */
507 used_space = (write_idx - idxd->ids_returned) & idxd->desc_ring_mask;
509 const int ret = RTE_MIN((idxd->desc_ring_mask - used_space),
510 (idxd->max_batch_size - idxd->batch_size));
511 return ret < 0 ? 0 : (uint16_t)ret;
515 idxd_configure(struct rte_dma_dev *dev __rte_unused, const struct rte_dma_conf *dev_conf,
518 if (sizeof(struct rte_dma_conf) != conf_sz)
521 if (dev_conf->nb_vchans != 1)
527 idxd_vchan_setup(struct rte_dma_dev *dev, uint16_t vchan __rte_unused,
528 const struct rte_dma_vchan_conf *qconf, uint32_t qconf_sz)
530 struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
531 uint16_t max_desc = qconf->nb_desc;
533 if (sizeof(struct rte_dma_vchan_conf) != qconf_sz)
538 if (!rte_is_power_of_2(max_desc))
539 max_desc = rte_align32pow2(max_desc);
540 IDXD_PMD_DEBUG("DMA dev %u using %u descriptors", dev->data->dev_id, max_desc);
541 idxd->desc_ring_mask = max_desc - 1;
542 idxd->qcfg.nb_desc = max_desc;
544 /* in case we are reconfiguring a device, free any existing memory */
545 rte_free(idxd->desc_ring);
547 /* allocate the descriptor ring at 2x size as batches can't wrap */
548 idxd->desc_ring = rte_zmalloc(NULL, sizeof(*idxd->desc_ring) * max_desc * 2, 0);
549 if (idxd->desc_ring == NULL)
551 idxd->desc_iova = rte_mem_virt2iova(idxd->desc_ring);
553 idxd->batch_idx_read = 0;
554 idxd->batch_idx_write = 0;
555 idxd->batch_start = 0;
556 idxd->batch_size = 0;
557 idxd->ids_returned = 0;
560 memset(idxd->batch_comp_ring, 0, sizeof(*idxd->batch_comp_ring) *
561 (idxd->max_batches + 1));
566 idxd_dmadev_create(const char *name, struct rte_device *dev,
567 const struct idxd_dmadev *base_idxd,
568 const struct rte_dma_dev_ops *ops)
570 struct idxd_dmadev *idxd = NULL;
571 struct rte_dma_dev *dmadev = NULL;
574 RTE_BUILD_BUG_ON(sizeof(struct idxd_hw_desc) != 64);
575 RTE_BUILD_BUG_ON(offsetof(struct idxd_hw_desc, size) != 32);
576 RTE_BUILD_BUG_ON(sizeof(struct idxd_completion) != 32);
579 IDXD_PMD_ERR("Invalid name of the device!");
584 /* Allocate device structure */
585 dmadev = rte_dma_pmd_allocate(name, dev->numa_node, sizeof(struct idxd_dmadev));
586 if (dmadev == NULL) {
587 IDXD_PMD_ERR("Unable to allocate dma device");
591 dmadev->dev_ops = ops;
592 dmadev->device = dev;
594 dmadev->fp_obj->copy = idxd_enqueue_copy;
595 dmadev->fp_obj->fill = idxd_enqueue_fill;
596 dmadev->fp_obj->submit = idxd_submit;
597 dmadev->fp_obj->completed = idxd_completed;
598 dmadev->fp_obj->completed_status = idxd_completed_status;
599 dmadev->fp_obj->burst_capacity = idxd_burst_capacity;
601 idxd = dmadev->data->dev_private;
602 *idxd = *base_idxd; /* copy over the main fields already passed in */
603 idxd->dmadev = dmadev;
605 /* allocate batch index ring and completion ring.
606 * The +1 is because we can never fully use
607 * the ring, otherwise read == write means both full and empty.
609 idxd->batch_comp_ring = rte_zmalloc_socket(NULL, (sizeof(idxd->batch_idx_ring[0]) +
610 sizeof(idxd->batch_comp_ring[0])) * (idxd->max_batches + 1),
611 sizeof(idxd->batch_comp_ring[0]), dev->numa_node);
612 if (idxd->batch_comp_ring == NULL) {
613 IDXD_PMD_ERR("Unable to reserve memory for batch data\n");
617 idxd->batch_idx_ring = (void *)&idxd->batch_comp_ring[idxd->max_batches+1];
618 idxd->batch_iova = rte_mem_virt2iova(idxd->batch_comp_ring);
620 dmadev->fp_obj->dev_private = idxd;
622 idxd->dmadev->state = RTE_DMA_DEV_READY;
628 rte_dma_pmd_release(name);
633 int idxd_pmd_logtype;
635 RTE_LOG_REGISTER_DEFAULT(idxd_pmd_logtype, WARNING);