drm_proc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "drmP.h"
  40. /***************************************************
  41. * Initialization, etc.
  42. **************************************************/
  43. /**
  44. * Proc file list.
  45. */
  46. static struct drm_info_list drm_proc_list[] = {
  47. {"name", drm_name_info, 0},
  48. {"vm", drm_vm_info, 0},
  49. {"clients", drm_clients_info, 0},
  50. {"queues", drm_queues_info, 0},
  51. {"bufs", drm_bufs_info, 0},
  52. {"gem_names", drm_gem_name_info, DRIVER_GEM},
  53. {"gem_objects", drm_gem_object_info, DRIVER_GEM},
  54. #if DRM_DEBUG_CODE
  55. {"vma", drm_vma_info, 0},
  56. #endif
  57. };
  58. #define DRM_PROC_ENTRIES ARRAY_SIZE(drm_proc_list)
  59. static int drm_proc_open(struct inode *inode, struct file *file)
  60. {
  61. struct drm_info_node* node = PDE(inode)->data;
  62. return single_open(file, node->info_ent->show, node);
  63. }
  64. static const struct file_operations drm_proc_fops = {
  65. .owner = THIS_MODULE,
  66. .open = drm_proc_open,
  67. .read = seq_read,
  68. .llseek = seq_lseek,
  69. .release = single_release,
  70. };
  71. /**
  72. * Initialize a given set of proc files for a device
  73. *
  74. * \param files The array of files to create
  75. * \param count The number of files given
  76. * \param root DRI proc dir entry.
  77. * \param minor device minor number
  78. * \return Zero on success, non-zero on failure
  79. *
  80. * Create a given set of proc files represented by an array of
  81. * gdm_proc_lists in the given root directory.
  82. */
  83. int drm_proc_create_files(struct drm_info_list *files, int count,
  84. struct proc_dir_entry *root, struct drm_minor *minor)
  85. {
  86. struct drm_device *dev = minor->dev;
  87. struct proc_dir_entry *ent;
  88. struct drm_info_node *tmp;
  89. char name[64];
  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 = drm_alloc(sizeof(struct drm_info_node), _DRM_DRIVER);
  97. ent = create_proc_entry(files[i].name, S_IFREG | S_IRUGO, root);
  98. if (!ent) {
  99. DRM_ERROR("Cannot create /proc/dri/%s/%s\n",
  100. name, files[i].name);
  101. drm_free(tmp, sizeof(struct drm_info_node),
  102. _DRM_DRIVER);
  103. ret = -1;
  104. goto fail;
  105. }
  106. ent->proc_fops = &drm_proc_fops;
  107. ent->data = tmp;
  108. tmp->minor = minor;
  109. tmp->info_ent = &files[i];
  110. list_add(&(tmp->list), &(minor->proc_nodes.list));
  111. }
  112. return 0;
  113. fail:
  114. for (i = 0; i < count; i++)
  115. remove_proc_entry(drm_proc_list[i].name, minor->proc_root);
  116. return ret;
  117. }
  118. /**
  119. * Initialize the DRI proc filesystem for a device
  120. *
  121. * \param dev DRM device
  122. * \param minor device minor number
  123. * \param root DRI proc dir entry.
  124. * \param dev_root resulting DRI device proc dir entry.
  125. * \return root entry pointer on success, or NULL on failure.
  126. *
  127. * Create the DRI proc root entry "/proc/dri", the device proc root entry
  128. * "/proc/dri/%minor%/", and each entry in proc_list as
  129. * "/proc/dri/%minor%/%name%".
  130. */
  131. int drm_proc_init(struct drm_minor *minor, int minor_id,
  132. struct proc_dir_entry *root)
  133. {
  134. struct drm_device *dev = minor->dev;
  135. char name[64];
  136. int ret;
  137. INIT_LIST_HEAD(&minor->proc_nodes.list);
  138. sprintf(name, "%d", minor_id);
  139. minor->proc_root = proc_mkdir(name, root);
  140. if (!minor->proc_root) {
  141. DRM_ERROR("Cannot create /proc/dri/%s\n", name);
  142. return -1;
  143. }
  144. ret = drm_proc_create_files(drm_proc_list, DRM_PROC_ENTRIES,
  145. minor->proc_root, minor);
  146. if (ret) {
  147. remove_proc_entry(name, root);
  148. minor->proc_root = NULL;
  149. DRM_ERROR("Failed to create core drm proc files\n");
  150. return ret;
  151. }
  152. if (dev->driver->proc_init) {
  153. ret = dev->driver->proc_init(minor);
  154. if (ret) {
  155. DRM_ERROR("DRM: Driver failed to initialize "
  156. "/proc/dri.\n");
  157. return ret;
  158. }
  159. }
  160. return 0;
  161. }
  162. int drm_proc_remove_files(struct drm_info_list *files, int count,
  163. struct drm_minor *minor)
  164. {
  165. struct list_head *pos, *q;
  166. struct drm_info_node *tmp;
  167. int i;
  168. for (i = 0; i < count; i++) {
  169. list_for_each_safe(pos, q, &minor->proc_nodes.list) {
  170. tmp = list_entry(pos, struct drm_info_node, list);
  171. if (tmp->info_ent == &files[i]) {
  172. remove_proc_entry(files[i].name,
  173. minor->proc_root);
  174. list_del(pos);
  175. drm_free(tmp, sizeof(struct drm_info_node),
  176. _DRM_DRIVER);
  177. }
  178. }
  179. }
  180. return 0;
  181. }
  182. /**
  183. * Cleanup the proc filesystem resources.
  184. *
  185. * \param minor device minor number.
  186. * \param root DRI proc dir entry.
  187. * \param dev_root DRI device proc dir entry.
  188. * \return always zero.
  189. *
  190. * Remove all proc entries created by proc_init().
  191. */
  192. int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root)
  193. {
  194. struct drm_device *dev = minor->dev;
  195. char name[64];
  196. if (!root || !minor->proc_root)
  197. return 0;
  198. if (dev->driver->proc_cleanup)
  199. dev->driver->proc_cleanup(minor);
  200. drm_proc_remove_files(drm_proc_list, DRM_PROC_ENTRIES, minor);
  201. sprintf(name, "%d", minor->index);
  202. remove_proc_entry(name, root);
  203. return 0;
  204. }