drm_debugfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 <linux/slab.h>
  34. #include <linux/export.h>
  35. #include <drm/drmP.h>
  36. #include <drm/drm_edid.h>
  37. #if defined(CONFIG_DEBUG_FS)
  38. /***************************************************
  39. * Initialization, etc.
  40. **************************************************/
  41. static const struct drm_info_list drm_debugfs_list[] = {
  42. {"name", drm_name_info, 0},
  43. {"vm", drm_vm_info, 0},
  44. {"clients", drm_clients_info, 0},
  45. {"bufs", drm_bufs_info, 0},
  46. {"gem_names", drm_gem_name_info, DRIVER_GEM},
  47. #if DRM_DEBUG_CODE
  48. {"vma", drm_vma_info, 0},
  49. #endif
  50. };
  51. #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
  52. static int drm_debugfs_open(struct inode *inode, struct file *file)
  53. {
  54. struct drm_info_node *node = inode->i_private;
  55. return single_open(file, node->info_ent->show, node);
  56. }
  57. static const struct file_operations drm_debugfs_fops = {
  58. .owner = THIS_MODULE,
  59. .open = drm_debugfs_open,
  60. .read = seq_read,
  61. .llseek = seq_lseek,
  62. .release = single_release,
  63. };
  64. /**
  65. * Initialize a given set of debugfs files for a device
  66. *
  67. * \param files The array of files to create
  68. * \param count The number of files given
  69. * \param root DRI debugfs dir entry.
  70. * \param minor device minor number
  71. * \return Zero on success, non-zero on failure
  72. *
  73. * Create a given set of debugfs files represented by an array of
  74. * gdm_debugfs_lists in the given root directory.
  75. */
  76. int drm_debugfs_create_files(const struct drm_info_list *files, int count,
  77. struct dentry *root, struct drm_minor *minor)
  78. {
  79. struct drm_device *dev = minor->dev;
  80. struct dentry *ent;
  81. struct drm_info_node *tmp;
  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 = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
  89. if (tmp == NULL) {
  90. ret = -1;
  91. goto fail;
  92. }
  93. ent = debugfs_create_file(files[i].name, S_IFREG | S_IRUGO,
  94. root, tmp, &drm_debugfs_fops);
  95. if (!ent) {
  96. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s/%s\n",
  97. root->d_name.name, files[i].name);
  98. kfree(tmp);
  99. ret = -1;
  100. goto fail;
  101. }
  102. tmp->minor = minor;
  103. tmp->dent = ent;
  104. tmp->info_ent = &files[i];
  105. mutex_lock(&minor->debugfs_lock);
  106. list_add(&tmp->list, &minor->debugfs_list);
  107. mutex_unlock(&minor->debugfs_lock);
  108. }
  109. return 0;
  110. fail:
  111. drm_debugfs_remove_files(files, count, minor);
  112. return ret;
  113. }
  114. EXPORT_SYMBOL(drm_debugfs_create_files);
  115. /**
  116. * Initialize the DRI debugfs filesystem for a device
  117. *
  118. * \param dev DRM device
  119. * \param minor device minor number
  120. * \param root DRI debugfs dir entry.
  121. *
  122. * Create the DRI debugfs root entry "/sys/kernel/debug/dri", the device debugfs root entry
  123. * "/sys/kernel/debug/dri/%minor%/", and each entry in debugfs_list as
  124. * "/sys/kernel/debug/dri/%minor%/%name%".
  125. */
  126. int drm_debugfs_init(struct drm_minor *minor, int minor_id,
  127. struct dentry *root)
  128. {
  129. struct drm_device *dev = minor->dev;
  130. char name[64];
  131. int ret;
  132. INIT_LIST_HEAD(&minor->debugfs_list);
  133. mutex_init(&minor->debugfs_lock);
  134. sprintf(name, "%d", minor_id);
  135. minor->debugfs_root = debugfs_create_dir(name, root);
  136. if (!minor->debugfs_root) {
  137. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s\n", name);
  138. return -1;
  139. }
  140. ret = drm_debugfs_create_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES,
  141. minor->debugfs_root, minor);
  142. if (ret) {
  143. debugfs_remove(minor->debugfs_root);
  144. minor->debugfs_root = NULL;
  145. DRM_ERROR("Failed to create core drm debugfs files\n");
  146. return ret;
  147. }
  148. if (dev->driver->debugfs_init) {
  149. ret = dev->driver->debugfs_init(minor);
  150. if (ret) {
  151. DRM_ERROR("DRM: Driver failed to initialize "
  152. "/sys/kernel/debug/dri.\n");
  153. return ret;
  154. }
  155. }
  156. return 0;
  157. }
  158. /**
  159. * Remove a list of debugfs files
  160. *
  161. * \param files The list of files
  162. * \param count The number of files
  163. * \param minor The minor of which we should remove the files
  164. * \return always zero.
  165. *
  166. * Remove all debugfs entries created by debugfs_init().
  167. */
  168. int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
  169. struct drm_minor *minor)
  170. {
  171. struct list_head *pos, *q;
  172. struct drm_info_node *tmp;
  173. int i;
  174. mutex_lock(&minor->debugfs_lock);
  175. for (i = 0; i < count; i++) {
  176. list_for_each_safe(pos, q, &minor->debugfs_list) {
  177. tmp = list_entry(pos, struct drm_info_node, list);
  178. if (tmp->info_ent == &files[i]) {
  179. debugfs_remove(tmp->dent);
  180. list_del(pos);
  181. kfree(tmp);
  182. }
  183. }
  184. }
  185. mutex_unlock(&minor->debugfs_lock);
  186. return 0;
  187. }
  188. EXPORT_SYMBOL(drm_debugfs_remove_files);
  189. /**
  190. * Cleanup the debugfs filesystem resources.
  191. *
  192. * \param minor device minor number.
  193. * \return always zero.
  194. *
  195. * Remove all debugfs entries created by debugfs_init().
  196. */
  197. int drm_debugfs_cleanup(struct drm_minor *minor)
  198. {
  199. struct drm_device *dev = minor->dev;
  200. if (!minor->debugfs_root)
  201. return 0;
  202. if (dev->driver->debugfs_cleanup)
  203. dev->driver->debugfs_cleanup(minor);
  204. drm_debugfs_remove_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES, minor);
  205. debugfs_remove(minor->debugfs_root);
  206. minor->debugfs_root = NULL;
  207. return 0;
  208. }
  209. static int connector_show(struct seq_file *m, void *data)
  210. {
  211. struct drm_connector *connector = m->private;
  212. const char *status;
  213. switch (connector->force) {
  214. case DRM_FORCE_ON:
  215. status = "on\n";
  216. break;
  217. case DRM_FORCE_ON_DIGITAL:
  218. status = "digital\n";
  219. break;
  220. case DRM_FORCE_OFF:
  221. status = "off\n";
  222. break;
  223. case DRM_FORCE_UNSPECIFIED:
  224. status = "unspecified\n";
  225. break;
  226. default:
  227. return 0;
  228. }
  229. seq_puts(m, status);
  230. return 0;
  231. }
  232. static int connector_open(struct inode *inode, struct file *file)
  233. {
  234. struct drm_connector *dev = inode->i_private;
  235. return single_open(file, connector_show, dev);
  236. }
  237. static ssize_t connector_write(struct file *file, const char __user *ubuf,
  238. size_t len, loff_t *offp)
  239. {
  240. struct seq_file *m = file->private_data;
  241. struct drm_connector *connector = m->private;
  242. char buf[12];
  243. if (len > sizeof(buf) - 1)
  244. return -EINVAL;
  245. if (copy_from_user(buf, ubuf, len))
  246. return -EFAULT;
  247. buf[len] = '\0';
  248. if (!strcmp(buf, "on"))
  249. connector->force = DRM_FORCE_ON;
  250. else if (!strcmp(buf, "digital"))
  251. connector->force = DRM_FORCE_ON_DIGITAL;
  252. else if (!strcmp(buf, "off"))
  253. connector->force = DRM_FORCE_OFF;
  254. else if (!strcmp(buf, "unspecified"))
  255. connector->force = DRM_FORCE_UNSPECIFIED;
  256. else
  257. return -EINVAL;
  258. return len;
  259. }
  260. static int edid_show(struct seq_file *m, void *data)
  261. {
  262. struct drm_connector *connector = m->private;
  263. struct drm_property_blob *edid = connector->edid_blob_ptr;
  264. if (connector->override_edid && edid)
  265. seq_write(m, edid->data, edid->length);
  266. return 0;
  267. }
  268. static int edid_open(struct inode *inode, struct file *file)
  269. {
  270. struct drm_connector *dev = inode->i_private;
  271. return single_open(file, edid_show, dev);
  272. }
  273. static ssize_t edid_write(struct file *file, const char __user *ubuf,
  274. size_t len, loff_t *offp)
  275. {
  276. struct seq_file *m = file->private_data;
  277. struct drm_connector *connector = m->private;
  278. char *buf;
  279. struct edid *edid;
  280. int ret;
  281. buf = memdup_user(ubuf, len);
  282. if (IS_ERR(buf))
  283. return PTR_ERR(buf);
  284. edid = (struct edid *) buf;
  285. if (len == 5 && !strncmp(buf, "reset", 5)) {
  286. connector->override_edid = false;
  287. ret = drm_mode_connector_update_edid_property(connector, NULL);
  288. } else if (len < EDID_LENGTH ||
  289. EDID_LENGTH * (1 + edid->extensions) > len)
  290. ret = -EINVAL;
  291. else {
  292. connector->override_edid = false;
  293. ret = drm_mode_connector_update_edid_property(connector, edid);
  294. if (!ret)
  295. connector->override_edid = true;
  296. }
  297. kfree(buf);
  298. return (ret) ? ret : len;
  299. }
  300. static const struct file_operations drm_edid_fops = {
  301. .owner = THIS_MODULE,
  302. .open = edid_open,
  303. .read = seq_read,
  304. .llseek = seq_lseek,
  305. .release = single_release,
  306. .write = edid_write
  307. };
  308. static const struct file_operations drm_connector_fops = {
  309. .owner = THIS_MODULE,
  310. .open = connector_open,
  311. .read = seq_read,
  312. .llseek = seq_lseek,
  313. .release = single_release,
  314. .write = connector_write
  315. };
  316. int drm_debugfs_connector_add(struct drm_connector *connector)
  317. {
  318. struct drm_minor *minor = connector->dev->primary;
  319. struct dentry *root, *ent;
  320. if (!minor->debugfs_root)
  321. return -1;
  322. root = debugfs_create_dir(connector->name, minor->debugfs_root);
  323. if (!root)
  324. return -ENOMEM;
  325. connector->debugfs_entry = root;
  326. /* force */
  327. ent = debugfs_create_file("force", S_IRUGO | S_IWUSR, root, connector,
  328. &drm_connector_fops);
  329. if (!ent)
  330. goto error;
  331. /* edid */
  332. ent = debugfs_create_file("edid_override", S_IRUGO | S_IWUSR, root,
  333. connector, &drm_edid_fops);
  334. if (!ent)
  335. goto error;
  336. return 0;
  337. error:
  338. debugfs_remove_recursive(connector->debugfs_entry);
  339. connector->debugfs_entry = NULL;
  340. return -ENOMEM;
  341. }
  342. void drm_debugfs_connector_remove(struct drm_connector *connector)
  343. {
  344. if (!connector->debugfs_entry)
  345. return;
  346. debugfs_remove_recursive(connector->debugfs_entry);
  347. connector->debugfs_entry = NULL;
  348. }
  349. #endif /* CONFIG_DEBUG_FS */