drm_debugfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Created: Sun Dec 21 13:08:50 2008 by [email protected]
  3. *
  4. * Copyright 2008 Ben Gamari <[email protected]>
  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/export.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/slab.h>
  29. #include <linux/uaccess.h>
  30. #include <drm/drm_atomic.h>
  31. #include <drm/drm_auth.h>
  32. #include <drm/drm_client.h>
  33. #include <drm/drm_debugfs.h>
  34. #include <drm/drm_device.h>
  35. #include <drm/drm_drv.h>
  36. #include <drm/drm_edid.h>
  37. #include <drm/drm_file.h>
  38. #include <drm/drm_gem.h>
  39. #include "drm_crtc_internal.h"
  40. #include "drm_internal.h"
  41. #if defined(CONFIG_DEBUG_FS)
  42. /***************************************************
  43. * Initialization, etc.
  44. **************************************************/
  45. static int drm_name_info(struct seq_file *m, void *data)
  46. {
  47. struct drm_info_node *node = (struct drm_info_node *) m->private;
  48. struct drm_minor *minor = node->minor;
  49. struct drm_device *dev = minor->dev;
  50. struct drm_master *master;
  51. mutex_lock(&dev->master_mutex);
  52. master = dev->master;
  53. seq_printf(m, "%s", dev->driver->name);
  54. if (dev->dev)
  55. seq_printf(m, " dev=%s", dev_name(dev->dev));
  56. if (master && master->unique)
  57. seq_printf(m, " master=%s", master->unique);
  58. if (dev->unique)
  59. seq_printf(m, " unique=%s", dev->unique);
  60. seq_printf(m, "\n");
  61. mutex_unlock(&dev->master_mutex);
  62. return 0;
  63. }
  64. static int drm_clients_info(struct seq_file *m, void *data)
  65. {
  66. struct drm_info_node *node = (struct drm_info_node *) m->private;
  67. struct drm_device *dev = node->minor->dev;
  68. struct drm_file *priv;
  69. kuid_t uid;
  70. seq_printf(m,
  71. "%20s %5s %3s master a %5s %10s\n",
  72. "command",
  73. "pid",
  74. "dev",
  75. "uid",
  76. "magic");
  77. /* dev->filelist is sorted youngest first, but we want to present
  78. * oldest first (i.e. kernel, servers, clients), so walk backwardss.
  79. */
  80. mutex_lock(&dev->filelist_mutex);
  81. list_for_each_entry_reverse(priv, &dev->filelist, lhead) {
  82. struct task_struct *task;
  83. bool is_current_master = drm_is_current_master(priv);
  84. rcu_read_lock(); /* locks pid_task()->comm */
  85. task = pid_task(priv->pid, PIDTYPE_PID);
  86. uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
  87. seq_printf(m, "%20s %5d %3d %c %c %5d %10u\n",
  88. task ? task->comm : "<unknown>",
  89. pid_vnr(priv->pid),
  90. priv->minor->index,
  91. is_current_master ? 'y' : 'n',
  92. priv->authenticated ? 'y' : 'n',
  93. from_kuid_munged(seq_user_ns(m), uid),
  94. priv->magic);
  95. rcu_read_unlock();
  96. }
  97. mutex_unlock(&dev->filelist_mutex);
  98. return 0;
  99. }
  100. static int drm_gem_one_name_info(int id, void *ptr, void *data)
  101. {
  102. struct drm_gem_object *obj = ptr;
  103. struct seq_file *m = data;
  104. seq_printf(m, "%6d %8zd %7d %8d\n",
  105. obj->name, obj->size,
  106. obj->handle_count,
  107. kref_read(&obj->refcount));
  108. return 0;
  109. }
  110. static int drm_gem_name_info(struct seq_file *m, void *data)
  111. {
  112. struct drm_info_node *node = (struct drm_info_node *) m->private;
  113. struct drm_device *dev = node->minor->dev;
  114. seq_printf(m, " name size handles refcount\n");
  115. mutex_lock(&dev->object_name_lock);
  116. idr_for_each(&dev->object_name_idr, drm_gem_one_name_info, m);
  117. mutex_unlock(&dev->object_name_lock);
  118. return 0;
  119. }
  120. static const struct drm_info_list drm_debugfs_list[] = {
  121. {"name", drm_name_info, 0},
  122. {"clients", drm_clients_info, 0},
  123. {"gem_names", drm_gem_name_info, DRIVER_GEM},
  124. };
  125. #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
  126. static int drm_debugfs_open(struct inode *inode, struct file *file)
  127. {
  128. struct drm_info_node *node = inode->i_private;
  129. return single_open(file, node->info_ent->show, node);
  130. }
  131. static const struct file_operations drm_debugfs_fops = {
  132. .owner = THIS_MODULE,
  133. .open = drm_debugfs_open,
  134. .read = seq_read,
  135. .llseek = seq_lseek,
  136. .release = single_release,
  137. };
  138. /**
  139. * drm_debugfs_create_files - Initialize a given set of debugfs files for DRM
  140. * minor
  141. * @files: The array of files to create
  142. * @count: The number of files given
  143. * @root: DRI debugfs dir entry.
  144. * @minor: device minor number
  145. *
  146. * Create a given set of debugfs files represented by an array of
  147. * &struct drm_info_list in the given root directory. These files will be removed
  148. * automatically on drm_debugfs_cleanup().
  149. */
  150. void drm_debugfs_create_files(const struct drm_info_list *files, int count,
  151. struct dentry *root, struct drm_minor *minor)
  152. {
  153. struct drm_device *dev = minor->dev;
  154. struct drm_info_node *tmp;
  155. int i;
  156. for (i = 0; i < count; i++) {
  157. u32 features = files[i].driver_features;
  158. if (features && !drm_core_check_all_features(dev, features))
  159. continue;
  160. tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
  161. if (tmp == NULL)
  162. continue;
  163. tmp->minor = minor;
  164. tmp->dent = debugfs_create_file(files[i].name,
  165. S_IFREG | S_IRUGO, root, tmp,
  166. &drm_debugfs_fops);
  167. tmp->info_ent = &files[i];
  168. mutex_lock(&minor->debugfs_lock);
  169. list_add(&tmp->list, &minor->debugfs_list);
  170. mutex_unlock(&minor->debugfs_lock);
  171. }
  172. }
  173. EXPORT_SYMBOL(drm_debugfs_create_files);
  174. int drm_debugfs_init(struct drm_minor *minor, int minor_id,
  175. struct dentry *root)
  176. {
  177. struct drm_device *dev = minor->dev;
  178. char name[64];
  179. INIT_LIST_HEAD(&minor->debugfs_list);
  180. mutex_init(&minor->debugfs_lock);
  181. sprintf(name, "%d", minor_id);
  182. minor->debugfs_root = debugfs_create_dir(name, root);
  183. drm_debugfs_create_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES,
  184. minor->debugfs_root, minor);
  185. if (drm_drv_uses_atomic_modeset(dev)) {
  186. drm_atomic_debugfs_init(minor);
  187. }
  188. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  189. drm_framebuffer_debugfs_init(minor);
  190. drm_client_debugfs_init(minor);
  191. }
  192. if (dev->driver->debugfs_init)
  193. dev->driver->debugfs_init(minor);
  194. return 0;
  195. }
  196. int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
  197. struct drm_minor *minor)
  198. {
  199. struct list_head *pos, *q;
  200. struct drm_info_node *tmp;
  201. int i;
  202. mutex_lock(&minor->debugfs_lock);
  203. for (i = 0; i < count; i++) {
  204. list_for_each_safe(pos, q, &minor->debugfs_list) {
  205. tmp = list_entry(pos, struct drm_info_node, list);
  206. if (tmp->info_ent == &files[i]) {
  207. debugfs_remove(tmp->dent);
  208. list_del(pos);
  209. kfree(tmp);
  210. }
  211. }
  212. }
  213. mutex_unlock(&minor->debugfs_lock);
  214. return 0;
  215. }
  216. EXPORT_SYMBOL(drm_debugfs_remove_files);
  217. static void drm_debugfs_remove_all_files(struct drm_minor *minor)
  218. {
  219. struct drm_info_node *node, *tmp;
  220. mutex_lock(&minor->debugfs_lock);
  221. list_for_each_entry_safe(node, tmp, &minor->debugfs_list, list) {
  222. debugfs_remove(node->dent);
  223. list_del(&node->list);
  224. kfree(node);
  225. }
  226. mutex_unlock(&minor->debugfs_lock);
  227. }
  228. void drm_debugfs_cleanup(struct drm_minor *minor)
  229. {
  230. if (!minor->debugfs_root)
  231. return;
  232. drm_debugfs_remove_all_files(minor);
  233. debugfs_remove_recursive(minor->debugfs_root);
  234. minor->debugfs_root = NULL;
  235. }
  236. static int connector_show(struct seq_file *m, void *data)
  237. {
  238. struct drm_connector *connector = m->private;
  239. seq_printf(m, "%s\n", drm_get_connector_force_name(connector->force));
  240. return 0;
  241. }
  242. static int connector_open(struct inode *inode, struct file *file)
  243. {
  244. struct drm_connector *dev = inode->i_private;
  245. return single_open(file, connector_show, dev);
  246. }
  247. static ssize_t connector_write(struct file *file, const char __user *ubuf,
  248. size_t len, loff_t *offp)
  249. {
  250. struct seq_file *m = file->private_data;
  251. struct drm_connector *connector = m->private;
  252. char buf[12];
  253. if (len > sizeof(buf) - 1)
  254. return -EINVAL;
  255. if (copy_from_user(buf, ubuf, len))
  256. return -EFAULT;
  257. buf[len] = '\0';
  258. if (sysfs_streq(buf, "on"))
  259. connector->force = DRM_FORCE_ON;
  260. else if (sysfs_streq(buf, "digital"))
  261. connector->force = DRM_FORCE_ON_DIGITAL;
  262. else if (sysfs_streq(buf, "off"))
  263. connector->force = DRM_FORCE_OFF;
  264. else if (sysfs_streq(buf, "unspecified"))
  265. connector->force = DRM_FORCE_UNSPECIFIED;
  266. else
  267. return -EINVAL;
  268. return len;
  269. }
  270. static int edid_show(struct seq_file *m, void *data)
  271. {
  272. struct drm_connector *connector = m->private;
  273. struct drm_property_blob *edid = connector->edid_blob_ptr;
  274. if (connector->override_edid && edid)
  275. seq_write(m, edid->data, edid->length);
  276. return 0;
  277. }
  278. static int edid_open(struct inode *inode, struct file *file)
  279. {
  280. struct drm_connector *dev = inode->i_private;
  281. return single_open(file, edid_show, dev);
  282. }
  283. static ssize_t edid_write(struct file *file, const char __user *ubuf,
  284. size_t len, loff_t *offp)
  285. {
  286. struct seq_file *m = file->private_data;
  287. struct drm_connector *connector = m->private;
  288. char *buf;
  289. int ret;
  290. buf = memdup_user(ubuf, len);
  291. if (IS_ERR(buf))
  292. return PTR_ERR(buf);
  293. if (len == 5 && !strncmp(buf, "reset", 5))
  294. ret = drm_edid_override_reset(connector);
  295. else
  296. ret = drm_edid_override_set(connector, buf, len);
  297. kfree(buf);
  298. return ret ? ret : len;
  299. }
  300. /*
  301. * Returns the min and max vrr vfreq through the connector's debugfs file.
  302. * Example usage: cat /sys/kernel/debug/dri/0/DP-1/vrr_range
  303. */
  304. static int vrr_range_show(struct seq_file *m, void *data)
  305. {
  306. struct drm_connector *connector = m->private;
  307. if (connector->status != connector_status_connected)
  308. return -ENODEV;
  309. seq_printf(m, "Min: %u\n", connector->display_info.monitor_range.min_vfreq);
  310. seq_printf(m, "Max: %u\n", connector->display_info.monitor_range.max_vfreq);
  311. return 0;
  312. }
  313. DEFINE_SHOW_ATTRIBUTE(vrr_range);
  314. /*
  315. * Returns Connector's max supported bpc through debugfs file.
  316. * Example usage: cat /sys/kernel/debug/dri/0/DP-1/output_bpc
  317. */
  318. static int output_bpc_show(struct seq_file *m, void *data)
  319. {
  320. struct drm_connector *connector = m->private;
  321. if (connector->status != connector_status_connected)
  322. return -ENODEV;
  323. seq_printf(m, "Maximum: %u\n", connector->display_info.bpc);
  324. return 0;
  325. }
  326. DEFINE_SHOW_ATTRIBUTE(output_bpc);
  327. static const struct file_operations drm_edid_fops = {
  328. .owner = THIS_MODULE,
  329. .open = edid_open,
  330. .read = seq_read,
  331. .llseek = seq_lseek,
  332. .release = single_release,
  333. .write = edid_write
  334. };
  335. static const struct file_operations drm_connector_fops = {
  336. .owner = THIS_MODULE,
  337. .open = connector_open,
  338. .read = seq_read,
  339. .llseek = seq_lseek,
  340. .release = single_release,
  341. .write = connector_write
  342. };
  343. void drm_debugfs_connector_add(struct drm_connector *connector)
  344. {
  345. struct drm_minor *minor = connector->dev->primary;
  346. struct dentry *root;
  347. if (!minor->debugfs_root)
  348. return;
  349. root = debugfs_create_dir(connector->name, minor->debugfs_root);
  350. connector->debugfs_entry = root;
  351. /* force */
  352. debugfs_create_file("force", S_IRUGO | S_IWUSR, root, connector,
  353. &drm_connector_fops);
  354. /* edid */
  355. debugfs_create_file("edid_override", S_IRUGO | S_IWUSR, root, connector,
  356. &drm_edid_fops);
  357. /* vrr range */
  358. debugfs_create_file("vrr_range", S_IRUGO, root, connector,
  359. &vrr_range_fops);
  360. /* max bpc */
  361. debugfs_create_file("output_bpc", 0444, root, connector,
  362. &output_bpc_fops);
  363. if (connector->funcs->debugfs_init)
  364. connector->funcs->debugfs_init(connector, root);
  365. }
  366. void drm_debugfs_connector_remove(struct drm_connector *connector)
  367. {
  368. if (!connector->debugfs_entry)
  369. return;
  370. debugfs_remove_recursive(connector->debugfs_entry);
  371. connector->debugfs_entry = NULL;
  372. }
  373. void drm_debugfs_crtc_add(struct drm_crtc *crtc)
  374. {
  375. struct drm_minor *minor = crtc->dev->primary;
  376. struct dentry *root;
  377. char *name;
  378. name = kasprintf(GFP_KERNEL, "crtc-%d", crtc->index);
  379. if (!name)
  380. return;
  381. root = debugfs_create_dir(name, minor->debugfs_root);
  382. kfree(name);
  383. crtc->debugfs_entry = root;
  384. drm_debugfs_crtc_crc_add(crtc);
  385. }
  386. void drm_debugfs_crtc_remove(struct drm_crtc *crtc)
  387. {
  388. debugfs_remove_recursive(crtc->debugfs_entry);
  389. crtc->debugfs_entry = NULL;
  390. }
  391. #endif /* CONFIG_DEBUG_FS */