xref: /openbmc/linux/drivers/gpu/drm/drm_debugfs.c (revision 4fc4dca8)
1 /*
2  * Created: Sun Dec 21 13:08:50 2008 by bgamari@gmail.com
3  *
4  * Copyright 2008 Ben Gamari <bgamari@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <linux/debugfs.h>
27 #include <linux/export.h>
28 #include <linux/seq_file.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
31 
32 #include <drm/drm_atomic.h>
33 #include <drm/drm_auth.h>
34 #include <drm/drm_client.h>
35 #include <drm/drm_debugfs.h>
36 #include <drm/drm_device.h>
37 #include <drm/drm_drv.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_file.h>
40 #include <drm/drm_gem.h>
41 
42 #include "drm_crtc_internal.h"
43 #include "drm_internal.h"
44 
45 #if defined(CONFIG_DEBUG_FS)
46 
47 /***************************************************
48  * Initialization, etc.
49  **************************************************/
50 
51 static int drm_name_info(struct seq_file *m, void *data)
52 {
53 	struct drm_info_node *node = (struct drm_info_node *) m->private;
54 	struct drm_minor *minor = node->minor;
55 	struct drm_device *dev = minor->dev;
56 	struct drm_master *master;
57 
58 	mutex_lock(&dev->master_mutex);
59 	master = dev->master;
60 	seq_printf(m, "%s", dev->driver->name);
61 	if (dev->dev)
62 		seq_printf(m, " dev=%s", dev_name(dev->dev));
63 	if (master && master->unique)
64 		seq_printf(m, " master=%s", master->unique);
65 	if (dev->unique)
66 		seq_printf(m, " unique=%s", dev->unique);
67 	seq_printf(m, "\n");
68 	mutex_unlock(&dev->master_mutex);
69 
70 	return 0;
71 }
72 
73 static int drm_clients_info(struct seq_file *m, void *data)
74 {
75 	struct drm_info_node *node = (struct drm_info_node *) m->private;
76 	struct drm_device *dev = node->minor->dev;
77 	struct drm_file *priv;
78 	kuid_t uid;
79 
80 	seq_printf(m,
81 		   "%20s %5s %3s master a %5s %10s\n",
82 		   "command",
83 		   "pid",
84 		   "dev",
85 		   "uid",
86 		   "magic");
87 
88 	/* dev->filelist is sorted youngest first, but we want to present
89 	 * oldest first (i.e. kernel, servers, clients), so walk backwardss.
90 	 */
91 	mutex_lock(&dev->filelist_mutex);
92 	list_for_each_entry_reverse(priv, &dev->filelist, lhead) {
93 		struct task_struct *task;
94 
95 		rcu_read_lock(); /* locks pid_task()->comm */
96 		task = pid_task(priv->pid, PIDTYPE_PID);
97 		uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
98 		seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u\n",
99 			   task ? task->comm : "<unknown>",
100 			   pid_vnr(priv->pid),
101 			   priv->minor->index,
102 			   drm_is_current_master(priv) ? 'y' : 'n',
103 			   priv->authenticated ? 'y' : 'n',
104 			   from_kuid_munged(seq_user_ns(m), uid),
105 			   priv->magic);
106 		rcu_read_unlock();
107 	}
108 	mutex_unlock(&dev->filelist_mutex);
109 	return 0;
110 }
111 
112 static int drm_gem_one_name_info(int id, void *ptr, void *data)
113 {
114 	struct drm_gem_object *obj = ptr;
115 	struct seq_file *m = data;
116 
117 	seq_printf(m, "%6d %8zd %7d %8d\n",
118 		   obj->name, obj->size,
119 		   obj->handle_count,
120 		   kref_read(&obj->refcount));
121 	return 0;
122 }
123 
124 static int drm_gem_name_info(struct seq_file *m, void *data)
125 {
126 	struct drm_info_node *node = (struct drm_info_node *) m->private;
127 	struct drm_device *dev = node->minor->dev;
128 
129 	seq_printf(m, "  name     size handles refcount\n");
130 
131 	mutex_lock(&dev->object_name_lock);
132 	idr_for_each(&dev->object_name_idr, drm_gem_one_name_info, m);
133 	mutex_unlock(&dev->object_name_lock);
134 
135 	return 0;
136 }
137 
138 static const struct drm_info_list drm_debugfs_list[] = {
139 	{"name", drm_name_info, 0},
140 	{"clients", drm_clients_info, 0},
141 	{"gem_names", drm_gem_name_info, DRIVER_GEM},
142 };
143 #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
144 
145 
146 static int drm_debugfs_open(struct inode *inode, struct file *file)
147 {
148 	struct drm_info_node *node = inode->i_private;
149 
150 	return single_open(file, node->info_ent->show, node);
151 }
152 
153 
154 static const struct file_operations drm_debugfs_fops = {
155 	.owner = THIS_MODULE,
156 	.open = drm_debugfs_open,
157 	.read = seq_read,
158 	.llseek = seq_lseek,
159 	.release = single_release,
160 };
161 
162 
163 /**
164  * drm_debugfs_create_files - Initialize a given set of debugfs files for DRM
165  * 			minor
166  * @files: The array of files to create
167  * @count: The number of files given
168  * @root: DRI debugfs dir entry.
169  * @minor: device minor number
170  *
171  * Create a given set of debugfs files represented by an array of
172  * &struct drm_info_list in the given root directory. These files will be removed
173  * automatically on drm_debugfs_cleanup().
174  */
175 int drm_debugfs_create_files(const struct drm_info_list *files, int count,
176 			     struct dentry *root, struct drm_minor *minor)
177 {
178 	struct drm_device *dev = minor->dev;
179 	struct dentry *ent;
180 	struct drm_info_node *tmp;
181 	int i, ret;
182 
183 	for (i = 0; i < count; i++) {
184 		u32 features = files[i].driver_features;
185 
186 		if (features != 0 &&
187 		    (dev->driver->driver_features & features) != features)
188 			continue;
189 
190 		tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
191 		if (tmp == NULL) {
192 			ret = -1;
193 			goto fail;
194 		}
195 		ent = debugfs_create_file(files[i].name, S_IFREG | S_IRUGO,
196 					  root, tmp, &drm_debugfs_fops);
197 		if (!ent) {
198 			DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/%s\n",
199 				  root, files[i].name);
200 			kfree(tmp);
201 			ret = -1;
202 			goto fail;
203 		}
204 
205 		tmp->minor = minor;
206 		tmp->dent = ent;
207 		tmp->info_ent = &files[i];
208 
209 		mutex_lock(&minor->debugfs_lock);
210 		list_add(&tmp->list, &minor->debugfs_list);
211 		mutex_unlock(&minor->debugfs_lock);
212 	}
213 	return 0;
214 
215 fail:
216 	drm_debugfs_remove_files(files, count, minor);
217 	return ret;
218 }
219 EXPORT_SYMBOL(drm_debugfs_create_files);
220 
221 int drm_debugfs_init(struct drm_minor *minor, int minor_id,
222 		     struct dentry *root)
223 {
224 	struct drm_device *dev = minor->dev;
225 	char name[64];
226 	int ret;
227 
228 	INIT_LIST_HEAD(&minor->debugfs_list);
229 	mutex_init(&minor->debugfs_lock);
230 	sprintf(name, "%d", minor_id);
231 	minor->debugfs_root = debugfs_create_dir(name, root);
232 	if (!minor->debugfs_root) {
233 		DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s\n", name);
234 		return -1;
235 	}
236 
237 	ret = drm_debugfs_create_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES,
238 				       minor->debugfs_root, minor);
239 	if (ret) {
240 		debugfs_remove(minor->debugfs_root);
241 		minor->debugfs_root = NULL;
242 		DRM_ERROR("Failed to create core drm debugfs files\n");
243 		return ret;
244 	}
245 
246 	if (drm_drv_uses_atomic_modeset(dev)) {
247 		ret = drm_atomic_debugfs_init(minor);
248 		if (ret) {
249 			DRM_ERROR("Failed to create atomic debugfs files\n");
250 			return ret;
251 		}
252 	}
253 
254 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
255 		ret = drm_framebuffer_debugfs_init(minor);
256 		if (ret) {
257 			DRM_ERROR("Failed to create framebuffer debugfs file\n");
258 			return ret;
259 		}
260 
261 		ret = drm_client_debugfs_init(minor);
262 		if (ret) {
263 			DRM_ERROR("Failed to create client debugfs file\n");
264 			return ret;
265 		}
266 	}
267 
268 	if (dev->driver->debugfs_init) {
269 		ret = dev->driver->debugfs_init(minor);
270 		if (ret) {
271 			DRM_ERROR("DRM: Driver failed to initialize "
272 				  "/sys/kernel/debug/dri.\n");
273 			return ret;
274 		}
275 	}
276 	return 0;
277 }
278 
279 
280 int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
281 			     struct drm_minor *minor)
282 {
283 	struct list_head *pos, *q;
284 	struct drm_info_node *tmp;
285 	int i;
286 
287 	mutex_lock(&minor->debugfs_lock);
288 	for (i = 0; i < count; i++) {
289 		list_for_each_safe(pos, q, &minor->debugfs_list) {
290 			tmp = list_entry(pos, struct drm_info_node, list);
291 			if (tmp->info_ent == &files[i]) {
292 				debugfs_remove(tmp->dent);
293 				list_del(pos);
294 				kfree(tmp);
295 			}
296 		}
297 	}
298 	mutex_unlock(&minor->debugfs_lock);
299 	return 0;
300 }
301 EXPORT_SYMBOL(drm_debugfs_remove_files);
302 
303 static void drm_debugfs_remove_all_files(struct drm_minor *minor)
304 {
305 	struct drm_info_node *node, *tmp;
306 
307 	mutex_lock(&minor->debugfs_lock);
308 	list_for_each_entry_safe(node, tmp, &minor->debugfs_list, list) {
309 		debugfs_remove(node->dent);
310 		list_del(&node->list);
311 		kfree(node);
312 	}
313 	mutex_unlock(&minor->debugfs_lock);
314 }
315 
316 int drm_debugfs_cleanup(struct drm_minor *minor)
317 {
318 	if (!minor->debugfs_root)
319 		return 0;
320 
321 	drm_debugfs_remove_all_files(minor);
322 
323 	debugfs_remove_recursive(minor->debugfs_root);
324 	minor->debugfs_root = NULL;
325 
326 	return 0;
327 }
328 
329 static int connector_show(struct seq_file *m, void *data)
330 {
331 	struct drm_connector *connector = m->private;
332 
333 	seq_printf(m, "%s\n", drm_get_connector_force_name(connector->force));
334 
335 	return 0;
336 }
337 
338 static int connector_open(struct inode *inode, struct file *file)
339 {
340 	struct drm_connector *dev = inode->i_private;
341 
342 	return single_open(file, connector_show, dev);
343 }
344 
345 static ssize_t connector_write(struct file *file, const char __user *ubuf,
346 			       size_t len, loff_t *offp)
347 {
348 	struct seq_file *m = file->private_data;
349 	struct drm_connector *connector = m->private;
350 	char buf[12];
351 
352 	if (len > sizeof(buf) - 1)
353 		return -EINVAL;
354 
355 	if (copy_from_user(buf, ubuf, len))
356 		return -EFAULT;
357 
358 	buf[len] = '\0';
359 
360 	if (!strcmp(buf, "on"))
361 		connector->force = DRM_FORCE_ON;
362 	else if (!strcmp(buf, "digital"))
363 		connector->force = DRM_FORCE_ON_DIGITAL;
364 	else if (!strcmp(buf, "off"))
365 		connector->force = DRM_FORCE_OFF;
366 	else if (!strcmp(buf, "unspecified"))
367 		connector->force = DRM_FORCE_UNSPECIFIED;
368 	else
369 		return -EINVAL;
370 
371 	return len;
372 }
373 
374 static int edid_show(struct seq_file *m, void *data)
375 {
376 	struct drm_connector *connector = m->private;
377 	struct drm_property_blob *edid = connector->edid_blob_ptr;
378 
379 	if (connector->override_edid && edid)
380 		seq_write(m, edid->data, edid->length);
381 
382 	return 0;
383 }
384 
385 static int edid_open(struct inode *inode, struct file *file)
386 {
387 	struct drm_connector *dev = inode->i_private;
388 
389 	return single_open(file, edid_show, dev);
390 }
391 
392 static ssize_t edid_write(struct file *file, const char __user *ubuf,
393 			  size_t len, loff_t *offp)
394 {
395 	struct seq_file *m = file->private_data;
396 	struct drm_connector *connector = m->private;
397 	char *buf;
398 	struct edid *edid;
399 	int ret;
400 
401 	buf = memdup_user(ubuf, len);
402 	if (IS_ERR(buf))
403 		return PTR_ERR(buf);
404 
405 	edid = (struct edid *) buf;
406 
407 	if (len == 5 && !strncmp(buf, "reset", 5)) {
408 		connector->override_edid = false;
409 		ret = drm_connector_update_edid_property(connector, NULL);
410 	} else if (len < EDID_LENGTH ||
411 		   EDID_LENGTH * (1 + edid->extensions) > len)
412 		ret = -EINVAL;
413 	else {
414 		connector->override_edid = false;
415 		ret = drm_connector_update_edid_property(connector, edid);
416 		if (!ret)
417 			connector->override_edid = true;
418 	}
419 
420 	kfree(buf);
421 
422 	return (ret) ? ret : len;
423 }
424 
425 static const struct file_operations drm_edid_fops = {
426 	.owner = THIS_MODULE,
427 	.open = edid_open,
428 	.read = seq_read,
429 	.llseek = seq_lseek,
430 	.release = single_release,
431 	.write = edid_write
432 };
433 
434 
435 static const struct file_operations drm_connector_fops = {
436 	.owner = THIS_MODULE,
437 	.open = connector_open,
438 	.read = seq_read,
439 	.llseek = seq_lseek,
440 	.release = single_release,
441 	.write = connector_write
442 };
443 
444 int drm_debugfs_connector_add(struct drm_connector *connector)
445 {
446 	struct drm_minor *minor = connector->dev->primary;
447 	struct dentry *root, *ent;
448 
449 	if (!minor->debugfs_root)
450 		return -1;
451 
452 	root = debugfs_create_dir(connector->name, minor->debugfs_root);
453 	if (!root)
454 		return -ENOMEM;
455 
456 	connector->debugfs_entry = root;
457 
458 	/* force */
459 	ent = debugfs_create_file("force", S_IRUGO | S_IWUSR, root, connector,
460 				  &drm_connector_fops);
461 	if (!ent)
462 		goto error;
463 
464 	/* edid */
465 	ent = debugfs_create_file("edid_override", S_IRUGO | S_IWUSR, root,
466 				  connector, &drm_edid_fops);
467 	if (!ent)
468 		goto error;
469 
470 	return 0;
471 
472 error:
473 	debugfs_remove_recursive(connector->debugfs_entry);
474 	connector->debugfs_entry = NULL;
475 	return -ENOMEM;
476 }
477 
478 void drm_debugfs_connector_remove(struct drm_connector *connector)
479 {
480 	if (!connector->debugfs_entry)
481 		return;
482 
483 	debugfs_remove_recursive(connector->debugfs_entry);
484 
485 	connector->debugfs_entry = NULL;
486 }
487 
488 int drm_debugfs_crtc_add(struct drm_crtc *crtc)
489 {
490 	struct drm_minor *minor = crtc->dev->primary;
491 	struct dentry *root;
492 	char *name;
493 
494 	name = kasprintf(GFP_KERNEL, "crtc-%d", crtc->index);
495 	if (!name)
496 		return -ENOMEM;
497 
498 	root = debugfs_create_dir(name, minor->debugfs_root);
499 	kfree(name);
500 	if (!root)
501 		return -ENOMEM;
502 
503 	crtc->debugfs_entry = root;
504 
505 	if (drm_debugfs_crtc_crc_add(crtc))
506 		goto error;
507 
508 	return 0;
509 
510 error:
511 	drm_debugfs_crtc_remove(crtc);
512 	return -ENOMEM;
513 }
514 
515 void drm_debugfs_crtc_remove(struct drm_crtc *crtc)
516 {
517 	debugfs_remove_recursive(crtc->debugfs_entry);
518 	crtc->debugfs_entry = NULL;
519 }
520 
521 #endif /* CONFIG_DEBUG_FS */
522