On 2022/11/7 19:12, fanghao (A) wrote:
在 2022/11/7 16:21, liulongfang 写道:
These functions are all operation functions for the device, and they are only called in the device driver and do not need to be presented to the user APP. Therefore, move them to the internal public file wd_util.c
Signed-off-by: liulongfang liulongfang@huawei.com
drv/hisi_qm_udrv.c | 1 - include/wd.h | 137 -------------------------------------------- include/wd_common.h | 16 +++++- include/wd_util.h | 137 ++++++++++++++++++++++++++++++++++++++++++++ wd.c | 135 +------------------------------------------ wd_util.c | 125 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 276 insertions(+), 275 deletions(-)
diff --git a/drv/hisi_qm_udrv.c b/drv/hisi_qm_udrv.c index a3b5dc1..6647840 100644 --- a/drv/hisi_qm_udrv.c +++ b/drv/hisi_qm_udrv.c @@ -8,7 +8,6 @@ #include <sys/mman.h> #include "hisi_qm_udrv.h" -#include "wd_util.h" #define QM_DBELL_CMD_SQ 0 #define QM_DBELL_CMD_CQ 1 diff --git a/include/wd.h b/include/wd.h index 13a6272..b19d607 100644 --- a/include/wd.h +++ b/include/wd.h @@ -184,47 +184,6 @@ struct wd_datalist { struct wd_datalist *next; }; -#if defined(__AARCH64_CMODEL_SMALL__) && __AARCH64_CMODEL_SMALL__ -#define dsb(opt) { asm volatile("dsb " #opt : : : "memory"); } -#define rmb() dsb(ld) /* read fence */ -#define wmb() dsb(st) /* write fence */ -#define mb() dsb(sy) /* rw fence */ -#else -#define rmb() __sync_synchronize() /* read fence */ -#define wmb() __sync_synchronize() /* write fence */ -#define mb() __sync_synchronize() /* rw fence */ -#endif
-static inline uint32_t wd_ioread32(void *addr) -{ - uint32_t ret;
- ret = *((volatile uint32_t *)addr); - rmb(); - return ret; -}
-static inline uint64_t wd_ioread64(void *addr) -{ - uint64_t ret;
- ret = *((volatile uint64_t *)addr); - rmb(); - return ret; -}
-static inline void wd_iowrite32(void *addr, uint32_t value) -{ - wmb(); - *((volatile uint32_t *)addr) = value; -}
-static inline void wd_iowrite64(void *addr, uint64_t value) -{ - wmb(); - *((volatile uint64_t *)addr) = value; -}
enum wd_ctx_mode { CTX_MODE_SYNC = 0, CTX_MODE_ASYNC, @@ -280,81 +239,6 @@ handle_t wd_request_ctx(struct uacce_dev *dev); */ void wd_release_ctx(handle_t h_ctx); -/**
- wd_ctx_start() - Start a context.
- @h_ctx: The handle of context which will be started.
- Return 0 if successful or less than 0 otherwise.
- Context will be started after calling this function. If necessary resource
- (e.g. MMIO and DUS) already got, tasks can be received by context.
- */
-int wd_ctx_start(handle_t h_ctx);
-/**
- wd_release_ctx_force() - Release a context forcely.
- @h_ctx: The handle of context which will be released.
- Return 0 if successful or less than 0 otherwise.
- Context will be stopped and related hardware will be released, which avoids
- release delay in wd_release_ctx(). After calling this function, context
- related hardware resource will be released, however, fd is still there.
- wd_release_ctx mush be used to release context finally, other APIs about
- context can not work with this context after calling wd_release_ctx_force.
- */
-int wd_release_ctx_force(handle_t h_ctx);
-/**
- wd_ctx_set_priv() - Store some information in context.
- @h_ctx: The handle of context.
- @priv: The pointer of memory which stores above information.
- Return 0 if successful or less than 0 otherwise.
- */
-int wd_ctx_set_priv(handle_t h_ctx, void *priv);
-/**
- wd_ctx_get_priv() - Get stored information in context.
- @h_ctx: The handle of context.
- Return pointer of memory of stored information if successful or NULL
- otherwise.
- */
-void *wd_ctx_get_priv(handle_t h_ctx);
-/**
- wd_ctx_get_api() - Get api string of context.
- @h_ctx: The handle of context.
- Return api string or NULL otherwise.
- This function is a wrapper of reading /sys/class/uacce/<dev>/api, which is
- used to define api version between user space and kernel driver.
- */
-char *wd_ctx_get_api(handle_t h_ctx);
-/**
- wd_ctx_mmap_qfr() - Map and get the base address of one context region.
- @h_ctx: The handle of context.
- @qfrt: Name of context region, which could be got in kernel head file
- * include/uapi/misc/uacce/uacce.h
- Return pointer of context region if successful or NULL otherwise.
- Normally, UACCE_QFRT_MMIO is for MMIO registers of one context,
- UACCE_QFRT_DUS is for task communication memory of one context.
- */
-void *wd_ctx_mmap_qfr(handle_t h_ctx, enum uacce_qfrt qfrt);
-/**
- wd_ctx_unmap_qfr() - Unmap one context region.
- @h_ctx: The handle of context.
- @qfrt: Name of context region, which could be got in kernel head file
- * include/uapi/misc/uacce/uacce.h.
- */
-void wd_ctx_unmap_qfr(handle_t h_ctx, enum uacce_qfrt qfrt);
/** * wd_ctx_wait() - Wait task in context finished. * @h_ctx: The handle of context. @@ -435,27 +319,6 @@ struct uacce_dev *wd_get_accel_dev(const char *alg_name); */ void wd_free_list_accels(struct uacce_dev_list *list); -/**
- wd_ctx_set_io_cmd() - Send ioctl command to context.
- @h_ctx: The handle of context.
- @cmd: ioctl command which could be found in Linux kernel head file,
- * include/uapi/misc/uacce/uacce.h, hisi_qm.h...
- @arg: Command output buffer if some information will be got from kernel or
- * NULL otherwise.
- This function is a wrapper of ioctl.
- */
-int wd_ctx_set_io_cmd(handle_t h_ctx, unsigned long cmd, void *arg);
-/**
- wd_ctx_get_region_size() - Get region offset size
- @h_ctx: The handle of context.
- @qfrt: Name of context region, which could be got in kernel head file
- * include/uapi/misc/uacce/uacce.h
- Return device region size.
- */
-unsigned long wd_ctx_get_region_size(handle_t h_ctx, enum uacce_qfrt qfrt);
enum wd_page_type { WD_HUGE_PAGE = 0, WD_NORMAL_PAGE, diff --git a/include/wd_common.h b/include/wd_common.h index 91dd066..941179c 100644 --- a/include/wd_common.h +++ b/include/wd_common.h @@ -7,13 +7,23 @@ #ifndef __WD_COMMON_H #define __WD_COMMON_H +#include "uacce.h"
#ifdef __cplusplus extern "C" { #endif -enum wd_buff_type { - WD_FLAT_BUF, - WD_SGL_BUF, +#define MAX_DEV_NAME_LEN 256
+struct wd_ctx_h { + int fd; + char dev_path[MAX_DEV_NAME_LEN]; + char *dev_name; + char *drv_name; + unsigned long qfrs_offs[UACCE_QFRT_MAX]; + void *qfrs_base[UACCE_QFRT_MAX]; + struct uacce_dev *dev; + void *priv; }; #ifdef __cplusplus diff --git a/include/wd_util.h b/include/wd_util.h index 0ef5bdf..aac2705 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -18,6 +18,143 @@ extern "C" { #endif +#if defined(__AARCH64_CMODEL_SMALL__) && __AARCH64_CMODEL_SMALL__ +#define dsb(opt) { asm volatile("dsb " #opt : : : "memory"); } +#define rmb() dsb(ld) /* read fence */ +#define wmb() dsb(st) /* write fence */ +#define mb() dsb(sy) /* rw fence */ +#else +#define rmb() __sync_synchronize() /* read fence */ +#define wmb() __sync_synchronize() /* write fence */ +#define mb() __sync_synchronize() /* rw fence */ +#endif
+static inline uint32_t wd_ioread32(void *addr) +{ + uint32_t ret;
+ ret = *((volatile uint32_t *)addr); + rmb(); + return ret; +}
+static inline uint64_t wd_ioread64(void *addr) +{ + uint64_t ret;
+ ret = *((volatile uint64_t *)addr); + rmb(); + return ret; +}
+static inline void wd_iowrite32(void *addr, uint32_t value) +{ + wmb(); + *((volatile uint32_t *)addr) = value; +}
+static inline void wd_iowrite64(void *addr, uint64_t value) +{ + wmb(); + *((volatile uint64_t *)addr) = value; +}
+/**
- wd_ctx_start() - Start a context.
- @h_ctx: The handle of context which will be started.
- Return 0 if successful or less than 0 otherwise.
- Context will be started after calling this function. If necessary resource
- (e.g. MMIO and DUS) already got, tasks can be received by context.
- */
+int wd_ctx_start(handle_t h_ctx);
+/**
- wd_release_ctx_force() - Release a context forcely.
- @h_ctx: The handle of context which will be released.
- Return 0 if successful or less than 0 otherwise.
- Context will be stopped and related hardware will be released, which avoids
- release delay in wd_release_ctx(). After calling this function, context
- related hardware resource will be released, however, fd is still there.
- wd_release_ctx mush be used to release context finally, other APIs about
- context can not work with this context after calling wd_release_ctx_force.
- */
+int wd_release_ctx_force(handle_t h_ctx);
+/**
- wd_ctx_set_priv() - Store some information in context.
- @h_ctx: The handle of context.
- @priv: The pointer of memory which stores above information.
- Return 0 if successful or less than 0 otherwise.
- */
+int wd_ctx_set_priv(handle_t h_ctx, void *priv);
+/**
- wd_ctx_get_priv() - Get stored information in context.
- @h_ctx: The handle of context.
- Return pointer of memory of stored information if successful or NULL
- otherwise.
- */
+void *wd_ctx_get_priv(handle_t h_ctx);
+/**
- wd_ctx_get_api() - Get api string of context.
- @h_ctx: The handle of context.
- Return api string or NULL otherwise.
- This function is a wrapper of reading /sys/class/uacce/<dev>/api, which is
- used to define api version between user space and kernel driver.
- */
+char *wd_ctx_get_api(handle_t h_ctx);
+/**
- wd_ctx_mmap_qfr() - Map and get the base address of one context region.
- @h_ctx: The handle of context.
- @qfrt: Name of context region, which could be got in kernel head file
- * include/uapi/misc/uacce/uacce.h
- Return pointer of context region if successful or NULL otherwise.
- Normally, UACCE_QFRT_MMIO is for MMIO registers of one context,
- UACCE_QFRT_DUS is for task communication memory of one context.
- */
+void *wd_ctx_mmap_qfr(handle_t h_ctx, enum uacce_qfrt qfrt);
+/**
- wd_ctx_unmap_qfr() - Unmap one context region.
- @h_ctx: The handle of context.
- @qfrt: Name of context region, which could be got in kernel head file
- * include/uapi/misc/uacce/uacce.h.
- */
+void wd_ctx_unmap_qfr(handle_t h_ctx, enum uacce_qfrt qfrt);
+/**
- wd_ctx_set_io_cmd() - Send ioctl command to context.
- @h_ctx: The handle of context.
- @cmd: ioctl command which could be found in Linux kernel head file,
- * include/uapi/misc/uacce/uacce.h, hisi_qm.h...
- @arg: Command output buffer if some information will be got from kernel or
- * NULL otherwise.
- This function is a wrapper of ioctl.
- */
+int wd_ctx_set_io_cmd(handle_t h_ctx, unsigned long cmd, void *arg);
+/**
- wd_ctx_get_region_size() - Get region offset size
- @h_ctx: The handle of context.
- @qfrt: Name of context region, which could be got in kernel head file
- * include/uapi/misc/uacce/uacce.h
- Return device region size.
- */
+unsigned long wd_ctx_get_region_size(handle_t h_ctx, enum uacce_qfrt qfrt);
struct wd_ctx_internal { handle_t ctx; __u8 op_type; diff --git a/wd.c b/wd.c index 048e7c1..5ab4002 100644 --- a/wd.c +++ b/wd.c @@ -12,13 +12,12 @@ #include <stdbool.h> #include <stdlib.h> #include <string.h> -#include <sys/ioctl.h> -#include <sys/mman.h> #include <sys/stat.h> #include <numa.h> #include <sched.h> #include "wd.h" +#include "wd_common.h" #define SYS_CLASS_DIR "/sys/class/uacce" @@ -31,17 +30,6 @@ enum UADK_LOG_LEVEL { static int uadk_log_level; -struct wd_ctx_h { - int fd; - char dev_path[MAX_DEV_NAME_LEN]; - char *dev_name; - char *drv_name; - unsigned long qfrs_offs[UACCE_QFRT_MAX]; - void *qfrs_base[UACCE_QFRT_MAX]; - struct uacce_dev *dev; - void *priv; -};
static void wd_parse_log_level(void) { const char *syslog_file = "/etc/rsyslog.conf"; @@ -430,114 +418,6 @@ void wd_release_ctx(handle_t h_ctx) free(ctx); } -int wd_ctx_start(handle_t h_ctx) -{ - struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx; - int ret;
- if (!ctx) - return -WD_EINVAL;
- ret = wd_ctx_set_io_cmd(h_ctx, UACCE_CMD_START, NULL); - if (ret) - WD_ERR("failed to start on %s (%d), ret = %d!\n", - ctx->dev_path, -errno, ret);
- return ret; -}
-int wd_release_ctx_force(handle_t h_ctx) -{ - struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx; - int ret;
- if (!ctx) - return -WD_EINVAL;
- ret = wd_ctx_set_io_cmd(h_ctx, UACCE_CMD_PUT_Q, NULL); - if (ret) - WD_ERR("failed to stop on %s (%d), ret = %d!\n", - ctx->dev_path, -errno, ret);
- return ret; -}
-void *wd_ctx_mmap_qfr(handle_t h_ctx, enum uacce_qfrt qfrt) -{ - struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx; - off_t off = qfrt * getpagesize(); - size_t size; - void *addr;
- if (!ctx || qfrt >= UACCE_QFRT_MAX || !ctx->qfrs_offs[qfrt]) { - WD_ERR("failed to check input ctx or qfrt!\n"); - return NULL; - }
- size = ctx->qfrs_offs[qfrt];
- addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, ctx->fd, off); - if (addr == MAP_FAILED) { - WD_ERR("failed to mmap, qfrt = %d, err = %d!\n", qfrt, -errno); - return NULL; - }
- ctx->qfrs_base[qfrt] = addr;
- return addr; -}
-void wd_ctx_unmap_qfr(handle_t h_ctx, enum uacce_qfrt qfrt) -{ - struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
- if (!ctx || qfrt >= UACCE_QFRT_MAX) - return;
- if (ctx->qfrs_offs[qfrt] != 0) - munmap(ctx->qfrs_base[qfrt], ctx->qfrs_offs[qfrt]); -}
-unsigned long wd_ctx_get_region_size(handle_t h_ctx, enum uacce_qfrt qfrt) -{ - struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx; - if (!ctx || qfrt >= UACCE_QFRT_MAX) - return 0; - return ctx->qfrs_offs[qfrt]; -}
-void *wd_ctx_get_priv(handle_t h_ctx) -{ - struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
- if (!ctx) - return NULL;
- return ctx->priv; -}
-int wd_ctx_set_priv(handle_t h_ctx, void *priv) -{ - struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
- if (!ctx) - return -WD_EINVAL;
- ctx->priv = priv;
- return 0; -}
-char *wd_ctx_get_api(handle_t h_ctx) -{ - struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
- if (!ctx || !ctx->dev) - return NULL;
- return ctx->dev->api; -}
int wd_ctx_wait(handle_t h_ctx, __u16 ms) { struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx; @@ -780,19 +660,6 @@ struct uacce_dev *wd_get_accel_dev(const char *alg_name) return target; } -int wd_ctx_set_io_cmd(handle_t h_ctx, unsigned long cmd, void *arg) -{ - struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
- if (!ctx) - return -WD_EINVAL;
- if (!arg) - return ioctl(ctx->fd, cmd);
- return ioctl(ctx->fd, cmd, arg); -}
void wd_get_version(void) { const char *wd_released_time = UADK_RELEASED_TIME; diff --git a/wd_util.c b/wd_util.c index 57c3a33..5384696 100644 --- a/wd_util.c +++ b/wd_util.c @@ -9,9 +9,13 @@ #include <pthread.h> #include <semaphore.h> #include <string.h> +#include <sys/ioctl.h> +#include <sys/mman.h> #include <ctype.h>
#include "wd_sched.h" #include "wd_util.h" +#include "wd_common.h" #define WD_ASYNC_DEF_POLL_NUM 1 #define WD_ASYNC_DEF_QUEUE_DEPTH 1024 @@ -62,6 +66,127 @@ struct async_task_queue { int (*alg_poll_ctx)(__u32, __u32, __u32 *); }; +int wd_ctx_set_io_cmd(handle_t h_ctx, unsigned long cmd, void *arg) +{ + struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
+ if (!ctx) + return -WD_EINVAL;
+ if (!arg) + return ioctl(ctx->fd, cmd);
+ return ioctl(ctx->fd, cmd, arg); +}
+int wd_ctx_start(handle_t h_ctx) +{ + struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx; + int ret;
+ if (!ctx) + return -WD_EINVAL;
+ ret = wd_ctx_set_io_cmd(h_ctx, UACCE_CMD_START, NULL); + if (ret) + WD_ERR("failed to start on %s (%d), ret = %d!\n", + ctx->dev_path, -errno, ret);
+ return ret; +}
+int wd_release_ctx_force(handle_t h_ctx) +{ + struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx; + int ret;
+ if (!ctx) + return -WD_EINVAL;
+ ret = wd_ctx_set_io_cmd(h_ctx, UACCE_CMD_PUT_Q, NULL); + if (ret) + WD_ERR("failed to stop on %s (%d), ret = %d!\n", + ctx->dev_path, -errno, ret);
+ return ret; +}
+void *wd_ctx_mmap_qfr(handle_t h_ctx, enum uacce_qfrt qfrt) +{ + struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx; + off_t off = qfrt * getpagesize(); + size_t size; + void *addr;
+ if (!ctx || qfrt >= UACCE_QFRT_MAX || !ctx->qfrs_offs[qfrt]) { + WD_ERR("failed to check input ctx or qfrt!\n"); + return NULL; + }
+ size = ctx->qfrs_offs[qfrt];
+ addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, ctx->fd, off); + if (addr == MAP_FAILED) { + WD_ERR("failed to mmap, qfrt = %d, err = %d!\n", qfrt, -errno); + return NULL; + }
+ ctx->qfrs_base[qfrt] = addr;
+ return addr; +}
+void wd_ctx_unmap_qfr(handle_t h_ctx, enum uacce_qfrt qfrt) +{ + struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
+ if (!ctx || qfrt >= UACCE_QFRT_MAX) + return;
+ if (ctx->qfrs_offs[qfrt] != 0) + munmap(ctx->qfrs_base[qfrt], ctx->qfrs_offs[qfrt]); +}
+unsigned long wd_ctx_get_region_size(handle_t h_ctx, enum uacce_qfrt qfrt) +{ + struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx; + if (!ctx || qfrt >= UACCE_QFRT_MAX) + return 0; + return ctx->qfrs_offs[qfrt]; +}
+void *wd_ctx_get_priv(handle_t h_ctx) +{ + struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
+ if (!ctx) + return NULL;
+ return ctx->priv; +}
+int wd_ctx_set_priv(handle_t h_ctx, void *priv) +{ + struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
+ if (!ctx) + return -WD_EINVAL;
+ ctx->priv = priv;
+ return 0; +}
+char *wd_ctx_get_api(handle_t h_ctx) +{ + struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
+ if (!ctx || !ctx->dev) + return NULL;
+ return ctx->dev->api; +}
Do not agree. The wd api for drivers is also API. not only service for APP.
These functions are still APIs, but moved to wd util.h for driver services. Thanks, Longfang
static void clone_ctx_to_internal(struct wd_ctx *ctx, struct wd_ctx_internal *ctx_in) {
.