1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2021 HiSilicon Limited
3 * Copyright(c) 2021 Intel Corporation
4 * Copyright(c) 2021 Marvell International Ltd
5 * Copyright(c) 2021 SmartShare Systems
14 * DMA (Direct Memory Access) device API.
16 * The DMA framework is built on the following model:
18 * --------------- --------------- ---------------
19 * | virtual DMA | | virtual DMA | | virtual DMA |
20 * | channel | | channel | | channel |
21 * --------------- --------------- ---------------
23 * ------------------ |
25 * ------------ ------------
26 * | dmadev | | dmadev |
27 * ------------ ------------
29 * ------------------ ------------------
30 * | HW DMA channel | | HW DMA channel |
31 * ------------------ ------------------
33 * --------------------------------
35 * ---------------------
36 * | HW DMA Controller |
37 * ---------------------
39 * The DMA controller could have multiple HW-DMA-channels (aka. HW-DMA-queues),
40 * each HW-DMA-channel should be represented by a dmadev.
42 * The dmadev could create multiple virtual DMA channels, each virtual DMA
43 * channel represents a different transfer context. The DMA operation request
44 * must be submitted to the virtual DMA channel. e.g. Application could create
45 * virtual DMA channel 0 for memory-to-memory transfer scenario, and create
46 * virtual DMA channel 1 for memory-to-device transfer scenario.
48 * This framework uses 'int16_t dev_id' as the device identifier of a dmadev,
49 * and 'uint16_t vchan' as the virtual DMA channel identifier in one dmadev.
51 * The functions exported by the dmadev API to setup a device designated by its
52 * device identifier must be invoked in the following order:
53 * - rte_dma_configure()
54 * - rte_dma_vchan_setup()
57 * Then, the application can invoke dataplane functions to process jobs.
59 * If the application wants to change the configuration (i.e. invoke
60 * rte_dma_configure() or rte_dma_vchan_setup()), it must invoke
61 * rte_dma_stop() first to stop the device and then do the reconfiguration
62 * before invoking rte_dma_start() again. The dataplane functions should not
63 * be invoked when the device is stopped.
65 * Finally, an application can close a dmadev by invoking the rte_dma_close()
68 * About MT-safe, all the functions of the dmadev API implemented by a PMD are
69 * lock-free functions which assume to not be invoked in parallel on different
70 * logical cores to work on the same target dmadev object.
71 * @note Different virtual DMA channels on the same dmadev *DO NOT* support
72 * parallel invocation because these virtual DMA channels share the same
78 #include <rte_bitops.h>
79 #include <rte_common.h>
80 #include <rte_compat.h>
87 /** Maximum number of devices if rte_dma_dev_max() is not called. */
88 #define RTE_DMADEV_DEFAULT_MAX 64
92 * @b EXPERIMENTAL: this API may change without prior notice.
94 * Configure the maximum number of dmadevs.
95 * @note This function can be invoked before the primary process rte_eal_init()
96 * to change the maximum number of dmadevs. If not invoked, the maximum number
97 * of dmadevs is @see RTE_DMADEV_DEFAULT_MAX
100 * maximum number of dmadevs.
103 * 0 on success. Otherwise negative value is returned.
106 int rte_dma_dev_max(size_t dev_max);
110 * @b EXPERIMENTAL: this API may change without prior notice.
112 * Get the device identifier for the named DMA device.
118 * Returns DMA device identifier on success.
119 * - <0: Failure to find named DMA device.
122 int rte_dma_get_dev_id_by_name(const char *name);
126 * @b EXPERIMENTAL: this API may change without prior notice.
128 * Check whether the dev_id is valid.
134 * - If the device index is valid (true) or not (false).
137 bool rte_dma_is_valid(int16_t dev_id);
141 * @b EXPERIMENTAL: this API may change without prior notice.
143 * Get the total number of DMA devices that have been successfully
147 * The total number of usable DMA devices.
150 uint16_t rte_dma_count_avail(void);
152 /**@{@name DMA capability
153 * @see struct rte_dma_info::dev_capa
155 /** Support memory-to-memory transfer */
156 #define RTE_DMA_CAPA_MEM_TO_MEM RTE_BIT64(0)
157 /** Support memory-to-device transfer. */
158 #define RTE_DMA_CAPA_MEM_TO_DEV RTE_BIT64(1)
159 /** Support device-to-memory transfer. */
160 #define RTE_DMA_CAPA_DEV_TO_MEM RTE_BIT64(2)
161 /** Support device-to-device transfer. */
162 #define RTE_DMA_CAPA_DEV_TO_DEV RTE_BIT64(3)
163 /** Support SVA which could use VA as DMA address.
164 * If device support SVA then application could pass any VA address like memory
165 * from rte_malloc(), rte_memzone(), malloc, stack memory.
166 * If device don't support SVA, then application should pass IOVA address which
167 * from rte_malloc(), rte_memzone().
169 #define RTE_DMA_CAPA_SVA RTE_BIT64(4)
170 /** Support work in silent mode.
171 * In this mode, application don't required to invoke rte_dma_completed*()
173 * @see struct rte_dma_conf::silent_mode
175 #define RTE_DMA_CAPA_SILENT RTE_BIT64(5)
176 /** Support copy operation.
177 * This capability start with index of 32, so that it could leave gap between
178 * normal capability and ops capability.
180 #define RTE_DMA_CAPA_OPS_COPY RTE_BIT64(32)
181 /** Support scatter-gather list copy operation. */
182 #define RTE_DMA_CAPA_OPS_COPY_SG RTE_BIT64(33)
183 /** Support fill operation. */
184 #define RTE_DMA_CAPA_OPS_FILL RTE_BIT64(34)
188 * A structure used to retrieve the information of a DMA device.
190 * @see rte_dma_info_get
192 struct rte_dma_info {
193 const char *dev_name; /**< Unique device name. */
194 /** Device capabilities (RTE_DMA_CAPA_*). */
196 /** Maximum number of virtual DMA channels supported. */
198 /** Maximum allowed number of virtual DMA channel descriptors. */
200 /** Minimum allowed number of virtual DMA channel descriptors. */
202 /** Maximum number of source or destination scatter-gather entry
204 * If the device does not support COPY_SG capability, this value can be
206 * If the device supports COPY_SG capability, then rte_dma_copy_sg()
207 * parameter nb_src/nb_dst should not exceed this value.
210 /** NUMA node connection, -1 if unknown. */
212 /** Number of virtual DMA channel configured. */
218 * @b EXPERIMENTAL: this API may change without prior notice.
220 * Retrieve information of a DMA device.
223 * The identifier of the device.
224 * @param[out] dev_info
225 * A pointer to a structure of type *rte_dma_info* to be filled with the
226 * information of the device.
229 * 0 on success. Otherwise negative value is returned.
232 int rte_dma_info_get(int16_t dev_id, struct rte_dma_info *dev_info);
235 * A structure used to configure a DMA device.
237 * @see rte_dma_configure
239 struct rte_dma_conf {
240 /** The number of virtual DMA channels to set up for the DMA device.
241 * This value cannot be greater than the field 'max_vchans' of struct
242 * rte_dma_info which get from rte_dma_info_get().
245 /** Indicates whether to enable silent mode.
246 * false-default mode, true-silent mode.
247 * This value can be set to true only when the SILENT capability is
250 * @see RTE_DMA_CAPA_SILENT
257 * @b EXPERIMENTAL: this API may change without prior notice.
259 * Configure a DMA device.
261 * This function must be invoked first before any other function in the
262 * API. This function can also be re-invoked when a device is in the
266 * The identifier of the device to configure.
268 * The DMA device configuration structure encapsulated into rte_dma_conf
272 * 0 on success. Otherwise negative value is returned.
275 int rte_dma_configure(int16_t dev_id, const struct rte_dma_conf *dev_conf);
279 * @b EXPERIMENTAL: this API may change without prior notice.
281 * Start a DMA device.
283 * The device start step is the last one and consists of setting the DMA
284 * to start accepting jobs.
287 * The identifier of the device.
290 * 0 on success. Otherwise negative value is returned.
293 int rte_dma_start(int16_t dev_id);
297 * @b EXPERIMENTAL: this API may change without prior notice.
301 * The device can be restarted with a call to rte_dma_start().
304 * The identifier of the device.
307 * 0 on success. Otherwise negative value is returned.
310 int rte_dma_stop(int16_t dev_id);
314 * @b EXPERIMENTAL: this API may change without prior notice.
316 * Close a DMA device.
318 * The device cannot be restarted after this call.
321 * The identifier of the device.
324 * 0 on success. Otherwise negative value is returned.
327 int rte_dma_close(int16_t dev_id);
330 * DMA transfer direction defines.
332 * @see struct rte_dma_vchan_conf::direction
334 enum rte_dma_direction {
335 /** DMA transfer direction - from memory to memory.
337 * @see struct rte_dma_vchan_conf::direction
339 RTE_DMA_DIR_MEM_TO_MEM,
340 /** DMA transfer direction - from memory to device.
341 * In a typical scenario, the SoCs are installed on host servers as
342 * iNICs through the PCIe interface. In this case, the SoCs works in
343 * EP(endpoint) mode, it could initiate a DMA move request from memory
344 * (which is SoCs memory) to device (which is host memory).
346 * @see struct rte_dma_vchan_conf::direction
348 RTE_DMA_DIR_MEM_TO_DEV,
349 /** DMA transfer direction - from device to memory.
350 * In a typical scenario, the SoCs are installed on host servers as
351 * iNICs through the PCIe interface. In this case, the SoCs works in
352 * EP(endpoint) mode, it could initiate a DMA move request from device
353 * (which is host memory) to memory (which is SoCs memory).
355 * @see struct rte_dma_vchan_conf::direction
357 RTE_DMA_DIR_DEV_TO_MEM,
358 /** DMA transfer direction - from device to device.
359 * In a typical scenario, the SoCs are installed on host servers as
360 * iNICs through the PCIe interface. In this case, the SoCs works in
361 * EP(endpoint) mode, it could initiate a DMA move request from device
362 * (which is host memory) to the device (which is another host memory).
364 * @see struct rte_dma_vchan_conf::direction
366 RTE_DMA_DIR_DEV_TO_DEV,
370 * DMA access port type defines.
372 * @see struct rte_dma_port_param::port_type
374 enum rte_dma_port_type {
376 RTE_DMA_PORT_PCIE, /**< The DMA access port is PCIe. */
380 * A structure used to descript DMA access port parameters.
382 * @see struct rte_dma_vchan_conf::src_port
383 * @see struct rte_dma_vchan_conf::dst_port
385 struct rte_dma_port_param {
386 /** The device access port type.
388 * @see enum rte_dma_port_type
390 enum rte_dma_port_type port_type;
393 /** PCIe access port parameters.
395 * The following model shows SoC's PCIe module connects to
396 * multiple PCIe hosts and multiple endpoints. The PCIe module
397 * has an integrated DMA controller.
399 * If the DMA wants to access the memory of host A, it can be
400 * initiated by PF1 in core0, or by VF0 of PF0 in core0.
404 * | ----------PCIe module----------
407 * | ----- ------------------
408 * | | | | PCIe Core0 |
409 * | | | | | -----------
410 * | | | | PF-0 -- VF-0 | | Host A |
411 * | | |--------| |- VF-1 |--------| Root |
412 * | | | | PF-1 | | Complex |
413 * | | | | PF-2 | -----------
414 * | | | ------------------
416 * | | | ------------------
417 * | | | | PCIe Core1 |
418 * | | | | | -----------
419 * | | | | PF-0 -- VF-0 | | Host B |
420 * |-----| |--------| PF-1 -- VF-0 |--------| Root |
421 * | | | | |- VF-1 | | Complex |
422 * | | | | PF-2 | -----------
423 * | | | ------------------
425 * | | | ------------------
427 * | | | | |--------| EP |
428 * | | |--------| PCIe Core2 | ------
430 * | | | | |--------| EP |
432 * | ----- ------------------
436 * @note If some fields can not be supported by the
437 * hardware/driver, then the driver ignores those fields.
438 * Please check driver-specific documentation for limitations
443 uint64_t coreid : 4; /**< PCIe core id used. */
444 uint64_t pfid : 8; /**< PF id used. */
445 uint64_t vfen : 1; /**< VF enable bit. */
446 uint64_t vfid : 16; /**< VF id used. */
447 /** The pasid filed in TLP packet. */
449 /** The attributes filed in TLP packet. */
451 /** The processing hint filed in TLP packet. */
453 /** The steering tag filed in TLP packet. */
457 uint64_t reserved[2]; /**< Reserved for future fields. */
461 * A structure used to configure a virtual DMA channel.
463 * @see rte_dma_vchan_setup
465 struct rte_dma_vchan_conf {
466 /** Transfer direction
468 * @see enum rte_dma_direction
470 enum rte_dma_direction direction;
471 /** Number of descriptor for the virtual DMA channel */
473 /** 1) Used to describes the device access port parameter in the
474 * device-to-memory transfer scenario.
475 * 2) Used to describes the source device access port parameter in the
476 * device-to-device transfer scenario.
478 * @see struct rte_dma_port_param
480 struct rte_dma_port_param src_port;
481 /** 1) Used to describes the device access port parameter in the
482 * memory-to-device transfer scenario.
483 * 2) Used to describes the destination device access port parameter in
484 * the device-to-device transfer scenario.
486 * @see struct rte_dma_port_param
488 struct rte_dma_port_param dst_port;
493 * @b EXPERIMENTAL: this API may change without prior notice.
495 * Allocate and set up a virtual DMA channel.
498 * The identifier of the device.
500 * The identifier of virtual DMA channel. The value must be in the range
501 * [0, nb_vchans - 1] previously supplied to rte_dma_configure().
503 * The virtual DMA channel configuration structure encapsulated into
504 * rte_dma_vchan_conf object.
507 * 0 on success. Otherwise negative value is returned.
510 int rte_dma_vchan_setup(int16_t dev_id, uint16_t vchan,
511 const struct rte_dma_vchan_conf *conf);
514 * A structure used to retrieve statistics.
516 * @see rte_dma_stats_get
518 struct rte_dma_stats {
519 /** Count of operations which were submitted to hardware. */
521 /** Count of operations which were completed, including successful and
522 * failed completions.
525 /** Count of operations which failed to complete. */
530 * Special ID, which is used to represent all virtual DMA channels.
532 * @see rte_dma_stats_get
533 * @see rte_dma_stats_reset
535 #define RTE_DMA_ALL_VCHAN 0xFFFFu
539 * @b EXPERIMENTAL: this API may change without prior notice.
541 * Retrieve basic statistics of a or all virtual DMA channel(s).
544 * The identifier of the device.
546 * The identifier of virtual DMA channel.
547 * If equal RTE_DMA_ALL_VCHAN means all channels.
549 * The basic statistics structure encapsulated into rte_dma_stats
553 * 0 on success. Otherwise negative value is returned.
556 int rte_dma_stats_get(int16_t dev_id, uint16_t vchan,
557 struct rte_dma_stats *stats);
561 * @b EXPERIMENTAL: this API may change without prior notice.
563 * Reset basic statistics of a or all virtual DMA channel(s).
566 * The identifier of the device.
568 * The identifier of virtual DMA channel.
569 * If equal RTE_DMA_ALL_VCHAN means all channels.
572 * 0 on success. Otherwise negative value is returned.
575 int rte_dma_stats_reset(int16_t dev_id, uint16_t vchan);
579 * @b EXPERIMENTAL: this API may change without prior notice.
581 * Dump DMA device info.
584 * The identifier of the device.
586 * The file to write the output to.
589 * 0 on success. Otherwise negative value is returned.
592 int rte_dma_dump(int16_t dev_id, FILE *f);
598 #endif /* RTE_DMADEV_H */