drm_debugfs.c 10 KB

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