drm_proc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
  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. kfree(tmp);
  102. ret = -1;
  103. goto fail;
  104. }
  105. ent->proc_fops = &drm_proc_fops;
  106. ent->data = tmp;
  107. tmp->minor = minor;
  108. tmp->info_ent = &files[i];
  109. list_add(&(tmp->list), &(minor->proc_nodes.list));
  110. }
  111. return 0;
  112. fail:
  113. for (i = 0; i < count; i++)
  114. remove_proc_entry(drm_proc_list[i].name, minor->proc_root);
  115. return ret;
  116. }
  117. /**
  118. * Initialize the DRI proc filesystem for a device
  119. *
  120. * \param dev DRM device
  121. * \param minor device minor number
  122. * \param root DRI proc dir entry.
  123. * \param dev_root resulting DRI device proc dir entry.
  124. * \return root entry pointer on success, or NULL on failure.
  125. *
  126. * Create the DRI proc root entry "/proc/dri", the device proc root entry
  127. * "/proc/dri/%minor%/", and each entry in proc_list as
  128. * "/proc/dri/%minor%/%name%".
  129. */
  130. int drm_proc_init(struct drm_minor *minor, int minor_id,
  131. struct proc_dir_entry *root)
  132. {
  133. struct drm_device *dev = minor->dev;
  134. char name[64];
  135. int ret;
  136. INIT_LIST_HEAD(&minor->proc_nodes.list);
  137. sprintf(name, "%d", minor_id);
  138. minor->proc_root = proc_mkdir(name, root);
  139. if (!minor->proc_root) {
  140. DRM_ERROR("Cannot create /proc/dri/%s\n", name);
  141. return -1;
  142. }
  143. ret = drm_proc_create_files(drm_proc_list, DRM_PROC_ENTRIES,
  144. minor->proc_root, minor);
  145. if (ret) {
  146. remove_proc_entry(name, root);
  147. minor->proc_root = NULL;
  148. DRM_ERROR("Failed to create core drm proc files\n");
  149. return ret;
  150. }
  151. if (dev->driver->proc_init) {
  152. ret = dev->driver->proc_init(minor);
  153. if (ret) {
  154. DRM_ERROR("DRM: Driver failed to initialize "
  155. "/proc/dri.\n");
  156. return ret;
  157. }
  158. }
  159. return 0;
  160. }
  161. int drm_proc_remove_files(struct drm_info_list *files, int count,
  162. struct drm_minor *minor)
  163. {
  164. struct list_head *pos, *q;
  165. struct drm_info_node *tmp;
  166. int i;
  167. for (i = 0; i < count; i++) {
  168. list_for_each_safe(pos, q, &minor->proc_nodes.list) {
  169. tmp = list_entry(pos, struct drm_info_node, list);
  170. if (tmp->info_ent == &files[i]) {
  171. remove_proc_entry(files[i].name,
  172. minor->proc_root);
  173. list_del(pos);
  174. kfree(tmp);
  175. }
  176. }
  177. }
  178. return 0;
  179. }
  180. /**
  181. * Cleanup the proc filesystem resources.
  182. *
  183. * \param minor device minor number.
  184. * \param root DRI proc dir entry.
  185. * \param dev_root DRI device proc dir entry.
  186. * \return always zero.
  187. *
  188. * Remove all proc entries created by proc_init().
  189. */
  190. int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root)
  191. {
  192. struct drm_device *dev = minor->dev;
  193. char name[64];
  194. if (!root || !minor->proc_root)
  195. return 0;
  196. if (dev->driver->proc_cleanup)
  197. dev->driver->proc_cleanup(minor);
  198. drm_proc_remove_files(drm_proc_list, DRM_PROC_ENTRIES, minor);
  199. sprintf(name, "%d", minor->index);
  200. remove_proc_entry(name, root);
  201. return 0;
  202. }