2 * Copyright (C) 2012-2017 ARM Limited or its affiliates.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 #include <linux/kernel.h>
18 #include "ssi_config.h"
19 #include "ssi_driver.h"
20 #include "cc_crypto_ctx.h"
21 #include "ssi_sysfs.h"
23 #ifdef ENABLE_CC_SYSFS
25 static struct ssi_drvdata *sys_get_drvdata(void);
29 #include <asm/timex.h>
39 const char *op_type_name;
40 const char *stat_phase_name[MAX_STAT_PHASES];
43 static struct stat_name stat_name_db[MAX_STAT_OP_TYPES] =
46 /* STAT_OP_TYPE_NULL */
47 .op_type_name = "NULL",
48 .stat_phase_name = {NULL},
51 .op_type_name = "Encode",
52 .stat_phase_name[STAT_PHASE_0] = "Init and sanity checks",
53 .stat_phase_name[STAT_PHASE_1] = "Map buffers",
54 .stat_phase_name[STAT_PHASE_2] = "Create sequence",
55 .stat_phase_name[STAT_PHASE_3] = "Send Request",
56 .stat_phase_name[STAT_PHASE_4] = "HW-Q push",
57 .stat_phase_name[STAT_PHASE_5] = "Sequence completion",
58 .stat_phase_name[STAT_PHASE_6] = "HW cycles",
60 { .op_type_name = "Decode",
61 .stat_phase_name[STAT_PHASE_0] = "Init and sanity checks",
62 .stat_phase_name[STAT_PHASE_1] = "Map buffers",
63 .stat_phase_name[STAT_PHASE_2] = "Create sequence",
64 .stat_phase_name[STAT_PHASE_3] = "Send Request",
65 .stat_phase_name[STAT_PHASE_4] = "HW-Q push",
66 .stat_phase_name[STAT_PHASE_5] = "Sequence completion",
67 .stat_phase_name[STAT_PHASE_6] = "HW cycles",
69 { .op_type_name = "Setkey",
70 .stat_phase_name[STAT_PHASE_0] = "Init and sanity checks",
71 .stat_phase_name[STAT_PHASE_1] = "Copy key to ctx",
72 .stat_phase_name[STAT_PHASE_2] = "Create sequence",
73 .stat_phase_name[STAT_PHASE_3] = "Send Request",
74 .stat_phase_name[STAT_PHASE_4] = "HW-Q push",
75 .stat_phase_name[STAT_PHASE_5] = "Sequence completion",
76 .stat_phase_name[STAT_PHASE_6] = "HW cycles",
79 .op_type_name = "Generic",
80 .stat_phase_name[STAT_PHASE_0] = "Interrupt",
81 .stat_phase_name[STAT_PHASE_1] = "ISR-to-Tasklet",
82 .stat_phase_name[STAT_PHASE_2] = "Tasklet start-to-end",
83 .stat_phase_name[STAT_PHASE_3] = "Tasklet:user_cb()",
84 .stat_phase_name[STAT_PHASE_4] = "Tasklet:dx_X_complete() - w/o X_complete()",
85 .stat_phase_name[STAT_PHASE_5] = "",
86 .stat_phase_name[STAT_PHASE_6] = "HW cycles",
91 * Structure used to create a directory
92 * and its attributes in sysfs.
95 struct kobject *sys_dir_kobj;
96 struct attribute_group sys_dir_attr_group;
97 struct attribute **sys_dir_attr_list;
99 struct ssi_drvdata *drvdata; /* Associated driver context */
102 /* top level directory structures */
103 struct sys_dir sys_top_dir;
105 static DEFINE_SPINLOCK(stat_lock);
108 static struct stat_item stat_host_db[MAX_STAT_OP_TYPES][MAX_STAT_PHASES];
109 static struct stat_item stat_cc_db[MAX_STAT_OP_TYPES][MAX_STAT_PHASES];
112 static void init_db(struct stat_item item[MAX_STAT_OP_TYPES][MAX_STAT_PHASES])
117 for (i = 0; i < MAX_STAT_OP_TYPES; i++) {
118 for (j = 0; j < MAX_STAT_PHASES; j++) {
119 item[i][j].min = 0xFFFFFFFF;
122 item[i][j].count = 0;
127 static void update_db(struct stat_item *item, unsigned int result)
131 if (result < item->min)
133 if (result > item->max)
137 static void display_db(struct stat_item item[MAX_STAT_OP_TYPES][MAX_STAT_PHASES])
142 for (i = STAT_OP_TYPE_ENCODE; i < MAX_STAT_OP_TYPES; i++) {
143 for (j = 0; j < MAX_STAT_PHASES; j++) {
144 if (item[i][j].count > 0) {
145 avg = (u64)item[i][j].sum;
146 do_div(avg, item[i][j].count);
147 SSI_LOG_ERR("%s, %s: min=%d avg=%d max=%d sum=%lld count=%d\n",
148 stat_name_db[i].op_type_name, stat_name_db[i].stat_phase_name[j],
149 item[i][j].min, (int)avg, item[i][j].max, (long long)item[i][j].sum, item[i][j].count);
156 /**************************************
157 * Attributes show functions section *
158 **************************************/
160 static ssize_t ssi_sys_stats_host_db_clear(struct kobject *kobj,
161 struct kobj_attribute *attr, const char *buf, size_t count)
163 init_db(stat_host_db);
167 static ssize_t ssi_sys_stats_cc_db_clear(struct kobject *kobj,
168 struct kobj_attribute *attr, const char *buf, size_t count)
174 static ssize_t ssi_sys_stat_host_db_show(struct kobject *kobj,
175 struct kobj_attribute *attr, char *buf)
179 u32 min_cyc, max_cyc;
181 ssize_t buf_len, tmp_len = 0;
183 buf_len = scnprintf(buf, PAGE_SIZE,
184 "phase\t\t\t\t\t\t\tmin[cy]\tavg[cy]\tmax[cy]\t#samples\n");
185 if (buf_len < 0)/* scnprintf shouldn't return negative value according to its implementation*/
187 for (i = STAT_OP_TYPE_ENCODE; i < MAX_STAT_OP_TYPES; i++) {
188 for (j = 0; j < MAX_STAT_PHASES - 1; j++) {
189 if (stat_host_db[i][j].count > 0) {
190 avg = (u64)stat_host_db[i][j].sum;
191 do_div(avg, stat_host_db[i][j].count);
192 min_cyc = stat_host_db[i][j].min;
193 max_cyc = stat_host_db[i][j].max;
195 avg = min_cyc = max_cyc = 0;
197 tmp_len = scnprintf(line, 512,
198 "%s::%s\t\t\t\t\t%6u\t%6u\t%6u\t%7u\n",
199 stat_name_db[i].op_type_name,
200 stat_name_db[i].stat_phase_name[j],
201 min_cyc, (unsigned int)avg, max_cyc,
202 stat_host_db[i][j].count);
203 if (tmp_len < 0)/* scnprintf shouldn't return negative value according to its implementation*/
205 if (buf_len + tmp_len >= PAGE_SIZE)
208 strncat(buf, line, 512);
214 static ssize_t ssi_sys_stat_cc_db_show(struct kobject *kobj,
215 struct kobj_attribute *attr, char *buf)
219 u32 min_cyc, max_cyc;
221 ssize_t buf_len, tmp_len = 0;
223 buf_len = scnprintf(buf, PAGE_SIZE,
224 "phase\tmin[cy]\tavg[cy]\tmax[cy]\t#samples\n");
225 if (buf_len < 0)/* scnprintf shouldn't return negative value according to its implementation*/
227 for (i = STAT_OP_TYPE_ENCODE; i < MAX_STAT_OP_TYPES; i++) {
228 if (stat_cc_db[i][STAT_PHASE_6].count > 0) {
229 avg = (u64)stat_cc_db[i][STAT_PHASE_6].sum;
230 do_div(avg, stat_cc_db[i][STAT_PHASE_6].count);
231 min_cyc = stat_cc_db[i][STAT_PHASE_6].min;
232 max_cyc = stat_cc_db[i][STAT_PHASE_6].max;
234 avg = min_cyc = max_cyc = 0;
236 tmp_len = scnprintf(line, 256,
237 "%s\t%6u\t%6u\t%6u\t%7u\n",
238 stat_name_db[i].op_type_name,
242 stat_cc_db[i][STAT_PHASE_6].count);
244 if (tmp_len < 0)/* scnprintf shouldn't return negative value according to its implementation*/
247 if (buf_len + tmp_len >= PAGE_SIZE)
250 strncat(buf, line, 256);
255 void update_host_stat(unsigned int op_type, unsigned int phase, cycles_t result)
259 spin_lock_irqsave(&stat_lock, flags);
260 update_db(&(stat_host_db[op_type][phase]), (unsigned int)result);
261 spin_unlock_irqrestore(&stat_lock, flags);
265 unsigned int op_type,
267 unsigned int elapsed_cycles)
269 update_db(&(stat_cc_db[op_type][phase]), elapsed_cycles);
272 void display_all_stat_db(void)
274 SSI_LOG_ERR("\n======= CYCLE COUNT STATS =======\n");
275 display_db(stat_host_db);
276 SSI_LOG_ERR("\n======= CC HW CYCLE COUNT STATS =======\n");
277 display_db(stat_cc_db);
279 #endif /*CC_CYCLE_COUNT*/
283 static ssize_t ssi_sys_regdump_show(struct kobject *kobj,
284 struct kobj_attribute *attr, char *buf)
286 struct ssi_drvdata *drvdata = sys_get_drvdata();
288 void __iomem* cc_base = drvdata->cc_base;
291 register_value = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, HOST_SIGNATURE));
292 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%s \t(0x%lX)\t 0x%08X \n", "HOST_SIGNATURE ", DX_HOST_SIGNATURE_REG_OFFSET, register_value);
293 register_value = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, HOST_IRR));
294 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%s \t(0x%lX)\t 0x%08X \n", "HOST_IRR ", DX_HOST_IRR_REG_OFFSET, register_value);
295 register_value = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, HOST_POWER_DOWN_EN));
296 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%s \t(0x%lX)\t 0x%08X \n", "HOST_POWER_DOWN_EN ", DX_HOST_POWER_DOWN_EN_REG_OFFSET, register_value);
297 register_value = CC_HAL_READ_REGISTER(CC_REG_OFFSET(CRY_KERNEL, AXIM_MON_ERR));
298 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%s \t(0x%lX)\t 0x%08X \n", "AXIM_MON_ERR ", DX_AXIM_MON_ERR_REG_OFFSET, register_value);
299 register_value = CC_HAL_READ_REGISTER(CC_REG_OFFSET(CRY_KERNEL, DSCRPTR_QUEUE_CONTENT));
300 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%s \t(0x%lX)\t 0x%08X \n", "DSCRPTR_QUEUE_CONTENT", DX_DSCRPTR_QUEUE_CONTENT_REG_OFFSET, register_value);
304 static ssize_t ssi_sys_help_show(struct kobject *kobj,
305 struct kobj_attribute *attr, char *buf)
308 "cat reg_dump ", "Print several of CC register values",
309 #if defined CC_CYCLE_COUNT
310 "cat stats_host ", "Print host statistics",
311 "echo <number> > stats_host", "Clear host statistics database",
312 "cat stats_cc ", "Print CC statistics",
313 "echo <number> > stats_cc ", "Clear CC statistics database",
316 int i = 0, offset = 0;
318 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "Usage:\n");
319 for (i = 0; i < ARRAY_SIZE(help_str); i += 2)
320 offset += scnprintf(buf + offset, PAGE_SIZE - offset, "%s\t\t%s\n", help_str[i], help_str[i + 1]);
325 /********************************************************
327 ********************************************************/
329 * Structure used to create a directory
330 * and its attributes in sysfs.
333 struct kobject *sys_dir_kobj;
334 struct attribute_group sys_dir_attr_group;
335 struct attribute **sys_dir_attr_list;
337 struct ssi_drvdata *drvdata; /* Associated driver context */
340 /* top level directory structures */
341 static struct sys_dir sys_top_dir;
343 /* TOP LEVEL ATTRIBUTES */
344 static struct kobj_attribute ssi_sys_top_level_attrs[] = {
345 __ATTR(dump_regs, 0444, ssi_sys_regdump_show, NULL),
346 __ATTR(help, 0444, ssi_sys_help_show, NULL),
347 #if defined CC_CYCLE_COUNT
348 __ATTR(stats_host, 0664, ssi_sys_stat_host_db_show, ssi_sys_stats_host_db_clear),
349 __ATTR(stats_cc, 0664, ssi_sys_stat_cc_db_show, ssi_sys_stats_cc_db_clear),
354 static struct ssi_drvdata *sys_get_drvdata(void)
356 /* TODO: supporting multiple SeP devices would require avoiding
357 * global "top_dir" and finding associated "top_dir" by traversing
358 * up the tree to the kobject which matches one of the top_dir's
360 return sys_top_dir.drvdata;
363 static int sys_init_dir(struct sys_dir *sys_dir, struct ssi_drvdata *drvdata,
364 struct kobject *parent_dir_kobj, const char *dir_name,
365 struct kobj_attribute *attrs, u32 num_of_attrs)
369 memset(sys_dir, 0, sizeof(struct sys_dir));
371 sys_dir->drvdata = drvdata;
373 /* initialize directory kobject */
374 sys_dir->sys_dir_kobj =
375 kobject_create_and_add(dir_name, parent_dir_kobj);
377 if (!(sys_dir->sys_dir_kobj))
379 /* allocate memory for directory's attributes list */
380 sys_dir->sys_dir_attr_list =
381 kzalloc(sizeof(struct attribute *) * (num_of_attrs + 1),
384 if (!(sys_dir->sys_dir_attr_list)) {
385 kobject_put(sys_dir->sys_dir_kobj);
389 sys_dir->num_of_attrs = num_of_attrs;
391 /* initialize attributes list */
392 for (i = 0; i < num_of_attrs; ++i)
393 sys_dir->sys_dir_attr_list[i] = &(attrs[i].attr);
395 /* last list entry should be NULL */
396 sys_dir->sys_dir_attr_list[num_of_attrs] = NULL;
398 sys_dir->sys_dir_attr_group.attrs = sys_dir->sys_dir_attr_list;
400 return sysfs_create_group(sys_dir->sys_dir_kobj,
401 &(sys_dir->sys_dir_attr_group));
404 static void sys_free_dir(struct sys_dir *sys_dir)
409 kfree(sys_dir->sys_dir_attr_list);
411 if (sys_dir->sys_dir_kobj)
412 kobject_put(sys_dir->sys_dir_kobj);
415 int ssi_sysfs_init(struct kobject *sys_dev_obj, struct ssi_drvdata *drvdata)
419 #if defined CC_CYCLE_COUNT
420 /* Init. statistics */
421 init_db(stat_host_db);
425 SSI_LOG_ERR("setup sysfs under %s\n", sys_dev_obj->name);
427 /* Initialize top directory */
428 retval = sys_init_dir(&sys_top_dir, drvdata, sys_dev_obj,
429 "cc_info", ssi_sys_top_level_attrs,
430 ARRAY_SIZE(ssi_sys_top_level_attrs));
434 void ssi_sysfs_fini(void)
436 sys_free_dir(&sys_top_dir);
439 #endif /*ENABLE_CC_SYSFS*/