1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
13 #include <rte_bitops.h>
14 #include <rte_compat.h>
18 * Generic library to interact with GPU computing device.
20 * The API is not thread-safe.
21 * Device management must be done by a single thread.
24 * @b EXPERIMENTAL: this API may change without prior notice.
31 /** Maximum number of devices if rte_gpu_init() is not called. */
32 #define RTE_GPU_DEFAULT_MAX 32
34 /** Empty device ID. */
35 #define RTE_GPU_ID_NONE -1
36 /** Catch-all device ID. */
37 #define RTE_GPU_ID_ANY INT16_MIN
39 /** Catch-all callback data. */
40 #define RTE_GPU_CALLBACK_ANY_DATA ((void *)-1)
42 /** Access variable as volatile. */
43 #define RTE_GPU_VOLATILE(x) (*(volatile typeof(x) *)&(x))
45 /** Max number of packets per communication list. */
46 #define RTE_GPU_COMM_LIST_PKTS_MAX 1024
48 /** Store device info. */
50 /** Unique identifier name. */
52 /** Opaque handler of the device context. */
56 /** ID of the parent device, RTE_GPU_ID_NONE if no parent */
58 /** Total processors available on device. */
59 uint32_t processor_count;
60 /** Total memory available on device. */
62 /* Local NUMA memory ID. -1 if unknown. */
66 /** Flags passed in notification callback. */
68 /** Device is just initialized. */
70 /** Device is going to be released. */
74 /** Prototype of event callback function. */
75 typedef void (rte_gpu_callback_t)(int16_t dev_id,
76 enum rte_gpu_event event, void *user_data);
78 /** Memory where communication flag is allocated. */
79 enum rte_gpu_comm_flag_type {
80 /** Allocate flag on CPU memory visible from device. */
81 RTE_GPU_COMM_FLAG_CPU = 0,
84 /** Communication flag to coordinate CPU with the device. */
85 struct rte_gpu_comm_flag {
86 /** Device that will use the device flag. */
88 /** Pointer to flag memory area. */
90 /** Type of memory used to allocate the flag. */
91 enum rte_gpu_comm_flag_type mtype;
94 /** List of packets shared among CPU and device. */
95 struct rte_gpu_comm_pkt {
96 /** Address of the packet in memory (e.g. mbuf->buf_addr). */
98 /** Size in byte of the packet. */
102 /** Possible status for the list of packets shared among CPU and device. */
103 enum rte_gpu_comm_list_status {
104 /** Packet list can be filled with new mbufs, no one is using it. */
105 RTE_GPU_COMM_LIST_FREE = 0,
106 /** Packet list has been filled with new mbufs and it's ready to be used .*/
107 RTE_GPU_COMM_LIST_READY,
108 /** Packet list has been processed, it's ready to be freed. */
109 RTE_GPU_COMM_LIST_DONE,
110 /** Some error occurred during packet list processing. */
111 RTE_GPU_COMM_LIST_ERROR,
115 * Communication list holding a number of lists of packets
116 * each having a status flag.
118 struct rte_gpu_comm_list {
119 /** Device that will use the communication list. */
121 /** List of mbufs populated by the CPU with a set of mbufs. */
122 struct rte_mbuf **mbufs;
123 /** List of packets populated by the CPU with a set of mbufs info. */
124 struct rte_gpu_comm_pkt *pkt_list;
125 /** Number of packets in the list. */
127 /** Status of the list. */
128 enum rte_gpu_comm_list_status status;
133 * @b EXPERIMENTAL: this API may change without prior notice.
135 * Initialize the device array before probing devices.
136 * If not called, the maximum of probed devices is RTE_GPU_DEFAULT_MAX.
139 * Maximum number of devices.
142 * 0 on success, -rte_errno otherwise:
143 * - ENOMEM if out of memory
145 * - EBUSY if already initialized
148 int rte_gpu_init(size_t dev_max);
152 * @b EXPERIMENTAL: this API may change without prior notice.
154 * Return the number of GPU detected and associated to DPDK.
157 * The number of available computing devices.
160 uint16_t rte_gpu_count_avail(void);
164 * @b EXPERIMENTAL: this API may change without prior notice.
166 * Check if the device is valid and initialized in DPDK.
169 * The input device ID.
172 * - True if dev_id is a valid and initialized computing device.
176 bool rte_gpu_is_valid(int16_t dev_id);
180 * @b EXPERIMENTAL: this API may change without prior notice.
182 * Create a virtual device representing a context in the parent device.
185 * Unique string to identify the device.
187 * Device ID of the parent.
188 * @param child_context
189 * Opaque context handler.
192 * Device ID of the new created child, -rte_errno otherwise:
193 * - EINVAL if empty name
194 * - ENAMETOOLONG if long name
195 * - EEXIST if existing device name
196 * - ENODEV if invalid parent
197 * - EPERM if secondary process
198 * - ENOENT if too many devices
199 * - ENOMEM if out of space
202 int16_t rte_gpu_add_child(const char *name,
203 int16_t parent, uint64_t child_context);
207 * @b EXPERIMENTAL: this API may change without prior notice.
209 * Get the ID of the next valid GPU initialized in DPDK.
212 * The initial device ID to start the research.
214 * The device ID of the parent.
215 * RTE_GPU_ID_NONE means no parent.
216 * RTE_GPU_ID_ANY means no or any parent.
219 * Next device ID corresponding to a valid and initialized computing device,
220 * RTE_GPU_ID_NONE if there is none.
223 int16_t rte_gpu_find_next(int16_t dev_id, int16_t parent);
227 * @b EXPERIMENTAL: this API may change without prior notice.
229 * Macro to iterate over all valid GPU devices.
232 * The ID of the next possible valid device, usually 0 to iterate all.
234 #define RTE_GPU_FOREACH(dev_id) \
235 RTE_GPU_FOREACH_CHILD(dev_id, RTE_GPU_ID_ANY)
239 * @b EXPERIMENTAL: this API may change without prior notice.
241 * Macro to iterate over all valid computing devices having no parent.
244 * The ID of the next possible valid device, usually 0 to iterate all.
246 #define RTE_GPU_FOREACH_PARENT(dev_id) \
247 RTE_GPU_FOREACH_CHILD(dev_id, RTE_GPU_ID_NONE)
251 * @b EXPERIMENTAL: this API may change without prior notice.
253 * Macro to iterate over all valid children of a computing device parent.
256 * The ID of the next possible valid device, usually 0 to iterate all.
258 * The device ID of the parent.
260 #define RTE_GPU_FOREACH_CHILD(dev_id, parent) \
261 for (dev_id = rte_gpu_find_next(0, parent); \
263 dev_id = rte_gpu_find_next(dev_id + 1, parent))
267 * @b EXPERIMENTAL: this API may change without prior notice.
269 * Close device or child context.
270 * All resources are released.
273 * Device ID to close.
276 * 0 on success, -rte_errno otherwise:
277 * - ENODEV if invalid dev_id
278 * - EPERM if driver error
281 int rte_gpu_close(int16_t dev_id);
285 * @b EXPERIMENTAL: this API may change without prior notice.
287 * Register a function as event callback.
288 * A function may be registered multiple times for different events.
291 * Device ID to get notified about.
292 * RTE_GPU_ID_ANY means all devices.
294 * Device event to be registered for.
296 * Callback function to be called on event.
298 * Optional parameter passed in the callback.
301 * 0 on success, -rte_errno otherwise:
302 * - ENODEV if invalid dev_id
303 * - EINVAL if NULL function
304 * - ENOMEM if out of memory
307 int rte_gpu_callback_register(int16_t dev_id, enum rte_gpu_event event,
308 rte_gpu_callback_t *function, void *user_data);
312 * @b EXPERIMENTAL: this API may change without prior notice.
314 * Unregister for an event.
317 * Device ID to be silenced.
318 * RTE_GPU_ID_ANY means all devices.
322 * Registered function.
324 * Optional parameter as registered.
325 * RTE_GPU_CALLBACK_ANY_DATA is a catch-all.
328 * 0 on success, -rte_errno otherwise:
329 * - ENODEV if invalid dev_id
330 * - EINVAL if NULL function
333 int rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event,
334 rte_gpu_callback_t *function, void *user_data);
338 * @b EXPERIMENTAL: this API may change without prior notice.
340 * Return device specific info.
343 * Device ID to get info.
345 * Memory structure to fill with the info.
348 * 0 on success, -rte_errno otherwise:
349 * - ENODEV if invalid dev_id
350 * - EINVAL if NULL info
351 * - EPERM if driver error
354 int rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info);
358 * @b EXPERIMENTAL: this API may change without prior notice.
360 * Allocate a chunk of memory in the device.
363 * Device ID requiring allocated memory.
365 * Number of bytes to allocate.
366 * Requesting 0 will do nothing.
369 * A pointer to the allocated memory, otherwise NULL and rte_errno is set:
370 * - ENODEV if invalid dev_id
371 * - EINVAL if reserved flags
372 * - ENOTSUP if operation not supported by the driver
373 * - E2BIG if size is higher than limit
374 * - ENOMEM if out of space
375 * - EPERM if driver error
378 void *rte_gpu_mem_alloc(int16_t dev_id, size_t size)
383 * @b EXPERIMENTAL: this API may change without prior notice.
385 * Deallocate a chunk of memory allocated with rte_gpu_mem_alloc().
388 * Reference device ID.
390 * Pointer to the memory area to be deallocated.
391 * NULL is a no-op accepted value.
394 * 0 on success, -rte_errno otherwise:
395 * - ENODEV if invalid dev_id
396 * - ENOTSUP if operation not supported by the driver
397 * - EPERM if driver error
400 int rte_gpu_mem_free(int16_t dev_id, void *ptr);
404 * @b EXPERIMENTAL: this API may change without prior notice.
406 * Register a chunk of memory on the CPU usable by the device.
409 * Device ID requiring allocated memory.
411 * Number of bytes to allocate.
412 * Requesting 0 will do nothing.
414 * Pointer to the memory area to be registered.
415 * NULL is a no-op accepted value.
418 * A pointer to the allocated memory, otherwise NULL and rte_errno is set:
419 * - ENODEV if invalid dev_id
420 * - EINVAL if reserved flags
421 * - ENOTSUP if operation not supported by the driver
422 * - E2BIG if size is higher than limit
423 * - ENOMEM if out of space
424 * - EPERM if driver error
427 int rte_gpu_mem_register(int16_t dev_id, size_t size, void *ptr);
431 * @b EXPERIMENTAL: this API may change without prior notice.
433 * Deregister a chunk of memory previously registered with rte_gpu_mem_register()
436 * Reference device ID.
438 * Pointer to the memory area to be unregistered.
439 * NULL is a no-op accepted value.
442 * 0 on success, -rte_errno otherwise:
443 * - ENODEV if invalid dev_id
444 * - ENOTSUP if operation not supported by the driver
445 * - EPERM if driver error
448 int rte_gpu_mem_unregister(int16_t dev_id, void *ptr);
452 * @b EXPERIMENTAL: this API may change without prior notice.
454 * Enforce a GPU write memory barrier.
457 * Reference device ID.
460 * 0 on success, -rte_errno otherwise:
461 * - ENODEV if invalid dev_id
462 * - ENOTSUP if operation not supported by the driver
463 * - EPERM if driver error
466 int rte_gpu_wmb(int16_t dev_id);
470 * @b EXPERIMENTAL: this API may change without prior notice.
472 * Create a communication flag that can be shared
473 * between CPU threads and device workload to exchange some status info
474 * (e.g. work is done, processing can start, etc..).
477 * Reference device ID.
479 * Pointer to the memory area of the devflag structure.
481 * Type of memory to allocate the communication flag.
484 * 0 on success, -rte_errno otherwise:
485 * - ENODEV if invalid dev_id
486 * - EINVAL if invalid inputs
487 * - ENOTSUP if operation not supported by the driver
488 * - ENOMEM if out of space
489 * - EPERM if driver error
492 int rte_gpu_comm_create_flag(uint16_t dev_id,
493 struct rte_gpu_comm_flag *devflag,
494 enum rte_gpu_comm_flag_type mtype);
498 * @b EXPERIMENTAL: this API may change without prior notice.
500 * Deallocate a communication flag.
503 * Pointer to the memory area of the devflag structure.
506 * 0 on success, -rte_errno otherwise:
507 * - ENODEV if invalid dev_id
508 * - EINVAL if NULL devflag
509 * - ENOTSUP if operation not supported by the driver
510 * - EPERM if driver error
513 int rte_gpu_comm_destroy_flag(struct rte_gpu_comm_flag *devflag);
517 * @b EXPERIMENTAL: this API may change without prior notice.
519 * Set the value of a communication flag as the input value.
520 * Flag memory area is treated as volatile.
521 * The flag must have been allocated with RTE_GPU_COMM_FLAG_CPU.
524 * Pointer to the memory area of the devflag structure.
526 * Value to set in the flag.
529 * 0 on success, -rte_errno otherwise:
530 * - EINVAL if invalid input params
533 int rte_gpu_comm_set_flag(struct rte_gpu_comm_flag *devflag,
538 * @b EXPERIMENTAL: this API may change without prior notice.
540 * Get the value of the communication flag.
541 * Flag memory area is treated as volatile.
542 * The flag must have been allocated with RTE_GPU_COMM_FLAG_CPU.
545 * Pointer to the memory area of the devflag structure.
550 * 0 on success, -rte_errno otherwise:
551 * - EINVAL if invalid input params
554 int rte_gpu_comm_get_flag_value(struct rte_gpu_comm_flag *devflag,
559 * @b EXPERIMENTAL: this API may change without prior notice.
561 * Create a communication list that can be used to share packets
562 * between CPU and device.
563 * Each element of the list contains:
564 * - a packet list of RTE_GPU_COMM_LIST_PKTS_MAX elements
565 * - number of packets in the list
566 * - a status flag to communicate if the packet list is FREE,
567 * READY to be processed, DONE with processing.
569 * The list is allocated in CPU-visible memory.
570 * At creation time, every list is in FREE state.
573 * Reference device ID.
574 * @param num_comm_items
575 * Number of items in the communication list.
578 * A pointer to the allocated list, otherwise NULL and rte_errno is set:
579 * - EINVAL if invalid input params
582 struct rte_gpu_comm_list *rte_gpu_comm_create_list(uint16_t dev_id,
583 uint32_t num_comm_items);
587 * @b EXPERIMENTAL: this API may change without prior notice.
589 * Destroy a communication list.
592 * Communication list to be destroyed.
593 * @param num_comm_items
594 * Number of items in the communication list.
597 * 0 on success, -rte_errno otherwise:
598 * - EINVAL if invalid input params
601 int rte_gpu_comm_destroy_list(struct rte_gpu_comm_list *comm_list,
602 uint32_t num_comm_items);
606 * @b EXPERIMENTAL: this API may change without prior notice.
608 * Populate the packets list of the communication item
609 * with info from a list of mbufs.
610 * Status flag of that packet list is set to READY.
612 * @param comm_list_item
613 * Communication list item to fill.
620 * 0 on success, -rte_errno otherwise:
621 * - EINVAL if invalid input params
622 * - ENOTSUP if mbufs are chained (multiple segments)
625 int rte_gpu_comm_populate_list_pkts(struct rte_gpu_comm_list *comm_list_item,
626 struct rte_mbuf **mbufs, uint32_t num_mbufs);
630 * @b EXPERIMENTAL: this API may change without prior notice.
632 * Reset a communication list item to the original state.
633 * The status flag set to FREE and mbufs are returned to the pool.
635 * @param comm_list_item
636 * Communication list item to reset.
639 * 0 on success, -rte_errno otherwise:
640 * - EINVAL if invalid input params
643 int rte_gpu_comm_cleanup_list(struct rte_gpu_comm_list *comm_list_item);
649 #endif /* RTE_GPUDEV_H */