summaryrefstree
path: root/kernel/drivers/vision/iva/iva_sysfs.c
blob: d5d18939258f1ca45372346e7b331d9051822db2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* iva_sysfs.c
 *
 * Copyright (C) 2016 Samsung Electronics Co.Ltd
 * Authors:
 *	Ilkwan Kim <ilkwan.kim@samsung.com>
 *	Hoon Choi <hoon98.choi@samsung.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 */

#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/time.h>
#include <linux/sysfs.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include "iva_ctrl_ioctl.h"
#include "iva_ctrl.h"
#include "iva_mem.h"
#include "iva_ipc_queue.h"

static int iva_sysfs_print(char *buf, size_t buf_sz, const char *fmt, ...)
{
	int	strlen;
	va_list	ap;

	va_start(ap, fmt);
	strlen = vsnprintf(buf, buf_sz, fmt, ap);
	va_end(ap);

	return strlen;
}

#define IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size, format, ...)		\
	do {									\
		tmp_size = iva_sysfs_print(tmp_buf, sizeof(tmp_buf), 		\
						format, ## __VA_ARGS__);	\
		if ((buf_size + tmp_size + 1) > PAGE_SIZE)			\
			goto exit;	/* no space */				\
		strncat(buf, tmp_buf, tmp_size);				\
		buf_size += tmp_size;						\
	} while (0)

static ssize_t iva_mem_show(struct device *dev,
		struct device_attribute *dev_attr, char *buf)
{
	struct iva_dev_data 	*iva = dev_get_drvdata(dev);
	struct iva_mem_map 	*iva_map_node;
	struct list_head	*work_node;
	int 			i = 0;
	ssize_t 		buf_size = 0;
	char			tmp_buf[256];
	int			tmp_size;

	buf[0] = 0;

	mutex_lock(&iva->mem_map_lock);
	IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
			"------------- show iva mapped list (%4d)----------\n",
			iva->map_nr);

	list_for_each(work_node, &iva->mem_map_list) {
		iva_map_node = list_entry(work_node, struct iva_mem_map, node);
		IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
				"[%d] buf_fd:%04d,\tflags:0x%x,\tio_va:0x%08lx,"
				"\tsize:0x%08x, 0x%08x,\tref_cnt:%d\n", i,
				iva_map_node->shared_fd, iva_map_node->flags,
				iva_map_node->io_va,
				iva_map_node->req_size, iva_map_node->act_size,
				iva_mem_map_read_refcnt(iva_map_node));
		i++;
	}

	IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
		"---------------------------------------------------\n");
exit:
	mutex_unlock(&iva->mem_map_lock);

	return buf_size;
}

static ssize_t iva_mem_proc_show(struct device *dev,
		struct device_attribute *dev_attr, char *buf)
{
	struct iva_dev_data 	*iva = dev_get_drvdata(dev);
	struct iva_mem_map 	*iva_map_node;
	struct iva_proc 	*iva_proc_node;
	struct list_head	*work_node_parent;
	int 			i = 0;
	ssize_t 		buf_size = 0;
	char			tmp_buf[256];
	int			tmp_size;

	mutex_lock(&iva->mem_map_lock);
	list_for_each(work_node_parent, &iva->proc_head) {
		iva_proc_node = list_entry(work_node_parent,
				struct iva_proc, proc_node);

		IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
			"------- show iva mapped list of pid(%2d) -------\n",
			iva_proc_node->pid);
		if (hash_empty(iva_proc_node->h_mem_map)) {
			IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
				" (No mem_map in this process) ");
			continue;
		}

		hash_for_each(iva_proc_node->h_mem_map, i, iva_map_node, h_node) {
			IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
				"[%d] buf_fd:%04d,\tflags:0x%x,\tio_va:0x%08lx,"
				"\tsize:0x%08x, 0x%08x\tref_cnt:%d\n", i,
				iva_map_node->shared_fd, iva_map_node->flags,
				iva_map_node->io_va,
				iva_map_node->req_size, iva_map_node->act_size,
				iva_mem_map_read_refcnt(iva_map_node));
			i++;
		}
		IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
			"------------------------------------------------\n");
	}
exit:
	mutex_unlock(&iva->mem_map_lock);
	return buf_size;
}


static ssize_t iva_ipcq_show_trans(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct iva_dev_data 	*iva = dev_get_drvdata(dev);
	struct iva_ipcq_stat	*ipcq_stat = &iva->mcu_ipcq.ipcq_stat;
	struct list_head	*work_node;
	struct iva_ipcq_trans	*trans_item;
	unsigned long		rem_nsec;
	u64			ts_ns;
	int			i = 0;
	ssize_t 		buf_size = 0;
	char			tmp_buf[256];
	int			tmp_size;

	IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
			"------- ipc queue transactions"
			"(max nr:%d, nr: %d) -------\n",
			ipcq_stat->trans_nr_max, ipcq_stat->trans_nr);

	list_for_each(work_node, &ipcq_stat->trans_head) {
		trans_item = list_entry(work_node, struct iva_ipcq_trans, node);

		ts_ns = trans_item->ts_nsec;
		rem_nsec = do_div(ts_ns, 1000000000);
		IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
				"(%03d) [%05lu.%06lu] ", i,
				(unsigned long) ts_ns,
				rem_nsec / 1000);

		/* TO DO */
		if (trans_item->send) {
			struct ipc_cmd_param *ipc_msg_c = &trans_item->cmd_param;

			IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
					"--> ");

			switch (ipc_msg_c->ipc_cmd_type) {
			case 5:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"SCHEDULE_TABLE\n");
			#ifdef CONFIG_IVA_RT_PARAM
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"\t\tGST: 0x%08X"
						"\tsize(0x%X, %d)\n",
						ipc_msg_c->data.p.p_g_sched_t,
						ipc_msg_c->data.p.g_sched_t_size,
						ipc_msg_c->data.p.g_sched_t_size);

				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"\t\tPRM: 0x%08X"
						"\tsize(0x%X, %d)\n",
						ipc_msg_c->data.p.p_rt_param_t,
						ipc_msg_c->data.p.rt_param_size,
						ipc_msg_c->data.p.rt_param_size);
			#endif
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"\t\tTSS: 0x%08X"
						"\tsize(0x%X, %d)\n",
						ipc_msg_c->data.p.p_rt_table,
						ipc_msg_c->data.p.rt_table_size,
						ipc_msg_c->data.p.rt_table_size);

				break;
			case 10:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"FRAME_START\n");
				break;
			case 12:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"FRAME_DONE\n");
				break;
			case 11:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"NODE_START\n");
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"\t\tTABLE ID: 0x%08X"
						"\tGUID: 0x%08X\n",
						ipc_msg_c->data.m.table_id,
						ipc_msg_c->data.m.guid);
				break;
			case 9:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"EVICT\n");
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"\t\tTABLE ID: 0x%08X\n",
						ipc_msg_c->data.m.table_id);
				break;
			case 7:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"UPDATE_SCHEDULE_TABLE\n");
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"\t\tTSS: 0x%08X"
						"\tsize(0x%X, %d)\n",
						ipc_msg_c->data.p.p_rt_table,
						ipc_msg_c->data.p.rt_table_size,
						ipc_msg_c->data.p.rt_table_size);
				break;
			default:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"cmd type(%d)\n",
						ipc_msg_c->ipc_cmd_type);
			}
		} else {
			struct ipc_res_param *ipc_msg_r = &trans_item->res_param;
			IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
					"<-- ");
			switch (ipc_msg_r->ipc_cmd_type) {
			case 6:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"SCHEDULE_TABLE_ACK\n");
				break;
			case 12:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"FRAME_DONE\n");
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"\t\tTABLE ID(0x%08X)"
						"\tGUID(0x%08X)\n",
					ipc_msg_r->data.m.table_id,
					ipc_msg_r->data.m.guid);
				break;
			case 13:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"NODE_DONE\n");
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"\t\tTABLE ID(0x%08X)"
						"\tGUID(0x%08X)\n",
					ipc_msg_r->data.m.table_id,
					ipc_msg_r->data.m.guid);
				break;
			case 9:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"EVICT\n");
				break;
			case 2:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"DEINIT\n");
				break;
			case 8:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"UPDATE_SCHEDULE_TABLE_ACK\n");
				break;
			default:
				IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
						"cmd type(%d)\n",
						ipc_msg_r->ipc_cmd_type);
			}
		}
		i++;
	}
	IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
			"---------------------------------------"
			"---------------\n");
