Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 2 | * \file drm_proc.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 40 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 41 | #include <linux/slab.h> |
Paul Gortmaker | 2d1a8a4 | 2011-08-30 18:16:33 -0400 | [diff] [blame] | 42 | #include <linux/export.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 43 | #include <drm/drmP.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 45 | /*************************************************** |
| 46 | * Initialization, etc. |
| 47 | **************************************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Proc file list. |
| 51 | */ |
David Howells | ce089b5 | 2013-04-12 15:23:25 +0100 | [diff] [blame] | 52 | static const struct drm_info_list drm_proc_list[] = { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 53 | {"name", drm_name_info, 0}, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 54 | {"vm", drm_vm_info, 0}, |
| 55 | {"clients", drm_clients_info, 0}, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 56 | {"bufs", drm_bufs_info, 0}, |
| 57 | {"gem_names", drm_gem_name_info, DRIVER_GEM}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #if DRM_DEBUG_CODE |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 59 | {"vma", drm_vma_info, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
| 61 | }; |
Ahmed S. Darwish | 8311d57 | 2007-02-09 10:30:10 +1100 | [diff] [blame] | 62 | #define DRM_PROC_ENTRIES ARRAY_SIZE(drm_proc_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 64 | static int drm_proc_open(struct inode *inode, struct file *file) |
| 65 | { |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 66 | struct drm_info_node* node = PDE_DATA(inode); |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 67 | |
| 68 | return single_open(file, node->info_ent->show, node); |
| 69 | } |
| 70 | |
| 71 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /** |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 81 | * Initialize a given set of proc files for a device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 83 | * \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 | */ |
David Howells | ce089b5 | 2013-04-12 15:23:25 +0100 | [diff] [blame] | 92 | static int drm_proc_create_files(const struct drm_info_list *files, int count, |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 93 | 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; |
David Howells | 8bc742e | 2013-04-12 16:15:07 +0100 | [diff] [blame] | 98 | int i; |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 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 Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 107 | tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL); |
David Howells | 8bc742e | 2013-04-12 16:15:07 +0100 | [diff] [blame] | 108 | if (!tmp) |
| 109 | return -1; |
| 110 | |
Alexey Dobriyan | 3b51096 | 2009-08-28 22:58:07 +0400 | [diff] [blame] | 111 | tmp->minor = minor; |
| 112 | tmp->info_ent = &files[i]; |
| 113 | list_add(&tmp->list, &minor->proc_nodes.list); |
| 114 | |
| 115 | ent = proc_create_data(files[i].name, S_IRUGO, root, |
| 116 | &drm_proc_fops, tmp); |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 117 | if (!ent) { |
David Howells | b63e6aa | 2013-04-12 15:34:31 +0100 | [diff] [blame] | 118 | DRM_ERROR("Cannot create /proc/dri/%u/%s\n", |
| 119 | minor->index, files[i].name); |
Alexey Dobriyan | 3b51096 | 2009-08-28 22:58:07 +0400 | [diff] [blame] | 120 | list_del(&tmp->list); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 121 | kfree(tmp); |
David Howells | 8bc742e | 2013-04-12 16:15:07 +0100 | [diff] [blame] | 122 | return -1; |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 123 | } |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 124 | } |
| 125 | return 0; |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Initialize the DRI proc filesystem for a device |
| 130 | * |
| 131 | * \param dev DRM device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | * \param root DRI proc dir entry. |
| 133 | * \param dev_root resulting DRI device proc dir entry. |
| 134 | * \return root entry pointer on success, or NULL on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 135 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | * Create the DRI proc root entry "/proc/dri", the device proc root entry |
| 137 | * "/proc/dri/%minor%/", and each entry in proc_list as |
| 138 | * "/proc/dri/%minor%/%name%". |
| 139 | */ |
David Howells | b63e6aa | 2013-04-12 15:34:31 +0100 | [diff] [blame] | 140 | int drm_proc_init(struct drm_minor *minor, struct proc_dir_entry *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
David Howells | b63e6aa | 2013-04-12 15:34:31 +0100 | [diff] [blame] | 142 | char name[12]; |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 143 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 145 | INIT_LIST_HEAD(&minor->proc_nodes.list); |
David Howells | b63e6aa | 2013-04-12 15:34:31 +0100 | [diff] [blame] | 146 | sprintf(name, "%u", minor->index); |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 147 | minor->proc_root = proc_mkdir(name, root); |
| 148 | if (!minor->proc_root) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | DRM_ERROR("Cannot create /proc/dri/%s\n", name); |
| 150 | return -1; |
| 151 | } |
| 152 | |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 153 | ret = drm_proc_create_files(drm_proc_list, DRM_PROC_ENTRIES, |
| 154 | minor->proc_root, minor); |
| 155 | if (ret) { |
David Howells | 8bc742e | 2013-04-12 16:15:07 +0100 | [diff] [blame] | 156 | remove_proc_subtree(name, root); |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 157 | minor->proc_root = NULL; |
| 158 | DRM_ERROR("Failed to create core drm proc files\n"); |
| 159 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | return 0; |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 163 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 164 | |
David Howells | ce089b5 | 2013-04-12 15:23:25 +0100 | [diff] [blame] | 165 | static int drm_proc_remove_files(const struct drm_info_list *files, int count, |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 166 | struct drm_minor *minor) |
| 167 | { |
| 168 | struct list_head *pos, *q; |
| 169 | struct drm_info_node *tmp; |
| 170 | int i; |
| 171 | |
| 172 | for (i = 0; i < count; i++) { |
| 173 | list_for_each_safe(pos, q, &minor->proc_nodes.list) { |
| 174 | tmp = list_entry(pos, struct drm_info_node, list); |
| 175 | if (tmp->info_ent == &files[i]) { |
| 176 | remove_proc_entry(files[i].name, |
| 177 | minor->proc_root); |
| 178 | list_del(pos); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 179 | kfree(tmp); |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Cleanup the proc filesystem resources. |
| 188 | * |
| 189 | * \param minor device minor number. |
| 190 | * \param root DRI proc dir entry. |
| 191 | * \param dev_root DRI device proc dir entry. |
| 192 | * \return always zero. |
| 193 | * |
| 194 | * Remove all proc entries created by proc_init(). |
| 195 | */ |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 196 | int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | char name[64]; |
| 199 | |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 200 | if (!root || !minor->proc_root) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 201 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Ben Gamari | 955b12d | 2009-02-17 20:08:49 -0500 | [diff] [blame] | 203 | drm_proc_remove_files(drm_proc_list, DRM_PROC_ENTRIES, minor); |
| 204 | |
Dave Airlie | 2c14f28 | 2008-04-21 16:47:32 +1000 | [diff] [blame] | 205 | sprintf(name, "%d", minor->index); |
David Howells | 8bc742e | 2013-04-12 16:15:07 +0100 | [diff] [blame] | 206 | remove_proc_subtree(name, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |