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>
12 #include "rte_gpudev.h"
13 #include "gpudev_driver.h"
16 RTE_LOG_REGISTER_DEFAULT(gpu_logtype, NOTICE);
17 #define GPU_LOG(level, ...) \
18 rte_log(RTE_LOG_ ## level, gpu_logtype, RTE_FMT("gpu: " \
19 RTE_FMT_HEAD(__VA_ARGS__, ) "\n", RTE_FMT_TAIL(__VA_ARGS__, )))
21 /* Set any driver error as EPERM */
22 #define GPU_DRV_RET(function) \
23 ((function != 0) ? -(rte_errno = EPERM) : (rte_errno = 0))
25 /* Array of devices */
26 static struct rte_gpu *gpus;
27 /* Number of currently valid devices */
28 static int16_t gpu_max;
29 /* Number of currently valid devices */
30 static int16_t gpu_count;
32 /* Event callback object */
33 struct rte_gpu_callback {
34 TAILQ_ENTRY(rte_gpu_callback) next;
35 rte_gpu_callback_t *function;
37 enum rte_gpu_event event;
39 static rte_rwlock_t gpu_callback_lock = RTE_RWLOCK_INITIALIZER;
40 static void gpu_free_callbacks(struct rte_gpu *dev);
43 rte_gpu_init(size_t dev_max)
45 if (dev_max == 0 || dev_max > INT16_MAX) {
46 GPU_LOG(ERR, "invalid array size");
51 /* No lock, it must be called before or during first probing. */
53 GPU_LOG(ERR, "already initialized");
58 gpus = calloc(dev_max, sizeof(struct rte_gpu));
60 GPU_LOG(ERR, "cannot initialize library");
70 rte_gpu_count_avail(void)
76 rte_gpu_is_valid(int16_t dev_id)
78 if (dev_id >= 0 && dev_id < gpu_max &&
79 gpus[dev_id].state == RTE_GPU_STATE_INITIALIZED)
85 rte_gpu_find_next(int16_t dev_id)
89 while (dev_id < gpu_max &&
90 gpus[dev_id].state == RTE_GPU_STATE_UNUSED)
93 if (dev_id >= gpu_max)
94 return RTE_GPU_ID_NONE;
99 gpu_find_free_id(void)
103 for (dev_id = 0; dev_id < gpu_max; dev_id++) {
104 if (gpus[dev_id].state == RTE_GPU_STATE_UNUSED)
107 return RTE_GPU_ID_NONE;
110 static struct rte_gpu *
111 gpu_get_by_id(int16_t dev_id)
113 if (!rte_gpu_is_valid(dev_id))
115 return &gpus[dev_id];
119 rte_gpu_get_by_name(const char *name)
129 RTE_GPU_FOREACH(dev_id) {
131 if (strncmp(name, dev->name, RTE_DEV_NAME_MAX_LEN) == 0)
138 rte_gpu_allocate(const char *name)
143 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
144 GPU_LOG(ERR, "only primary process can allocate device");
149 GPU_LOG(ERR, "allocate device without a name");
154 /* implicit initialization of library before adding first device */
155 if (gpus == NULL && rte_gpu_init(RTE_GPU_DEFAULT_MAX) < 0)
158 if (rte_gpu_get_by_name(name) != NULL) {
159 GPU_LOG(ERR, "device with name %s already exists", name);
163 dev_id = gpu_find_free_id();
164 if (dev_id == RTE_GPU_ID_NONE) {
165 GPU_LOG(ERR, "reached maximum number of devices");
171 memset(dev, 0, sizeof(*dev));
173 if (rte_strscpy(dev->name, name, RTE_DEV_NAME_MAX_LEN) < 0) {
174 GPU_LOG(ERR, "device name too long: %s", name);
175 rte_errno = ENAMETOOLONG;
178 dev->info.name = dev->name;
179 dev->info.dev_id = dev_id;
180 dev->info.numa_node = -1;
181 TAILQ_INIT(&dev->callbacks);
184 GPU_LOG(DEBUG, "new device %s (id %d) of total %d",
185 name, dev_id, gpu_count);
190 rte_gpu_complete_new(struct rte_gpu *dev)
195 dev->state = RTE_GPU_STATE_INITIALIZED;
196 dev->state = RTE_GPU_STATE_INITIALIZED;
197 rte_gpu_notify(dev, RTE_GPU_EVENT_NEW);
201 rte_gpu_release(struct rte_gpu *dev)
208 GPU_LOG(DEBUG, "free device %s (id %d)",
209 dev->info.name, dev->info.dev_id);
210 rte_gpu_notify(dev, RTE_GPU_EVENT_DEL);
212 gpu_free_callbacks(dev);
213 dev->state = RTE_GPU_STATE_UNUSED;
220 rte_gpu_close(int16_t dev_id)
222 int firsterr, binerr;
223 int *lasterr = &firsterr;
226 dev = gpu_get_by_id(dev_id);
228 GPU_LOG(ERR, "close invalid device ID %d", dev_id);
233 if (dev->ops.dev_close != NULL) {
234 *lasterr = GPU_DRV_RET(dev->ops.dev_close(dev));
239 *lasterr = rte_gpu_release(dev);
241 rte_errno = -firsterr;
246 rte_gpu_callback_register(int16_t dev_id, enum rte_gpu_event event,
247 rte_gpu_callback_t *function, void *user_data)
249 int16_t next_dev, last_dev;
250 struct rte_gpu_callback_list *callbacks;
251 struct rte_gpu_callback *callback;
253 if (!rte_gpu_is_valid(dev_id) && dev_id != RTE_GPU_ID_ANY) {
254 GPU_LOG(ERR, "register callback of invalid ID %d", dev_id);
258 if (function == NULL) {
259 GPU_LOG(ERR, "cannot register callback without function");
264 if (dev_id == RTE_GPU_ID_ANY) {
266 last_dev = gpu_max - 1;
268 next_dev = last_dev = dev_id;
271 rte_rwlock_write_lock(&gpu_callback_lock);
273 callbacks = &gpus[next_dev].callbacks;
275 /* check if not already registered */
276 TAILQ_FOREACH(callback, callbacks, next) {
277 if (callback->event == event &&
278 callback->function == function &&
279 callback->user_data == user_data) {
280 GPU_LOG(INFO, "callback already registered");
285 callback = malloc(sizeof(*callback));
286 if (callback == NULL) {
287 GPU_LOG(ERR, "cannot allocate callback");
290 callback->function = function;
291 callback->user_data = user_data;
292 callback->event = event;
293 TAILQ_INSERT_TAIL(callbacks, callback, next);
295 } while (++next_dev <= last_dev);
296 rte_rwlock_write_unlock(&gpu_callback_lock);
302 rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event,
303 rte_gpu_callback_t *function, void *user_data)
305 int16_t next_dev, last_dev;
306 struct rte_gpu_callback_list *callbacks;
307 struct rte_gpu_callback *callback, *nextcb;
309 if (!rte_gpu_is_valid(dev_id) && dev_id != RTE_GPU_ID_ANY) {
310 GPU_LOG(ERR, "unregister callback of invalid ID %d", dev_id);
314 if (function == NULL) {
315 GPU_LOG(ERR, "cannot unregister callback without function");
320 if (dev_id == RTE_GPU_ID_ANY) {
322 last_dev = gpu_max - 1;
324 next_dev = last_dev = dev_id;
327 rte_rwlock_write_lock(&gpu_callback_lock);
329 callbacks = &gpus[next_dev].callbacks;
330 RTE_TAILQ_FOREACH_SAFE(callback, callbacks, next, nextcb) {
331 if (callback->event != event ||
332 callback->function != function ||
333 (callback->user_data != user_data &&
334 user_data != (void *)-1))
336 TAILQ_REMOVE(callbacks, callback, next);
339 } while (++next_dev <= last_dev);
340 rte_rwlock_write_unlock(&gpu_callback_lock);
346 gpu_free_callbacks(struct rte_gpu *dev)
348 struct rte_gpu_callback_list *callbacks;
349 struct rte_gpu_callback *callback, *nextcb;
351 callbacks = &dev->callbacks;
352 rte_rwlock_write_lock(&gpu_callback_lock);
353 RTE_TAILQ_FOREACH_SAFE(callback, callbacks, next, nextcb) {
354 TAILQ_REMOVE(callbacks, callback, next);
357 rte_rwlock_write_unlock(&gpu_callback_lock);
361 rte_gpu_notify(struct rte_gpu *dev, enum rte_gpu_event event)
364 struct rte_gpu_callback *callback;
366 dev_id = dev->info.dev_id;
367 rte_rwlock_read_lock(&gpu_callback_lock);
368 TAILQ_FOREACH(callback, &dev->callbacks, next) {
369 if (callback->event != event || callback->function == NULL)
371 callback->function(dev_id, event, callback->user_data);
373 rte_rwlock_read_unlock(&gpu_callback_lock);
377 rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info)
381 dev = gpu_get_by_id(dev_id);
383 GPU_LOG(ERR, "query invalid device ID %d", dev_id);
388 GPU_LOG(ERR, "query without storage");
393 if (dev->ops.dev_info_get == NULL) {
397 return GPU_DRV_RET(dev->ops.dev_info_get(dev, info));