1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include <linux/kernel.h>
7 
8 #include "i915_debugfs_params.h"
9 #include "i915_drv.h"
10 #include "i915_params.h"
11 
12 /* int param */
13 static int i915_param_int_show(struct seq_file *m, void *data)
14 {
15 	int *value = m->private;
16 
17 	seq_printf(m, "%d\n", *value);
18 
19 	return 0;
20 }
21 
22 static int i915_param_int_open(struct inode *inode, struct file *file)
23 {
24 	return single_open(file, i915_param_int_show, inode->i_private);
25 }
26 
27 static ssize_t i915_param_int_write(struct file *file,
28 				    const char __user *ubuf, size_t len,
29 				    loff_t *offp)
30 {
31 	struct seq_file *m = file->private_data;
32 	int *value = m->private;
33 	int ret;
34 
35 	ret = kstrtoint_from_user(ubuf, len, 0, value);
36 	if (ret) {
37 		/* support boolean values too */
38 		bool b;
39 
40 		ret = kstrtobool_from_user(ubuf, len, &b);
41 		if (!ret)
42 			*value = b;
43 	}
44 
45 	return ret ?: len;
46 }
47 
48 static const struct file_operations i915_param_int_fops = {
49 	.owner = THIS_MODULE,
50 	.open = i915_param_int_open,
51 	.read = seq_read,
52 	.write = i915_param_int_write,
53 	.llseek = default_llseek,
54 	.release = single_release,
55 };
56 
57 static const struct file_operations i915_param_int_fops_ro = {
58 	.owner = THIS_MODULE,
59 	.open = i915_param_int_open,
60 	.read = seq_read,
61 	.llseek = default_llseek,
62 	.release = single_release,
63 };
64 
65 /* unsigned int param */
66 static int i915_param_uint_show(struct seq_file *m, void *data)
67 {
68 	unsigned int *value = m->private;
69 
70 	seq_printf(m, "%u\n", *value);
71 
72 	return 0;
73 }
74 
75 static int i915_param_uint_open(struct inode *inode, struct file *file)
76 {
77 	return single_open(file, i915_param_uint_show, inode->i_private);
78 }
79 
80 static ssize_t i915_param_uint_write(struct file *file,
81 				     const char __user *ubuf, size_t len,
82 				     loff_t *offp)
83 {
84 	struct seq_file *m = file->private_data;
85 	unsigned int *value = m->private;
86 	int ret;
87 
88 	ret = kstrtouint_from_user(ubuf, len, 0, value);
89 	if (ret) {
90 		/* support boolean values too */
91 		bool b;
92 
93 		ret = kstrtobool_from_user(ubuf, len, &b);
94 		if (!ret)
95 			*value = b;
96 	}
97 
98 	return ret ?: len;
99 }
100 
101 static const struct file_operations i915_param_uint_fops = {
102 	.owner = THIS_MODULE,
103 	.open = i915_param_uint_open,
104 	.read = seq_read,
105 	.write = i915_param_uint_write,
106 	.llseek = default_llseek,
107 	.release = single_release,
108 };
109 
110 static const struct file_operations i915_param_uint_fops_ro = {
111 	.owner = THIS_MODULE,
112 	.open = i915_param_uint_open,
113 	.read = seq_read,
114 	.llseek = default_llseek,
115 	.release = single_release,
116 };
117 
118 /* char * param */
119 static int i915_param_charp_show(struct seq_file *m, void *data)
120 {
121 	const char **s = m->private;
122 
123 	seq_printf(m, "%s\n", *s);
124 
125 	return 0;
126 }
127 
128 static int i915_param_charp_open(struct inode *inode, struct file *file)
129 {
130 	return single_open(file, i915_param_charp_show, inode->i_private);
131 }
132 
133 static ssize_t i915_param_charp_write(struct file *file,
134 				      const char __user *ubuf, size_t len,
135 				      loff_t *offp)
136 {
137 	struct seq_file *m = file->private_data;
138 	char **s = m->private;
139 	char *new, *old;
140 
141 	/* FIXME: remove locking after params aren't the module params */
142 	kernel_param_lock(THIS_MODULE);
143 
144 	old = *s;
145 	new = strndup_user(ubuf, PAGE_SIZE);
146 	if (IS_ERR(new)) {
147 		len = PTR_ERR(new);
148 		goto out;
149 	}
150 
151 	*s = new;
152 
153 	kfree(old);
154 out:
155 	kernel_param_unlock(THIS_MODULE);
156 
157 	return len;
158 }
159 
160 static const struct file_operations i915_param_charp_fops = {
161 	.owner = THIS_MODULE,
162 	.open = i915_param_charp_open,
163 	.read = seq_read,
164 	.write = i915_param_charp_write,
165 	.llseek = default_llseek,
166 	.release = single_release,
167 };
168 
169 static const struct file_operations i915_param_charp_fops_ro = {
170 	.owner = THIS_MODULE,
171 	.open = i915_param_charp_open,
172 	.read = seq_read,
173 	.llseek = default_llseek,
174 	.release = single_release,
175 };
176 
177 #define RO(mode) (((mode) & 0222) == 0)
178 
179 static struct dentry *
180 i915_debugfs_create_int(const char *name, umode_t mode,
181 			struct dentry *parent, int *value)
182 {
183 	return debugfs_create_file_unsafe(name, mode, parent, value,
184 					  RO(mode) ? &i915_param_int_fops_ro :
185 					  &i915_param_int_fops);
186 }
187 
188 static struct dentry *
189 i915_debugfs_create_uint(const char *name, umode_t mode,
190 			 struct dentry *parent, unsigned int *value)
191 {
192 	return debugfs_create_file_unsafe(name, mode, parent, value,
193 					  RO(mode) ? &i915_param_uint_fops_ro :
194 					  &i915_param_uint_fops);
195 }
196 
197 static struct dentry *
198 i915_debugfs_create_charp(const char *name, umode_t mode,
199 			  struct dentry *parent, char **value)
200 {
201 	return debugfs_create_file(name, mode, parent, value,
202 				   RO(mode) ? &i915_param_charp_fops_ro :
203 				   &i915_param_charp_fops);
204 }
205 
206 static __always_inline void
207 _i915_param_create_file(struct dentry *parent, const char *name,
208 			const char *type, int mode, void *value)
209 {
210 	if (!mode)
211 		return;
212 
213 	if (!__builtin_strcmp(type, "bool"))
214 		debugfs_create_bool(name, mode, parent, value);
215 	else if (!__builtin_strcmp(type, "int"))
216 		i915_debugfs_create_int(name, mode, parent, value);
217 	else if (!__builtin_strcmp(type, "unsigned int"))
218 		i915_debugfs_create_uint(name, mode, parent, value);
219 	else if (!__builtin_strcmp(type, "unsigned long"))
220 		debugfs_create_ulong(name, mode, parent, value);
221 	else if (!__builtin_strcmp(type, "char *"))
222 		i915_debugfs_create_charp(name, mode, parent, value);
223 	else
224 		WARN(1, "no debugfs fops defined for param type %s (i915.%s)\n",
225 		     type, name);
226 }
227 
228 /* add a subdirectory with files for each i915 param */
229 struct dentry *i915_debugfs_params(struct drm_i915_private *i915)
230 {
231 	struct drm_minor *minor = i915->drm.primary;
232 	struct i915_params *params = &i915_modparams;
233 	struct dentry *dir;
234 
235 	dir = debugfs_create_dir("i915_params", minor->debugfs_root);
236 	if (IS_ERR(dir))
237 		return dir;
238 
239 	/*
240 	 * Note: We could create files for params needing special handling
241 	 * here. Set mode in params to 0 to skip the generic create file, or
242 	 * just let the generic create file fail silently with -EEXIST.
243 	 */
244 
245 #define REGISTER(T, x, unused, mode, ...) _i915_param_create_file(dir, #x, #T, mode, &params->x);
246 	I915_PARAMS_FOR_EACH(REGISTER);
247 #undef REGISTER
248 
249 	return dir;
250 }
251