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