• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 
19 #ifndef __DRV_OSAL_LIB_H__
20 #define __DRV_OSAL_LIB_H__
21 
22 #include "drv_cipher_define.h"
23 #ifdef __HuaweiLite__
24 #include "drv_osal_lib_liteos.h"
25 #else
26 #include "drv_osal_lib_linux.h"
27 #endif
28 
29 #define CIPHER_CHECK_MMZ_PHY
30 
31 #ifdef CIPHER_CHECK_MMZ_PHY
32 #include "osal_mmz.h"
33 extern hil_mmb_t *hil_mmb_getby_phys_2(unsigned long addr, unsigned long *out_offset);
34 #endif
35 
36 #ifdef CIPHER_BUILTIN
37 extern int is_hicma_address(phys_addr_t phys, unsigned long size);
38 #endif
39 
40 /* return uuid */
41 #define crypto_chk_owner_err_return(local) \
42     do { \
43         CRYPTO_OWNER owner; \
44         crypto_get_owner(&owner); \
45         if (memcmp(&owner, local, sizeof(owner)) != 0) { \
46             hi_log_error("return user uuid failed\n"); \
47             hi_log_print_err_code(HI_ERR_CIPHER_ILLEGAL_UUID); \
48             return HI_ERR_CIPHER_ILLEGAL_UUID; \
49         } \
50     } while (0)
51 
52 /* max length module name */
53 #define CRYPTO_MODULE_NAME_LEN             16
54 
55 /* the max cipher hard channel count. */
56 #define CRYPTO_HARD_CHANNEL_MAX            0x08
57 
58 /* secure mmz or not, not used */
59 #define SEC_MMZ                            0x00
60 
61 /* Cipher cmd param buffer size for ioctrl. */
62 #define CRYPTO_CMD_PARAM_SIZE              256
63 
64 #ifndef CONFIG_HI_PROC_SHOW_SUPPORT
65 #define HI_PROC_SUPPORT                 0
66 #else
67 #define HI_PROC_SUPPORT                 1
68 #endif
69 
70 /* struct channel
71  * the context of hardware channel.
72  */
73 typedef struct {
74     /* the state of instance, open or closed. */
75     hi_u32 open;
76 
77     /* the context of channel, which is defined by specific module. */
78     hi_void *ctx;
79 } channel_context;
80 
81 /* struct of crypto_mem. */
82 typedef struct {
83     compat_addr dma_addr;      /* dam addr, may be mmz or smmu */
84     compat_addr mmz_addr;      /* mmz addr, sometimes the smmu must mapped from mmz */
85     hi_void *dma_virt;         /* cpu virtual addr mapped from dam addr */
86     hi_u32 dma_size;           /* dma memory size */
87     hi_void *user_buf;         /* buffer of user */
88 } crypto_mem;
89 
90 /* -------------------------------------------------------------------------------------------------------------
91  * Definition of basic data types. The data types are applicable to both the application layer and kernel codes.
92  * -------------------------------------------------------------------------------------------------------------
93  *
94  * define of HI_HANDLE :
95  * bit31                                                           bit0
96  *   |<----   16bit --------->|<---   8bit    --->|<---  8bit   --->|
97  *   |--------------------------------------------------------------|
98  *   |      HI_MOD_ID_E       |  mod defined data |     chnID       |
99  *   |--------------------------------------------------------------|
100  *
101  * mod defined data: private data define by each module(for example: sub-mod id), usually, set to 0.
102  */
103 #define hi_handle_init(mod, private_data, chnid)  \
104     (hi_handle)((((mod) & 0xffff) << 16) | ((((private_data) & 0xff) << 8)) | (((chnid) & 0xff)))
105 
106 #define hi_handle_get_modid(handle)          (((handle) >> 16) & 0xffff)
107 #define hi_handle_get_private_data(handle)   (((handle) >> 8) & 0xff)
108 #define hi_handle_get_chnid(handle)          (((handle)) & 0xff)
109 
110 /* cipher drv mode init. */
111 hi_s32 cipher_drv_mod_init(hi_void);
112 
113 /* cipher drv mode deinit. */
114 hi_void cipher_drv_mod_exit(hi_void);
115 
116 /* cipher check addr. */
117 hi_s32 cipher_check_mmz_phy_addr(hi_phys_addr_t phy_addr, hi_u32 length);
118 
119 /* cipher crypto ioctl. */
120 hi_s32 crypto_ioctl(hi_u32 cmd, hi_void *argp);
121 
122 /* cipher crypto entry for module init. */
123 hi_s32 crypto_entry(void);
124 
125 /* cipher crypto entry for module exit. */
126 hi_s32 crypto_exit(void);
127 
128 /* cipher crypto release. */
129 hi_s32 crypto_release(void);
130 
131 /*
132  * \brief  cipher get device.
133  */
134 hi_void *cipher_get_device(hi_void);
135 
136 /*
137  * \brief  init dma memory.
138  */
139 hi_void crypto_mem_init(hi_void);
140 
141 /*
142  * \brief  deinit dma memory.
143  */
144 hi_void crypto_mem_deinit(hi_void);
145 
146 /*
147  * \brief  crypto cpuc flush dcache area.
148  */
149 hi_void crypto_cpuc_flush_dcache_area(hi_void *kvir, hi_u32 length);
150 
151 /*
152  * \brief  allocate and map a dma memory.
153  * \param[in] mem  The struct of crypto_mem.
154  * \param[in] size The size of mem.
155  * \param[in] name The name of mem.
156  * \return         HI_SUCCESS if successful, or HI_BASE_ERR_MALLOC_FAILED.
157  */
158 hi_s32 crypto_mem_create(crypto_mem *mem, hi_u32 type, const char *name, hi_u32 size);
159 
160 /*
161  * \brief  destroy and unmap a dma memory.
162  * \param[in] mem  The struct of crypto_mem.
163  * \return         0 if successful, or HI_BASE_ERR_UNMAP_FAILED.
164  */
165 hi_s32 crypto_mem_destroy(crypto_mem *mem);
166 
167 /*
168  * \brief  allocate and map a dma memory.
169  * \param[in] mem  The struct of crypto_mem.
170  * \param[in] size The size of mem.
171  * \param[in] name The name of mem.
172  * \return         HI_SUCCESS if successful, or HI_BASE_ERR_MALLOC_FAILED.
173  */
174 hi_s32 hash_mem_create(crypto_mem *mem, hi_u32 type, const char *name, hi_u32 size);
175 
176 /*
177  * \brief  destroy and unmap a dma memory.
178  * \param[in] mem  The struct of crypto_mem.
179  * \return         0 if successful, or HI_BASE_ERR_UNMAP_FAILED.
180  */
181 hi_s32 hash_mem_destroy(crypto_mem *mem);
182 
183 /*
184  * \brief  map a dma memory.
185  * \param[in] mem  The struct of crypto_mem.
186  * \param[in] dma_ddr The address of dma mem.
187  * \param[in] dma_size The size of dma mem.
188  * \return         HI_SUCCESS if successful, or HI_BASE_ERR_MAP_FAILED.
189  */
190 hi_s32 crypto_mem_open(crypto_mem *mem, compat_addr dma_ddr, hi_u32 dma_size);
191 
192 /*
193  * \brief  unmap a dma memory.
194  * \param[in] mem  The struct of crypto_mem.
195  * \param[in] dma_ddr The address of dma mem.
196  * \return         HI_SUCCESS if successful, or HI_BASE_ERR_UNMAP_FAILED.
197  */
198 hi_s32 crypto_mem_close(crypto_mem *mem);
199 
200 /*
201  * \brief  attach a cpu buffer with dma memory.
202  * \param[in] mem  The struct of crypto_mem.
203  * \param[in] buffer The user's buffer.
204  * \return         HI_SUCCESS if successful, or HI_FAILURE.
205  */
206 hi_s32 crypto_mem_attach(crypto_mem *mem, hi_void *buffer);
207 
208 /*
209  * \brief  get dma memory physical address
210  * \param[in] mem The struct of crypto_mem.
211  * \return         dma_addr if successful, or zero.
212  */
213 hi_s32 crypto_mem_phys(crypto_mem *mem, compat_addr *dma_addr);
214 
215 /*
216  * \brief  get dma memory virtual address
217  * \param[in] mem The struct of crypto_mem.
218  * \return         dma_addr if successful, or zero.
219  */
220 hi_void *crypto_mem_virt(const crypto_mem *mem);
221 
222 /*
223  * \brief  check whether cpu is secure or not.
224  * \retval secure cpu, true is returned otherwise false is returned.
225  */
226 hi_u32 crypto_is_sec_cpu(hi_void);
227 
228 /*
229  * \brief  map the physics addr to cpu within the base table, contains the base addr and crg addr.
230  * \retval    on success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
231  */
232 hi_s32 module_addr_map(hi_void);
233 
234 /*
235  * \brief  unmap the physics addr to cpu within the base table, contains the base addr and crg addr.
236  * \retval    on success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
237  */
238 hi_s32 module_addr_unmap(hi_void);
239 
240 /*
241  * \brief  get secure cpu type.
242  */
243 hi_u32 module_get_secure(hi_void);
244 
245 /*
246  * \brief  enable a module, open clock  and remove reset signal.
247  * \param[in]  id The module id.
248  * \retval    NA
249  */
250 hi_void module_enable(module_id id);
251 
252 /*
253  * \brief  disable a module, close clock and set reset signal.
254  * \param[in] id The module id.
255  * \retval    NA
256  */
257 hi_void module_disable(module_id id);
258 
259 /*
260  * \brief  get attribute of module.
261  * \param[in]  id The module id.
262  * \param[out] int_valid enable interrupt or not.
263  * \param[out] int_num interrupt number of module.
264  * \param[out] name name of module.
265  * \retval    on success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
266  */
267 hi_void module_get_attr(module_id id, hi_u32 *int_valid, hi_u32 *int_num, const char **name);
268 
269 /*
270  * \brief  set irq number.
271  * \param[in]  id The module id.
272  * \param[in]  irq number.
273  * \retval    NA.
274  */
275 hi_void module_set_irq(module_id id, hi_u32 irq);
276 
277 /*
278  * \brief  read a register.
279  * \param[in]  id The module id.
280  * \param[in]  offset The module id.
281  * \retval    the value of register
282  */
283 hi_u32 module_reg_read(module_id id, hi_u32 offset);
284 
285 /*
286  * \brief  hex to string.
287  * \param[in]  buf The string buffer.
288  * \param[in]  val The value of hex.
289  * \retval    NA
290  */
291 hi_void hex2str(char buf[MUL_VAL_2], hi_u32 buf_len, hi_u8 val);
292 
293 /*
294  * \brief  write a register.
295  * \param[in]  id The module id.
296  * \retval    NA
297  */
298 hi_void module_reg_write(module_id id, hi_u32 offset, hi_u32 val);
299 
300 /* cipher module read and write a register */
301 #define symc_read(offset)         module_reg_read(CRYPTO_MODULE_ID_SYMC, offset)
302 #define symc_write(offset, val)   module_reg_write(CRYPTO_MODULE_ID_SYMC, offset, val)
303 
304 /* hash module read and write a register */
305 #define hash_read(offset)         module_reg_read(CRYPTO_MODULE_ID_HASH, offset)
306 #define hash_write(offset, val)   module_reg_write(CRYPTO_MODULE_ID_HASH, offset, val)
307 
308 /* rsa module read and write a register */
309 #define ifep_rsa_read(offset)       module_reg_read(CRYPTO_MODULE_ID_IFEP_RSA, offset)
310 #define ifep_rsa_write(offset, val) module_reg_write(CRYPTO_MODULE_ID_IFEP_RSA, offset, val)
311 
312 /* trng module read and write a register */
313 #define trng_read(offset)         module_reg_read(CRYPTO_MODULE_ID_TRNG, offset)
314 #define trng_write(offset, val)   module_reg_write(CRYPTO_MODULE_ID_TRNG, offset, val)
315 
316 /* sm2 module read and write a register */
317 #define sm2_read(offset)         module_reg_read(CRYPTO_MODULE_ID_SM2, offset)
318 #define sm2_write(offset, val)   module_reg_write(CRYPTO_MODULE_ID_SM2, offset, val)
319 
320 /* smmu module read and write a register */
321 #define smmu_read(offset)         module_reg_read(CRYPTO_MODULE_ID_SMMU, offset)
322 #define smmu_write(offset, val)   module_reg_write(CRYPTO_MODULE_ID_SMMU, offset, val)
323 
324 /*
325  * \brief  Initialize the channel list.
326  * \param[in]  ctx The context of channel.
327  * \param[in]  num The channel numbers, max is 32.
328  * \param[in]  ctx_size The size of context.
329  * \retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
330  */
331 hi_s32 crypto_channel_init(channel_context *ctx, hi_u32 num, hi_u32 ctx_size);
332 
333 /*
334  * \brief  Deinitialize the channel list.
335  * \param[in]  ctx The context of channel.
336  * \param[in]  num The channel numbers, max is 32.
337  * \retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
338  */
339 hi_s32 crypto_channel_deinit(channel_context *ctx, hi_u32 num);
340 
341 /*
342  * \brief  allocate a channel.
343  * \param[in]  ctx The context of channel.
344  * \param[in]  num The channel numbers, max is 32.
345  * \param[in]  mask Mask which channel allowed be alloc, max is 32.
346  * \param[out] id The id of channel.
347  * \retval     On success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
348  */
349 hi_s32 crypto_channel_alloc(channel_context *ctx, hi_u32 num, hi_u32 mask, hi_u32 *id);
350 
351 /*
352  * \brief  free a channel.
353  * \param[in]  ctx The context of channel.
354  * \param[in]  num The channel numbers, max is 32.
355  * \param[in] id The id of channel.
356  * \retval    on success, HI_SUCCESS is returned.  On error, HI_FAILURE is returned.
357  */
358 hi_void crypto_channel_free(channel_context *ctx, hi_u32 num, hi_u32 id);
359 
360 /*
361  * \brief  get the private data of hard channel.
362  * \param[in] ctx The context of channel.
363  * \param[in] num The channel numbers, max is 32.
364  * \param[in] id The id of channel.
365  * \retval    on success, the address of context is returned.  On error, NULL is returned..
366  */
367 hi_void *crypto_channel_get_context(const channel_context *ctx, hi_u32 num, hi_u32 id);
368 
369 /*
370  * \brief  get the rang.
371  * \retval    random number.
372  */
373 hi_u32 get_rand(hi_void);
374 
375 hi_void smmu_get_table_addr(hi_phys_addr_t *rdaddr, hi_phys_addr_t *wraddr, hi_u64 *table);
376 
377 /* allow modules to modify, default value is HI_ID_STB, the general module id. */
378 #define LOG_D_MODULE_ID             HI_ID_CIPHER
379 #define LOG_D_FUNCTRACE             0
380 #define LOG_D_UNFTRACE              0
381 
382 /* allow modules to define internal error code, from 0x1000. */
383 #define log_errcode_def(errid)      (hi_u32)(((LOG_D_MODULE_ID) << 16)  | (errid))
384 
385 /* General Error Code, All modules can extend according to the rule */
386 #define HI_LOG_ERR_MEM              log_errcode_def(0x0001)      /* Memory Operation Error */
387 #define HI_LOG_ERR_SEM              log_errcode_def(0x0002)      /* Semaphore Operation Error */
388 #define HI_LOG_ERR_FILE             log_errcode_def(0x0003)      /* File Operation Error */
389 #define HI_LOG_ERR_LOCK             log_errcode_def(0x0004)      /* Lock Operation Error */
390 #define HI_LOG_ERR_PARAM            log_errcode_def(0x0005)      /* Invalid Parameter */
391 #define HI_LOG_ERR_TIMER            log_errcode_def(0x0006)      /* Timer error */
392 #define HI_LOG_ERR_THREAD           log_errcode_def(0x0007)      /* Thread Operation Error */
393 #define HI_LOG_ERR_TIMEOUT          log_errcode_def(0x0008)      /* Time Out Error */
394 #define HI_LOG_ERR_DEVICE           log_errcode_def(0x0009)      /* Device Operation Error */
395 #define HI_LOG_ERR_STATUS           log_errcode_def(0x0010)      /* Status Error */
396 #define HI_LOG_ERR_IOCTRL           log_errcode_def(0x0011)      /* IO Operation Error */
397 #define HI_LOG_ERR_INUSE            log_errcode_def(0x0012)      /* In use */
398 #define HI_LOG_ERR_EXIST            log_errcode_def(0x0013)      /* Have exist */
399 #define HI_LOG_ERR_NOEXIST          log_errcode_def(0x0014)      /* no exist */
400 #define HI_LOG_ERR_UNSUPPORTED      log_errcode_def(0x0015)      /* Unsupported */
401 #define HI_LOG_ERR_UNAVAILABLE      log_errcode_def(0x0016)      /* Unavailable */
402 #define HI_LOG_ERR_UNINITED         log_errcode_def(0x0017)      /* Uninited */
403 #define HI_LOG_ERR_DATABASE         log_errcode_def(0x0018)      /* Database Operation Error */
404 #define HI_LOG_ERR_OVERFLOW         log_errcode_def(0x0019)      /* Overflow */
405 #define HI_LOG_ERR_EXTERNAL         log_errcode_def(0x0020)      /* External Error */
406 #define HI_LOG_ERR_UNKNOWNED        log_errcode_def(0x0021)      /* Unknown Error */
407 #define HI_LOG_ERR_FLASH            log_errcode_def(0x0022)      /* Flash Operation Error. */
408 #define HI_LOG_ERR_ILLEGAL_IMAGE    log_errcode_def(0x0023)      /* Illegal Image */
409 #define HI_LOG_ERR_ILLEGAL_UUID     log_errcode_def(0x0023)      /* Illegal UUID */
410 #define HI_LOG_ERR_NOPERMISSION     log_errcode_def(0x0023)      /* No Permission */
411 
412 /* Function trace log, strictly prohibited to expand */
413 #define hi_log_print_func_war(Func, ErrCode)  hi_log_warn("Call %s return [0x%08X]\n", #Func, (unsigned int)(ErrCode));
414 #define hi_log_print_func_err(Func, ErrCode)  hi_log_error("Call %s return [0x%08X]\n", #Func, (unsigned int)(ErrCode));
415 #define hi_log_print_err_code(ErrCode)        hi_log_error("Error Code: [0x%08X]\n", (unsigned int)(ErrCode));
416 
417 /* Used for displaying more detailed error information */
418 #define hi_err_print_s32(val)     hi_log_error("%s = %d\n",        #val, (val))
419 #define hi_err_print_u32(val)     hi_log_error("%s = %u\n",        #val, (val))
420 #define hi_err_print_s64(val)     hi_log_error("%s = %lld\n",      #val, (val))
421 #define hi_err_print_u64(val)     hi_log_error("%s = %llu\n",      #val, (val))
422 #define hi_err_print_h32(val)     hi_log_error("%s = 0x%08X\n",    #val, (val))
423 #define hi_err_print_h64(val)     hi_log_error("%s = 0x%016llX\n", #val, (val))
424 #define hi_err_print_str(val)     hi_log_error("%s = %s\n",        #val, (val))
425 #define hi_err_print_float(val)   hi_log_error("%s = %f\n",        #val, (val))
426 #define hi_err_print_info(val)    hi_log_error("<%s>\n", (val))
427 
428 /* Used for displaying more detailed warning information */
429 #define hi_log_print_s32(val)     hi_log_warn("%s = %d\n",        #val, (val))
430 #define hi_log_print_u32(val)     hi_log_warn("%s = %u\n",        #val, (val))
431 #define hi_log_print_s64(val)     hi_log_warn("%s = %lld\n",      #val, (val))
432 #define hi_log_print_u64(val)     hi_log_warn("%s = %llu\n",      #val, (val))
433 #define hi_log_print_h32(val)     hi_log_warn("%s = 0x%08X\n",    #val, (val))
434 #define hi_log_print_h64(val)     hi_log_warn("%s = 0x%016llX\n", #val, (val))
435 #define hi_log_print_str(val)     hi_log_warn("%s = %s\n",        #val, (val))
436 #define hi_log_print_float(val)   hi_log_warn("%s = %f\n",        #val, (val))
437 #define hi_log_print_info(val)    hi_log_warn("<%s>\n", (val))
438 
439 /* Only used for self debug, Can be expanded as needed */
440 #define hi_dbg_print_s32(val)     hi_log_debug("%s = %d\n",       #val, (val))
441 #define hi_dbg_print_u32(val)     hi_log_debug("%s = %u\n",       #val, (val))
442 #define hi_dbg_print_s64(val)     hi_log_debug("%s = %lld\n",     #val, (val))
443 #define hi_dbg_print_u64(val)     hi_log_debug("%s = %llu\n",     #val, (val))
444 #define hi_dbg_print_h32(val)     hi_log_debug("%s = 0x%08X\n",   #val, (val))
445 #define hi_dbg_print_h64(val)     hi_log_debug("%s = 0x%016llX\n", #val, (val))
446 #define hi_dbg_print_str(val)     hi_log_debug("%s = %s\n",       #val, (val))
447 #define hi_dbg_print_float(val)   hi_log_debug("%s = %f\n",       #val, (val))
448 #define hi_dbg_print_info(val)    hi_log_debug("<%s>\n", (val))
449 
450 #if (LOG_D_FUNCTRACE == 1) || (LOG_D_UNFTRACE == 1)
451 /* Only used for unf interface */
452 #define hi_unf_func_enter()                  hi_log_debug(" >>>>>>[Enter]\n")
453 #define hi_unf_func_exit()                   hi_log_debug(" <<<<<<[Exit]\n")
454 #else
455 #define hi_unf_func_enter()
456 #define hi_unf_func_exit()
457 #endif
458 
459 #if LOG_D_FUNCTRACE
460 /* Used for all interface except unf */
461 #define hi_log_func_enter()                  hi_log_debug(" =====>[Enter]\n")
462 #define hi_log_func_exit()                   hi_log_debug(" =====>[Exit]\n")
463 #else
464 #define hi_log_func_enter()
465 #define hi_log_func_exit()
466 #endif
467 
468 #define hi_log_check(fn_func)                                 \
469     do {                                                      \
470         hi_s32 _ierr_code = fn_func;                          \
471         if (_ierr_code != HI_SUCCESS) {                       \
472             hi_log_print_func_err(#fn_func, _ierr_code);      \
473         }                                                     \
474     } while (0)
475 
476 
477 #define hi_log_chk_param_return(_val)                         \
478     do {                                                      \
479         if (_val) {                                           \
480             hi_log_print_err_code(HI_ERR_CIPHER_INVALID_PARAM); \
481             return HI_ERR_CIPHER_INVALID_PARAM;                \
482         }                                                     \
483     } while (0)
484 
485 #define hi_log_chk_init_err_return(_init_count)               \
486     do {                                                      \
487         if ((_init_count) == 0) {                             \
488             hi_log_print_err_code(HI_ERR_CIPHER_NOT_INIT);    \
489             return HI_ERR_CIPHER_NOT_INIT;                    \
490         }                                                     \
491     } while (0)
492 
493 #ifdef CIPHER_DEBUG_TEST_SUPPORT
494 #define PRINT_HEX_BLOCK_LEN     16
495 void crypto_print_hex(const hi_char *name, hi_u8 *str, hi_u32 len);
496 #define hi_print_hex(name, str, len) crypto_print_hex((const hi_char *)name, (hi_u8 *)str, (hi_u32)len)
497 #else
498 #define hi_print_hex(name, str, len) print_string(name, str, len)
499 #endif
500 
501 #endif  /* End of #ifndef __DRV_OSAL_LIB_H__ */
502