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 * Description: for osal adapt.
15 *
16 * Create: 2023-04-21
17 */
18
19 #include "crypto_osal_lib.h"
20 #include "drv_trng.h"
21 #include "crypto_osal_adapt.h"
22 #include "crypto_drv_common.h"
23 #include "tcxo.h"
24 #include "osal_adapt.h"
25
crypto_osal_get_phys_addr(const crypto_buf_attr * buf)26 unsigned long crypto_osal_get_phys_addr(const crypto_buf_attr *buf)
27 {
28 crypto_chk_return(buf == NULL, 0, "buf is NULL\n");
29 return (unsigned long)(buf->phys_addr);
30 }
31
crypto_cache_flush(uintptr_t base_addr,td_u32 size)32 void crypto_cache_flush(uintptr_t base_addr, td_u32 size)
33 {
34 osal_dcache_region_clean((void *)base_addr, size);
35 }
36
crypto_cache_inv(uintptr_t base_addr,td_u32 size)37 void crypto_cache_inv(uintptr_t base_addr, td_u32 size)
38 {
39 osal_dcache_region_inv((void *)base_addr, size);
40 }
41
crypto_cache_all(void)42 void crypto_cache_all(void)
43 {
44 osal_dcache_flush_all();
45 }
46
crypto_dump_phys_addr(const char * name,const td_phys_addr_t phys_addr,td_u32 data_len)47 void crypto_dump_phys_addr(const char *name, const td_phys_addr_t phys_addr, td_u32 data_len)
48 {
49 crypto_dump_data(name, (void *)(uintptr_t)phys_addr, data_len);
50 }
51
crypto_data_buf_check(const crypto_buf_attr * buf_attr,td_u32 length)52 td_bool crypto_data_buf_check(const crypto_buf_attr *buf_attr, td_u32 length)
53 {
54 crypto_unused(buf_attr);
55 crypto_unused(length);
56 return TD_TRUE;
57 }
58
59 #if defined(CONFIG_CRYPTO_PERF_STATISTICS)
60 #define CRYPTO_TIMER_COUNT 10
61 #define CRYPTO_ITEM_COUNT_IN_ONE_TIMER 1024
62 typedef struct {
63 td_u64 start_time;
64 const td_char *name;
65 td_u64 cost_times[CRYPTO_ITEM_COUNT_IN_ONE_TIMER];
66 const td_char *item_name_list[CRYPTO_ITEM_COUNT_IN_ONE_TIMER];
67 td_u32 current_idx;
68 } crypto_timer_t;
69
70 static crypto_timer_t g_timer_list[CRYPTO_TIMER_COUNT];
71
crypto_timer_start(td_u32 timer_id,const td_char * name)72 td_void crypto_timer_start(td_u32 timer_id, const td_char *name)
73 {
74 if (timer_id >= CRYPTO_TIMER_COUNT) {
75 crypto_log_err("invalid timer_id\n");
76 return;
77 }
78 g_timer_list[timer_id].start_time = uapi_tcxo_get_us();
79 g_timer_list[timer_id].name = name;
80 g_timer_list[timer_id].current_idx = 0;
81 }
82
crypto_timer_end(td_u32 timer_id,const td_char * item_name)83 td_u64 crypto_timer_end(td_u32 timer_id, const td_char *item_name)
84 {
85 td_u64 cost_in_us = 0;
86 td_u32 idx = 0;
87 if (timer_id >= CRYPTO_TIMER_COUNT) {
88 crypto_log_err("invalid timer_id\n");
89 return 0;
90 }
91 if (g_timer_list[timer_id].current_idx >= CRYPTO_ITEM_COUNT_IN_ONE_TIMER) {
92 crypto_log_err("item count overflow\n");
93 return 0;
94 }
95
96 idx = g_timer_list[timer_id].current_idx;
97 cost_in_us = uapi_tcxo_get_us() - g_timer_list[timer_id].start_time;
98 g_timer_list[timer_id].cost_times[idx] = cost_in_us;
99 g_timer_list[timer_id].item_name_list[idx] = item_name;
100 g_timer_list[timer_id].start_time = uapi_tcxo_get_us();
101 g_timer_list[timer_id].current_idx++;
102 return cost_in_us;
103 }
104
crypto_timer_print(td_u32 timer_id)105 td_void crypto_timer_print(td_u32 timer_id)
106 {
107 crypto_timer_t *timer = TD_NULL;
108 td_u32 i;
109 if (timer_id >= CRYPTO_TIMER_COUNT) {
110 crypto_log_err("invalid timer_id\r\n");
111 return;
112 }
113
114 timer = &g_timer_list[timer_id];
115 if (timer->current_idx == 0) {
116 return;
117 }
118 crypto_print("%s:\r\n", timer->name);
119 for (i = 0; i < timer->current_idx; i++) {
120 crypto_print("%d. %s cost %lld us\r\n", i, timer->item_name_list[i], timer->cost_times[i]);
121 }
122 }
123
crypto_timer_print_all(td_void)124 td_void crypto_timer_print_all(td_void)
125 {
126 td_u32 i;
127 for (i = 0; i < CRYPTO_TIMER_COUNT; i++) {
128 crypto_timer_print(i);
129 }
130 }
131 #endif