1 /**
2  * \file drm_proc.c
3  * /proc support for DRM
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  *
8  * \par Acknowledgements:
9  *    Matthew J Sottek <matthew.j.sottek@intel.com> sent in a patch to fix
10  *    the problem with the proc files not outputting all their information.
11  */
12 
13 /*
14  * Created: Mon Jan 11 09:48:47 1999 by faith@valinux.com
15  *
16  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
17  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
18  * All Rights Reserved.
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining a
21  * copy of this software and associated documentation files (the "Software"),
22  * to deal in the Software without restriction, including without limitation
23  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
24  * and/or sell copies of the Software, and to permit persons to whom the
25  * Software is furnished to do so, subject to the following conditions:
26  *
27  * The above copyright notice and this permission notice (including the next
28  * paragraph) shall be included in all copies or substantial portions of the
29  * Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
34  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
35  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
36  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
37  * OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 #include <linux/seq_file.h>
41 #include <linux/slab.h>
42 #include <linux/export.h>
43 #include "drmP.h"
44 
45 /***************************************************
46  * Initialization, etc.
47  **************************************************/
48 
49 /**
50  * Proc file list.
51  */
52 static struct drm_info_list drm_proc_list[] = {
53 	{"name", drm_name_info, 0},
54 	{"vm", drm_vm_info, 0},
55 	{"clients", drm_clients_info, 0},
56 	{"queues", drm_queues_info, 0},
57 	{"bufs", drm_bufs_info, 0},
58 	{"gem_names", drm_gem_name_info, DRIVER_GEM},
59 #if DRM_DEBUG_CODE
60 	{"vma", drm_vma_info, 0},
61 #endif
62 };
63 #define DRM_PROC_ENTRIES ARRAY_SIZE(drm_proc_list)
64 
drm_proc_open(struct inode * inode,struct file * file)65 static int drm_proc_open(struct inode *inode, struct file *file)
66 {
67 	struct drm_info_node* node = PDE(inode)->data;
68 
69 	return single_open(file, node->info_ent->show, node);
70 }
71 
72 static const struct file_operations drm_proc_fops = {
73 	.owner = THIS_MODULE,
74 	.open = drm_proc_open,
75 	.read = seq_read,
76 	.llseek = seq_lseek,
77 	.release = single_release,
78 };
79 
80 
81 /**
82  * Initialize a given set of proc files for a device
83  *
84  * \param files The array of files to create
85  * \param count The number of files given
86  * \param root DRI proc dir entry.
87  * \param minor device minor number
88  * \return Zero on success, non-zero on failure
89  *
90  * Create a given set of proc files represented by an array of
91  * gdm_proc_lists in the given root directory.
92  */
drm_proc_create_files(struct drm_info_list * files,int count,struct proc_dir_entry * root,struct drm_minor * minor)93 int drm_proc_create_files(struct drm_info_list *files, int count,
94 			  struct proc_dir_entry *root, struct drm_minor *minor)
95 {
96 	struct drm_device *dev = minor->dev;
97 	struct proc_dir_entry *ent;
98 	struct drm_info_node *tmp;
99 	int i, ret;
100 
101 	for (i = 0; i < count; i++) {
102 		u32 features = files[i].driver_features;
103 
104 		if (features != 0 &&
105 		    (dev->driver->driver_features & features) != features)
106 			continue;
107 
108 		tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
109 		if (tmp == NULL) {
110 			ret = -1;
111 			goto fail;
112 		}
113 		tmp->minor = minor;
114 		tmp->info_ent = &files[i];
115 		list_add(&tmp->list, &minor->proc_nodes.list);
116 
117 		ent = proc_create_data(files[i].name, S_IRUGO, root,
118 				       &drm_proc_fops, tmp);
119 		if (!ent) {
120 			DRM_ERROR("Cannot create /proc/dri/%s/%s\n",
121 				  root->name, files[i].name);
122 			list_del(&tmp->list);
123 			kfree(tmp);
124 			ret = -1;
125 			goto fail;
126 		}
127 
128 	}
129 	return 0;
130 
131 fail:
132 	for (i = 0; i < count; i++)
133 		remove_proc_entry(drm_proc_list[i].name, minor->proc_root);
134 	return ret;
135 }
136 
137 /**
138  * Initialize the DRI proc filesystem for a device
139  *
140  * \param dev DRM device
141  * \param minor device minor number
142  * \param root DRI proc dir entry.
143  * \param dev_root resulting DRI device proc dir entry.
144  * \return root entry pointer on success, or NULL on failure.
145  *
146  * Create the DRI proc root entry "/proc/dri", the device proc root entry
147  * "/proc/dri/%minor%/", and each entry in proc_list as
148  * "/proc/dri/%minor%/%name%".
149  */
drm_proc_init(struct drm_minor * minor,int minor_id,struct proc_dir_entry * root)150 int drm_proc_init(struct drm_minor *minor, int minor_id,
151 		  struct proc_dir_entry *root)
152 {
153 	char name[64];
154 	int ret;
155 
156 	INIT_LIST_HEAD(&minor->proc_nodes.list);
157 	sprintf(name, "%d", minor_id);
158 	minor->proc_root = proc_mkdir(name, root);
159 	if (!minor->proc_root) {
160 		DRM_ERROR("Cannot create /proc/dri/%s\n", name);
161 		return -1;
162 	}
163 
164 	ret = drm_proc_create_files(drm_proc_list, DRM_PROC_ENTRIES,
165 				    minor->proc_root, minor);
166 	if (ret) {
167 		remove_proc_entry(name, root);
168 		minor->proc_root = NULL;
169 		DRM_ERROR("Failed to create core drm proc files\n");
170 		return ret;
171 	}
172 
173 	return 0;
174 }
175 
drm_proc_remove_files(struct drm_info_list * files,int count,struct drm_minor * minor)176 int drm_proc_remove_files(struct drm_info_list *files, int count,
177 			  struct drm_minor *minor)
178 {
179 	struct list_head *pos, *q;
180 	struct drm_info_node *tmp;
181 	int i;
182 
183 	for (i = 0; i < count; i++) {
184 		list_for_each_safe(pos, q, &minor->proc_nodes.list) {
185 			tmp = list_entry(pos, struct drm_info_node, list);
186 			if (tmp->info_ent == &files[i]) {
187 				remove_proc_entry(files[i].name,
188 						  minor->proc_root);
189 				list_del(pos);
190 				kfree(tmp);
191 			}
192 		}
193 	}
194 	return 0;
195 }
196 
197 /**
198  * Cleanup the proc filesystem resources.
199  *
200  * \param minor device minor number.
201  * \param root DRI proc dir entry.
202  * \param dev_root DRI device proc dir entry.
203  * \return always zero.
204  *
205  * Remove all proc entries created by proc_init().
206  */
drm_proc_cleanup(struct drm_minor * minor,struct proc_dir_entry * root)207 int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root)
208 {
209 	char name[64];
210 
211 	if (!root || !minor->proc_root)
212 		return 0;
213 
214 	drm_proc_remove_files(drm_proc_list, DRM_PROC_ENTRIES, minor);
215 
216 	sprintf(name, "%d", minor->index);
217 	remove_proc_entry(name, root);
218 
219 	return 0;
220 }
221 
222