drm_debugfs.c 9.9 KB

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