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 /** Store device info. */
43 /** Unique identifier name. */
45 /** Opaque handler of the device context. */
49 /** ID of the parent device, RTE_GPU_ID_NONE if no parent */
51 /** Total processors available on device. */
52 uint32_t processor_count;
53 /** Total memory available on device. */
55 /* Local NUMA memory ID. -1 if unknown. */
59 /** Flags passed in notification callback. */
61 /** Device is just initialized. */
63 /** Device is going to be released. */
67 /** Prototype of event callback function. */
68 typedef void (rte_gpu_callback_t)(int16_t dev_id,
69 enum rte_gpu_event event, void *user_data);
73 * @b EXPERIMENTAL: this API may change without prior notice.
75 * Initialize the device array before probing devices.
76 * If not called, the maximum of probed devices is RTE_GPU_DEFAULT_MAX.
79 * Maximum number of devices.
82 * 0 on success, -rte_errno otherwise:
83 * - ENOMEM if out of memory
85 * - EBUSY if already initialized
88 int rte_gpu_init(size_t dev_max);
92 * @b EXPERIMENTAL: this API may change without prior notice.
94 * Return the number of GPU detected and associated to DPDK.
97 * The number of available computing devices.
100 uint16_t rte_gpu_count_avail(void);
104 * @b EXPERIMENTAL: this API may change without prior notice.
106 * Check if the device is valid and initialized in DPDK.
109 * The input device ID.
112 * - True if dev_id is a valid and initialized computing device.
116 bool rte_gpu_is_valid(int16_t dev_id);
120 * @b EXPERIMENTAL: this API may change without prior notice.
122 * Create a virtual device representing a context in the parent device.
125 * Unique string to identify the device.
127 * Device ID of the parent.
128 * @param child_context
129 * Opaque context handler.
132 * Device ID of the new created child, -rte_errno otherwise:
133 * - EINVAL if empty name
134 * - ENAMETOOLONG if long name
135 * - EEXIST if existing device name
136 * - ENODEV if invalid parent
137 * - EPERM if secondary process
138 * - ENOENT if too many devices
139 * - ENOMEM if out of space
142 int16_t rte_gpu_add_child(const char *name,
143 int16_t parent, uint64_t child_context);
147 * @b EXPERIMENTAL: this API may change without prior notice.
149 * Get the ID of the next valid GPU initialized in DPDK.
152 * The initial device ID to start the research.
154 * The device ID of the parent.
155 * RTE_GPU_ID_NONE means no parent.
156 * RTE_GPU_ID_ANY means no or any parent.
159 * Next device ID corresponding to a valid and initialized computing device,
160 * RTE_GPU_ID_NONE if there is none.
163 int16_t rte_gpu_find_next(int16_t dev_id, int16_t parent);
167 * @b EXPERIMENTAL: this API may change without prior notice.
169 * Macro to iterate over all valid GPU devices.
172 * The ID of the next possible valid device, usually 0 to iterate all.
174 #define RTE_GPU_FOREACH(dev_id) \
175 RTE_GPU_FOREACH_CHILD(dev_id, RTE_GPU_ID_ANY)
179 * @b EXPERIMENTAL: this API may change without prior notice.
181 * Macro to iterate over all valid computing devices having no parent.
184 * The ID of the next possible valid device, usually 0 to iterate all.
186 #define RTE_GPU_FOREACH_PARENT(dev_id) \
187 RTE_GPU_FOREACH_CHILD(dev_id, RTE_GPU_ID_NONE)
191 * @b EXPERIMENTAL: this API may change without prior notice.
193 * Macro to iterate over all valid children of a computing device parent.
196 * The ID of the next possible valid device, usually 0 to iterate all.
198 * The device ID of the parent.
200 #define RTE_GPU_FOREACH_CHILD(dev_id, parent) \
201 for (dev_id = rte_gpu_find_next(0, parent); \
203 dev_id = rte_gpu_find_next(dev_id + 1, parent))
207 * @b EXPERIMENTAL: this API may change without prior notice.
209 * Close device or child context.
210 * All resources are released.
213 * Device ID to close.
216 * 0 on success, -rte_errno otherwise:
217 * - ENODEV if invalid dev_id
218 * - EPERM if driver error
221 int rte_gpu_close(int16_t dev_id);
225 * @b EXPERIMENTAL: this API may change without prior notice.
227 * Register a function as event callback.
228 * A function may be registered multiple times for different events.
231 * Device ID to get notified about.
232 * RTE_GPU_ID_ANY means all devices.
234 * Device event to be registered for.
236 * Callback function to be called on event.
238 * Optional parameter passed in the callback.
241 * 0 on success, -rte_errno otherwise:
242 * - ENODEV if invalid dev_id
243 * - EINVAL if NULL function
244 * - ENOMEM if out of memory
247 int rte_gpu_callback_register(int16_t dev_id, enum rte_gpu_event event,
248 rte_gpu_callback_t *function, void *user_data);
252 * @b EXPERIMENTAL: this API may change without prior notice.
254 * Unregister for an event.
257 * Device ID to be silenced.
258 * RTE_GPU_ID_ANY means all devices.
262 * Registered function.
264 * Optional parameter as registered.
265 * RTE_GPU_CALLBACK_ANY_DATA is a catch-all.
268 * 0 on success, -rte_errno otherwise:
269 * - ENODEV if invalid dev_id
270 * - EINVAL if NULL function
273 int rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event,
274 rte_gpu_callback_t *function, void *user_data);
278 * @b EXPERIMENTAL: this API may change without prior notice.
280 * Return device specific info.
283 * Device ID to get info.
285 * Memory structure to fill with the info.
288 * 0 on success, -rte_errno otherwise:
289 * - ENODEV if invalid dev_id
290 * - EINVAL if NULL info
291 * - EPERM if driver error
294 int rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info);
298 * @b EXPERIMENTAL: this API may change without prior notice.
300 * Allocate a chunk of memory in the device.
303 * Device ID requiring allocated memory.
305 * Number of bytes to allocate.
306 * Requesting 0 will do nothing.
309 * A pointer to the allocated memory, otherwise NULL and rte_errno is set:
310 * - ENODEV if invalid dev_id
311 * - EINVAL if reserved flags
312 * - ENOTSUP if operation not supported by the driver
313 * - E2BIG if size is higher than limit
314 * - ENOMEM if out of space
315 * - EPERM if driver error
318 void *rte_gpu_mem_alloc(int16_t dev_id, size_t size)
323 * @b EXPERIMENTAL: this API may change without prior notice.
325 * Deallocate a chunk of memory allocated with rte_gpu_mem_alloc().
328 * Reference device ID.
330 * Pointer to the memory area to be deallocated.
331 * NULL is a no-op accepted value.
334 * 0 on success, -rte_errno otherwise:
335 * - ENODEV if invalid dev_id
336 * - ENOTSUP if operation not supported by the driver
337 * - EPERM if driver error
340 int rte_gpu_mem_free(int16_t dev_id, void *ptr);
344 * @b EXPERIMENTAL: this API may change without prior notice.
346 * Register a chunk of memory on the CPU usable by the device.
349 * Device ID requiring allocated memory.
351 * Number of bytes to allocate.
352 * Requesting 0 will do nothing.
354 * Pointer to the memory area to be registered.
355 * NULL is a no-op accepted value.
358 * A pointer to the allocated memory, otherwise NULL and rte_errno is set:
359 * - ENODEV if invalid dev_id
360 * - EINVAL if reserved flags
361 * - ENOTSUP if operation not supported by the driver
362 * - E2BIG if size is higher than limit
363 * - ENOMEM if out of space
364 * - EPERM if driver error
367 int rte_gpu_mem_register(int16_t dev_id, size_t size, void *ptr);
371 * @b EXPERIMENTAL: this API may change without prior notice.
373 * Deregister a chunk of memory previously registered with rte_gpu_mem_register()
376 * Reference device ID.
378 * Pointer to the memory area to be unregistered.
379 * NULL is a no-op accepted value.
382 * 0 on success, -rte_errno otherwise:
383 * - ENODEV if invalid dev_id
384 * - ENOTSUP if operation not supported by the driver
385 * - EPERM if driver error
388 int rte_gpu_mem_unregister(int16_t dev_id, void *ptr);
394 #endif /* RTE_GPUDEV_H */