blob: ff5456b7df728ebc7c5bc32fce102fb65ed5d5bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_proc.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * /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
Ben Gamari955b12d2009-02-17 20:08:49 -050040#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040042#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010043#include <drm/drmP.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Ben Gamari955b12d2009-02-17 20:08:49 -050045/***************************************************
46 * Initialization, etc.
47 **************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/**
50 * Proc file list.
51 */
Ben Gamari955b12d2009-02-17 20:08:49 -050052static struct drm_info_list drm_proc_list[] = {
Eric Anholt673a3942008-07-30 12:06:12 -070053 {"name", drm_name_info, 0},
Eric Anholt673a3942008-07-30 12:06:12 -070054 {"vm", drm_vm_info, 0},
55 {"clients", drm_clients_info, 0},
Eric Anholt673a3942008-07-30 12:06:12 -070056 {"bufs", drm_bufs_info, 0},
57 {"gem_names", drm_gem_name_info, DRIVER_GEM},
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#if DRM_DEBUG_CODE
Ben Gamari955b12d2009-02-17 20:08:49 -050059 {"vma", drm_vma_info, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61};
Ahmed S. Darwish8311d572007-02-09 10:30:10 +110062#define DRM_PROC_ENTRIES ARRAY_SIZE(drm_proc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Ben Gamari955b12d2009-02-17 20:08:49 -050064static int drm_proc_open(struct inode *inode, struct file *file)
65{
66 struct drm_info_node* node = PDE(inode)->data;
67
68 return single_open(file, node->info_ent->show, node);
69}
70
71static const struct file_operations drm_proc_fops = {
72 .owner = THIS_MODULE,
73 .open = drm_proc_open,
74 .read = seq_read,
75 .llseek = seq_lseek,
76 .release = single_release,
77};
78
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/**
Ben Gamari955b12d2009-02-17 20:08:49 -050081 * Initialize a given set of proc files for a device
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
Ben Gamari955b12d2009-02-17 20:08:49 -050083 * \param files The array of files to create
84 * \param count The number of files given
85 * \param root DRI proc dir entry.
86 * \param minor device minor number
87 * \return Zero on success, non-zero on failure
88 *
89 * Create a given set of proc files represented by an array of
90 * gdm_proc_lists in the given root directory.
91 */
Sachin Kamat27fc4f12012-08-01 17:15:30 +053092static int drm_proc_create_files(struct drm_info_list *files, int count,
Ben Gamari955b12d2009-02-17 20:08:49 -050093 struct proc_dir_entry *root, struct drm_minor *minor)
94{
95 struct drm_device *dev = minor->dev;
96 struct proc_dir_entry *ent;
97 struct drm_info_node *tmp;
Ben Gamari955b12d2009-02-17 20:08:49 -050098 int i, ret;
99
100 for (i = 0; i < count; i++) {
101 u32 features = files[i].driver_features;
102
103 if (features != 0 &&
104 (dev->driver->driver_features & features) != features)
105 continue;
106
Eric Anholt9a298b22009-03-24 12:23:04 -0700107 tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
Roel Kluin1ae70072009-08-29 22:20:34 +0200108 if (tmp == NULL) {
109 ret = -1;
110 goto fail;
111 }
Alexey Dobriyan3b510962009-08-28 22:58:07 +0400112 tmp->minor = minor;
113 tmp->info_ent = &files[i];
114 list_add(&tmp->list, &minor->proc_nodes.list);
115
116 ent = proc_create_data(files[i].name, S_IRUGO, root,
117 &drm_proc_fops, tmp);
Ben Gamari955b12d2009-02-17 20:08:49 -0500118 if (!ent) {
119 DRM_ERROR("Cannot create /proc/dri/%s/%s\n",
Marcin Slusarza0f92192011-10-10 19:32:17 +0200120 root->name, files[i].name);
Alexey Dobriyan3b510962009-08-28 22:58:07 +0400121 list_del(&tmp->list);
Eric Anholt9a298b22009-03-24 12:23:04 -0700122 kfree(tmp);
Ben Gamari955b12d2009-02-17 20:08:49 -0500123 ret = -1;
124 goto fail;
125 }
126
Ben Gamari955b12d2009-02-17 20:08:49 -0500127 }
128 return 0;
129
130fail:
131 for (i = 0; i < count; i++)
132 remove_proc_entry(drm_proc_list[i].name, minor->proc_root);
133 return ret;
134}
135
136/**
137 * Initialize the DRI proc filesystem for a device
138 *
139 * \param dev DRM device
140 * \param minor device minor number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * \param root DRI proc dir entry.
142 * \param dev_root resulting DRI device proc dir entry.
143 * \return root entry pointer on success, or NULL on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000144 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * Create the DRI proc root entry "/proc/dri", the device proc root entry
146 * "/proc/dri/%minor%/", and each entry in proc_list as
147 * "/proc/dri/%minor%/%name%".
148 */
Dave Airlie2c14f282008-04-21 16:47:32 +1000149int drm_proc_init(struct drm_minor *minor, int minor_id,
150 struct proc_dir_entry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000152 char name[64];
Ben Gamari955b12d2009-02-17 20:08:49 -0500153 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Ben Gamari955b12d2009-02-17 20:08:49 -0500155 INIT_LIST_HEAD(&minor->proc_nodes.list);
Dave Airlie2c14f282008-04-21 16:47:32 +1000156 sprintf(name, "%d", minor_id);
Ben Gamari955b12d2009-02-17 20:08:49 -0500157 minor->proc_root = proc_mkdir(name, root);
158 if (!minor->proc_root) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 DRM_ERROR("Cannot create /proc/dri/%s\n", name);
160 return -1;
161 }
162
Ben Gamari955b12d2009-02-17 20:08:49 -0500163 ret = drm_proc_create_files(drm_proc_list, DRM_PROC_ENTRIES,
164 minor->proc_root, minor);
165 if (ret) {
166 remove_proc_entry(name, root);
167 minor->proc_root = NULL;
168 DRM_ERROR("Failed to create core drm proc files\n");
169 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
172 return 0;
Ben Gamari955b12d2009-02-17 20:08:49 -0500173}
Eric Anholt673a3942008-07-30 12:06:12 -0700174
Sachin Kamat27fc4f12012-08-01 17:15:30 +0530175static int drm_proc_remove_files(struct drm_info_list *files, int count,
Ben Gamari955b12d2009-02-17 20:08:49 -0500176 struct drm_minor *minor)
177{
178 struct list_head *pos, *q;
179 struct drm_info_node *tmp;
180 int i;
181
182 for (i = 0; i < count; i++) {
183 list_for_each_safe(pos, q, &minor->proc_nodes.list) {
184 tmp = list_entry(pos, struct drm_info_node, list);
185 if (tmp->info_ent == &files[i]) {
186 remove_proc_entry(files[i].name,
187 minor->proc_root);
188 list_del(pos);
Eric Anholt9a298b22009-03-24 12:23:04 -0700189 kfree(tmp);
Ben Gamari955b12d2009-02-17 20:08:49 -0500190 }
191 }
192 }
193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/**
197 * Cleanup the proc filesystem resources.
198 *
199 * \param minor device minor number.
200 * \param root DRI proc dir entry.
201 * \param dev_root DRI device proc dir entry.
202 * \return always zero.
203 *
204 * Remove all proc entries created by proc_init().
205 */
Dave Airlie2c14f282008-04-21 16:47:32 +1000206int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 char name[64];
209
Ben Gamari955b12d2009-02-17 20:08:49 -0500210 if (!root || !minor->proc_root)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Ben Gamari955b12d2009-02-17 20:08:49 -0500213 drm_proc_remove_files(drm_proc_list, DRM_PROC_ENTRIES, minor);
214
Dave Airlie2c14f282008-04-21 16:47:32 +1000215 sprintf(name, "%d", minor->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 remove_proc_entry(name, root);
217
218 return 0;
219}
220