1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
7 #include <rte_rwlock.h>
8 #include <rte_string_fns.h>
9 #include <rte_memzone.h>
10 #include <rte_errno.h>
13 #include "rte_gpudev.h"
14 #include "gpudev_driver.h"
17 RTE_LOG_REGISTER_DEFAULT(gpu_logtype, NOTICE);
18 #define GPU_LOG(level, ...) \
19 rte_log(RTE_LOG_ ## level, gpu_logtype, RTE_FMT("gpu: " \
20 RTE_FMT_HEAD(__VA_ARGS__, ) "\n", RTE_FMT_TAIL(__VA_ARGS__, )))
22 /* Set any driver error as EPERM */
23 #define GPU_DRV_RET(function) \
24 ((function != 0) ? -(rte_errno = EPERM) : (rte_errno = 0))
26 /* Array of devices */
27 static struct rte_gpu *gpus;
28 /* Number of currently valid devices */
29 static int16_t gpu_max;
30 /* Number of currently valid devices */
31 static int16_t gpu_count;
33 /* Shared memory between processes. */
34 static const char *GPU_MEMZONE = "rte_gpu_shared";
36 __extension__ struct rte_gpu_mpshared gpus[0];
39 /* Event callback object */
40 struct rte_gpu_callback {
41 TAILQ_ENTRY(rte_gpu_callback) next;
42 rte_gpu_callback_t *function;
44 enum rte_gpu_event event;
46 static rte_rwlock_t gpu_callback_lock = RTE_RWLOCK_INITIALIZER;
47 static void gpu_free_callbacks(struct rte_gpu *dev);
50 rte_gpu_init(size_t dev_max)
52 if (dev_max == 0 || dev_max > INT16_MAX) {
53 GPU_LOG(ERR, "invalid array size");
58 /* No lock, it must be called before or during first probing. */
60 GPU_LOG(ERR, "already initialized");
65 gpus = calloc(dev_max, sizeof(struct rte_gpu));
67 GPU_LOG(ERR, "cannot initialize library");
77 rte_gpu_count_avail(void)
83 rte_gpu_is_valid(int16_t dev_id)
85 if (dev_id >= 0 && dev_id < gpu_max &&
86 gpus[dev_id].process_state == RTE_GPU_STATE_INITIALIZED)
92 gpu_match_parent(int16_t dev_id, int16_t parent)
94 if (parent == RTE_GPU_ID_ANY)
96 return gpus[dev_id].mpshared->info.parent == parent;
100 rte_gpu_find_next(int16_t dev_id, int16_t parent)
104 while (dev_id < gpu_max &&
105 (gpus[dev_id].process_state == RTE_GPU_STATE_UNUSED ||
106 !gpu_match_parent(dev_id, parent)))
109 if (dev_id >= gpu_max)
110 return RTE_GPU_ID_NONE;
115 gpu_find_free_id(void)
119 for (dev_id = 0; dev_id < gpu_max; dev_id++) {
120 if (gpus[dev_id].process_state == RTE_GPU_STATE_UNUSED)
123 return RTE_GPU_ID_NONE;
126 static struct rte_gpu *
127 gpu_get_by_id(int16_t dev_id)
129 if (!rte_gpu_is_valid(dev_id))
131 return &gpus[dev_id];
135 rte_gpu_get_by_name(const char *name)
145 RTE_GPU_FOREACH(dev_id) {
147 if (strncmp(name, dev->mpshared->name, RTE_DEV_NAME_MAX_LEN) == 0)
154 gpu_shared_mem_init(void)
156 const struct rte_memzone *memzone;
158 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
159 memzone = rte_memzone_reserve(GPU_MEMZONE,
160 sizeof(*gpu_shared_mem) +
161 sizeof(*gpu_shared_mem->gpus) * gpu_max,
164 memzone = rte_memzone_lookup(GPU_MEMZONE);
166 if (memzone == NULL) {
167 GPU_LOG(ERR, "cannot initialize shared memory");
172 gpu_shared_mem = memzone->addr;
177 rte_gpu_allocate(const char *name)
182 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
183 GPU_LOG(ERR, "only primary process can allocate device");
188 GPU_LOG(ERR, "allocate device without a name");
193 /* implicit initialization of library before adding first device */
194 if (gpus == NULL && rte_gpu_init(RTE_GPU_DEFAULT_MAX) < 0)
197 /* initialize shared memory before adding first device */
198 if (gpu_shared_mem == NULL && gpu_shared_mem_init() < 0)
201 if (rte_gpu_get_by_name(name) != NULL) {
202 GPU_LOG(ERR, "device with name %s already exists", name);
206 dev_id = gpu_find_free_id();
207 if (dev_id == RTE_GPU_ID_NONE) {
208 GPU_LOG(ERR, "reached maximum number of devices");
214 memset(dev, 0, sizeof(*dev));
216 dev->mpshared = &gpu_shared_mem->gpus[dev_id];
217 memset(dev->mpshared, 0, sizeof(*dev->mpshared));
219 if (rte_strscpy(dev->mpshared->name, name, RTE_DEV_NAME_MAX_LEN) < 0) {
220 GPU_LOG(ERR, "device name too long: %s", name);
221 rte_errno = ENAMETOOLONG;
224 dev->mpshared->info.name = dev->mpshared->name;
225 dev->mpshared->info.dev_id = dev_id;
226 dev->mpshared->info.numa_node = -1;
227 dev->mpshared->info.parent = RTE_GPU_ID_NONE;
228 TAILQ_INIT(&dev->callbacks);
229 __atomic_fetch_add(&dev->mpshared->process_refcnt, 1, __ATOMIC_RELAXED);
232 GPU_LOG(DEBUG, "new device %s (id %d) of total %d",
233 name, dev_id, gpu_count);
238 rte_gpu_attach(const char *name)
242 struct rte_gpu_mpshared *shared_dev;
244 if (rte_eal_process_type() != RTE_PROC_SECONDARY) {
245 GPU_LOG(ERR, "only secondary process can attach device");
250 GPU_LOG(ERR, "attach device without a name");
255 /* implicit initialization of library before adding first device */
256 if (gpus == NULL && rte_gpu_init(RTE_GPU_DEFAULT_MAX) < 0)
259 /* initialize shared memory before adding first device */
260 if (gpu_shared_mem == NULL && gpu_shared_mem_init() < 0)
263 for (dev_id = 0; dev_id < gpu_max; dev_id++) {
264 shared_dev = &gpu_shared_mem->gpus[dev_id];
265 if (strncmp(name, shared_dev->name, RTE_DEV_NAME_MAX_LEN) == 0)
268 if (dev_id >= gpu_max) {
269 GPU_LOG(ERR, "device with name %s not found", name);
274 memset(dev, 0, sizeof(*dev));
276 TAILQ_INIT(&dev->callbacks);
277 dev->mpshared = shared_dev;
278 __atomic_fetch_add(&dev->mpshared->process_refcnt, 1, __ATOMIC_RELAXED);
281 GPU_LOG(DEBUG, "attached device %s (id %d) of total %d",
282 name, dev_id, gpu_count);
287 rte_gpu_add_child(const char *name, int16_t parent, uint64_t child_context)
291 if (!rte_gpu_is_valid(parent)) {
292 GPU_LOG(ERR, "add child to invalid parent ID %d", parent);
297 dev = rte_gpu_allocate(name);
301 dev->mpshared->info.parent = parent;
302 dev->mpshared->info.context = child_context;
304 rte_gpu_complete_new(dev);
305 return dev->mpshared->info.dev_id;
309 rte_gpu_complete_new(struct rte_gpu *dev)
314 dev->process_state = RTE_GPU_STATE_INITIALIZED;
315 rte_gpu_notify(dev, RTE_GPU_EVENT_NEW);
319 rte_gpu_release(struct rte_gpu *dev)
321 int16_t dev_id, child;
327 dev_id = dev->mpshared->info.dev_id;
328 RTE_GPU_FOREACH_CHILD(child, dev_id) {
329 GPU_LOG(ERR, "cannot release device %d with child %d",
335 GPU_LOG(DEBUG, "free device %s (id %d)",
336 dev->mpshared->info.name, dev->mpshared->info.dev_id);
337 rte_gpu_notify(dev, RTE_GPU_EVENT_DEL);
339 gpu_free_callbacks(dev);
340 dev->process_state = RTE_GPU_STATE_UNUSED;
341 __atomic_fetch_sub(&dev->mpshared->process_refcnt, 1, __ATOMIC_RELAXED);
348 rte_gpu_close(int16_t dev_id)
350 int firsterr, binerr;
351 int *lasterr = &firsterr;
354 dev = gpu_get_by_id(dev_id);
356 GPU_LOG(ERR, "close invalid device ID %d", dev_id);
361 if (dev->ops.dev_close != NULL) {
362 *lasterr = GPU_DRV_RET(dev->ops.dev_close(dev));
367 *lasterr = rte_gpu_release(dev);
369 rte_errno = -firsterr;
374 rte_gpu_callback_register(int16_t dev_id, enum rte_gpu_event event,
375 rte_gpu_callback_t *function, void *user_data)
377 int16_t next_dev, last_dev;
378 struct rte_gpu_callback_list *callbacks;
379 struct rte_gpu_callback *callback;
381 if (!rte_gpu_is_valid(dev_id) && dev_id != RTE_GPU_ID_ANY) {
382 GPU_LOG(ERR, "register callback of invalid ID %d", dev_id);
386 if (function == NULL) {
387 GPU_LOG(ERR, "cannot register callback without function");
392 if (dev_id == RTE_GPU_ID_ANY) {
394 last_dev = gpu_max - 1;
396 next_dev = last_dev = dev_id;
399 rte_rwlock_write_lock(&gpu_callback_lock);
401 callbacks = &gpus[next_dev].callbacks;
403 /* check if not already registered */
404 TAILQ_FOREACH(callback, callbacks, next) {
405 if (callback->event == event &&
406 callback->function == function &&
407 callback->user_data == user_data) {
408 GPU_LOG(INFO, "callback already registered");
413 callback = malloc(sizeof(*callback));
414 if (callback == NULL) {
415 GPU_LOG(ERR, "cannot allocate callback");
418 callback->function = function;
419 callback->user_data = user_data;
420 callback->event = event;
421 TAILQ_INSERT_TAIL(callbacks, callback, next);
423 } while (++next_dev <= last_dev);
424 rte_rwlock_write_unlock(&gpu_callback_lock);
430 rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event,
431 rte_gpu_callback_t *function, void *user_data)
433 int16_t next_dev, last_dev;
434 struct rte_gpu_callback_list *callbacks;
435 struct rte_gpu_callback *callback, *nextcb;
437 if (!rte_gpu_is_valid(dev_id) && dev_id != RTE_GPU_ID_ANY) {
438 GPU_LOG(ERR, "unregister callback of invalid ID %d", dev_id);
442 if (function == NULL) {
443 GPU_LOG(ERR, "cannot unregister callback without function");
448 if (dev_id == RTE_GPU_ID_ANY) {
450 last_dev = gpu_max - 1;
452 next_dev = last_dev = dev_id;
455 rte_rwlock_write_lock(&gpu_callback_lock);
457 callbacks = &gpus[next_dev].callbacks;
458 RTE_TAILQ_FOREACH_SAFE(callback, callbacks, next, nextcb) {
459 if (callback->event != event ||
460 callback->function != function ||
461 (callback->user_data != user_data &&
462 user_data != (void *)-1))
464 TAILQ_REMOVE(callbacks, callback, next);
467 } while (++next_dev <= last_dev);
468 rte_rwlock_write_unlock(&gpu_callback_lock);
474 gpu_free_callbacks(struct rte_gpu *dev)
476 struct rte_gpu_callback_list *callbacks;
477 struct rte_gpu_callback *callback, *nextcb;
479 callbacks = &dev->callbacks;
480 rte_rwlock_write_lock(&gpu_callback_lock);
481 RTE_TAILQ_FOREACH_SAFE(callback, callbacks, next, nextcb) {
482 TAILQ_REMOVE(callbacks, callback, next);
485 rte_rwlock_write_unlock(&gpu_callback_lock);
489 rte_gpu_notify(struct rte_gpu *dev, enum rte_gpu_event event)
492 struct rte_gpu_callback *callback;
494 dev_id = dev->mpshared->info.dev_id;
495 rte_rwlock_read_lock(&gpu_callback_lock);
496 TAILQ_FOREACH(callback, &dev->callbacks, next) {
497 if (callback->event != event || callback->function == NULL)
499 callback->function(dev_id, event, callback->user_data);
501 rte_rwlock_read_unlock(&gpu_callback_lock);
505 rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info)
509 dev = gpu_get_by_id(dev_id);
511 GPU_LOG(ERR, "query invalid device ID %d", dev_id);
516 GPU_LOG(ERR, "query without storage");
521 if (dev->ops.dev_info_get == NULL) {
522 *info = dev->mpshared->info;
525 return GPU_DRV_RET(dev->ops.dev_info_get(dev, info));