drm_debugfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  138. ret = drm_framebuffer_debugfs_init(minor);
  139. if (ret) {
  140. DRM_ERROR("Failed to create framebuffer debugfs file\n");
  141. return ret;
  142. }
  143. }
  144. if (dev->driver->debugfs_init) {
  145. ret = dev->driver->debugfs_init(minor);
  146. if (ret) {
  147. DRM_ERROR("DRM: Driver failed to initialize "
  148. "/sys/kernel/debug/dri.\n");
  149. return ret;
  150. }
  151. }
  152. return 0;
  153. }
  154. int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
  155. struct drm_minor *minor)
  156. {
  157. struct list_head *pos, *q;
  158. struct drm_info_node *tmp;
  159. int i;
  160. mutex_lock(&minor->debugfs_lock);
  161. for (i = 0; i < count; i++) {
  162. list_for_each_safe(pos, q, &minor->debugfs_list) {
  163. tmp = list_entry(pos, struct drm_info_node, list);
  164. if (tmp->info_ent == &files[i]) {
  165. debugfs_remove(tmp->dent);
  166. list_del(pos);
  167. kfree(tmp);
  168. }
  169. }
  170. }
  171. mutex_unlock(&minor->debugfs_lock);
  172. return 0;
  173. }
  174. EXPORT_SYMBOL(drm_debugfs_remove_files);
  175. static void drm_debugfs_remove_all_files(struct drm_minor *minor)
  176. {
  177. struct drm_info_node *node, *tmp;
  178. mutex_lock(&minor->debugfs_lock);
  179. list_for_each_entry_safe(node, tmp, &minor->debugfs_list, list) {
  180. debugfs_remove(node->dent);
  181. list_del(&node->list);
  182. kfree(node);
  183. }
  184. mutex_unlock(&minor->debugfs_lock);
  185. }
  186. int drm_debugfs_cleanup(struct drm_minor *minor)
  187. {
  188. if (!minor->debugfs_root)
  189. return 0;
  190. drm_debugfs_remove_all_files(minor);
  191. debugfs_remove_recursive(minor->debugfs_root);
  192. minor->debugfs_root = NULL;
  193. return 0;
  194. }
  195. static int connector_show(struct seq_file *m, void *data)
  196. {
  197. struct drm_connector *connector = m->private;
  198. seq_printf(m, "%s\n", drm_get_connector_force_name(connector->force));
  199. return 0;
  200. }
  201. static int connector_open(struct inode *inode, struct file *file)
  202. {
  203. struct drm_connector *dev = inode->i_private;
  204. return single_open(file, connector_show, dev);
  205. }
  206. static ssize_t connector_write(struct file *file, const char __user *ubuf,
  207. size_t len, loff_t *offp)
  208. {
  209. struct seq_file *m = file->private_data;
  210. struct drm_connector *connector = m->private;
  211. char buf[12];
  212. if (len > sizeof(buf) - 1)
  213. return -EINVAL;
  214. if (copy_from_user(buf, ubuf, len))
  215. return -EFAULT;
  216. buf[len] = '\0';
  217. if (!strcmp(buf, "on"))
  218. connector->force = DRM_FORCE_ON;
  219. else if (!strcmp(buf, "digital"))
  220. connector->force = DRM_FORCE_ON_DIGITAL;
  221. else if (!strcmp(buf, "off"))
  222. connector->force = DRM_FORCE_OFF;
  223. else if (!strcmp(buf, "unspecified"))
  224. connector->force = DRM_FORCE_UNSPECIFIED;
  225. else
  226. return -EINVAL;
  227. return len;
  228. }
  229. static int edid_show(struct seq_file *m, void *data)
  230. {
  231. struct drm_connector *connector = m->private;
  232. struct drm_property_blob *edid = connector->edid_blob_ptr;
  233. if (connector->override_edid && edid)
  234. seq_write(m, edid->data, edid->length);
  235. return 0;
  236. }
  237. static int edid_open(struct inode *inode, struct file *file)
  238. {
  239. struct drm_connector *dev = inode->i_private;
  240. return single_open(file, edid_show, dev);
  241. }
  242. static ssize_t edid_write(struct file *file, const char __user *ubuf,
  243. size_t len, loff_t *offp)
  244. {
  245. struct seq_file *m = file->private_data;
  246. struct drm_connector *connector = m->private;
  247. char *buf;
  248. struct edid *edid;
  249. int ret;
  250. buf = memdup_user(ubuf, len);
  251. if (IS_ERR(buf))
  252. return PTR_ERR(buf);
  253. edid = (struct edid *) buf;
  254. if (len == 5 && !strncmp(buf, "reset", 5)) {
  255. connector->override_edid = false;
  256. ret = drm_mode_connector_update_edid_property(connector, NULL);
  257. } else if (len < EDID_LENGTH ||
  258. EDID_LENGTH * (1 + edid->extensions) > len)
  259. ret = -EINVAL;
  260. else {
  261. connector->override_edid = false;
  262. ret = drm_mode_connector_update_edid_property(connector, edid);
  263. if (!ret)
  264. connector->override_edid = true;
  265. }
  266. kfree(buf);
  267. return (ret) ? ret : len;
  268. }
  269. static const struct file_operations drm_edid_fops = {
  270. .owner = THIS_MODULE,
  271. .open = edid_open,
  272. .read = seq_read,
  273. .llseek = seq_lseek,
  274. .release = single_release,
  275. .write = edid_write
  276. };
  277. static const struct file_operations drm_connector_fops = {
  278. .owner = THIS_MODULE,
  279. .open = connector_open,
  280. .read = seq_read,
  281. .llseek = seq_lseek,
  282. .release = single_release,
  283. .write = connector_write
  284. };
  285. int drm_debugfs_connector_add(struct drm_connector *connector)
  286. {
  287. struct drm_minor *minor = connector->dev->primary;
  288. struct dentry *root, *ent;
  289. if (!minor->debugfs_root)
  290. return -1;
  291. root = debugfs_create_dir(connector->name, minor->debugfs_root);
  292. if (!root)
  293. return -ENOMEM;
  294. connector->debugfs_entry = root;
  295. /* force */
  296. ent = debugfs_create_file("force", S_IRUGO | S_IWUSR, root, connector,
  297. &drm_connector_fops);
  298. if (!ent)
  299. goto error;
  300. /* edid */
  301. ent = debugfs_create_file("edid_override", S_IRUGO | S_IWUSR, root,
  302. connector, &drm_edid_fops);
  303. if (!ent)
  304. goto error;
  305. return 0;
  306. error:
  307. debugfs_remove_recursive(connector->debugfs_entry);
  308. connector->debugfs_entry = NULL;
  309. return -ENOMEM;
  310. }
  311. void drm_debugfs_connector_remove(struct drm_connector *connector)
  312. {
  313. if (!connector->debugfs_entry)
  314. return;
  315. debugfs_remove_recursive(connector->debugfs_entry);
  316. connector->debugfs_entry = NULL;
  317. }
  318. int drm_debugfs_crtc_add(struct drm_crtc *crtc)
  319. {
  320. struct drm_minor *minor = crtc->dev->primary;
  321. struct dentry *root;
  322. char *name;
  323. name = kasprintf(GFP_KERNEL, "crtc-%d", crtc->index);
  324. if (!name)
  325. return -ENOMEM;
  326. root = debugfs_create_dir(name, minor->debugfs_root);
  327. kfree(name);
  328. if (!root)
  329. return -ENOMEM;
  330. crtc->debugfs_entry = root;
  331. if (drm_debugfs_crtc_crc_add(crtc))
  332. goto error;
  333. return 0;
  334. error:
  335. drm_debugfs_crtc_remove(crtc);
  336. return -ENOMEM;
  337. }
  338. void drm_debugfs_crtc_remove(struct drm_crtc *crtc)
  339. {
  340. debugfs_remove_recursive(crtc->debugfs_entry);
  341. crtc->debugfs_entry = NULL;
  342. }
  343. #endif /* CONFIG_DEBUG_FS */