Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / i915_debugfs_params.c
blob4e2b077692cb5ace70cd5ea64fabd17df9738ef1
1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2019 Intel Corporation
4 */
6 #include <linux/kernel.h>
8 #include "i915_debugfs_params.h"
9 #include "i915_drv.h"
10 #include "i915_params.h"
12 /* int param */
13 static int i915_param_int_show(struct seq_file *m, void *data)
15 int *value = m->private;
17 seq_printf(m, "%d\n", *value);
19 return 0;
22 static int i915_param_int_open(struct inode *inode, struct file *file)
24 return single_open(file, i915_param_int_show, inode->i_private);
27 static ssize_t i915_param_int_write(struct file *file,
28 const char __user *ubuf, size_t len,
29 loff_t *offp)
31 struct seq_file *m = file->private_data;
32 int *value = m->private;
33 int ret;
35 ret = kstrtoint_from_user(ubuf, len, 0, value);
36 if (ret) {
37 /* support boolean values too */
38 bool b;
40 ret = kstrtobool_from_user(ubuf, len, &b);
41 if (!ret)
42 *value = b;
45 return ret ?: len;
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,
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,
65 /* unsigned int param */
66 static int i915_param_uint_show(struct seq_file *m, void *data)
68 unsigned int *value = m->private;
70 seq_printf(m, "%u\n", *value);
72 return 0;
75 static int i915_param_uint_open(struct inode *inode, struct file *file)
77 return single_open(file, i915_param_uint_show, inode->i_private);
80 static ssize_t i915_param_uint_write(struct file *file,
81 const char __user *ubuf, size_t len,
82 loff_t *offp)
84 struct seq_file *m = file->private_data;
85 unsigned int *value = m->private;
86 int ret;
88 ret = kstrtouint_from_user(ubuf, len, 0, value);
89 if (ret) {
90 /* support boolean values too */
91 bool b;
93 ret = kstrtobool_from_user(ubuf, len, &b);
94 if (!ret)
95 *value = b;
98 return ret ?: len;
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,
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,
118 /* char * param */
119 static int i915_param_charp_show(struct seq_file *m, void *data)
121 const char **s = m->private;
123 seq_printf(m, "%s\n", *s);
125 return 0;
128 static int i915_param_charp_open(struct inode *inode, struct file *file)
130 return single_open(file, i915_param_charp_show, inode->i_private);
133 static ssize_t i915_param_charp_write(struct file *file,
134 const char __user *ubuf, size_t len,
135 loff_t *offp)
137 struct seq_file *m = file->private_data;
138 char **s = m->private;
139 char *new, *old;
141 old = *s;
142 new = strndup_user(ubuf, PAGE_SIZE);
143 if (IS_ERR(new)) {
144 len = PTR_ERR(new);
145 goto out;
148 *s = new;
150 kfree(old);
151 out:
152 return len;
155 static const struct file_operations i915_param_charp_fops = {
156 .owner = THIS_MODULE,
157 .open = i915_param_charp_open,
158 .read = seq_read,
159 .write = i915_param_charp_write,
160 .llseek = default_llseek,
161 .release = single_release,
164 static const struct file_operations i915_param_charp_fops_ro = {
165 .owner = THIS_MODULE,
166 .open = i915_param_charp_open,
167 .read = seq_read,
168 .llseek = default_llseek,
169 .release = single_release,
172 #define RO(mode) (((mode) & 0222) == 0)
174 static struct dentry *
175 i915_debugfs_create_int(const char *name, umode_t mode,
176 struct dentry *parent, int *value)
178 return debugfs_create_file_unsafe(name, mode, parent, value,
179 RO(mode) ? &i915_param_int_fops_ro :
180 &i915_param_int_fops);
183 static struct dentry *
184 i915_debugfs_create_uint(const char *name, umode_t mode,
185 struct dentry *parent, unsigned int *value)
187 return debugfs_create_file_unsafe(name, mode, parent, value,
188 RO(mode) ? &i915_param_uint_fops_ro :
189 &i915_param_uint_fops);
192 static struct dentry *
193 i915_debugfs_create_charp(const char *name, umode_t mode,
194 struct dentry *parent, char **value)
196 return debugfs_create_file(name, mode, parent, value,
197 RO(mode) ? &i915_param_charp_fops_ro :
198 &i915_param_charp_fops);
201 static __always_inline void
202 _i915_param_create_file(struct dentry *parent, const char *name,
203 const char *type, int mode, void *value)
205 if (!mode)
206 return;
208 if (!__builtin_strcmp(type, "bool"))
209 debugfs_create_bool(name, mode, parent, value);
210 else if (!__builtin_strcmp(type, "int"))
211 i915_debugfs_create_int(name, mode, parent, value);
212 else if (!__builtin_strcmp(type, "unsigned int"))
213 i915_debugfs_create_uint(name, mode, parent, value);
214 else if (!__builtin_strcmp(type, "unsigned long"))
215 debugfs_create_ulong(name, mode, parent, value);
216 else if (!__builtin_strcmp(type, "char *"))
217 i915_debugfs_create_charp(name, mode, parent, value);
218 else
219 WARN(1, "no debugfs fops defined for param type %s (i915.%s)\n",
220 type, name);
223 /* add a subdirectory with files for each i915 param */
224 struct dentry *i915_debugfs_params(struct drm_i915_private *i915)
226 struct drm_minor *minor = i915->drm.primary;
227 struct i915_params *params = &i915->params;
228 struct dentry *dir;
230 dir = debugfs_create_dir("i915_params", minor->debugfs_root);
231 if (IS_ERR(dir))
232 return dir;
235 * Note: We could create files for params needing special handling
236 * here. Set mode in params to 0 to skip the generic create file, or
237 * just let the generic create file fail silently with -EEXIST.
240 #define REGISTER(T, x, unused, mode, ...) _i915_param_create_file(dir, #x, #T, mode, &params->x);
241 I915_PARAMS_FOR_EACH(REGISTER);
242 #undef REGISTER
244 return dir;