drm_debugfs.c 11 KB

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