xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nouveau_debugfs.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_debugfs.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $	*/
2 
3 /*
4  * Copyright (C) 2009 Red Hat <bskeggs@redhat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial
16  * portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  */
27 
28 /*
29  * Authors:
30  *  Ben Skeggs <bskeggs@redhat.com>
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: nouveau_debugfs.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $");
35 
36 #include <linux/debugfs.h>
37 #include <nvif/class.h>
38 #include <nvif/if0001.h>
39 #include "nouveau_debugfs.h"
40 #include "nouveau_drv.h"
41 
42 static int
nouveau_debugfs_vbios_image(struct seq_file * m,void * data)43 nouveau_debugfs_vbios_image(struct seq_file *m, void *data)
44 {
45 	struct drm_info_node *node = (struct drm_info_node *) m->private;
46 	struct nouveau_drm *drm = nouveau_drm(node->minor->dev);
47 	int i;
48 
49 	for (i = 0; i < drm->vbios.length; i++)
50 		seq_printf(m, "%c", drm->vbios.data[i]);
51 	return 0;
52 }
53 
54 static int
nouveau_debugfs_strap_peek(struct seq_file * m,void * data)55 nouveau_debugfs_strap_peek(struct seq_file *m, void *data)
56 {
57 	struct drm_info_node *node = m->private;
58 	struct nouveau_drm *drm = nouveau_drm(node->minor->dev);
59 	int ret;
60 
61 	ret = pm_runtime_get_sync(drm->dev->dev);
62 	if (ret < 0 && ret != -EACCES)
63 		return ret;
64 
65 	seq_printf(m, "0x%08x\n",
66 		   nvif_rd32(&drm->client.device.object, 0x101000));
67 
68 	pm_runtime_mark_last_busy(drm->dev->dev);
69 	pm_runtime_put_autosuspend(drm->dev->dev);
70 
71 	return 0;
72 }
73 
74 static int
nouveau_debugfs_pstate_get(struct seq_file * m,void * data)75 nouveau_debugfs_pstate_get(struct seq_file *m, void *data)
76 {
77 	struct drm_device *drm = m->private;
78 	struct nouveau_debugfs *debugfs = nouveau_debugfs(drm);
79 	struct nvif_object *ctrl = &debugfs->ctrl;
80 	struct nvif_control_pstate_info_v0 info = {};
81 	int ret, i;
82 
83 	if (!debugfs)
84 		return -ENODEV;
85 
86 	ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_INFO, &info, sizeof(info));
87 	if (ret)
88 		return ret;
89 
90 	for (i = 0; i < info.count + 1; i++) {
91 		const s32 state = i < info.count ? i :
92 			NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT;
93 		struct nvif_control_pstate_attr_v0 attr = {
94 			.state = state,
95 			.index = 0,
96 		};
97 
98 		ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_ATTR,
99 				&attr, sizeof(attr));
100 		if (ret)
101 			return ret;
102 
103 		if (i < info.count)
104 			seq_printf(m, "%02x:", attr.state);
105 		else
106 			seq_printf(m, "%s:", info.pwrsrc == 0 ? "DC" :
107 					     info.pwrsrc == 1 ? "AC" : "--");
108 
109 		attr.index = 0;
110 		do {
111 			attr.state = state;
112 			ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_ATTR,
113 					&attr, sizeof(attr));
114 			if (ret)
115 				return ret;
116 
117 			seq_printf(m, " %s %d", attr.name, attr.min);
118 			if (attr.min != attr.max)
119 				seq_printf(m, "-%d", attr.max);
120 			seq_printf(m, " %s", attr.unit);
121 		} while (attr.index);
122 
123 		if (state >= 0) {
124 			if (info.ustate_ac == state)
125 				seq_printf(m, " AC");
126 			if (info.ustate_dc == state)
127 				seq_printf(m, " DC");
128 			if (info.pstate == state)
129 				seq_printf(m, " *");
130 		} else {
131 			if (info.ustate_ac < -1)
132 				seq_printf(m, " AC");
133 			if (info.ustate_dc < -1)
134 				seq_printf(m, " DC");
135 		}
136 
137 		seq_printf(m, "\n");
138 	}
139 
140 	return 0;
141 }
142 
143 static ssize_t
nouveau_debugfs_pstate_set(struct file * file,const char __user * ubuf,size_t len,loff_t * offp)144 nouveau_debugfs_pstate_set(struct file *file, const char __user *ubuf,
145 			   size_t len, loff_t *offp)
146 {
147 	struct seq_file *m = file->private_data;
148 	struct drm_device *drm = m->private;
149 	struct nouveau_debugfs *debugfs = nouveau_debugfs(drm);
150 	struct nvif_object *ctrl = &debugfs->ctrl;
151 	struct nvif_control_pstate_user_v0 args = { .pwrsrc = -EINVAL };
152 	char buf[32] = {}, *tmp, *cur = buf;
153 	long value, ret;
154 
155 	if (!debugfs)
156 		return -ENODEV;
157 
158 	if (len >= sizeof(buf))
159 		return -EINVAL;
160 
161 	if (copy_from_user(buf, ubuf, len))
162 		return -EFAULT;
163 
164 	if ((tmp = strchr(buf, '\n')))
165 		*tmp = '\0';
166 
167 	if (!strncasecmp(cur, "dc:", 3)) {
168 		args.pwrsrc = 0;
169 		cur += 3;
170 	} else
171 	if (!strncasecmp(cur, "ac:", 3)) {
172 		args.pwrsrc = 1;
173 		cur += 3;
174 	}
175 
176 	if (!strcasecmp(cur, "none"))
177 		args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_UNKNOWN;
178 	else
179 	if (!strcasecmp(cur, "auto"))
180 		args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_PERFMON;
181 	else {
182 		ret = kstrtol(cur, 16, &value);
183 		if (ret)
184 			return ret;
185 		args.ustate = value;
186 	}
187 
188 	ret = pm_runtime_get_sync(drm->dev);
189 	if (ret < 0 && ret != -EACCES)
190 		return ret;
191 	ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_USER, &args, sizeof(args));
192 	pm_runtime_put_autosuspend(drm->dev);
193 	if (ret < 0)
194 		return ret;
195 
196 	return len;
197 }
198 
199 static int
nouveau_debugfs_pstate_open(struct inode * inode,struct file * file)200 nouveau_debugfs_pstate_open(struct inode *inode, struct file *file)
201 {
202 	return single_open(file, nouveau_debugfs_pstate_get, inode->i_private);
203 }
204 
205 static const struct file_operations nouveau_pstate_fops = {
206 	.owner = THIS_MODULE,
207 	.open = nouveau_debugfs_pstate_open,
208 	.read = seq_read,
209 	.write = nouveau_debugfs_pstate_set,
210 };
211 
212 static struct drm_info_list nouveau_debugfs_list[] = {
213 	{ "vbios.rom",  nouveau_debugfs_vbios_image, 0, NULL },
214 	{ "strap_peek", nouveau_debugfs_strap_peek, 0, NULL },
215 };
216 #define NOUVEAU_DEBUGFS_ENTRIES ARRAY_SIZE(nouveau_debugfs_list)
217 
218 static const struct nouveau_debugfs_files {
219 	const char *name;
220 	const struct file_operations *fops;
221 } nouveau_debugfs_files[] = {
222 	{"pstate", &nouveau_pstate_fops},
223 };
224 
225 int
nouveau_drm_debugfs_init(struct drm_minor * minor)226 nouveau_drm_debugfs_init(struct drm_minor *minor)
227 {
228 	struct nouveau_drm *drm = nouveau_drm(minor->dev);
229 	struct dentry *dentry;
230 	int i, ret;
231 
232 	for (i = 0; i < ARRAY_SIZE(nouveau_debugfs_files); i++) {
233 		dentry = debugfs_create_file(nouveau_debugfs_files[i].name,
234 					     S_IRUGO | S_IWUSR,
235 					     minor->debugfs_root, minor->dev,
236 					     nouveau_debugfs_files[i].fops);
237 		if (!dentry)
238 			return -ENOMEM;
239 	}
240 
241 	ret = drm_debugfs_create_files(nouveau_debugfs_list,
242 				       NOUVEAU_DEBUGFS_ENTRIES,
243 				       minor->debugfs_root, minor);
244 	if (ret)
245 		return ret;
246 
247 	/* Set the size of the vbios since we know it, and it's confusing to
248 	 * userspace if it wants to seek() but the file has a length of 0
249 	 */
250 	dentry = debugfs_lookup("vbios.rom", minor->debugfs_root);
251 	if (!dentry)
252 		return 0;
253 
254 	d_inode(dentry)->i_size = drm->vbios.length;
255 	dput(dentry);
256 
257 	return 0;
258 }
259 
260 int
nouveau_debugfs_init(struct nouveau_drm * drm)261 nouveau_debugfs_init(struct nouveau_drm *drm)
262 {
263 	int ret;
264 
265 	drm->debugfs = kzalloc(sizeof(*drm->debugfs), GFP_KERNEL);
266 	if (!drm->debugfs)
267 		return -ENOMEM;
268 
269 	ret = nvif_object_init(&drm->client.device.object, 0,
270 			       NVIF_CLASS_CONTROL, NULL, 0,
271 			       &drm->debugfs->ctrl);
272 	if (ret)
273 		return ret;
274 
275 	return 0;
276 }
277 
278 void
nouveau_debugfs_fini(struct nouveau_drm * drm)279 nouveau_debugfs_fini(struct nouveau_drm *drm)
280 {
281 	if (drm->debugfs && drm->debugfs->ctrl.priv)
282 		nvif_object_fini(&drm->debugfs->ctrl);
283 
284 	kfree(drm->debugfs);
285 	drm->debugfs = NULL;
286 }
287