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 --- Makefile.am | 6 +- drv/hisi_qm_udrv.c | 1 - include/wd.h | 137 -------------------------------------------- include/wd_common.h | 16 +++++- include/wd_util.h | 125 ++++++++++++++++++++++++++++++++++++++++ libwd.map | 10 ---- wd.c | 135 +------------------------------------------ wd_util.c | 128 ++++++++++++++++++++++++++++++++++++++++- 8 files changed, 269 insertions(+), 289 deletions(-)
diff --git a/Makefile.am b/Makefile.am index c4957bc..d831e37 100644 --- a/Makefile.am +++ b/Makefile.am @@ -65,7 +65,7 @@ libwd_comp_la_SOURCES=wd_comp.c wd_comp.h wd_comp_drv.h wd_util.c wd_util.h \ wd_sched.c wd_sched.h wd.c wd.h
libhisi_zip_la_SOURCES=drv/hisi_comp.c hisi_comp.h drv/hisi_qm_udrv.c \ - hisi_qm_udrv.h wd_comp_drv.h + hisi_qm_udrv.h wd_util.c wd_util.h wd_comp_drv.h
libwd_crypto_la_SOURCES=wd_cipher.c wd_cipher.h wd_cipher_drv.h \ wd_aead.c wd_aead.h wd_aead_drv.h \ @@ -78,10 +78,10 @@ libwd_crypto_la_SOURCES=wd_cipher.c wd_cipher.h wd_cipher_drv.h \ wd.c wd.h
libhisi_sec_la_SOURCES=drv/hisi_sec.c drv/hisi_qm_udrv.c \ - hisi_qm_udrv.h wd_cipher_drv.h wd_aead_drv.h + hisi_qm_udrv.h wd_util.c wd_util.h wd_cipher_drv.h wd_aead_drv.h
libhisi_hpre_la_SOURCES=drv/hisi_hpre.c drv/hisi_qm_udrv.c \ - hisi_qm_udrv.h wd_hpre_drv.h + hisi_qm_udrv.h wd_util.c wd_util.h wd_hpre_drv.h if WD_STATIC_DRV AM_CFLAGS += -DWD_STATIC_DRV AM_CFLAGS += -DWD_NO_LOG 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 136d661..c2dddfb 100644 --- a/include/wd.h +++ b/include/wd.h @@ -154,47 +154,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; -} - static inline void *WD_ERR_PTR(uintptr_t error) { return (void *)error; @@ -260,81 +219,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. @@ -425,27 +309,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 9b581d4..0e47496 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -19,6 +19,131 @@ 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_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/libwd.map b/libwd.map index 459f9ba..335a8fc 100644 --- a/libwd.map +++ b/libwd.map @@ -2,7 +2,6 @@ UADK_2.0 { global: wd_request_ctx; wd_release_ctx; - wd_ctx_get_api; wd_ctx_wait; wd_is_sva; wd_is_isolate; @@ -12,15 +11,6 @@ global: wd_get_accel_list; wd_get_accel_dev; wd_free_list_accels; - - wd_release_ctx_force; - wd_ctx_set_priv; - wd_ctx_get_priv; - wd_ctx_start; - wd_ctx_mmap_qfr; - wd_ctx_unmap_qfr; - wd_ctx_set_io_cmd; - wd_ctx_get_region_size; wd_ctx_get_dev_name;
wd_block_alloc; diff --git a/wd.c b/wd.c index 629c0df..d86ce35 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"; @@ -440,114 +428,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; @@ -808,19 +688,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); -} - struct bitmask *wd_create_device_nodemask(struct uacce_dev_list *list) { struct uacce_dev_list *p; diff --git a/wd_util.c b/wd_util.c index 433dd56..0aa412c 100644 --- a/wd_util.c +++ b/wd_util.c @@ -8,9 +8,12 @@ #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 @@ -64,6 +67,129 @@ 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; +} + static void clone_ctx_to_internal(struct wd_ctx *ctx, struct wd_ctx_internal *ctx_in) {