drm_debugfs.c 11 KB

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