drm_proc.c 5.9 KB

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