summaryrefstree
path: root/kernel/drivers/vision/npu/npu-debug.c
blob: f3c196f579f9b2ed5b3eff9116e54d1bfa353cb0 (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
/*
 * Samsung Exynos SoC series NPU driver
 *
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *	http://www.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/debugfs.h>
#include <linux/delay.h>
#include <linux/ctype.h>
#include <linux/types.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <asm/bitops.h>

#include "npu-debug.h"
#include "npu-log.h"
#include "npu-ver.h"

#define DEBUG_FS_ROOT_NAME "npu"
#define DEBUG_FS_UNITTEST_NAME "idiot"
#define NPU_DEBUG_FILENAME_LEN 32 /* Maximum file name length under npu */

#ifdef CONFIG_VISION_UNITTEST
#define IDIOT_ALL_TESTCASE_INCLUDE "idiot-npu-all-tests.h"
#include "idiot-decl.h"
#endif

/* Singleton reference to debug object */
static struct npu_debug *npu_debug_ref;

typedef enum {
	FS_READY = 0,
} npu_debug_state_bits_e;

static inline void set_state_bit(npu_debug_state_bits_e state_bit)
{
	int old = test_and_set_bit(state_bit, &(npu_debug_ref->state));

	if (old) {
		npu_warn("state(%d): state set requested but already set.", state_bit);
	}
}

static inline void clear_state_bit(npu_debug_state_bits_e state_bit)
{
	int old = test_and_clear_bit(state_bit, &(npu_debug_ref->state));

	if (!old) {
		npu_warn("state(%d): state clear requested but already cleared.", state_bit);
	}
}

static inline int check_state_bit(npu_debug_state_bits_e state_bit)
{
	return test_bit(state_bit, &(npu_debug_ref->state));
}

#ifdef CONFIG_VISION_UNITTEST
static const struct file_operations npu_debug_unittest_fops = {
    .owner = THIS_MODULE, .open = IDIOT_open, .read = IDIOT_read, .write = IDIOT_write,
};
#endif

int npu_debug_register_arg(
	const char *name, void *private_arg,
	const struct file_operations *ops)
{
	int			ret	    = 0;
	struct npu_device	*npu_device;
	struct device		*dev;
	mode_t			mode = 0;
	struct dentry		*dbgfs_entry;

	/* Check whether the debugfs is properly initialized */
	if (!check_state_bit(FS_READY)) {
		npu_warn("DebugFS not initialized or disabled. Skip creation [%s]\n", name);
		ret = 0;
		goto err_exit;
	}

	/* Default parameter is npu_debug object */
	if (private_arg == NULL) {
		private_arg = (void *)npu_debug_ref;
	}

	/* Retrieve struct device to use devm_XX api */
	npu_device = container_of(npu_debug_ref, struct npu_device, debug);
	BUG_ON(!npu_device);
	dev = npu_device->dev;
	BUG_ON(!dev);

	if (name == NULL) {
		npu_err("name is null\n");
		ret = -EFAULT;
		goto err_exit;
	}
	if (ops == NULL) {
		npu_err("ops is null\n");
		ret = -EFAULT;
		goto err_exit;
	}

	/* Setting file permission based on file_operation member */
	if (ops->read || ops->compat_ioctl || ops->unlocked_ioctl)
		mode |= 0400;	/* Read permission to owner */

	if (ops->write)
		mode |= 0200;	/* Write permission to owner */

	/* Register */
	dbgfs_entry = debugfs_create_file(name, mode,
			npu_debug_ref->dfile_root, private_arg, ops);
	if (IS_ERR(dbgfs_entry)) {
		npu_err("fail in DebugFS registration (%s)\n", name);
		ret = PTR_ERR(dbgfs_entry);
		goto err_exit;
	}

	npu_info("success in DebugFS registration (%s) : Mode %04o\n"
		 , name, mode);

	return 0;

err_exit:
	return ret;
}

int npu_debug_register(const char *name, const struct file_operations *ops)
{
	return npu_debug_register_arg(name, NULL, ops);
}

int npu_debug_probe(struct npu_device *npu_device)
{
	int ret = 0;

	BUG_ON(!npu_device);

	/* Save reference */
	npu_debug_ref = &npu_device->debug;
	memset(npu_debug_ref, 0, sizeof(*npu_debug_ref));

	probe_info("Loading npu_debug : starting\n");
	npu_debug_ref->dfile_root = debugfs_create_dir(DEBUG_FS_ROOT_NAME, NULL);

	if (!npu_debug_ref->dfile_root) {
		probe_err("Loading npu_debug : debugfs root [%s] can not be created\n",
			  DEBUG_FS_ROOT_NAME);

		npu_debug_ref->dfile_root = NULL;
		ret = -ENOENT;
		goto err_exit;
	}
	probe_info("Loading npu_debug : debugfs root [%s] created\n"
		   , DEBUG_FS_ROOT_NAME);
	set_state_bit(FS_READY);

#ifdef CONFIG_VISION_UNITTEST
	ret = npu_debug_register(DEBUG_FS_UNITTEST_NAME
				 , &npu_debug_unittest_fops);
	if (ret) {
		probe_err("loading npu_debug : debugfs for unittest can not be created\n");
		goto err_exit;
	}
#endif

	ret = npu_ver_probe(npu_device);
	if (ret) {
		probe_err("loading npu_debug : npu_ver_probe failed.\n");
		goto err_exit;
	}

	probe_info("loading npu_debug : completed.\n");
	return 0;

err_exit:
	return ret;
}

int npu_debug_release(void)
{
	int ret;
	struct npu_device	*npu_device;
	struct device		*dev;

	npu_info("start in unloading npu_debug\n");
	if (!check_state_bit(FS_READY)) {
		/* No need to clean-up */
		npu_err("not ready in npu_debug\n");
		return -1;
	}

	/* Retrieve struct device to use devm_XX api */
	npu_device = container_of(npu_debug_ref, struct npu_device, debug);
	dev = npu_device->dev;

	/* remove version info */
	ret = npu_ver_release(npu_device);
	if (ret) {
		npu_err("npu_ver_release error : ret = %d\n", ret);
		return ret;
	}

	/* Remove debug fs entries */
	debugfs_remove_recursive(npu_debug_ref->dfile_root);
	npu_info("unloading npu_debug: root node is removed.\n");

	clear_state_bit(FS_READY);
	npu_info("complete in unloading npu_debug\n");
	return 0;
}

int npu_debug_open(struct npu_device *npu_device)
{
	npu_info("start in npu_debug open\n");
	npu_info("complete in npu_debug open\n");
	return 0;
}

int npu_debug_close(struct npu_device *npu_device)
{
	npu_info("start in npu_debug close\n");
	npu_info("complete in npu_debug close\n");
	return 0;
}

int npu_debug_start(struct npu_device *npu_device)
{
	npu_info("start in npu_debug start\n");
	npu_info("complete in npu_debug start\n");
	return 0;
}

int npu_debug_stop(struct npu_device *npu_device)
{
	npu_info("start in npu_debug stop\n");
	npu_info("complete in npu_debug stop\n");
	return 0;
}