drm_debugfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Created: Sun Dec 21 13:08:50 2008 by bgamari@gmail.com
  3. *
  4. * Copyright 2008 Ben Gamari <bgamari@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <linux/debugfs.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/slab.h>
  28. #include <linux/export.h>
  29. #include <drm/drm_debugfs.h>
  30. #include <drm/drm_edid.h>
  31. #include <drm/drm_atomic.h>
  32. #include <drm/drmP.h>
  33. #include "drm_internal.h"
  34. #include "drm_crtc_internal.h"
  35. #if defined(CONFIG_DEBUG_FS)
  36. /***************************************************
  37. * Initialization, etc.
  38. **************************************************/
  39. static const struct drm_info_list drm_debugfs_list[] = {
  40. {"name", drm_name_info, 0},
  41. {"clients", drm_clients_info, 0},
  42. {"gem_names", drm_gem_name_info, DRIVER_GEM},
  43. };
  44. #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
  45. static int drm_debugfs_open(struct inode *inode, struct file *file)
  46. {
  47. struct drm_info_node *node = inode->i_private;
  48. return single_open(file, node->info_ent->show, node);
  49. }
  50. static const struct file_operations drm_debugfs_fops = {
  51. .owner = THIS_MODULE,
  52. .open = drm_debugfs_open,
  53. .read = seq_read,
  54. .llseek = seq_lseek,
  55. .release = single_release,
  56. };
  57. /**
  58. * drm_debugfs_create_files - Initialize a given set of debugfs files for DRM
  59. * minor
  60. * @files: The array of files to create
  61. * @count: The number of files given
  62. * @root: DRI debugfs dir entry.
  63. * @minor: device minor number
  64. *
  65. * Create a given set of debugfs files represented by an array of
  66. * &struct drm_info_list in the given root directory. These files will be removed
  67. * automatically on drm_debugfs_cleanup().
  68. */
  69. int drm_debugfs_create_files(const struct drm_info_list *files, int count,
  70. struct dentry *root, struct drm_minor *minor)
  71. {
  72. struct drm_device *dev = minor->dev;
  73. struct dentry *ent;
  74. struct drm_info_node *tmp;
  75. int i, ret;
  76. for (i = 0; i < count; i++) {
  77. u32 features = files[i].driver_features;
  78. if (features != 0 &&
  79. (dev->driver->driver_features & features) != features)
  80. continue;
  81. tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
  82. if (tmp == NULL) {
  83. ret = -1;
  84. goto fail;
  85. }
  86. ent = debugfs_create_file(files[i].name, S_IFREG | S_IRUGO,
  87. root, tmp, &drm_debugfs_fops);
  88. if (!ent) {
  89. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/%s\n",
  90. root, files[i].name);
  91. kfree(tmp);
  92. ret = -1;
  93. goto fail;
  94. }
  95. tmp->minor = minor;
  96. tmp->dent = ent;
  97. tmp->info_ent = &files[i];
  98. mutex_lock(&minor->debugfs_lock);
  99. list_add(&tmp->list, &minor->debugfs_list);
  100. mutex_unlock(&minor->debugfs_lock);
  101. }
  102. return 0;
  103. fail:
  104. drm_debugfs_remove_files(files, count, minor);
  105. return ret;
  106. }
  107. EXPORT_SYMBOL(drm_debugfs_create_files);
  108. int drm_debugfs_init(struct drm_minor *minor, int minor_id,
  109. struct dentry *root)
  110. {
  111. struct drm_device *dev = minor->dev;
  112. char name[64];
  113. int ret;
  114. INIT_LIST_HEAD(&minor->debugfs_list);
  115. mutex_init(&minor->debugfs_lock);
  116. sprintf(name, "%d", minor_id);
  117. minor->debugfs_root = debugfs_create_dir(name, root);
  118. if (!minor->debugfs_root) {
  119. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s\n", name);
  120. return -1;
  121. }
  122. ret = drm_debugfs_create_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES,
  123. minor->debugfs_root, minor);
  124. if (ret) {
  125. debugfs_remove(minor->debugfs_root);
  126. minor->debugfs_root = NULL;
  127. DRM_ERROR("Failed to create core drm debugfs files\n");
  128. return ret;
  129. }
  130. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  131. ret = drm_atomic_debugfs_init(minor);
  132. if (ret) {
  133. DRM_ERROR("Failed to create atomic debugfs files\n");
  134. return ret;
  135. }
  136. }
  137. ret = drm_framebuffer_debugfs_init(minor);
  138. if (ret) {
  139. DRM_ERROR("Failed to create framebuffer debugfs file\n");
  140. return ret;
  141. }
  142. if (dev->driver->debugfs_init) {
  143. ret = dev->driver->debugfs_init(minor);
  144. if (ret) {
  145. DRM_ERROR("DRM: Driver failed to initialize "
  146. "/sys/kernel/debug/dri.\n");
  147. return ret;
  148. }
  149. }
  150. return 0;
  151. }
  152. int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
  153. struct drm_minor *minor)
  154. {
  155. struct list_head *pos, *q;
  156. struct drm_info_node *tmp;
  157. int i;
  158. mutex_lock(&minor->debugfs_lock);
  159. for (i = 0; i < count; i++) {
  160. list_for_each_safe(pos, q, &minor->debugfs_list) {
  161. tmp = list_entry(pos, struct drm_info_node, list);
  162. if (tmp->info_ent == &files[i]) {
  163. debugfs_remove(tmp->dent);
  164. list_del(pos);
  165. kfree(tmp);
  166. }
  167. }
  168. }
  169. mutex_unlock(&minor->debugfs_lock);
  170. return 0;
  171. }
  172. EXPORT_SYMBOL(drm_debugfs_remove_files);
  173. static void drm_debugfs_remove_all_files(struct drm_minor *minor)
  174. {
  175. struct drm_info_node *node, *tmp;
  176. mutex_lock(&minor->debugfs_lock);
  177. list_for_each_entry_safe(node, tmp, &minor->debugfs_list, list) {
  178. debugfs_remove(node->dent);
  179. list_del(&node->list);
  180. kfree(node);
  181. }
  182. mutex_unlock(&minor->debugfs_lock);
  183. }
  184. int drm_debugfs_cleanup(struct drm_minor *minor)
  185. {
  186. if (!minor->debugfs_root)
  187. return 0;
  188. drm_debugfs_remove_all_files(minor);
  189. debugfs_remove_recursive(minor->debugfs_root);
  190. minor->debugfs_root = NULL;
  191. return 0;
  192. }
  193. static int connector_show(struct seq_file *m, void *data)
  194. {
  195. struct drm_connector *connector = m->private;
  196. seq_printf(m, "%s\n", drm_get_connector_force_name(connector->force));
  197. return 0;
  198. }
  199. static int connector_open(struct inode *inode, struct file *file)
  200. {
  201. struct drm_connector *dev = inode->i_private;
  202. return single_open(file, connector_show, dev);
  203. }
  204. static ssize_t connector_write(struct file *file, const char __user *ubuf,
  205. size_t len, loff_t *offp)
  206. {
  207. struct seq_file *m = file->private_data;
  208. struct drm_connector *connector = m->private;
  209. char buf[12];
  210. if (len > sizeof(buf) - 1)
  211. return -EINVAL;
  212. if (copy_from_user(buf, ubuf, len))
  213. return -EFAULT;
  214. buf[len] = '\0';
  215. if (!strcmp(buf, "on"))
  216. connector->force = DRM_FORCE_ON;
  217. else if (!strcmp(buf, "digital"))
  218. connector->force = DRM_FORCE_ON_DIGITAL;
  219. else if (!strcmp(buf, "off"))
  220. connector->force = DRM_FORCE_OFF;
  221. else if (!strcmp(buf, "unspecified"))
  222. connector->force = DRM_FORCE_UNSPECIFIED;
  223. else
  224. return -EINVAL;
  225. return len;
  226. }
  227. static int edid_show(struct seq_file *m, void *data)
  228. {
  229. struct drm_connector *connector = m->private;
  230. struct drm_property_blob *edid = connector->edid_blob_ptr;
  231. if (connector->override_edid && edid)
  232. seq_write(m, edid->data, edid->length);
  233. return 0;
  234. }
  235. static int edid_open(struct inode *inode, struct file *file)
  236. {
  237. struct drm_connector *dev = inode->i_private;
  238. return single_open(file, edid_show, dev);
  239. }
  240. static ssize_t edid_write(struct file *file, const char __user *ubuf,
  241. size_t len, loff_t *offp)
  242. {
  243. struct seq_file *m = file->private_data;
  244. struct drm_connector *connector = m->private;
  245. char *buf;
  246. struct edid *edid;
  247. int ret;
  248. buf = memdup_user(ubuf, len);
  249. if (IS_ERR(buf))
  250. return PTR_ERR(buf);
  251. edid = (struct edid *) buf;
  252. if (len == 5 && !strncmp(buf, "reset", 5)) {
  253. connector->override_edid = false;
  254. ret = drm_mode_connector_update_edid_property(connector, NULL);
  255. } else if (len < EDID_LENGTH ||
  256. EDID_LENGTH * (1 + edid->extensions) > len)
  257. ret = -EINVAL;
  258. else {
  259. connector->override_edid = false;
  260. ret = drm_mode_connector_update_edid_property(connector, edid);
  261. if (!ret)
  262. connector->override_edid = true;
  263. }
  264. kfree(buf);
  265. return (ret) ? ret : len;
  266. }
  267. static const struct file_operations drm_edid_fops = {
  268. .owner = THIS_MODULE,
  269. .open = edid_open,
  270. .read = seq_read,
  271. .llseek = seq_lseek,
  272. .release = single_release,
  273. .write = edid_write
  274. };
  275. static const struct file_operations drm_connector_fops = {
  276. .owner = THIS_MODULE,
  277. .open = connector_open,
  278. .read = seq_read,
  279. .llseek = seq_lseek,
  280. .release = single_release,
  281. .write = connector_write
  282. };
  283. int drm_debugfs_connector_add(struct drm_connector *connector)
  284. {
  285. struct drm_minor *minor = connector->dev->primary;
  286. struct dentry *root, *ent;
  287. if (!minor->debugfs_root)
  288. return -1;
  289. root = debugfs_create_dir(connector->name, minor->debugfs_root);
  290. if (!root)
  291. return -ENOMEM;
  292. connector->debugfs_entry = root;
  293. /* force */
  294. ent = debugfs_create_file("force", S_IRUGO | S_IWUSR, root, connector,
  295. &drm_connector_fops);
  296. if (!ent)
  297. goto error;
  298. /* edid */
  299. ent = debugfs_create_file("edid_override", S_IRUGO | S_IWUSR, root,
  300. connector, &drm_edid_fops);
  301. if (!ent)
  302. goto error;
  303. return 0;
  304. error:
  305. debugfs_remove_recursive(connector->debugfs_entry);
  306. connector->debugfs_entry = NULL;
  307. return -ENOMEM;
  308. }
  309. void drm_debugfs_connector_remove(struct drm_connector *connector)
  310. {
  311. if (!connector->debugfs_entry)
  312. return;
  313. debugfs_remove_recursive(connector->debugfs_entry);
  314. connector->debugfs_entry = NULL;
  315. }
  316. int drm_debugfs_crtc_add(struct drm_crtc *crtc)
  317. {
  318. struct drm_minor *minor = crtc->dev->primary;
  319. struct dentry *root;
  320. char *name;
  321. name = kasprintf(GFP_KERNEL, "crtc-%d", crtc->index);
  322. if (!name)
  323. return -ENOMEM;
  324. root = debugfs_create_dir(name, minor->debugfs_root);
  325. kfree(name);
  326. if (!root)
  327. return -ENOMEM;
  328. crtc->debugfs_entry = root;
  329. if (drm_debugfs_crtc_crc_add(crtc))
  330. goto error;
  331. return 0;
  332. error:
  333. drm_debugfs_crtc_remove(crtc);
  334. return -ENOMEM;
  335. }
  336. void drm_debugfs_crtc_remove(struct drm_crtc *crtc)
  337. {
  338. debugfs_remove_recursive(crtc->debugfs_entry);
  339. crtc->debugfs_entry = NULL;
  340. }
  341. #endif /* CONFIG_DEBUG_FS */