1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include <linux/kernel.h>
8 #include "i915_debugfs_params.h"
10 #include "i915_params.h"
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);
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,
31 struct seq_file *m = file->private_data;
32 int *value = m->private;
35 ret = kstrtoint_from_user(ubuf, len, 0, value);
37 /* support boolean values too */
40 ret = kstrtobool_from_user(ubuf, len, &b);
48 static const struct file_operations i915_param_int_fops = {
50 .open = i915_param_int_open,
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 = {
59 .open = i915_param_int_open,
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);
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,
84 struct seq_file *m = file->private_data;
85 unsigned int *value = m->private;
88 ret = kstrtouint_from_user(ubuf, len, 0, value);
90 /* support boolean values too */
93 ret = kstrtobool_from_user(ubuf, len, &b);
101 static const struct file_operations i915_param_uint_fops = {
102 .owner = THIS_MODULE,
103 .open = i915_param_uint_open,
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,
114 .llseek = default_llseek,
115 .release = single_release,
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);
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,
137 struct seq_file *m = file->private_data;
138 char **s = m->private;
142 new = strndup_user(ubuf, PAGE_SIZE);
155 static const struct file_operations i915_param_charp_fops = {
156 .owner = THIS_MODULE,
157 .open = i915_param_charp_open,
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,
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)
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);
219 WARN(1, "no debugfs fops defined for param type %s (i915.%s)\n",
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;
230 dir = debugfs_create_dir("i915_params", minor->debugfs_root);
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, ¶ms->x);
241 I915_PARAMS_FOR_EACH(REGISTER);