1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2021 NVIDIA Corporation & Affiliates
7 #include <rte_string_fns.h>
8 #include <rte_memzone.h>
9 #include <rte_malloc.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));
529 rte_gpu_mem_alloc(int16_t dev_id, size_t size, unsigned int align)
535 dev = gpu_get_by_id(dev_id);
537 GPU_LOG(ERR, "alloc mem for invalid device ID %d", dev_id);
542 if (dev->ops.mem_alloc == NULL) {
543 GPU_LOG(ERR, "mem allocation not supported");
548 if (size == 0) /* dry-run */
551 if (align && !rte_is_power_of_2(align)) {
552 GPU_LOG(ERR, "requested alignment is not a power of two %u", align);
557 ret = dev->ops.mem_alloc(dev, size, align, &ptr);
573 rte_gpu_mem_free(int16_t dev_id, void *ptr)
577 dev = gpu_get_by_id(dev_id);
579 GPU_LOG(ERR, "free mem for invalid device ID %d", dev_id);
584 if (dev->ops.mem_free == NULL) {
589 if (ptr == NULL) /* dry-run */
592 return GPU_DRV_RET(dev->ops.mem_free(dev, ptr));
596 rte_gpu_mem_register(int16_t dev_id, size_t size, void *ptr)
600 dev = gpu_get_by_id(dev_id);
602 GPU_LOG(ERR, "alloc mem for invalid device ID %d", dev_id);
607 if (dev->ops.mem_register == NULL) {
608 GPU_LOG(ERR, "mem registration not supported");
613 if (ptr == NULL || size == 0) /* dry-run */
616 return GPU_DRV_RET(dev->ops.mem_register(dev, size, ptr));
620 rte_gpu_mem_unregister(int16_t dev_id, void *ptr)
624 dev = gpu_get_by_id(dev_id);
626 GPU_LOG(ERR, "unregister mem for invalid device ID %d", dev_id);
631 if (dev->ops.mem_unregister == NULL) {
636 if (ptr == NULL) /* dry-run */
639 return GPU_DRV_RET(dev->ops.mem_unregister(dev, ptr));
643 rte_gpu_mem_cpu_map(int16_t dev_id, size_t size, void *ptr)
649 dev = gpu_get_by_id(dev_id);
651 GPU_LOG(ERR, "mem CPU map for invalid device ID %d", dev_id);
656 if (dev->ops.mem_cpu_map == NULL) {
657 GPU_LOG(ERR, "mem CPU map not supported");
662 if (ptr == NULL || size == 0) /* dry-run */
665 ret = GPU_DRV_RET(dev->ops.mem_cpu_map(dev, size, ptr, &ptr_out));
681 rte_gpu_mem_cpu_unmap(int16_t dev_id, void *ptr)
685 dev = gpu_get_by_id(dev_id);
687 GPU_LOG(ERR, "cpu_unmap mem for invalid device ID %d", dev_id);
692 if (dev->ops.mem_cpu_unmap == NULL) {
697 if (ptr == NULL) /* dry-run */
700 return GPU_DRV_RET(dev->ops.mem_cpu_unmap(dev, ptr));
704 rte_gpu_wmb(int16_t dev_id)
708 dev = gpu_get_by_id(dev_id);
710 GPU_LOG(ERR, "memory barrier for invalid device ID %d", dev_id);
715 if (dev->ops.wmb == NULL) {
719 return GPU_DRV_RET(dev->ops.wmb(dev));
723 rte_gpu_comm_create_flag(uint16_t dev_id, struct rte_gpu_comm_flag *devflag,
724 enum rte_gpu_comm_flag_type mtype)
729 if (devflag == NULL) {
733 if (mtype != RTE_GPU_COMM_FLAG_CPU) {
738 flag_size = sizeof(uint32_t);
740 devflag->ptr = rte_zmalloc(NULL, flag_size, 0);
741 if (devflag->ptr == NULL) {
746 ret = rte_gpu_mem_register(dev_id, flag_size, devflag->ptr);
752 devflag->mtype = mtype;
753 devflag->dev_id = dev_id;
759 rte_gpu_comm_destroy_flag(struct rte_gpu_comm_flag *devflag)
763 if (devflag == NULL) {
768 ret = rte_gpu_mem_unregister(devflag->dev_id, devflag->ptr);
774 rte_free(devflag->ptr);
780 rte_gpu_comm_set_flag(struct rte_gpu_comm_flag *devflag, uint32_t val)
782 if (devflag == NULL) {
787 if (devflag->mtype != RTE_GPU_COMM_FLAG_CPU) {
792 RTE_GPU_VOLATILE(*devflag->ptr) = val;
798 rte_gpu_comm_get_flag_value(struct rte_gpu_comm_flag *devflag, uint32_t *val)
800 if (devflag == NULL) {
804 if (devflag->mtype != RTE_GPU_COMM_FLAG_CPU) {
809 *val = RTE_GPU_VOLATILE(*devflag->ptr);
814 struct rte_gpu_comm_list *
815 rte_gpu_comm_create_list(uint16_t dev_id,
816 uint32_t num_comm_items)
818 struct rte_gpu_comm_list *comm_list;
823 if (num_comm_items == 0) {
828 dev = gpu_get_by_id(dev_id);
830 GPU_LOG(ERR, "memory barrier for invalid device ID %d", dev_id);
835 comm_list = rte_zmalloc(NULL,
836 sizeof(struct rte_gpu_comm_list) * num_comm_items, 0);
837 if (comm_list == NULL) {
842 ret = rte_gpu_mem_register(dev_id,
843 sizeof(struct rte_gpu_comm_list) * num_comm_items, comm_list);
849 for (idx_l = 0; idx_l < num_comm_items; idx_l++) {
850 comm_list[idx_l].pkt_list = rte_zmalloc(NULL,
851 sizeof(struct rte_gpu_comm_pkt) * RTE_GPU_COMM_LIST_PKTS_MAX, 0);
852 if (comm_list[idx_l].pkt_list == NULL) {
857 ret = rte_gpu_mem_register(dev_id,
858 sizeof(struct rte_gpu_comm_pkt) * RTE_GPU_COMM_LIST_PKTS_MAX,
859 comm_list[idx_l].pkt_list);
865 RTE_GPU_VOLATILE(comm_list[idx_l].status) = RTE_GPU_COMM_LIST_FREE;
866 comm_list[idx_l].num_pkts = 0;
867 comm_list[idx_l].dev_id = dev_id;
869 comm_list[idx_l].mbufs = rte_zmalloc(NULL,
870 sizeof(struct rte_mbuf *) * RTE_GPU_COMM_LIST_PKTS_MAX, 0);
871 if (comm_list[idx_l].mbufs == NULL) {
881 rte_gpu_comm_destroy_list(struct rte_gpu_comm_list *comm_list,
882 uint32_t num_comm_items)
888 if (comm_list == NULL) {
893 dev_id = comm_list[0].dev_id;
895 for (idx_l = 0; idx_l < num_comm_items; idx_l++) {
896 ret = rte_gpu_mem_unregister(dev_id, comm_list[idx_l].pkt_list);
902 rte_free(comm_list[idx_l].pkt_list);
903 rte_free(comm_list[idx_l].mbufs);
906 ret = rte_gpu_mem_unregister(dev_id, comm_list);
918 rte_gpu_comm_populate_list_pkts(struct rte_gpu_comm_list *comm_list_item,
919 struct rte_mbuf **mbufs, uint32_t num_mbufs)
923 if (comm_list_item == NULL || comm_list_item->pkt_list == NULL ||
924 mbufs == NULL || num_mbufs > RTE_GPU_COMM_LIST_PKTS_MAX) {
929 for (idx = 0; idx < num_mbufs; idx++) {
930 /* support only unchained mbufs */
931 if (unlikely((mbufs[idx]->nb_segs > 1) ||
932 (mbufs[idx]->next != NULL) ||
933 (mbufs[idx]->data_len != mbufs[idx]->pkt_len))) {
937 comm_list_item->pkt_list[idx].addr =
938 rte_pktmbuf_mtod_offset(mbufs[idx], uintptr_t, 0);
939 comm_list_item->pkt_list[idx].size = mbufs[idx]->pkt_len;
940 comm_list_item->mbufs[idx] = mbufs[idx];
943 RTE_GPU_VOLATILE(comm_list_item->num_pkts) = num_mbufs;
944 rte_gpu_wmb(comm_list_item->dev_id);
945 RTE_GPU_VOLATILE(comm_list_item->status) = RTE_GPU_COMM_LIST_READY;
951 rte_gpu_comm_cleanup_list(struct rte_gpu_comm_list *comm_list_item)
955 if (comm_list_item == NULL) {
960 if (RTE_GPU_VOLATILE(comm_list_item->status) ==
961 RTE_GPU_COMM_LIST_READY) {
962 GPU_LOG(ERR, "packet list is still in progress");
967 for (idx = 0; idx < RTE_GPU_COMM_LIST_PKTS_MAX; idx++) {
968 if (comm_list_item->pkt_list[idx].addr == 0)
971 comm_list_item->pkt_list[idx].addr = 0;
972 comm_list_item->pkt_list[idx].size = 0;
973 comm_list_item->mbufs[idx] = NULL;
976 RTE_GPU_VOLATILE(comm_list_item->status) = RTE_GPU_COMM_LIST_FREE;
977 RTE_GPU_VOLATILE(comm_list_item->num_pkts) = 0;