Home | History | Annotate | Line # | Download | only in drm
drm_debugfs.c revision 1.2.2.1
      1      1.1  riastrad /**
      2      1.1  riastrad  * \file drm_debugfs.c
      3      1.1  riastrad  * debugfs support for DRM
      4      1.1  riastrad  *
      5      1.1  riastrad  * \author Ben Gamari <bgamari (at) gmail.com>
      6      1.1  riastrad  */
      7      1.1  riastrad 
      8      1.1  riastrad /*
      9      1.1  riastrad  * Created: Sun Dec 21 13:08:50 2008 by bgamari (at) gmail.com
     10      1.1  riastrad  *
     11      1.1  riastrad  * Copyright 2008 Ben Gamari <bgamari (at) gmail.com>
     12      1.1  riastrad  *
     13      1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
     14      1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     15      1.1  riastrad  * to deal in the Software without restriction, including without limitation
     16      1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     17      1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     18      1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     19      1.1  riastrad  *
     20      1.1  riastrad  * The above copyright notice and this permission notice (including the next
     21      1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     22      1.1  riastrad  * Software.
     23      1.1  riastrad  *
     24      1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     25      1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     26      1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     27      1.1  riastrad  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     28      1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     29      1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     30      1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     31      1.1  riastrad  */
     32      1.1  riastrad 
     33      1.1  riastrad #include <linux/debugfs.h>
     34      1.1  riastrad #include <linux/seq_file.h>
     35      1.1  riastrad #include <linux/slab.h>
     36      1.1  riastrad #include <linux/export.h>
     37      1.1  riastrad #include <drm/drmP.h>
     38      1.1  riastrad 
     39      1.1  riastrad #if defined(CONFIG_DEBUG_FS)
     40      1.1  riastrad 
     41      1.1  riastrad /***************************************************
     42      1.1  riastrad  * Initialization, etc.
     43      1.1  riastrad  **************************************************/
     44      1.1  riastrad 
     45  1.2.2.1       tls static const struct drm_info_list drm_debugfs_list[] = {
     46      1.1  riastrad 	{"name", drm_name_info, 0},
     47      1.1  riastrad 	{"vm", drm_vm_info, 0},
     48      1.1  riastrad 	{"clients", drm_clients_info, 0},
     49      1.1  riastrad 	{"bufs", drm_bufs_info, 0},
     50      1.1  riastrad 	{"gem_names", drm_gem_name_info, DRIVER_GEM},
     51      1.1  riastrad #if DRM_DEBUG_CODE
     52      1.1  riastrad 	{"vma", drm_vma_info, 0},
     53      1.1  riastrad #endif
     54      1.1  riastrad };
     55      1.1  riastrad #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
     56      1.1  riastrad 
     57      1.1  riastrad 
     58      1.1  riastrad static int drm_debugfs_open(struct inode *inode, struct file *file)
     59      1.1  riastrad {
     60      1.1  riastrad 	struct drm_info_node *node = inode->i_private;
     61      1.1  riastrad 
     62      1.1  riastrad 	return single_open(file, node->info_ent->show, node);
     63      1.1  riastrad }
     64      1.1  riastrad 
     65      1.1  riastrad 
     66      1.1  riastrad static const struct file_operations drm_debugfs_fops = {
     67      1.1  riastrad 	.owner = THIS_MODULE,
     68      1.1  riastrad 	.open = drm_debugfs_open,
     69      1.1  riastrad 	.read = seq_read,
     70      1.1  riastrad 	.llseek = seq_lseek,
     71      1.1  riastrad 	.release = single_release,
     72      1.1  riastrad };
     73      1.1  riastrad 
     74      1.1  riastrad 
     75      1.1  riastrad /**
     76      1.1  riastrad  * Initialize a given set of debugfs files for a device
     77      1.1  riastrad  *
     78      1.1  riastrad  * \param files The array of files to create
     79      1.1  riastrad  * \param count The number of files given
     80      1.1  riastrad  * \param root DRI debugfs dir entry.
     81      1.1  riastrad  * \param minor device minor number
     82      1.1  riastrad  * \return Zero on success, non-zero on failure
     83      1.1  riastrad  *
     84      1.1  riastrad  * Create a given set of debugfs files represented by an array of
     85      1.1  riastrad  * gdm_debugfs_lists in the given root directory.
     86      1.1  riastrad  */
     87  1.2.2.1       tls int drm_debugfs_create_files(const struct drm_info_list *files, int count,
     88      1.1  riastrad 			     struct dentry *root, struct drm_minor *minor)
     89      1.1  riastrad {
     90      1.1  riastrad 	struct drm_device *dev = minor->dev;
     91      1.1  riastrad 	struct dentry *ent;
     92      1.1  riastrad 	struct drm_info_node *tmp;
     93      1.1  riastrad 	int i, ret;
     94      1.1  riastrad 
     95      1.1  riastrad 	for (i = 0; i < count; i++) {
     96      1.1  riastrad 		u32 features = files[i].driver_features;
     97      1.1  riastrad 
     98      1.1  riastrad 		if (features != 0 &&
     99      1.1  riastrad 		    (dev->driver->driver_features & features) != features)
    100      1.1  riastrad 			continue;
    101      1.1  riastrad 
    102      1.1  riastrad 		tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
    103      1.1  riastrad 		if (tmp == NULL) {
    104      1.1  riastrad 			ret = -1;
    105      1.1  riastrad 			goto fail;
    106      1.1  riastrad 		}
    107      1.1  riastrad 		ent = debugfs_create_file(files[i].name, S_IFREG | S_IRUGO,
    108      1.1  riastrad 					  root, tmp, &drm_debugfs_fops);
    109      1.1  riastrad 		if (!ent) {
    110      1.1  riastrad 			DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s/%s\n",
    111      1.1  riastrad 				  root->d_name.name, files[i].name);
    112      1.1  riastrad 			kfree(tmp);
    113      1.1  riastrad 			ret = -1;
    114      1.1  riastrad 			goto fail;
    115      1.1  riastrad 		}
    116      1.1  riastrad 
    117      1.1  riastrad 		tmp->minor = minor;
    118      1.1  riastrad 		tmp->dent = ent;
    119      1.1  riastrad 		tmp->info_ent = &files[i];
    120      1.1  riastrad 
    121      1.1  riastrad 		mutex_lock(&minor->debugfs_lock);
    122      1.1  riastrad 		list_add(&tmp->list, &minor->debugfs_list);
    123      1.1  riastrad 		mutex_unlock(&minor->debugfs_lock);
    124      1.1  riastrad 	}
    125      1.1  riastrad 	return 0;
    126      1.1  riastrad 
    127      1.1  riastrad fail:
    128      1.1  riastrad 	drm_debugfs_remove_files(files, count, minor);
    129      1.1  riastrad 	return ret;
    130      1.1  riastrad }
    131      1.1  riastrad EXPORT_SYMBOL(drm_debugfs_create_files);
    132      1.1  riastrad 
    133      1.1  riastrad /**
    134      1.1  riastrad  * Initialize the DRI debugfs filesystem for a device
    135      1.1  riastrad  *
    136      1.1  riastrad  * \param dev DRM device
    137      1.1  riastrad  * \param minor device minor number
    138      1.1  riastrad  * \param root DRI debugfs dir entry.
    139      1.1  riastrad  *
    140      1.1  riastrad  * Create the DRI debugfs root entry "/sys/kernel/debug/dri", the device debugfs root entry
    141      1.1  riastrad  * "/sys/kernel/debug/dri/%minor%/", and each entry in debugfs_list as
    142      1.1  riastrad  * "/sys/kernel/debug/dri/%minor%/%name%".
    143      1.1  riastrad  */
    144      1.1  riastrad int drm_debugfs_init(struct drm_minor *minor, int minor_id,
    145      1.1  riastrad 		     struct dentry *root)
    146      1.1  riastrad {
    147      1.1  riastrad 	struct drm_device *dev = minor->dev;
    148      1.1  riastrad 	char name[64];
    149      1.1  riastrad 	int ret;
    150      1.1  riastrad 
    151      1.1  riastrad 	INIT_LIST_HEAD(&minor->debugfs_list);
    152      1.1  riastrad 	mutex_init(&minor->debugfs_lock);
    153      1.2  christos 	snprintf(name, sizeof(name), "%d", minor_id);
    154      1.1  riastrad 	minor->debugfs_root = debugfs_create_dir(name, root);
    155      1.1  riastrad 	if (!minor->debugfs_root) {
    156      1.1  riastrad 		DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s\n", name);
    157      1.1  riastrad 		return -1;
    158      1.1  riastrad 	}
    159      1.1  riastrad 
    160      1.1  riastrad 	ret = drm_debugfs_create_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES,
    161      1.1  riastrad 				       minor->debugfs_root, minor);
    162      1.1  riastrad 	if (ret) {
    163      1.1  riastrad 		debugfs_remove(minor->debugfs_root);
    164      1.1  riastrad 		minor->debugfs_root = NULL;
    165      1.1  riastrad 		DRM_ERROR("Failed to create core drm debugfs files\n");
    166      1.1  riastrad 		return ret;
    167      1.1  riastrad 	}
    168      1.1  riastrad 
    169      1.1  riastrad 	if (dev->driver->debugfs_init) {
    170      1.1  riastrad 		ret = dev->driver->debugfs_init(minor);
    171      1.1  riastrad 		if (ret) {
    172      1.1  riastrad 			DRM_ERROR("DRM: Driver failed to initialize "
    173      1.1  riastrad 				  "/sys/kernel/debug/dri.\n");
    174      1.1  riastrad 			return ret;
    175      1.1  riastrad 		}
    176      1.1  riastrad 	}
    177      1.1  riastrad 	return 0;
    178      1.1  riastrad }
    179      1.1  riastrad 
    180      1.1  riastrad 
    181      1.1  riastrad /**
    182      1.1  riastrad  * Remove a list of debugfs files
    183      1.1  riastrad  *
    184      1.1  riastrad  * \param files The list of files
    185      1.1  riastrad  * \param count The number of files
    186      1.1  riastrad  * \param minor The minor of which we should remove the files
    187      1.1  riastrad  * \return always zero.
    188      1.1  riastrad  *
    189      1.1  riastrad  * Remove all debugfs entries created by debugfs_init().
    190      1.1  riastrad  */
    191  1.2.2.1       tls int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
    192      1.1  riastrad 			     struct drm_minor *minor)
    193      1.1  riastrad {
    194      1.1  riastrad 	struct list_head *pos, *q;
    195      1.1  riastrad 	struct drm_info_node *tmp;
    196      1.1  riastrad 	int i;
    197      1.1  riastrad 
    198      1.1  riastrad 	mutex_lock(&minor->debugfs_lock);
    199      1.1  riastrad 	for (i = 0; i < count; i++) {
    200      1.1  riastrad 		list_for_each_safe(pos, q, &minor->debugfs_list) {
    201      1.1  riastrad 			tmp = list_entry(pos, struct drm_info_node, list);
    202      1.1  riastrad 			if (tmp->info_ent == &files[i]) {
    203      1.1  riastrad 				debugfs_remove(tmp->dent);
    204      1.1  riastrad 				list_del(pos);
    205      1.1  riastrad 				kfree(tmp);
    206      1.1  riastrad 			}
    207      1.1  riastrad 		}
    208      1.1  riastrad 	}
    209      1.1  riastrad 	mutex_unlock(&minor->debugfs_lock);
    210      1.1  riastrad 	return 0;
    211      1.1  riastrad }
    212      1.1  riastrad EXPORT_SYMBOL(drm_debugfs_remove_files);
    213      1.1  riastrad 
    214      1.1  riastrad /**
    215      1.1  riastrad  * Cleanup the debugfs filesystem resources.
    216      1.1  riastrad  *
    217      1.1  riastrad  * \param minor device minor number.
    218      1.1  riastrad  * \return always zero.
    219      1.1  riastrad  *
    220      1.1  riastrad  * Remove all debugfs entries created by debugfs_init().
    221      1.1  riastrad  */
    222      1.1  riastrad int drm_debugfs_cleanup(struct drm_minor *minor)
    223      1.1  riastrad {
    224      1.1  riastrad 	struct drm_device *dev = minor->dev;
    225      1.1  riastrad 
    226      1.1  riastrad 	if (!minor->debugfs_root)
    227      1.1  riastrad 		return 0;
    228      1.1  riastrad 
    229      1.1  riastrad 	if (dev->driver->debugfs_cleanup)
    230      1.1  riastrad 		dev->driver->debugfs_cleanup(minor);
    231      1.1  riastrad 
    232      1.1  riastrad 	drm_debugfs_remove_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES, minor);
    233      1.1  riastrad 
    234      1.1  riastrad 	debugfs_remove(minor->debugfs_root);
    235      1.1  riastrad 	minor->debugfs_root = NULL;
    236      1.1  riastrad 
    237      1.1  riastrad 	return 0;
    238      1.1  riastrad }
    239      1.1  riastrad 
    240      1.1  riastrad #endif /* CONFIG_DEBUG_FS */
    241      1.1  riastrad 
    242