1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
12 #include <rte_bitops.h>
13 #include <rte_compat.h>
17 * Generic library to interact with GPU computing device.
19 * The API is not thread-safe.
20 * Device management must be done by a single thread.
23 * @b EXPERIMENTAL: this API may change without prior notice.
30 /** Maximum number of devices if rte_gpu_init() is not called. */
31 #define RTE_GPU_DEFAULT_MAX 32
33 /** Empty device ID. */
34 #define RTE_GPU_ID_NONE -1
35 /** Catch-all device ID. */
36 #define RTE_GPU_ID_ANY INT16_MIN
38 /** Catch-all callback data. */
39 #define RTE_GPU_CALLBACK_ANY_DATA ((void *)-1)
41 /** Access variable as volatile. */
42 #define RTE_GPU_VOLATILE(x) (*(volatile typeof(x) *)&(x))
44 /** Store device info. */
46 /** Unique identifier name. */
48 /** Opaque handler of the device context. */
52 /** ID of the parent device, RTE_GPU_ID_NONE if no parent */
54 /** Total processors available on device. */
55 uint32_t processor_count;
56 /** Total memory available on device. */
58 /* Local NUMA memory ID. -1 if unknown. */
62 /** Flags passed in notification callback. */
64 /** Device is just initialized. */
66 /** Device is going to be released. */
70 /** Prototype of event callback function. */
71 typedef void (rte_gpu_callback_t)(int16_t dev_id,
72 enum rte_gpu_event event, void *user_data);
74 /** Memory where communication flag is allocated. */
75 enum rte_gpu_comm_flag_type {
76 /** Allocate flag on CPU memory visible from device. */
77 RTE_GPU_COMM_FLAG_CPU = 0,
80 /** Communication flag to coordinate CPU with the device. */
81 struct rte_gpu_comm_flag {
82 /** Device that will use the device flag. */
84 /** Pointer to flag memory area. */
86 /** Type of memory used to allocate the flag. */
87 enum rte_gpu_comm_flag_type mtype;
92 * @b EXPERIMENTAL: this API may change without prior notice.
94 * Initialize the device array before probing devices.
95 * If not called, the maximum of probed devices is RTE_GPU_DEFAULT_MAX.
98 * Maximum number of devices.
101 * 0 on success, -rte_errno otherwise:
102 * - ENOMEM if out of memory
104 * - EBUSY if already initialized
107 int rte_gpu_init(size_t dev_max);
111 * @b EXPERIMENTAL: this API may change without prior notice.
113 * Return the number of GPU detected and associated to DPDK.
116 * The number of available computing devices.
119 uint16_t rte_gpu_count_avail(void);
123 * @b EXPERIMENTAL: this API may change without prior notice.
125 * Check if the device is valid and initialized in DPDK.
128 * The input device ID.
131 * - True if dev_id is a valid and initialized computing device.
135 bool rte_gpu_is_valid(int16_t dev_id);
139 * @b EXPERIMENTAL: this API may change without prior notice.
141 * Create a virtual device representing a context in the parent device.
144 * Unique string to identify the device.
146 * Device ID of the parent.
147 * @param child_context
148 * Opaque context handler.
151 * Device ID of the new created child, -rte_errno otherwise:
152 * - EINVAL if empty name
153 * - ENAMETOOLONG if long name
154 * - EEXIST if existing device name
155 * - ENODEV if invalid parent
156 * - EPERM if secondary process
157 * - ENOENT if too many devices
158 * - ENOMEM if out of space
161 int16_t rte_gpu_add_child(const char *name,
162 int16_t parent, uint64_t child_context);
166 * @b EXPERIMENTAL: this API may change without prior notice.
168 * Get the ID of the next valid GPU initialized in DPDK.
171 * The initial device ID to start the research.
173 * The device ID of the parent.
174 * RTE_GPU_ID_NONE means no parent.
175 * RTE_GPU_ID_ANY means no or any parent.
178 * Next device ID corresponding to a valid and initialized computing device,
179 * RTE_GPU_ID_NONE if there is none.
182 int16_t rte_gpu_find_next(int16_t dev_id, int16_t parent);
186 * @b EXPERIMENTAL: this API may change without prior notice.
188 * Macro to iterate over all valid GPU devices.
191 * The ID of the next possible valid device, usually 0 to iterate all.
193 #define RTE_GPU_FOREACH(dev_id) \
194 RTE_GPU_FOREACH_CHILD(dev_id, RTE_GPU_ID_ANY)
198 * @b EXPERIMENTAL: this API may change without prior notice.
200 * Macro to iterate over all valid computing devices having no parent.
203 * The ID of the next possible valid device, usually 0 to iterate all.
205 #define RTE_GPU_FOREACH_PARENT(dev_id) \
206 RTE_GPU_FOREACH_CHILD(dev_id, RTE_GPU_ID_NONE)
210 * @b EXPERIMENTAL: this API may change without prior notice.
212 * Macro to iterate over all valid children of a computing device parent.
215 * The ID of the next possible valid device, usually 0 to iterate all.
217 * The device ID of the parent.
219 #define RTE_GPU_FOREACH_CHILD(dev_id, parent) \
220 for (dev_id = rte_gpu_find_next(0, parent); \
222 dev_id = rte_gpu_find_next(dev_id + 1, parent))
226 * @b EXPERIMENTAL: this API may change without prior notice.
228 * Close device or child context.
229 * All resources are released.
232 * Device ID to close.
235 * 0 on success, -rte_errno otherwise:
236 * - ENODEV if invalid dev_id
237 * - EPERM if driver error
240 int rte_gpu_close(int16_t dev_id);
244 * @b EXPERIMENTAL: this API may change without prior notice.
246 * Register a function as event callback.
247 * A function may be registered multiple times for different events.
250 * Device ID to get notified about.
251 * RTE_GPU_ID_ANY means all devices.
253 * Device event to be registered for.
255 * Callback function to be called on event.
257 * Optional parameter passed in the callback.
260 * 0 on success, -rte_errno otherwise:
261 * - ENODEV if invalid dev_id
262 * - EINVAL if NULL function
263 * - ENOMEM if out of memory
266 int rte_gpu_callback_register(int16_t dev_id, enum rte_gpu_event event,
267 rte_gpu_callback_t *function, void *user_data);
271 * @b EXPERIMENTAL: this API may change without prior notice.
273 * Unregister for an event.
276 * Device ID to be silenced.
277 * RTE_GPU_ID_ANY means all devices.
281 * Registered function.
283 * Optional parameter as registered.
284 * RTE_GPU_CALLBACK_ANY_DATA is a catch-all.
287 * 0 on success, -rte_errno otherwise:
288 * - ENODEV if invalid dev_id
289 * - EINVAL if NULL function
292 int rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event,
293 rte_gpu_callback_t *function, void *user_data);
297 * @b EXPERIMENTAL: this API may change without prior notice.
299 * Return device specific info.
302 * Device ID to get info.
304 * Memory structure to fill with the info.
307 * 0 on success, -rte_errno otherwise:
308 * - ENODEV if invalid dev_id
309 * - EINVAL if NULL info
310 * - EPERM if driver error
313 int rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info);
317 * @b EXPERIMENTAL: this API may change without prior notice.
319 * Allocate a chunk of memory in the device.
322 * Device ID requiring allocated memory.
324 * Number of bytes to allocate.
325 * Requesting 0 will do nothing.
328 * A pointer to the allocated memory, otherwise NULL and rte_errno is set:
329 * - ENODEV if invalid dev_id
330 * - EINVAL if reserved flags
331 * - ENOTSUP if operation not supported by the driver
332 * - E2BIG if size is higher than limit
333 * - ENOMEM if out of space
334 * - EPERM if driver error
337 void *rte_gpu_mem_alloc(int16_t dev_id, size_t size)
342 * @b EXPERIMENTAL: this API may change without prior notice.
344 * Deallocate a chunk of memory allocated with rte_gpu_mem_alloc().
347 * Reference device ID.
349 * Pointer to the memory area to be deallocated.
350 * NULL is a no-op accepted value.
353 * 0 on success, -rte_errno otherwise:
354 * - ENODEV if invalid dev_id
355 * - ENOTSUP if operation not supported by the driver
356 * - EPERM if driver error
359 int rte_gpu_mem_free(int16_t dev_id, void *ptr);
363 * @b EXPERIMENTAL: this API may change without prior notice.
365 * Register a chunk of memory on the CPU usable by the device.
368 * Device ID requiring allocated memory.
370 * Number of bytes to allocate.
371 * Requesting 0 will do nothing.
373 * Pointer to the memory area to be registered.
374 * NULL is a no-op accepted value.
377 * A pointer to the allocated memory, otherwise NULL and rte_errno is set:
378 * - ENODEV if invalid dev_id
379 * - EINVAL if reserved flags
380 * - ENOTSUP if operation not supported by the driver
381 * - E2BIG if size is higher than limit
382 * - ENOMEM if out of space
383 * - EPERM if driver error
386 int rte_gpu_mem_register(int16_t dev_id, size_t size, void *ptr);
390 * @b EXPERIMENTAL: this API may change without prior notice.
392 * Deregister a chunk of memory previously registered with rte_gpu_mem_register()
395 * Reference device ID.
397 * Pointer to the memory area to be unregistered.
398 * NULL is a no-op accepted value.
401 * 0 on success, -rte_errno otherwise:
402 * - ENODEV if invalid dev_id
403 * - ENOTSUP if operation not supported by the driver
404 * - EPERM if driver error
407 int rte_gpu_mem_unregister(int16_t dev_id, void *ptr);
411 * @b EXPERIMENTAL: this API may change without prior notice.
413 * Enforce a GPU write memory barrier.
416 * Reference device ID.
419 * 0 on success, -rte_errno otherwise:
420 * - ENODEV if invalid dev_id
421 * - ENOTSUP if operation not supported by the driver
422 * - EPERM if driver error
425 int rte_gpu_wmb(int16_t dev_id);
429 * @b EXPERIMENTAL: this API may change without prior notice.
431 * Create a communication flag that can be shared
432 * between CPU threads and device workload to exchange some status info
433 * (e.g. work is done, processing can start, etc..).
436 * Reference device ID.
438 * Pointer to the memory area of the devflag structure.
440 * Type of memory to allocate the communication flag.
443 * 0 on success, -rte_errno otherwise:
444 * - ENODEV if invalid dev_id
445 * - EINVAL if invalid inputs
446 * - ENOTSUP if operation not supported by the driver
447 * - ENOMEM if out of space
448 * - EPERM if driver error
451 int rte_gpu_comm_create_flag(uint16_t dev_id,
452 struct rte_gpu_comm_flag *devflag,
453 enum rte_gpu_comm_flag_type mtype);
457 * @b EXPERIMENTAL: this API may change without prior notice.
459 * Deallocate a communication flag.
462 * Pointer to the memory area of the devflag structure.
465 * 0 on success, -rte_errno otherwise:
466 * - ENODEV if invalid dev_id
467 * - EINVAL if NULL devflag
468 * - ENOTSUP if operation not supported by the driver
469 * - EPERM if driver error
472 int rte_gpu_comm_destroy_flag(struct rte_gpu_comm_flag *devflag);
476 * @b EXPERIMENTAL: this API may change without prior notice.
478 * Set the value of a communication flag as the input value.
479 * Flag memory area is treated as volatile.
480 * The flag must have been allocated with RTE_GPU_COMM_FLAG_CPU.
483 * Pointer to the memory area of the devflag structure.
485 * Value to set in the flag.
488 * 0 on success, -rte_errno otherwise:
489 * - EINVAL if invalid input params
492 int rte_gpu_comm_set_flag(struct rte_gpu_comm_flag *devflag,
497 * @b EXPERIMENTAL: this API may change without prior notice.
499 * Get the value of the communication flag.
500 * Flag memory area is treated as volatile.
501 * The flag must have been allocated with RTE_GPU_COMM_FLAG_CPU.
504 * Pointer to the memory area of the devflag structure.
509 * 0 on success, -rte_errno otherwise:
510 * - EINVAL if invalid input params
513 int rte_gpu_comm_get_flag_value(struct rte_gpu_comm_flag *devflag,
520 #endif /* RTE_GPUDEV_H */