drm_debugfs.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * \file drm_debugfs.c
  3. * debugfs support for DRM
  4. *
  5. * \author Ben Gamari <bgamari@gmail.com>
  6. */
  7. /*
  8. * Created: Sun Dec 21 13:08:50 2008 by bgamari@gmail.com
  9. *
  10. * Copyright 2008 Ben Gamari <bgamari@gmail.com>
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice (including the next
  20. * paragraph) shall be included in all copies or substantial portions of the
  21. * Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  27. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  28. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  29. * OTHER DEALINGS IN THE SOFTWARE.
  30. */
  31. #include <linux/debugfs.h>
  32. #include <linux/seq_file.h>
  33. #include "drmP.h"
  34. #if defined(CONFIG_DEBUG_FS)
  35. /***************************************************
  36. * Initialization, etc.
  37. **************************************************/
  38. static struct drm_info_list drm_debugfs_list[] = {
  39. {"name", drm_name_info, 0},
  40. {"vm", drm_vm_info, 0},
  41. {"clients", drm_clients_info, 0},
  42. {"queues", drm_queues_info, 0},
  43. {"bufs", drm_bufs_info, 0},
  44. {"gem_names", drm_gem_name_info, DRIVER_GEM},
  45. {"gem_objects", drm_gem_object_info, DRIVER_GEM},
  46. #if DRM_DEBUG_CODE
  47. {"vma", drm_vma_info, 0},
  48. #endif
  49. };
  50. #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
  51. static int drm_debugfs_open(struct inode *inode, struct file *file)
  52. {
  53. struct drm_info_node *node = inode->i_private;
  54. return single_open(file, node->info_ent->show, node);
  55. }
  56. static const struct file_operations drm_debugfs_fops = {
  57. .owner = THIS_MODULE,
  58. .open = drm_debugfs_open,
  59. .read = seq_read,
  60. .llseek = seq_lseek,
  61. .release = single_release,
  62. };
  63. /**
  64. * Initialize a given set of debugfs files for a device
  65. *
  66. * \param files The array of files to create
  67. * \param count The number of files given
  68. * \param root DRI debugfs dir entry.
  69. * \param minor device minor number
  70. * \return Zero on success, non-zero on failure
  71. *
  72. * Create a given set of debugfs files represented by an array of
  73. * gdm_debugfs_lists in the given root directory.
  74. */
  75. int drm_debugfs_create_files(struct drm_info_list *files, int count,
  76. struct dentry *root, struct drm_minor *minor)
  77. {
  78. struct drm_device *dev = minor->dev;
  79. struct dentry *ent;
  80. struct drm_info_node *tmp;
  81. char name[64];
  82. int i, ret;
  83. for (i = 0; i < count; i++) {
  84. u32 features = files[i].driver_features;
  85. if (features != 0 &&
  86. (dev->driver->driver_features & features) != features)
  87. continue;
  88. tmp = drm_alloc(sizeof(struct drm_info_node),
  89. _DRM_DRIVER);
  90. ent = debugfs_create_file(files[i].name, S_IFREG | S_IRUGO,
  91. root, tmp, &drm_debugfs_fops);
  92. if (!ent) {
  93. DRM_ERROR("Cannot create /debugfs/dri/%s/%s\n",
  94. name, files[i].name);
  95. drm_free(tmp, sizeof(struct drm_info_node),
  96. _DRM_DRIVER);
  97. ret = -1;
  98. goto fail;
  99. }
  100. tmp->minor = minor;
  101. tmp->dent = ent;
  102. tmp->info_ent = &files[i];
  103. list_add(&(tmp->list), &(minor->debugfs_nodes.list));
  104. }
  105. return 0;
  106. fail:
  107. drm_debugfs_remove_files(files, count, minor);
  108. return ret;
  109. }
  110. EXPORT_SYMBOL(drm_debugfs_create_files);
  111. /**
  112. * Initialize the DRI debugfs filesystem for a device
  113. *
  114. * \param dev DRM device
  115. * \param minor device minor number
  116. * \param root DRI debugfs dir entry.
  117. *
  118. * Create the DRI debugfs root entry "/debugfs/dri", the device debugfs root entry
  119. * "/debugfs/dri/%minor%/", and each entry in debugfs_list as
  120. * "/debugfs/dri/%minor%/%name%".
  121. */
  122. int drm_debugfs_init(struct drm_minor *minor, int minor_id,
  123. struct dentry *root)
  124. {
  125. struct drm_device *dev = minor->dev;
  126. char name[64];
  127. int ret;
  128. INIT_LIST_HEAD(&minor->debugfs_nodes.list);
  129. sprintf(name, "%d", minor_id);
  130. minor->debugfs_root = debugfs_create_dir(name, root);
  131. if (!minor->debugfs_root) {
  132. DRM_ERROR("Cannot create /debugfs/dri/%s\n", name);
  133. return -1;
  134. }
  135. ret = drm_debugfs_create_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES,
  136. minor->debugfs_root, minor);
  137. if (ret) {
  138. debugfs_remove(minor->debugfs_root);
  139. minor->debugfs_root = NULL;
  140. DRM_ERROR("Failed to create core drm debugfs files\n");
  141. return ret;
  142. }
  143. if (dev->driver->debugfs_init) {
  144. ret = dev->driver->debugfs_init(minor);
  145. if (ret) {
  146. DRM_ERROR("DRM: Driver failed to initialize "
  147. "/debugfs/dri.\n");
  148. return ret;
  149. }
  150. }
  151. return 0;
  152. }
  153. /**
  154. * Remove a list of debugfs files
  155. *
  156. * \param files The list of files
  157. * \param count The number of files
  158. * \param minor The minor of which we should remove the files
  159. * \return always zero.
  160. *
  161. * Remove all debugfs entries created by debugfs_init().
  162. */
  163. int drm_debugfs_remove_files(struct drm_info_list *files, int count,
  164. struct drm_minor *minor)
  165. {
  166. struct list_head *pos, *q;
  167. struct drm_info_node *tmp;
  168. int i;
  169. for (i = 0; i < count; i++) {
  170. list_for_each_safe(pos, q, &minor->debugfs_nodes.list) {
  171. tmp = list_entry(pos, struct drm_info_node, list);
  172. if (tmp->info_ent == &files[i]) {
  173. debugfs_remove(tmp->dent);
  174. list_del(pos);
  175. drm_free(tmp, sizeof(struct drm_info_node),
  176. _DRM_DRIVER);
  177. }
  178. }
  179. }
  180. return 0;
  181. }
  182. EXPORT_SYMBOL(drm_debugfs_remove_files);
  183. /**
  184. * Cleanup the debugfs filesystem resources.
  185. *
  186. * \param minor device minor number.
  187. * \return always zero.
  188. *
  189. * Remove all debugfs entries created by debugfs_init().
  190. */
  191. int drm_debugfs_cleanup(struct drm_minor *minor)
  192. {
  193. struct drm_device *dev = minor->dev;
  194. if (!minor->debugfs_root)
  195. return 0;
  196. if (dev->driver->debugfs_cleanup)
  197. dev->driver->debugfs_cleanup(minor);
  198. drm_debugfs_remove_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES, minor);
  199. debugfs_remove(minor->debugfs_root);
  200. minor->debugfs_root = NULL;
  201. return 0;
  202. }
  203. #endif /* CONFIG_DEBUG_FS */