1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * Description: Provides cipher driver common adapt api. \n
16 *
17 * History: \n
18 * 2023-03-22, Create file. \n
19 */
20 #ifndef CRYPTO_OSAL_ADAPT_H
21 #define CRYPTO_OSAL_ADAPT_H
22
23 #include "crypto_osal_lib.h"
24 #include "crypto_common_struct.h"
25 #include "osal_addr.h"
26
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */
31 #endif /* __cplusplus */
32
33 /* Timer. */
34 #ifndef crypto_msleep
35 #define crypto_msleep osal_msleep
36 #endif
37
38 #ifndef crypto_udelay
39 #define crypto_udelay osal_udelay
40 #endif
41
42 /* map. */
43 #ifndef crypto_ioremap_nocache
44 #define crypto_ioremap_nocache osal_ioremap_nocache
45 #define crypto_iounmap osal_iounmap
46 #endif
47
48 /* Mutex. */
49 #ifndef crypto_mutex_init
50 #define crypto_mutex osal_mutex
51 #define crypto_mutex_init osal_mutex_init
52 #define crypto_mutex_destroy osal_mutex_destroy
53 #define crypto_mutex_lock osal_mutex_lock
54 #define crypto_mutex_unlock osal_mutex_unlock
55 #endif
56
57 /* deep sleep vote & other process. */
58 #ifndef crypto_service_preprocess
59 #define crypto_service_preprocess()
60 #define crypto_service_postprocess()
61 #endif
62
63 /* Log. */
64 #ifndef crypto_print
65 #define crypto_print osal_printk
66 #endif
67
68 #if !defined(CRYPTO_LOG_DEF)
69 #define crypto_log_fmt(LOG_LEVEL_LABEL, fmt, ...) \
70 crypto_print("[%s:%d]" LOG_LEVEL_LABEL ": " fmt, __func__, __LINE__, ##__VA_ARGS__)
71
72 #ifndef CRYPTO_LOG_LEVEL
73 #define CRYPTO_LOG_LEVEL 0
74 #endif
75 #if defined(CRYPTO_LOG_LEVEL) && (CRYPTO_LOG_LEVEL == 0)
76 #define crypto_log_err(fmt, ...) crypto_log_fmt("ERROR", fmt, ##__VA_ARGS__)
77 #define crypto_log_warn(fmt, ...)
78 #define crypto_log_notice(fmt, ...)
79 #define crypto_log_dbg(fmt, ...)
80 #define crypto_log_trace(fmt, ...)
81 #elif defined(CRYPTO_LOG_LEVEL) && (CRYPTO_LOG_LEVEL == 1)
82 #define crypto_log_err(fmt, ...) crypto_log_fmt("ERROR", fmt, ##__VA_ARGS__)
83 #define crypto_log_warn(fmt, ...) crypto_log_fmt("WARN:", fmt, ##__VA_ARGS__)
84 #define crypto_log_notice(fmt, ...)
85 #define crypto_log_dbg(fmt, ...)
86 #define crypto_log_trace(fmt, ...)
87 #elif defined(CRYPTO_LOG_LEVEL) && (CRYPTO_LOG_LEVEL == 2)
88 #define crypto_log_err(fmt, ...) crypto_log_fmt("ERROR", fmt, ##__VA_ARGS__)
89 #define crypto_log_warn(fmt, ...) crypto_log_fmt("WARN:", fmt, ##__VA_ARGS__)
90 #define crypto_log_notice(fmt, ...) crypto_log_fmt("NOTICE", fmt, ##__VA_ARGS__)
91 #define crypto_log_dbg(fmt, ...)
92 #define crypto_log_trace(fmt, ...)
93 #elif defined(CRYPTO_LOG_LEVEL) && (CRYPTO_LOG_LEVEL == 3)
94 #define crypto_log_err(fmt, ...) crypto_log_fmt("ERROR", fmt, ##__VA_ARGS__)
95 #define crypto_log_warn(fmt, ...) crypto_log_fmt("WARN:", fmt, ##__VA_ARGS__)
96 #define crypto_log_notice(fmt, ...) crypto_log_fmt("NOTICE", fmt, ##__VA_ARGS__)
97 #define crypto_log_dbg(fmt, ...) crypto_log_fmt("DBG", fmt, ##__VA_ARGS__)
98 #define crypto_log_trace(fmt, ...)
99 #elif defined(CRYPTO_LOG_LEVEL) && (CRYPTO_LOG_LEVEL == 4)
100 #define crypto_log_err(fmt, ...) crypto_log_fmt("ERROR", fmt, ##__VA_ARGS__)
101 #define crypto_log_warn(fmt, ...) crypto_log_fmt("WARN:", fmt, ##__VA_ARGS__)
102 #define crypto_log_notice(fmt, ...) crypto_log_fmt("NOTICE", fmt, ##__VA_ARGS__)
103 #define crypto_log_dbg(fmt, ...) crypto_log_fmt("DBG", fmt, ##__VA_ARGS__)
104 #define crypto_log_trace(fmt, ...) crypto_log_fmt("TRACE", fmt, ##__VA_ARGS__)
105 #endif
106
107 #endif
108
109 /* Malloc. */
110 #ifndef crypto_malloc
111 #define crypto_malloc(x) (((x) > 0) ? osal_kmalloc((x), OSAL_GFP_KERNEL) : TD_NULL)
112 #define crypto_free(x) {if (((x) != TD_NULL)) osal_kfree(x);}
113 #endif
114
115 /* align check */
116 #ifndef crypto_addr_align_check
117 #define crypto_addr_align_check(addr) ((((addr) % 4) != 0) ? TD_FALSE : TD_TRUE)
118 #endif
119
120 /* ioctl_cmd. */
121 #ifndef CRYPTO_IOCTL_CMD_DEF
122 #define crypto_ioctl_cmd osal_ioctl_cmd
123 #endif
124
125 /* copy from/to user. */
126 td_s32 crypto_copy_from_user(td_void *to, unsigned long to_len, const td_void *from, unsigned long from_len);
127 td_s32 crypto_copy_to_user(td_void *to, unsigned long to_len, const td_void *from, unsigned long from_len);
128
129 /* crypto dump functions. */
130 void crypto_dump_data(const char *name, const td_u8 *data, td_u32 data_len);
131 void crypto_dump_phys_addr(const char *name, const td_phys_addr_t phys_addr, td_u32 data_len);
132
133 /* Check the Validation of but_attr. */
134 td_bool crypto_data_buf_check(const crypto_buf_attr *buf_attr, td_u32 length);
135
136 /* Crypto Owner Operation. */
137 #if !defined(crypto_owner) && (CRYPTO_ERROR_ENV != ERROR_ENV_NOOS)
138 #define crypto_owner long
139 #define crypto_owner_dump(owner) crypto_log_dbg("Owner's pid is 0x%x\n", owner)
crypto_get_owner(crypto_owner * owner)140 static inline td_s32 crypto_get_owner(crypto_owner *owner)
141 {
142 if (owner == TD_NULL) {
143 return CRYPTO_FAILURE;
144 }
145 *owner = crypto_getpid();
146 return CRYPTO_SUCCESS;
147 }
148 #endif
149
150 #ifdef __cplusplus
151 #if __cplusplus
152 }
153 #endif /* __cplusplus */
154 #endif /* __cplusplus */
155
156 #endif