xref: /dpdk/lib/gpudev/rte_gpudev.h (revision 1fd3de64ff47f8865f9cbd13bb8f20611beb3cfa)
18b8036a6SElena Agostini /* SPDX-License-Identifier: BSD-3-Clause
28b8036a6SElena Agostini  * Copyright (c) 2021 NVIDIA Corporation & Affiliates
38b8036a6SElena Agostini  */
48b8036a6SElena Agostini 
58b8036a6SElena Agostini #ifndef RTE_GPUDEV_H
68b8036a6SElena Agostini #define RTE_GPUDEV_H
78b8036a6SElena Agostini 
88b8036a6SElena Agostini #include <stddef.h>
98b8036a6SElena Agostini #include <stdint.h>
108b8036a6SElena Agostini #include <stdbool.h>
118b8036a6SElena Agostini 
12c7ebd65cSElena Agostini #include <rte_mbuf.h>
13e818c4e2SElena Agostini #include <rte_bitops.h>
148b8036a6SElena Agostini #include <rte_compat.h>
158b8036a6SElena Agostini 
168b8036a6SElena Agostini /**
178b8036a6SElena Agostini  * @file
188b8036a6SElena Agostini  * Generic library to interact with GPU computing device.
198b8036a6SElena Agostini  *
208b8036a6SElena Agostini  * The API is not thread-safe.
218b8036a6SElena Agostini  * Device management must be done by a single thread.
228b8036a6SElena Agostini  *
238b8036a6SElena Agostini  * @warning
248b8036a6SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
258b8036a6SElena Agostini  */
268b8036a6SElena Agostini 
278b8036a6SElena Agostini #ifdef __cplusplus
288b8036a6SElena Agostini extern "C" {
298b8036a6SElena Agostini #endif
308b8036a6SElena Agostini 
318b8036a6SElena Agostini /** Maximum number of devices if rte_gpu_init() is not called. */
328b8036a6SElena Agostini #define RTE_GPU_DEFAULT_MAX 32
338b8036a6SElena Agostini 
348b8036a6SElena Agostini /** Empty device ID. */
358b8036a6SElena Agostini #define RTE_GPU_ID_NONE -1
3618cb0756SThomas Monjalon /** Catch-all device ID. */
3718cb0756SThomas Monjalon #define RTE_GPU_ID_ANY INT16_MIN
3818cb0756SThomas Monjalon 
3918cb0756SThomas Monjalon /** Catch-all callback data. */
4018cb0756SThomas Monjalon #define RTE_GPU_CALLBACK_ANY_DATA ((void *)-1)
418b8036a6SElena Agostini 
42f56160a2SElena Agostini /** Access variable as volatile. */
43f56160a2SElena Agostini #define RTE_GPU_VOLATILE(x) (*(volatile typeof(x) *)&(x))
44f56160a2SElena Agostini 
45c7ebd65cSElena Agostini /** Max number of packets per communication list. */
46c7ebd65cSElena Agostini #define RTE_GPU_COMM_LIST_PKTS_MAX 1024
47c7ebd65cSElena Agostini 
488b8036a6SElena Agostini /** Store device info. */
498b8036a6SElena Agostini struct rte_gpu_info {
508b8036a6SElena Agostini 	/** Unique identifier name. */
518b8036a6SElena Agostini 	const char *name;
5282e5f6b6SThomas Monjalon 	/** Opaque handler of the device context. */
5382e5f6b6SThomas Monjalon 	uint64_t context;
548b8036a6SElena Agostini 	/** Device ID. */
558b8036a6SElena Agostini 	int16_t dev_id;
5682e5f6b6SThomas Monjalon 	/** ID of the parent device, RTE_GPU_ID_NONE if no parent */
5782e5f6b6SThomas Monjalon 	int16_t parent;
588b8036a6SElena Agostini 	/** Total processors available on device. */
598b8036a6SElena Agostini 	uint32_t processor_count;
608b8036a6SElena Agostini 	/** Total memory available on device. */
618b8036a6SElena Agostini 	size_t total_memory;
62*1fd3de64SElena Agostini 	/** GPU memory page size. */
63*1fd3de64SElena Agostini 	size_t page_size;
64*1fd3de64SElena Agostini 	/** Local NUMA memory ID. -1 if unknown. */
658b8036a6SElena Agostini 	int16_t numa_node;
668b8036a6SElena Agostini };
678b8036a6SElena Agostini 
6818cb0756SThomas Monjalon /** Flags passed in notification callback. */
6918cb0756SThomas Monjalon enum rte_gpu_event {
7018cb0756SThomas Monjalon 	/** Device is just initialized. */
7118cb0756SThomas Monjalon 	RTE_GPU_EVENT_NEW,
7218cb0756SThomas Monjalon 	/** Device is going to be released. */
7318cb0756SThomas Monjalon 	RTE_GPU_EVENT_DEL,
7418cb0756SThomas Monjalon };
7518cb0756SThomas Monjalon 
7618cb0756SThomas Monjalon /** Prototype of event callback function. */
7718cb0756SThomas Monjalon typedef void (rte_gpu_callback_t)(int16_t dev_id,
7818cb0756SThomas Monjalon 		enum rte_gpu_event event, void *user_data);
7918cb0756SThomas Monjalon 
80f56160a2SElena Agostini /** Memory where communication flag is allocated. */
81f56160a2SElena Agostini enum rte_gpu_comm_flag_type {
82f56160a2SElena Agostini 	/** Allocate flag on CPU memory visible from device. */
83f56160a2SElena Agostini 	RTE_GPU_COMM_FLAG_CPU = 0,
84f56160a2SElena Agostini };
85f56160a2SElena Agostini 
86f56160a2SElena Agostini /** Communication flag to coordinate CPU with the device. */
87f56160a2SElena Agostini struct rte_gpu_comm_flag {
88f56160a2SElena Agostini 	/** Device that will use the device flag. */
89f56160a2SElena Agostini 	uint16_t dev_id;
90f56160a2SElena Agostini 	/** Pointer to flag memory area. */
91f56160a2SElena Agostini 	uint32_t *ptr;
92f56160a2SElena Agostini 	/** Type of memory used to allocate the flag. */
93f56160a2SElena Agostini 	enum rte_gpu_comm_flag_type mtype;
94f56160a2SElena Agostini };
95f56160a2SElena Agostini 
96c7ebd65cSElena Agostini /** List of packets shared among CPU and device. */
97c7ebd65cSElena Agostini struct rte_gpu_comm_pkt {
98c7ebd65cSElena Agostini 	/** Address of the packet in memory (e.g. mbuf->buf_addr). */
99c7ebd65cSElena Agostini 	uintptr_t addr;
100c7ebd65cSElena Agostini 	/** Size in byte of the packet. */
101c7ebd65cSElena Agostini 	size_t size;
102c7ebd65cSElena Agostini };
103c7ebd65cSElena Agostini 
104c7ebd65cSElena Agostini /** Possible status for the list of packets shared among CPU and device. */
105c7ebd65cSElena Agostini enum rte_gpu_comm_list_status {
106c7ebd65cSElena Agostini 	/** Packet list can be filled with new mbufs, no one is using it. */
107c7ebd65cSElena Agostini 	RTE_GPU_COMM_LIST_FREE = 0,
108c7ebd65cSElena Agostini 	/** Packet list has been filled with new mbufs and it's ready to be used .*/
109c7ebd65cSElena Agostini 	RTE_GPU_COMM_LIST_READY,
110c7ebd65cSElena Agostini 	/** Packet list has been processed, it's ready to be freed. */
111c7ebd65cSElena Agostini 	RTE_GPU_COMM_LIST_DONE,
112c7ebd65cSElena Agostini 	/** Some error occurred during packet list processing. */
113c7ebd65cSElena Agostini 	RTE_GPU_COMM_LIST_ERROR,
114c7ebd65cSElena Agostini };
115c7ebd65cSElena Agostini 
116c7ebd65cSElena Agostini /**
117c7ebd65cSElena Agostini  * Communication list holding a number of lists of packets
118c7ebd65cSElena Agostini  * each having a status flag.
119c7ebd65cSElena Agostini  */
120c7ebd65cSElena Agostini struct rte_gpu_comm_list {
121c7ebd65cSElena Agostini 	/** Device that will use the communication list. */
122c7ebd65cSElena Agostini 	uint16_t dev_id;
123c7ebd65cSElena Agostini 	/** List of mbufs populated by the CPU with a set of mbufs. */
124c7ebd65cSElena Agostini 	struct rte_mbuf **mbufs;
125c7ebd65cSElena Agostini 	/** List of packets populated by the CPU with a set of mbufs info. */
126c7ebd65cSElena Agostini 	struct rte_gpu_comm_pkt *pkt_list;
127c7ebd65cSElena Agostini 	/** Number of packets in the list. */
128c7ebd65cSElena Agostini 	uint32_t num_pkts;
1299b8cae4dSElena Agostini 	/** Status of the list. CPU pointer. */
1309b8cae4dSElena Agostini 	enum rte_gpu_comm_list_status *status_h;
1319b8cae4dSElena Agostini 	/** Status of the list. GPU pointer. */
1329b8cae4dSElena Agostini 	enum rte_gpu_comm_list_status *status_d;
133c7ebd65cSElena Agostini };
134c7ebd65cSElena Agostini 
1358b8036a6SElena Agostini /**
1368b8036a6SElena Agostini  * @warning
1378b8036a6SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
1388b8036a6SElena Agostini  *
1398b8036a6SElena Agostini  * Initialize the device array before probing devices.
1408b8036a6SElena Agostini  * If not called, the maximum of probed devices is RTE_GPU_DEFAULT_MAX.
1418b8036a6SElena Agostini  *
1428b8036a6SElena Agostini  * @param dev_max
1438b8036a6SElena Agostini  *   Maximum number of devices.
1448b8036a6SElena Agostini  *
1458b8036a6SElena Agostini  * @return
1468b8036a6SElena Agostini  *   0 on success, -rte_errno otherwise:
1478b8036a6SElena Agostini  *   - ENOMEM if out of memory
1488b8036a6SElena Agostini  *   - EINVAL if 0 size
1498b8036a6SElena Agostini  *   - EBUSY if already initialized
1508b8036a6SElena Agostini  */
1518b8036a6SElena Agostini __rte_experimental
1528b8036a6SElena Agostini int rte_gpu_init(size_t dev_max);
1538b8036a6SElena Agostini 
1548b8036a6SElena Agostini /**
1558b8036a6SElena Agostini  * @warning
1568b8036a6SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
1578b8036a6SElena Agostini  *
1588b8036a6SElena Agostini  * Return the number of GPU detected and associated to DPDK.
1598b8036a6SElena Agostini  *
1608b8036a6SElena Agostini  * @return
1618b8036a6SElena Agostini  *   The number of available computing devices.
1628b8036a6SElena Agostini  */
1638b8036a6SElena Agostini __rte_experimental
1648b8036a6SElena Agostini uint16_t rte_gpu_count_avail(void);
1658b8036a6SElena Agostini 
1668b8036a6SElena Agostini /**
1678b8036a6SElena Agostini  * @warning
1688b8036a6SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
1698b8036a6SElena Agostini  *
1708b8036a6SElena Agostini  * Check if the device is valid and initialized in DPDK.
1718b8036a6SElena Agostini  *
1728b8036a6SElena Agostini  * @param dev_id
1738b8036a6SElena Agostini  *   The input device ID.
1748b8036a6SElena Agostini  *
1758b8036a6SElena Agostini  * @return
1768b8036a6SElena Agostini  *   - True if dev_id is a valid and initialized computing device.
1778b8036a6SElena Agostini  *   - False otherwise.
1788b8036a6SElena Agostini  */
1798b8036a6SElena Agostini __rte_experimental
1808b8036a6SElena Agostini bool rte_gpu_is_valid(int16_t dev_id);
1818b8036a6SElena Agostini 
1828b8036a6SElena Agostini /**
1838b8036a6SElena Agostini  * @warning
1848b8036a6SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
1858b8036a6SElena Agostini  *
18682e5f6b6SThomas Monjalon  * Create a virtual device representing a context in the parent device.
18782e5f6b6SThomas Monjalon  *
18882e5f6b6SThomas Monjalon  * @param name
18982e5f6b6SThomas Monjalon  *   Unique string to identify the device.
19082e5f6b6SThomas Monjalon  * @param parent
19182e5f6b6SThomas Monjalon  *   Device ID of the parent.
19282e5f6b6SThomas Monjalon  * @param child_context
19382e5f6b6SThomas Monjalon  *   Opaque context handler.
19482e5f6b6SThomas Monjalon  *
19582e5f6b6SThomas Monjalon  * @return
19682e5f6b6SThomas Monjalon  *   Device ID of the new created child, -rte_errno otherwise:
19782e5f6b6SThomas Monjalon  *   - EINVAL if empty name
19882e5f6b6SThomas Monjalon  *   - ENAMETOOLONG if long name
19982e5f6b6SThomas Monjalon  *   - EEXIST if existing device name
20082e5f6b6SThomas Monjalon  *   - ENODEV if invalid parent
20182e5f6b6SThomas Monjalon  *   - EPERM if secondary process
20282e5f6b6SThomas Monjalon  *   - ENOENT if too many devices
20382e5f6b6SThomas Monjalon  *   - ENOMEM if out of space
20482e5f6b6SThomas Monjalon  */
20582e5f6b6SThomas Monjalon __rte_experimental
20682e5f6b6SThomas Monjalon int16_t rte_gpu_add_child(const char *name,
20782e5f6b6SThomas Monjalon 		int16_t parent, uint64_t child_context);
20882e5f6b6SThomas Monjalon 
20982e5f6b6SThomas Monjalon /**
21082e5f6b6SThomas Monjalon  * @warning
21182e5f6b6SThomas Monjalon  * @b EXPERIMENTAL: this API may change without prior notice.
21282e5f6b6SThomas Monjalon  *
2138b8036a6SElena Agostini  * Get the ID of the next valid GPU initialized in DPDK.
2148b8036a6SElena Agostini  *
2158b8036a6SElena Agostini  * @param dev_id
2168b8036a6SElena Agostini  *   The initial device ID to start the research.
21782e5f6b6SThomas Monjalon  * @param parent
21882e5f6b6SThomas Monjalon  *   The device ID of the parent.
21982e5f6b6SThomas Monjalon  *   RTE_GPU_ID_NONE means no parent.
22082e5f6b6SThomas Monjalon  *   RTE_GPU_ID_ANY means no or any parent.
2218b8036a6SElena Agostini  *
2228b8036a6SElena Agostini  * @return
2238b8036a6SElena Agostini  *   Next device ID corresponding to a valid and initialized computing device,
2248b8036a6SElena Agostini  *   RTE_GPU_ID_NONE if there is none.
2258b8036a6SElena Agostini  */
2268b8036a6SElena Agostini __rte_experimental
22782e5f6b6SThomas Monjalon int16_t rte_gpu_find_next(int16_t dev_id, int16_t parent);
2288b8036a6SElena Agostini 
2298b8036a6SElena Agostini /**
2308b8036a6SElena Agostini  * @warning
2318b8036a6SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
2328b8036a6SElena Agostini  *
2338b8036a6SElena Agostini  * Macro to iterate over all valid GPU devices.
2348b8036a6SElena Agostini  *
2358b8036a6SElena Agostini  * @param dev_id
2368b8036a6SElena Agostini  *   The ID of the next possible valid device, usually 0 to iterate all.
2378b8036a6SElena Agostini  */
2388b8036a6SElena Agostini #define RTE_GPU_FOREACH(dev_id) \
23982e5f6b6SThomas Monjalon 	RTE_GPU_FOREACH_CHILD(dev_id, RTE_GPU_ID_ANY)
2408b8036a6SElena Agostini 
2418b8036a6SElena Agostini /**
2428b8036a6SElena Agostini  * @warning
2438b8036a6SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
2448b8036a6SElena Agostini  *
24582e5f6b6SThomas Monjalon  * Macro to iterate over all valid computing devices having no parent.
24682e5f6b6SThomas Monjalon  *
24782e5f6b6SThomas Monjalon  * @param dev_id
24882e5f6b6SThomas Monjalon  *   The ID of the next possible valid device, usually 0 to iterate all.
24982e5f6b6SThomas Monjalon  */
25082e5f6b6SThomas Monjalon #define RTE_GPU_FOREACH_PARENT(dev_id) \
25182e5f6b6SThomas Monjalon 	RTE_GPU_FOREACH_CHILD(dev_id, RTE_GPU_ID_NONE)
25282e5f6b6SThomas Monjalon 
25382e5f6b6SThomas Monjalon /**
25482e5f6b6SThomas Monjalon  * @warning
25582e5f6b6SThomas Monjalon  * @b EXPERIMENTAL: this API may change without prior notice.
25682e5f6b6SThomas Monjalon  *
25782e5f6b6SThomas Monjalon  * Macro to iterate over all valid children of a computing device parent.
25882e5f6b6SThomas Monjalon  *
25982e5f6b6SThomas Monjalon  * @param dev_id
26082e5f6b6SThomas Monjalon  *   The ID of the next possible valid device, usually 0 to iterate all.
26182e5f6b6SThomas Monjalon  * @param parent
26282e5f6b6SThomas Monjalon  *   The device ID of the parent.
26382e5f6b6SThomas Monjalon  */
26482e5f6b6SThomas Monjalon #define RTE_GPU_FOREACH_CHILD(dev_id, parent) \
26582e5f6b6SThomas Monjalon 	for (dev_id = rte_gpu_find_next(0, parent); \
26682e5f6b6SThomas Monjalon 	     dev_id >= 0; \
26782e5f6b6SThomas Monjalon 	     dev_id = rte_gpu_find_next(dev_id + 1, parent))
26882e5f6b6SThomas Monjalon 
26982e5f6b6SThomas Monjalon /**
27082e5f6b6SThomas Monjalon  * @warning
27182e5f6b6SThomas Monjalon  * @b EXPERIMENTAL: this API may change without prior notice.
27282e5f6b6SThomas Monjalon  *
27382e5f6b6SThomas Monjalon  * Close device or child context.
2748b8036a6SElena Agostini  * All resources are released.
2758b8036a6SElena Agostini  *
2768b8036a6SElena Agostini  * @param dev_id
2778b8036a6SElena Agostini  *   Device ID to close.
2788b8036a6SElena Agostini  *
2798b8036a6SElena Agostini  * @return
2808b8036a6SElena Agostini  *   0 on success, -rte_errno otherwise:
2818b8036a6SElena Agostini  *   - ENODEV if invalid dev_id
2828b8036a6SElena Agostini  *   - EPERM if driver error
2838b8036a6SElena Agostini  */
2848b8036a6SElena Agostini __rte_experimental
2858b8036a6SElena Agostini int rte_gpu_close(int16_t dev_id);
2868b8036a6SElena Agostini 
2878b8036a6SElena Agostini /**
2888b8036a6SElena Agostini  * @warning
2898b8036a6SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
2908b8036a6SElena Agostini  *
29118cb0756SThomas Monjalon  * Register a function as event callback.
29218cb0756SThomas Monjalon  * A function may be registered multiple times for different events.
29318cb0756SThomas Monjalon  *
29418cb0756SThomas Monjalon  * @param dev_id
29518cb0756SThomas Monjalon  *   Device ID to get notified about.
29618cb0756SThomas Monjalon  *   RTE_GPU_ID_ANY means all devices.
29718cb0756SThomas Monjalon  * @param event
29818cb0756SThomas Monjalon  *   Device event to be registered for.
29918cb0756SThomas Monjalon  * @param function
30018cb0756SThomas Monjalon  *   Callback function to be called on event.
30118cb0756SThomas Monjalon  * @param user_data
30218cb0756SThomas Monjalon  *   Optional parameter passed in the callback.
30318cb0756SThomas Monjalon  *
30418cb0756SThomas Monjalon  * @return
30518cb0756SThomas Monjalon  *   0 on success, -rte_errno otherwise:
30618cb0756SThomas Monjalon  *   - ENODEV if invalid dev_id
30718cb0756SThomas Monjalon  *   - EINVAL if NULL function
30818cb0756SThomas Monjalon  *   - ENOMEM if out of memory
30918cb0756SThomas Monjalon  */
31018cb0756SThomas Monjalon __rte_experimental
31118cb0756SThomas Monjalon int rte_gpu_callback_register(int16_t dev_id, enum rte_gpu_event event,
31218cb0756SThomas Monjalon 		rte_gpu_callback_t *function, void *user_data);
31318cb0756SThomas Monjalon 
31418cb0756SThomas Monjalon /**
31518cb0756SThomas Monjalon  * @warning
31618cb0756SThomas Monjalon  * @b EXPERIMENTAL: this API may change without prior notice.
31718cb0756SThomas Monjalon  *
31818cb0756SThomas Monjalon  * Unregister for an event.
31918cb0756SThomas Monjalon  *
32018cb0756SThomas Monjalon  * @param dev_id
32118cb0756SThomas Monjalon  *   Device ID to be silenced.
32218cb0756SThomas Monjalon  *   RTE_GPU_ID_ANY means all devices.
32318cb0756SThomas Monjalon  * @param event
32418cb0756SThomas Monjalon  *   Registered event.
32518cb0756SThomas Monjalon  * @param function
32618cb0756SThomas Monjalon  *   Registered function.
32718cb0756SThomas Monjalon  * @param user_data
32818cb0756SThomas Monjalon  *   Optional parameter as registered.
32918cb0756SThomas Monjalon  *   RTE_GPU_CALLBACK_ANY_DATA is a catch-all.
33018cb0756SThomas Monjalon  *
33118cb0756SThomas Monjalon  * @return
33218cb0756SThomas Monjalon  *   0 on success, -rte_errno otherwise:
33318cb0756SThomas Monjalon  *   - ENODEV if invalid dev_id
33418cb0756SThomas Monjalon  *   - EINVAL if NULL function
33518cb0756SThomas Monjalon  */
33618cb0756SThomas Monjalon __rte_experimental
33718cb0756SThomas Monjalon int rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event,
33818cb0756SThomas Monjalon 		rte_gpu_callback_t *function, void *user_data);
33918cb0756SThomas Monjalon 
34018cb0756SThomas Monjalon /**
34118cb0756SThomas Monjalon  * @warning
34218cb0756SThomas Monjalon  * @b EXPERIMENTAL: this API may change without prior notice.
34318cb0756SThomas Monjalon  *
3448b8036a6SElena Agostini  * Return device specific info.
3458b8036a6SElena Agostini  *
3468b8036a6SElena Agostini  * @param dev_id
3478b8036a6SElena Agostini  *   Device ID to get info.
3488b8036a6SElena Agostini  * @param info
3498b8036a6SElena Agostini  *   Memory structure to fill with the info.
3508b8036a6SElena Agostini  *
3518b8036a6SElena Agostini  * @return
3528b8036a6SElena Agostini  *   0 on success, -rte_errno otherwise:
3538b8036a6SElena Agostini  *   - ENODEV if invalid dev_id
3548b8036a6SElena Agostini  *   - EINVAL if NULL info
3558b8036a6SElena Agostini  *   - EPERM if driver error
3568b8036a6SElena Agostini  */
3578b8036a6SElena Agostini __rte_experimental
3588b8036a6SElena Agostini int rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info);
3598b8036a6SElena Agostini 
360e818c4e2SElena Agostini /**
361e818c4e2SElena Agostini  * @warning
362e818c4e2SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
363e818c4e2SElena Agostini  *
364e818c4e2SElena Agostini  * Allocate a chunk of memory in the device.
365e818c4e2SElena Agostini  *
366e818c4e2SElena Agostini  * @param dev_id
367e818c4e2SElena Agostini  *   Device ID requiring allocated memory.
368e818c4e2SElena Agostini  * @param size
369e818c4e2SElena Agostini  *   Number of bytes to allocate.
370e818c4e2SElena Agostini  *   Requesting 0 will do nothing.
371c8557ed4SElena Agostini  * @param align
372c8557ed4SElena Agostini  *   If 0, the return is a pointer that is suitably aligned
373c8557ed4SElena Agostini  *   for any kind of variable (in the same manner as malloc()).
374c8557ed4SElena Agostini  *   Otherwise, the return is a pointer that is a multiple of *align*.
375c8557ed4SElena Agostini  *   In this case, it must obviously be a power of two.
376e818c4e2SElena Agostini  *
377e818c4e2SElena Agostini  * @return
378e818c4e2SElena Agostini  *   A pointer to the allocated memory, otherwise NULL and rte_errno is set:
379e818c4e2SElena Agostini  *   - ENODEV if invalid dev_id
380c8557ed4SElena Agostini  *   - EINVAL if align is not a power of two
381e818c4e2SElena Agostini  *   - ENOTSUP if operation not supported by the driver
382e818c4e2SElena Agostini  *   - E2BIG if size is higher than limit
383e818c4e2SElena Agostini  *   - ENOMEM if out of space
384e818c4e2SElena Agostini  *   - EPERM if driver error
385e818c4e2SElena Agostini  */
386e818c4e2SElena Agostini __rte_experimental
387c8557ed4SElena Agostini void *rte_gpu_mem_alloc(int16_t dev_id, size_t size, unsigned int align)
388e818c4e2SElena Agostini __rte_alloc_size(2);
389e818c4e2SElena Agostini 
390e818c4e2SElena Agostini /**
391e818c4e2SElena Agostini  * @warning
392e818c4e2SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
393e818c4e2SElena Agostini  *
394e818c4e2SElena Agostini  * Deallocate a chunk of memory allocated with rte_gpu_mem_alloc().
395e818c4e2SElena Agostini  *
396e818c4e2SElena Agostini  * @param dev_id
397e818c4e2SElena Agostini  *   Reference device ID.
398e818c4e2SElena Agostini  * @param ptr
399e818c4e2SElena Agostini  *   Pointer to the memory area to be deallocated.
400e818c4e2SElena Agostini  *   NULL is a no-op accepted value.
401e818c4e2SElena Agostini  *
402e818c4e2SElena Agostini  * @return
403e818c4e2SElena Agostini  *   0 on success, -rte_errno otherwise:
404e818c4e2SElena Agostini  *   - ENODEV if invalid dev_id
405e818c4e2SElena Agostini  *   - ENOTSUP if operation not supported by the driver
406e818c4e2SElena Agostini  *   - EPERM if driver error
407e818c4e2SElena Agostini  */
408e818c4e2SElena Agostini __rte_experimental
409e818c4e2SElena Agostini int rte_gpu_mem_free(int16_t dev_id, void *ptr);
410e818c4e2SElena Agostini 
411e818c4e2SElena Agostini /**
412e818c4e2SElena Agostini  * @warning
413e818c4e2SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
414e818c4e2SElena Agostini  *
415e818c4e2SElena Agostini  * Register a chunk of memory on the CPU usable by the device.
416e818c4e2SElena Agostini  *
417e818c4e2SElena Agostini  * @param dev_id
418e818c4e2SElena Agostini  *   Device ID requiring allocated memory.
419e818c4e2SElena Agostini  * @param size
420e818c4e2SElena Agostini  *   Number of bytes to allocate.
421e818c4e2SElena Agostini  *   Requesting 0 will do nothing.
422e818c4e2SElena Agostini  * @param ptr
423e818c4e2SElena Agostini  *   Pointer to the memory area to be registered.
424e818c4e2SElena Agostini  *   NULL is a no-op accepted value.
425e818c4e2SElena Agostini 
426e818c4e2SElena Agostini  * @return
427e818c4e2SElena Agostini  *   A pointer to the allocated memory, otherwise NULL and rte_errno is set:
428e818c4e2SElena Agostini  *   - ENODEV if invalid dev_id
429e818c4e2SElena Agostini  *   - EINVAL if reserved flags
430e818c4e2SElena Agostini  *   - ENOTSUP if operation not supported by the driver
431e818c4e2SElena Agostini  *   - E2BIG if size is higher than limit
432e818c4e2SElena Agostini  *   - ENOMEM if out of space
433e818c4e2SElena Agostini  *   - EPERM if driver error
434e818c4e2SElena Agostini  */
435e818c4e2SElena Agostini __rte_experimental
436e818c4e2SElena Agostini int rte_gpu_mem_register(int16_t dev_id, size_t size, void *ptr);
437e818c4e2SElena Agostini 
438e818c4e2SElena Agostini /**
439e818c4e2SElena Agostini  * @warning
440e818c4e2SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
441e818c4e2SElena Agostini  *
442e818c4e2SElena Agostini  * Deregister a chunk of memory previously registered with rte_gpu_mem_register()
443e818c4e2SElena Agostini  *
444e818c4e2SElena Agostini  * @param dev_id
445e818c4e2SElena Agostini  *   Reference device ID.
446e818c4e2SElena Agostini  * @param ptr
447e818c4e2SElena Agostini  *   Pointer to the memory area to be unregistered.
448e818c4e2SElena Agostini  *   NULL is a no-op accepted value.
449e818c4e2SElena Agostini  *
450e818c4e2SElena Agostini  * @return
451e818c4e2SElena Agostini  *   0 on success, -rte_errno otherwise:
452e818c4e2SElena Agostini  *   - ENODEV if invalid dev_id
453e818c4e2SElena Agostini  *   - ENOTSUP if operation not supported by the driver
454e818c4e2SElena Agostini  *   - EPERM if driver error
455e818c4e2SElena Agostini  */
456e818c4e2SElena Agostini __rte_experimental
457e818c4e2SElena Agostini int rte_gpu_mem_unregister(int16_t dev_id, void *ptr);
458e818c4e2SElena Agostini 
4592d61b429SElena Agostini /**
4602d61b429SElena Agostini  * @warning
4612d61b429SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
4622d61b429SElena Agostini  *
463d69bb47dSElena Agostini  * Map a chunk of GPU memory to make it accessible from the CPU
464d69bb47dSElena Agostini  * using the memory pointer returned by the function.
465d69bb47dSElena Agostini  * GPU memory has to be allocated via rte_gpu_mem_alloc().
466d69bb47dSElena Agostini  *
467d69bb47dSElena Agostini  * @param dev_id
468d69bb47dSElena Agostini  *   Device ID requiring mapped memory.
469d69bb47dSElena Agostini  * @param size
470d69bb47dSElena Agostini  *   Number of bytes to map.
471d69bb47dSElena Agostini  *   Requesting 0 will do nothing.
472d69bb47dSElena Agostini  * @param ptr
473d69bb47dSElena Agostini  *   Pointer to the GPU memory area to be mapped.
474d69bb47dSElena Agostini  *   NULL is a no-op accepted value.
475d69bb47dSElena Agostini 
476d69bb47dSElena Agostini  * @return
477d69bb47dSElena Agostini  *   A pointer to the mapped GPU memory usable by the CPU, otherwise NULL and rte_errno is set:
478d69bb47dSElena Agostini  *   - ENODEV if invalid dev_id
479d69bb47dSElena Agostini  *   - ENOTSUP if operation not supported by the driver
480d69bb47dSElena Agostini  *   - E2BIG if size is higher than limit
481d69bb47dSElena Agostini  *   - ENOMEM if out of space
482d69bb47dSElena Agostini  *   - EPERM if driver error
483d69bb47dSElena Agostini  */
484d69bb47dSElena Agostini __rte_experimental
485d69bb47dSElena Agostini void *rte_gpu_mem_cpu_map(int16_t dev_id, size_t size, void *ptr);
486d69bb47dSElena Agostini 
487d69bb47dSElena Agostini /**
488d69bb47dSElena Agostini  * @warning
489d69bb47dSElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
490d69bb47dSElena Agostini  *
491d69bb47dSElena Agostini  * Unmap a chunk of GPU memory previously mapped with rte_gpu_mem_cpu_map()
492d69bb47dSElena Agostini  *
493d69bb47dSElena Agostini  * @param dev_id
494d69bb47dSElena Agostini  *   Reference device ID.
495d69bb47dSElena Agostini  * @param ptr
49677f40e04SElena Agostini  *   Pointer to the GPU memory area to be unmapped.
497d69bb47dSElena Agostini  *   NULL is a no-op accepted value.
498d69bb47dSElena Agostini  *
499d69bb47dSElena Agostini  * @return
500d69bb47dSElena Agostini  *   0 on success, -rte_errno otherwise:
501d69bb47dSElena Agostini  *   - ENODEV if invalid dev_id
502d69bb47dSElena Agostini  *   - ENOTSUP if operation not supported by the driver
503d69bb47dSElena Agostini  *   - EPERM if driver error
504d69bb47dSElena Agostini  */
505d69bb47dSElena Agostini __rte_experimental
506d69bb47dSElena Agostini int rte_gpu_mem_cpu_unmap(int16_t dev_id, void *ptr);
507d69bb47dSElena Agostini 
508d69bb47dSElena Agostini /**
509d69bb47dSElena Agostini  * @warning
510d69bb47dSElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
511d69bb47dSElena Agostini  *
5122d61b429SElena Agostini  * Enforce a GPU write memory barrier.
5132d61b429SElena Agostini  *
5142d61b429SElena Agostini  * @param dev_id
5152d61b429SElena Agostini  *   Reference device ID.
5162d61b429SElena Agostini  *
5172d61b429SElena Agostini  * @return
5182d61b429SElena Agostini  *   0 on success, -rte_errno otherwise:
5192d61b429SElena Agostini  *   - ENODEV if invalid dev_id
5202d61b429SElena Agostini  *   - ENOTSUP if operation not supported by the driver
5212d61b429SElena Agostini  *   - EPERM if driver error
5222d61b429SElena Agostini  */
5232d61b429SElena Agostini __rte_experimental
5242d61b429SElena Agostini int rte_gpu_wmb(int16_t dev_id);
5252d61b429SElena Agostini 
526f56160a2SElena Agostini /**
527f56160a2SElena Agostini  * @warning
528f56160a2SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
529f56160a2SElena Agostini  *
530f56160a2SElena Agostini  * Create a communication flag that can be shared
531f56160a2SElena Agostini  * between CPU threads and device workload to exchange some status info
532f56160a2SElena Agostini  * (e.g. work is done, processing can start, etc..).
533f56160a2SElena Agostini  *
534f56160a2SElena Agostini  * @param dev_id
535f56160a2SElena Agostini  *   Reference device ID.
536f56160a2SElena Agostini  * @param devflag
537f56160a2SElena Agostini  *   Pointer to the memory area of the devflag structure.
538f56160a2SElena Agostini  * @param mtype
539f56160a2SElena Agostini  *   Type of memory to allocate the communication flag.
540f56160a2SElena Agostini  *
541f56160a2SElena Agostini  * @return
542f56160a2SElena Agostini  *   0 on success, -rte_errno otherwise:
543f56160a2SElena Agostini  *   - ENODEV if invalid dev_id
544f56160a2SElena Agostini  *   - EINVAL if invalid inputs
545f56160a2SElena Agostini  *   - ENOTSUP if operation not supported by the driver
546f56160a2SElena Agostini  *   - ENOMEM if out of space
547f56160a2SElena Agostini  *   - EPERM if driver error
548f56160a2SElena Agostini  */
549f56160a2SElena Agostini __rte_experimental
550f56160a2SElena Agostini int rte_gpu_comm_create_flag(uint16_t dev_id,
551f56160a2SElena Agostini 		struct rte_gpu_comm_flag *devflag,
552f56160a2SElena Agostini 		enum rte_gpu_comm_flag_type mtype);
553f56160a2SElena Agostini 
554f56160a2SElena Agostini /**
555f56160a2SElena Agostini  * @warning
556f56160a2SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
557f56160a2SElena Agostini  *
558f56160a2SElena Agostini  * Deallocate a communication flag.
559f56160a2SElena Agostini  *
560f56160a2SElena Agostini  * @param devflag
561f56160a2SElena Agostini  *   Pointer to the memory area of the devflag structure.
562f56160a2SElena Agostini  *
563f56160a2SElena Agostini  * @return
564f56160a2SElena Agostini  *   0 on success, -rte_errno otherwise:
565f56160a2SElena Agostini  *   - ENODEV if invalid dev_id
566f56160a2SElena Agostini  *   - EINVAL if NULL devflag
567f56160a2SElena Agostini  *   - ENOTSUP if operation not supported by the driver
568f56160a2SElena Agostini  *   - EPERM if driver error
569f56160a2SElena Agostini  */
570f56160a2SElena Agostini __rte_experimental
571f56160a2SElena Agostini int rte_gpu_comm_destroy_flag(struct rte_gpu_comm_flag *devflag);
572f56160a2SElena Agostini 
573f56160a2SElena Agostini /**
574f56160a2SElena Agostini  * @warning
575f56160a2SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
576f56160a2SElena Agostini  *
577f56160a2SElena Agostini  * Set the value of a communication flag as the input value.
578f56160a2SElena Agostini  * Flag memory area is treated as volatile.
579f56160a2SElena Agostini  * The flag must have been allocated with RTE_GPU_COMM_FLAG_CPU.
580f56160a2SElena Agostini  *
581f56160a2SElena Agostini  * @param devflag
582f56160a2SElena Agostini  *   Pointer to the memory area of the devflag structure.
583f56160a2SElena Agostini  * @param val
584f56160a2SElena Agostini  *   Value to set in the flag.
585f56160a2SElena Agostini  *
586f56160a2SElena Agostini  * @return
587f56160a2SElena Agostini  *   0 on success, -rte_errno otherwise:
588f56160a2SElena Agostini  *   - EINVAL if invalid input params
589f56160a2SElena Agostini  */
590f56160a2SElena Agostini __rte_experimental
591f56160a2SElena Agostini int rte_gpu_comm_set_flag(struct rte_gpu_comm_flag *devflag,
592f56160a2SElena Agostini 		uint32_t val);
593f56160a2SElena Agostini 
594f56160a2SElena Agostini /**
595f56160a2SElena Agostini  * @warning
596f56160a2SElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
597f56160a2SElena Agostini  *
598f56160a2SElena Agostini  * Get the value of the communication flag.
599f56160a2SElena Agostini  * Flag memory area is treated as volatile.
600f56160a2SElena Agostini  * The flag must have been allocated with RTE_GPU_COMM_FLAG_CPU.
601f56160a2SElena Agostini  *
602f56160a2SElena Agostini  * @param devflag
603f56160a2SElena Agostini  *   Pointer to the memory area of the devflag structure.
604f56160a2SElena Agostini  * @param val
605f56160a2SElena Agostini  *   Flag output value.
606f56160a2SElena Agostini  *
607f56160a2SElena Agostini  * @return
608f56160a2SElena Agostini  *   0 on success, -rte_errno otherwise:
609f56160a2SElena Agostini  *   - EINVAL if invalid input params
610f56160a2SElena Agostini  */
611f56160a2SElena Agostini __rte_experimental
612f56160a2SElena Agostini int rte_gpu_comm_get_flag_value(struct rte_gpu_comm_flag *devflag,
613f56160a2SElena Agostini 		uint32_t *val);
614f56160a2SElena Agostini 
615c7ebd65cSElena Agostini /**
616c7ebd65cSElena Agostini  * @warning
617c7ebd65cSElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
618c7ebd65cSElena Agostini  *
619c7ebd65cSElena Agostini  * Create a communication list that can be used to share packets
620c7ebd65cSElena Agostini  * between CPU and device.
621c7ebd65cSElena Agostini  * Each element of the list contains:
622c7ebd65cSElena Agostini  *  - a packet list of RTE_GPU_COMM_LIST_PKTS_MAX elements
623c7ebd65cSElena Agostini  *  - number of packets in the list
624c7ebd65cSElena Agostini  *  - a status flag to communicate if the packet list is FREE,
625c7ebd65cSElena Agostini  *    READY to be processed, DONE with processing.
626c7ebd65cSElena Agostini  *
627c7ebd65cSElena Agostini  * The list is allocated in CPU-visible memory.
628c7ebd65cSElena Agostini  * At creation time, every list is in FREE state.
629c7ebd65cSElena Agostini  *
630c7ebd65cSElena Agostini  * @param dev_id
631c7ebd65cSElena Agostini  *   Reference device ID.
632c7ebd65cSElena Agostini  * @param num_comm_items
633c7ebd65cSElena Agostini  *   Number of items in the communication list.
634c7ebd65cSElena Agostini  *
635c7ebd65cSElena Agostini  * @return
636c7ebd65cSElena Agostini  *   A pointer to the allocated list, otherwise NULL and rte_errno is set:
637c7ebd65cSElena Agostini  *   - EINVAL if invalid input params
638c7ebd65cSElena Agostini  */
639c7ebd65cSElena Agostini __rte_experimental
640c7ebd65cSElena Agostini struct rte_gpu_comm_list *rte_gpu_comm_create_list(uint16_t dev_id,
641c7ebd65cSElena Agostini 		uint32_t num_comm_items);
642c7ebd65cSElena Agostini 
643c7ebd65cSElena Agostini /**
644c7ebd65cSElena Agostini  * @warning
645c7ebd65cSElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
646c7ebd65cSElena Agostini  *
647c7ebd65cSElena Agostini  * Destroy a communication list.
648c7ebd65cSElena Agostini  *
649c7ebd65cSElena Agostini  * @param comm_list
650c7ebd65cSElena Agostini  *   Communication list to be destroyed.
651c7ebd65cSElena Agostini  * @param num_comm_items
652c7ebd65cSElena Agostini  *   Number of items in the communication list.
653c7ebd65cSElena Agostini  *
654c7ebd65cSElena Agostini  * @return
655c7ebd65cSElena Agostini  *   0 on success, -rte_errno otherwise:
656c7ebd65cSElena Agostini  *   - EINVAL if invalid input params
657c7ebd65cSElena Agostini  */
658c7ebd65cSElena Agostini __rte_experimental
659c7ebd65cSElena Agostini int rte_gpu_comm_destroy_list(struct rte_gpu_comm_list *comm_list,
660c7ebd65cSElena Agostini 		uint32_t num_comm_items);
661c7ebd65cSElena Agostini 
662c7ebd65cSElena Agostini /**
663c7ebd65cSElena Agostini  * @warning
664c7ebd65cSElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
665c7ebd65cSElena Agostini  *
666c7ebd65cSElena Agostini  * Populate the packets list of the communication item
667c7ebd65cSElena Agostini  * with info from a list of mbufs.
668c7ebd65cSElena Agostini  * Status flag of that packet list is set to READY.
669c7ebd65cSElena Agostini  *
670c7ebd65cSElena Agostini  * @param comm_list_item
671c7ebd65cSElena Agostini  *   Communication list item to fill.
672c7ebd65cSElena Agostini  * @param mbufs
673c7ebd65cSElena Agostini  *   List of mbufs.
674c7ebd65cSElena Agostini  * @param num_mbufs
675c7ebd65cSElena Agostini  *   Number of mbufs.
676c7ebd65cSElena Agostini  *
677c7ebd65cSElena Agostini  * @return
678c7ebd65cSElena Agostini  *   0 on success, -rte_errno otherwise:
679c7ebd65cSElena Agostini  *   - EINVAL if invalid input params
680c7ebd65cSElena Agostini  *   - ENOTSUP if mbufs are chained (multiple segments)
681c7ebd65cSElena Agostini  */
682c7ebd65cSElena Agostini __rte_experimental
683c7ebd65cSElena Agostini int rte_gpu_comm_populate_list_pkts(struct rte_gpu_comm_list *comm_list_item,
684c7ebd65cSElena Agostini 		struct rte_mbuf **mbufs, uint32_t num_mbufs);
685c7ebd65cSElena Agostini 
686c7ebd65cSElena Agostini /**
687c7ebd65cSElena Agostini  * @warning
688c7ebd65cSElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
689c7ebd65cSElena Agostini  *
6909b8cae4dSElena Agostini  * Set status flag value of a communication list item.
6919b8cae4dSElena Agostini  *
6929b8cae4dSElena Agostini  * @param comm_list_item
6939b8cae4dSElena Agostini  *   Communication list item to query.
6949b8cae4dSElena Agostini  * @param status
6959b8cae4dSElena Agostini  *   Status value to set.
6969b8cae4dSElena Agostini  *
6979b8cae4dSElena Agostini  * @return
6989b8cae4dSElena Agostini  *   0 on success, -rte_errno otherwise:
6999b8cae4dSElena Agostini  *   - EINVAL if invalid input params
7009b8cae4dSElena Agostini  */
7019b8cae4dSElena Agostini __rte_experimental
7029b8cae4dSElena Agostini int rte_gpu_comm_set_status(struct rte_gpu_comm_list *comm_list_item,
7039b8cae4dSElena Agostini 		enum rte_gpu_comm_list_status status);
7049b8cae4dSElena Agostini 
7059b8cae4dSElena Agostini /**
7069b8cae4dSElena Agostini  * @warning
7079b8cae4dSElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
7089b8cae4dSElena Agostini  *
7099b8cae4dSElena Agostini  * Get status flag value of a communication list item.
7109b8cae4dSElena Agostini  *
7119b8cae4dSElena Agostini  * @param comm_list_item
7129b8cae4dSElena Agostini  *   Communication list item to query.
7139b8cae4dSElena Agostini  *   Input parameter.
7149b8cae4dSElena Agostini  * @param status
7159b8cae4dSElena Agostini  *   Communication list item status flag value.
7169b8cae4dSElena Agostini  *   Output parameter.
7179b8cae4dSElena Agostini  *
7189b8cae4dSElena Agostini  * @return
7199b8cae4dSElena Agostini  *   0 on success, -rte_errno otherwise:
7209b8cae4dSElena Agostini  *   - EINVAL if invalid input params
7219b8cae4dSElena Agostini  */
7229b8cae4dSElena Agostini __rte_experimental
7239b8cae4dSElena Agostini int rte_gpu_comm_get_status(struct rte_gpu_comm_list *comm_list_item,
7249b8cae4dSElena Agostini 		enum rte_gpu_comm_list_status *status);
7259b8cae4dSElena Agostini 
7269b8cae4dSElena Agostini /**
7279b8cae4dSElena Agostini  * @warning
7289b8cae4dSElena Agostini  * @b EXPERIMENTAL: this API may change without prior notice.
7299b8cae4dSElena Agostini  *
730c7ebd65cSElena Agostini  * Reset a communication list item to the original state.
731c7ebd65cSElena Agostini  * The status flag set to FREE and mbufs are returned to the pool.
732c7ebd65cSElena Agostini  *
733c7ebd65cSElena Agostini  * @param comm_list_item
734c7ebd65cSElena Agostini  *   Communication list item to reset.
735c7ebd65cSElena Agostini  *
736c7ebd65cSElena Agostini  * @return
737c7ebd65cSElena Agostini  *   0 on success, -rte_errno otherwise:
738c7ebd65cSElena Agostini  *   - EINVAL if invalid input params
739c7ebd65cSElena Agostini  */
740c7ebd65cSElena Agostini __rte_experimental
741c7ebd65cSElena Agostini int rte_gpu_comm_cleanup_list(struct rte_gpu_comm_list *comm_list_item);
742c7ebd65cSElena Agostini 
7438b8036a6SElena Agostini #ifdef __cplusplus
7448b8036a6SElena Agostini }
7458b8036a6SElena Agostini #endif
7468b8036a6SElena Agostini 
7478b8036a6SElena Agostini #endif /* RTE_GPUDEV_H */
748