exit:
	return buf_size;
}

static ssize_t iva_ipcq_set_trans(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
#define MAX_TRANSACTION_SAVA_NR		(40)
	struct iva_dev_data 	*iva = dev_get_drvdata(dev);
	struct iva_ipcq_stat	*ipcq_stat = &iva->mcu_ipcq.ipcq_stat;
	struct list_head	*work_node;
	struct list_head	*tmp_node;
	struct iva_ipcq_trans	*trans_item;
	int		value;
	int		err;

	err = kstrtoint(buf, 10, &value);
	if (err) {
		dev_err(dev, "only decimal value allowed, but(%s)\n", buf);
		return err;
	}

	if (value > MAX_TRANSACTION_SAVA_NR)
		value = MAX_TRANSACTION_SAVA_NR;
	else if (value <= 0)
		value = 0;	/* disable */

	/* clear */
	mutex_lock(&ipcq_stat->trans_mtx);
	list_for_each_safe(work_node, tmp_node,
			&ipcq_stat->trans_head) {
		if (ipcq_stat->trans_nr <= value)
			break;
		trans_item = list_entry(work_node,
			struct iva_ipcq_trans, node);
		list_del(work_node);
		devm_kfree(dev, trans_item);
		ipcq_stat->trans_nr--;
	}
	mutex_unlock(&ipcq_stat->trans_mtx);

	ipcq_stat->trans_nr_max = (uint32_t) value;
	dev_info(dev, "max ipcq trans %d\n", ipcq_stat->trans_nr_max);
	return count;
}

static ssize_t iva_mcu_log_ctrl_show(struct device *dev,
		struct device_attribute *dev_attr, char *buf)
{
	struct iva_dev_data 	*iva = dev_get_drvdata(dev);
	ssize_t 		buf_size = 0;
	char			tmp_buf[256];
	int			tmp_size;

	buf[0] = 0;
	IVA_SYSFS_PRINT(buf, buf_size, tmp_buf, tmp_size,
			"mcu_print_delay (%d usecs)\n",
			iva->mcu_print_delay);
exit:
	return buf_size;
}

static ssize_t iva_mcu_log_ctrl(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
#define MAX_TRANSACTION_SAVA_NR		(40)
	struct iva_dev_data 	*iva = dev_get_drvdata(dev);
	int		value;
	int		err;

	err = kstrtoint(buf, 10, &value);
	if (err) {
		dev_err(dev, "only decimal value allowed, but(%s)\n", buf);
		return err;
	}

	if (value <= 0)
		value = 0;

	iva->mcu_print_delay = (uint32_t) value;

	return count;
}

static DEVICE_ATTR(iva_mem_proc, S_IRUGO | S_IWUSR,
			iva_mem_proc_show, NULL);
static DEVICE_ATTR(iva_mem, S_IRUGO | S_IWUSR,
			iva_mem_show, NULL);
static DEVICE_ATTR(ipcq_trans, S_IRUGO | S_IWUSR,
			iva_ipcq_show_trans, iva_ipcq_set_trans);
static DEVICE_ATTR(mcu_log, S_IRUGO | S_IWUSR,
			iva_mcu_log_ctrl_show, iva_mcu_log_ctrl);

static struct attribute *iva_mem_attributes[] = {
	&dev_attr_iva_mem.attr,
	&dev_attr_iva_mem_proc.attr,
	&dev_attr_ipcq_trans.attr,
	&dev_attr_mcu_log.attr,
	NULL,
};

static struct attribute_group iva_attr_group = {
	.attrs = iva_mem_attributes,
};

int32_t iva_sysfs_init(struct device *dev)
{
	return sysfs_create_group(&dev->kobj, &iva_attr_group);
}

void iva_sysfs_deinit(struct device *dev)
{
	sysfs_remove_group(&dev->kobj, &iva_attr_group);
}