drm_debugfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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/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_bridge.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/drm_managed.h>
  40. #include <drm/drm_gpuvm.h>
  41. #include "drm_crtc_internal.h"
  42. #include "drm_internal.h"
  43. static struct dentry *accel_debugfs_root;
  44. static struct dentry *drm_debugfs_root;
  45. /***************************************************
  46. * Initialization, etc.
  47. **************************************************/
  48. static int drm_name_info(struct seq_file *m, void *data)
  49. {
  50. struct drm_debugfs_entry *entry = m->private;
  51. struct drm_device *dev = entry->dev;
  52. struct drm_master *master;
  53. mutex_lock(&dev->master_mutex);
  54. master = dev->master;
  55. seq_printf(m, "%s", dev->driver->name);
  56. if (dev->dev)
  57. seq_printf(m, " dev=%s", dev_name(dev->dev));
  58. if (master && master->unique)
  59. seq_printf(m, " master=%s", master->unique);
  60. if (dev->unique)
  61. seq_printf(m, " unique=%s", dev->unique);
  62. seq_printf(m, "\n");
  63. mutex_unlock(&dev->master_mutex);
  64. return 0;
  65. }
  66. static int drm_clients_info(struct seq_file *m, void *data)
  67. {
  68. struct drm_debugfs_entry *entry = m->private;
  69. struct drm_device *dev = entry->dev;
  70. struct drm_file *priv;
  71. kuid_t uid;
  72. seq_printf(m,
  73. "%20s %5s %3s master a %5s %10s %*s %20s\n",
  74. "command",
  75. "tgid",
  76. "dev",
  77. "uid",
  78. "magic",
  79. DRM_CLIENT_NAME_MAX_LEN,
  80. "name",
  81. "id");
  82. /* dev->filelist is sorted youngest first, but we want to present
  83. * oldest first (i.e. kernel, servers, clients), so walk backwardss.
  84. */
  85. mutex_lock(&dev->filelist_mutex);
  86. list_for_each_entry_reverse(priv, &dev->filelist, lhead) {
  87. bool is_current_master = drm_is_current_master(priv);
  88. struct task_struct *task;
  89. struct pid *pid;
  90. mutex_lock(&priv->client_name_lock);
  91. rcu_read_lock(); /* Locks priv->pid and pid_task()->comm! */
  92. pid = rcu_dereference(priv->pid);
  93. task = pid_task(pid, PIDTYPE_TGID);
  94. uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
  95. seq_printf(m, "%20s %5d %3d %c %c %5d %10u %*s %20llu\n",
  96. task ? task->comm : "<unknown>",
  97. pid_vnr(pid),
  98. priv->minor->index,
  99. is_current_master ? 'y' : 'n',
  100. priv->authenticated ? 'y' : 'n',
  101. from_kuid_munged(seq_user_ns(m), uid),
  102. priv->magic,
  103. DRM_CLIENT_NAME_MAX_LEN,
  104. priv->client_name ? priv->client_name : "<unset>",
  105. priv->client_id);
  106. rcu_read_unlock();
  107. mutex_unlock(&priv->client_name_lock);
  108. }
  109. mutex_unlock(&dev->filelist_mutex);
  110. return 0;
  111. }
  112. static int drm_gem_one_name_info(int id, void *ptr, void *data)
  113. {
  114. struct drm_gem_object *obj = ptr;
  115. struct seq_file *m = data;
  116. seq_printf(m, "%6d %8zd %7d %8d\n",
  117. obj->name, obj->size,
  118. obj->handle_count,
  119. kref_read(&obj->refcount));
  120. return 0;
  121. }
  122. static int drm_gem_name_info(struct seq_file *m, void *data)
  123. {
  124. struct drm_debugfs_entry *entry = m->private;
  125. struct drm_device *dev = entry->dev;
  126. seq_printf(m, " name size handles refcount\n");
  127. mutex_lock(&dev->object_name_lock);
  128. idr_for_each(&dev->object_name_idr, drm_gem_one_name_info, m);
  129. mutex_unlock(&dev->object_name_lock);
  130. return 0;
  131. }
  132. static const struct drm_debugfs_info drm_debugfs_list[] = {
  133. {"name", drm_name_info, 0},
  134. {"clients", drm_clients_info, 0},
  135. {"gem_names", drm_gem_name_info, DRIVER_GEM},
  136. };
  137. #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
  138. static int drm_debugfs_open(struct inode *inode, struct file *file)
  139. {
  140. struct drm_info_node *node = inode->i_private;
  141. if (!device_is_registered(node->minor->kdev))
  142. return -ENODEV;
  143. return single_open(file, node->info_ent->show, node);
  144. }
  145. static int drm_debugfs_entry_open(struct inode *inode, struct file *file)
  146. {
  147. struct drm_debugfs_entry *entry = inode->i_private;
  148. struct drm_debugfs_info *node = &entry->file;
  149. struct drm_minor *minor = entry->dev->primary ?: entry->dev->accel;
  150. if (!device_is_registered(minor->kdev))
  151. return -ENODEV;
  152. return single_open(file, node->show, entry);
  153. }
  154. static const struct file_operations drm_debugfs_entry_fops = {
  155. .owner = THIS_MODULE,
  156. .open = drm_debugfs_entry_open,
  157. .read = seq_read,
  158. .llseek = seq_lseek,
  159. .release = single_release,
  160. };
  161. static const struct file_operations drm_debugfs_fops = {
  162. .owner = THIS_MODULE,
  163. .open = drm_debugfs_open,
  164. .read = seq_read,
  165. .llseek = seq_lseek,
  166. .release = single_release,
  167. };
  168. /**
  169. * drm_debugfs_gpuva_info - dump the given DRM GPU VA space
  170. * @m: pointer to the &seq_file to write
  171. * @gpuvm: the &drm_gpuvm representing the GPU VA space
  172. *
  173. * Dumps the GPU VA mappings of a given DRM GPU VA manager.
  174. *
  175. * For each DRM GPU VA space drivers should call this function from their
  176. * &drm_info_list's show callback.
  177. *
  178. * Returns: 0 on success, -ENODEV if the &gpuvm is not initialized
  179. */
  180. int drm_debugfs_gpuva_info(struct seq_file *m,
  181. struct drm_gpuvm *gpuvm)
  182. {
  183. struct drm_gpuva *va, *kva = &gpuvm->kernel_alloc_node;
  184. if (!gpuvm->name)
  185. return -ENODEV;
  186. seq_printf(m, "DRM GPU VA space (%s) [0x%016llx;0x%016llx]\n",
  187. gpuvm->name, gpuvm->mm_start, gpuvm->mm_start + gpuvm->mm_range);
  188. seq_printf(m, "Kernel reserved node [0x%016llx;0x%016llx]\n",
  189. kva->va.addr, kva->va.addr + kva->va.range);
  190. seq_puts(m, "\n");
  191. seq_puts(m, " VAs | start | range | end | object | object offset\n");
  192. seq_puts(m, "-------------------------------------------------------------------------------------------------------------\n");
  193. drm_gpuvm_for_each_va(va, gpuvm) {
  194. if (unlikely(va == kva))
  195. continue;
  196. seq_printf(m, " | 0x%016llx | 0x%016llx | 0x%016llx | 0x%016llx | 0x%016llx\n",
  197. va->va.addr, va->va.range, va->va.addr + va->va.range,
  198. (u64)(uintptr_t)va->gem.obj, va->gem.offset);
  199. }
  200. return 0;
  201. }
  202. EXPORT_SYMBOL(drm_debugfs_gpuva_info);
  203. /**
  204. * drm_debugfs_create_files - Initialize a given set of debugfs files for DRM
  205. * minor
  206. * @files: The array of files to create
  207. * @count: The number of files given
  208. * @root: DRI debugfs dir entry.
  209. * @minor: device minor number
  210. *
  211. * Create a given set of debugfs files represented by an array of
  212. * &struct drm_info_list in the given root directory. These files will be removed
  213. * automatically on drm_debugfs_dev_fini().
  214. */
  215. void drm_debugfs_create_files(const struct drm_info_list *files, int count,
  216. struct dentry *root, struct drm_minor *minor)
  217. {
  218. struct drm_device *dev = minor->dev;
  219. struct drm_info_node *tmp;
  220. int i;
  221. for (i = 0; i < count; i++) {
  222. u32 features = files[i].driver_features;
  223. if (features && !drm_core_check_all_features(dev, features))
  224. continue;
  225. tmp = drmm_kzalloc(dev, sizeof(*tmp), GFP_KERNEL);
  226. if (tmp == NULL)
  227. continue;
  228. tmp->minor = minor;
  229. tmp->dent = debugfs_create_file(files[i].name,
  230. 0444, root, tmp,
  231. &drm_debugfs_fops);
  232. tmp->info_ent = &files[i];
  233. }
  234. }
  235. EXPORT_SYMBOL(drm_debugfs_create_files);
  236. int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
  237. struct dentry *root, struct drm_minor *minor)
  238. {
  239. int i;
  240. for (i = 0; i < count; i++) {
  241. struct dentry *dent = debugfs_lookup(files[i].name, root);
  242. if (!dent)
  243. continue;
  244. drmm_kfree(minor->dev, d_inode(dent)->i_private);
  245. debugfs_remove(dent);
  246. }
  247. return 0;
  248. }
  249. EXPORT_SYMBOL(drm_debugfs_remove_files);
  250. void drm_debugfs_bridge_params(void)
  251. {
  252. drm_bridge_debugfs_params(drm_debugfs_root);
  253. }
  254. void drm_debugfs_init_root(void)
  255. {
  256. drm_debugfs_root = debugfs_create_dir("dri", NULL);
  257. #if IS_ENABLED(CONFIG_DRM_ACCEL)
  258. accel_debugfs_root = debugfs_create_dir("accel", NULL);
  259. #endif
  260. }
  261. void drm_debugfs_remove_root(void)
  262. {
  263. #if IS_ENABLED(CONFIG_DRM_ACCEL)
  264. debugfs_remove(accel_debugfs_root);
  265. #endif
  266. debugfs_remove(drm_debugfs_root);
  267. }
  268. static int drm_debugfs_proc_info_show(struct seq_file *m, void *unused)
  269. {
  270. struct pid *pid;
  271. struct task_struct *task;
  272. struct drm_file *file = m->private;
  273. if (!file)
  274. return -EINVAL;
  275. rcu_read_lock();
  276. pid = rcu_dereference(file->pid);
  277. task = pid_task(pid, PIDTYPE_TGID);
  278. seq_printf(m, "pid: %d\n", task ? task->pid : 0);
  279. seq_printf(m, "comm: %s\n", task ? task->comm : "Unset");
  280. rcu_read_unlock();
  281. return 0;
  282. }
  283. static int drm_debufs_proc_info_open(struct inode *inode, struct file *file)
  284. {
  285. return single_open(file, drm_debugfs_proc_info_show, inode->i_private);
  286. }
  287. static const struct file_operations drm_debugfs_proc_info_fops = {
  288. .owner = THIS_MODULE,
  289. .open = drm_debufs_proc_info_open,
  290. .read = seq_read,
  291. .llseek = seq_lseek,
  292. .release = single_release,
  293. };
  294. /**
  295. * drm_debugfs_clients_add - Add a per client debugfs directory
  296. * @file: drm_file for a client
  297. *
  298. * Create the debugfs directory for each client. This will be used to populate
  299. * driver specific data for each client.
  300. *
  301. * Also add the process information debugfs file for each client to tag
  302. * which client belongs to which process.
  303. */
  304. void drm_debugfs_clients_add(struct drm_file *file)
  305. {
  306. char *client;
  307. client = kasprintf(GFP_KERNEL, "client-%llu", file->client_id);
  308. if (!client)
  309. return;
  310. /* Create a debugfs directory for the client in root on drm debugfs */
  311. file->debugfs_client = debugfs_create_dir(client, drm_debugfs_root);
  312. kfree(client);
  313. debugfs_create_file("proc_info", 0444, file->debugfs_client, file,
  314. &drm_debugfs_proc_info_fops);
  315. client = kasprintf(GFP_KERNEL, "../%s", file->minor->dev->unique);
  316. if (!client)
  317. return;
  318. /* Create a link from client_id to the drm device this client id belongs to */
  319. debugfs_create_symlink("device", file->debugfs_client, client);
  320. kfree(client);
  321. }
  322. /**
  323. * drm_debugfs_clients_remove - removes all debugfs directories and files
  324. * @file: drm_file for a client
  325. *
  326. * Removes the debugfs directories recursively from the client directory.
  327. *
  328. * There is also a possibility that debugfs files are open while the drm_file
  329. * is released.
  330. */
  331. void drm_debugfs_clients_remove(struct drm_file *file)
  332. {
  333. debugfs_remove_recursive(file->debugfs_client);
  334. file->debugfs_client = NULL;
  335. }
  336. /**
  337. * drm_debugfs_dev_init - create debugfs directory for the device
  338. * @dev: the device which we want to create the directory for
  339. *
  340. * Creates the debugfs directory for the device under the given root directory.
  341. */
  342. void drm_debugfs_dev_init(struct drm_device *dev)
  343. {
  344. if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL))
  345. dev->debugfs_root = debugfs_create_dir(dev->unique, accel_debugfs_root);
  346. else
  347. dev->debugfs_root = debugfs_create_dir(dev->unique, drm_debugfs_root);
  348. }
  349. /**
  350. * drm_debugfs_dev_fini - cleanup debugfs directory
  351. * @dev: the device to cleanup the debugfs stuff
  352. *
  353. * Remove the debugfs directory, might be called multiple times.
  354. */
  355. void drm_debugfs_dev_fini(struct drm_device *dev)
  356. {
  357. debugfs_remove_recursive(dev->debugfs_root);
  358. dev->debugfs_root = NULL;
  359. }
  360. void drm_debugfs_dev_register(struct drm_device *dev)
  361. {
  362. drm_debugfs_add_files(dev, drm_debugfs_list, DRM_DEBUGFS_ENTRIES);
  363. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  364. drm_framebuffer_debugfs_init(dev);
  365. drm_client_debugfs_init(dev);
  366. }
  367. if (drm_drv_uses_atomic_modeset(dev))
  368. drm_atomic_debugfs_init(dev);
  369. }
  370. int drm_debugfs_register(struct drm_minor *minor, int minor_id)
  371. {
  372. struct drm_device *dev = minor->dev;
  373. char name[64];
  374. sprintf(name, "%d", minor_id);
  375. minor->debugfs_symlink = debugfs_create_symlink(name, drm_debugfs_root,
  376. dev->unique);
  377. /* TODO: Only for compatibility with drivers */
  378. minor->debugfs_root = dev->debugfs_root;
  379. if (dev->driver->debugfs_init && dev->render != minor)
  380. dev->driver->debugfs_init(minor);
  381. return 0;
  382. }
  383. void drm_debugfs_unregister(struct drm_minor *minor)
  384. {
  385. debugfs_remove(minor->debugfs_symlink);
  386. minor->debugfs_symlink = NULL;
  387. }
  388. /**
  389. * drm_debugfs_add_file - Add a given file to the DRM device debugfs file list
  390. * @dev: drm device for the ioctl
  391. * @name: debugfs file name
  392. * @show: show callback
  393. * @data: driver-private data, should not be device-specific
  394. *
  395. * Add a given file entry to the DRM device debugfs file list to be created on
  396. * drm_debugfs_init.
  397. */
  398. void drm_debugfs_add_file(struct drm_device *dev, const char *name,
  399. int (*show)(struct seq_file*, void*), void *data)
  400. {
  401. struct drm_debugfs_entry *entry = drmm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
  402. if (!entry)
  403. return;
  404. entry->file.name = name;
  405. entry->file.show = show;
  406. entry->file.data = data;
  407. entry->dev = dev;
  408. debugfs_create_file(name, 0444, dev->debugfs_root, entry,
  409. &drm_debugfs_entry_fops);
  410. }
  411. EXPORT_SYMBOL(drm_debugfs_add_file);
  412. /**
  413. * drm_debugfs_add_files - Add an array of files to the DRM device debugfs file list
  414. * @dev: drm device for the ioctl
  415. * @files: The array of files to create
  416. * @count: The number of files given
  417. *
  418. * Add a given set of debugfs files represented by an array of
  419. * &struct drm_debugfs_info in the DRM device debugfs file list.
  420. */
  421. void drm_debugfs_add_files(struct drm_device *dev, const struct drm_debugfs_info *files, int count)
  422. {
  423. int i;
  424. for (i = 0; i < count; i++)
  425. drm_debugfs_add_file(dev, files[i].name, files[i].show, files[i].data);
  426. }
  427. EXPORT_SYMBOL(drm_debugfs_add_files);
  428. static int connector_show(struct seq_file *m, void *data)
  429. {
  430. struct drm_connector *connector = m->private;
  431. seq_printf(m, "%s\n", drm_get_connector_force_name(connector->force));
  432. return 0;
  433. }
  434. static int connector_open(struct inode *inode, struct file *file)
  435. {
  436. struct drm_connector *dev = inode->i_private;
  437. return single_open(file, connector_show, dev);
  438. }
  439. static ssize_t connector_write(struct file *file, const char __user *ubuf,
  440. size_t len, loff_t *offp)
  441. {
  442. struct seq_file *m = file->private_data;
  443. struct drm_connector *connector = m->private;
  444. char buf[12];
  445. if (len > sizeof(buf) - 1)
  446. return -EINVAL;
  447. if (copy_from_user(buf, ubuf, len))
  448. return -EFAULT;
  449. buf[len] = '\0';
  450. if (sysfs_streq(buf, "on"))
  451. connector->force = DRM_FORCE_ON;
  452. else if (sysfs_streq(buf, "digital"))
  453. connector->force = DRM_FORCE_ON_DIGITAL;
  454. else if (sysfs_streq(buf, "off"))
  455. connector->force = DRM_FORCE_OFF;
  456. else if (sysfs_streq(buf, "unspecified"))
  457. connector->force = DRM_FORCE_UNSPECIFIED;
  458. else
  459. return -EINVAL;
  460. return len;
  461. }
  462. static int edid_show(struct seq_file *m, void *data)
  463. {
  464. return drm_edid_override_show(m->private, m);
  465. }
  466. static int edid_open(struct inode *inode, struct file *file)
  467. {
  468. struct drm_connector *dev = inode->i_private;
  469. return single_open(file, edid_show, dev);
  470. }
  471. static ssize_t edid_write(struct file *file, const char __user *ubuf,
  472. size_t len, loff_t *offp)
  473. {
  474. struct seq_file *m = file->private_data;
  475. struct drm_connector *connector = m->private;
  476. char *buf;
  477. int ret;
  478. buf = memdup_user(ubuf, len);
  479. if (IS_ERR(buf))
  480. return PTR_ERR(buf);
  481. if (len == 5 && !strncmp(buf, "reset", 5))
  482. ret = drm_edid_override_reset(connector);
  483. else
  484. ret = drm_edid_override_set(connector, buf, len);
  485. kfree(buf);
  486. return ret ? ret : len;
  487. }
  488. /*
  489. * Returns the min and max vrr vfreq through the connector's debugfs file.
  490. * Example usage: cat /sys/kernel/debug/dri/0/DP-1/vrr_range
  491. */
  492. static int vrr_range_show(struct seq_file *m, void *data)
  493. {
  494. struct drm_connector *connector = m->private;
  495. if (connector->status != connector_status_connected)
  496. return -ENODEV;
  497. seq_printf(m, "Min: %u\n", connector->display_info.monitor_range.min_vfreq);
  498. seq_printf(m, "Max: %u\n", connector->display_info.monitor_range.max_vfreq);
  499. return 0;
  500. }
  501. DEFINE_SHOW_ATTRIBUTE(vrr_range);
  502. /*
  503. * Returns Connector's max supported bpc through debugfs file.
  504. * Example usage: cat /sys/kernel/debug/dri/0/DP-1/output_bpc
  505. */
  506. static int output_bpc_show(struct seq_file *m, void *data)
  507. {
  508. struct drm_connector *connector = m->private;
  509. if (connector->status != connector_status_connected)
  510. return -ENODEV;
  511. seq_printf(m, "Maximum: %u\n", connector->display_info.bpc);
  512. return 0;
  513. }
  514. DEFINE_SHOW_ATTRIBUTE(output_bpc);
  515. static const struct file_operations drm_edid_fops = {
  516. .owner = THIS_MODULE,
  517. .open = edid_open,
  518. .read = seq_read,
  519. .llseek = seq_lseek,
  520. .release = single_release,
  521. .write = edid_write
  522. };
  523. static const struct file_operations drm_connector_fops = {
  524. .owner = THIS_MODULE,
  525. .open = connector_open,
  526. .read = seq_read,
  527. .llseek = seq_lseek,
  528. .release = single_release,
  529. .write = connector_write
  530. };
  531. static ssize_t
  532. audio_infoframe_read(struct file *filp, char __user *ubuf, size_t count, loff_t *ppos)
  533. {
  534. struct drm_connector_hdmi_infoframe *infoframe;
  535. struct drm_connector *connector;
  536. union hdmi_infoframe *frame;
  537. u8 buf[HDMI_INFOFRAME_SIZE(AUDIO)];
  538. ssize_t len = 0;
  539. connector = filp->private_data;
  540. mutex_lock(&connector->hdmi.infoframes.lock);
  541. infoframe = &connector->hdmi.infoframes.audio;
  542. if (!infoframe->set)
  543. goto out;
  544. frame = &infoframe->data;
  545. len = hdmi_infoframe_pack(frame, buf, sizeof(buf));
  546. if (len < 0)
  547. goto out;
  548. len = simple_read_from_buffer(ubuf, count, ppos, buf, len);
  549. out:
  550. mutex_unlock(&connector->hdmi.infoframes.lock);
  551. return len;
  552. }
  553. static const struct file_operations audio_infoframe_fops = {
  554. .owner = THIS_MODULE,
  555. .open = simple_open,
  556. .read = audio_infoframe_read,
  557. };
  558. static int create_hdmi_audio_infoframe_file(struct drm_connector *connector,
  559. struct dentry *parent)
  560. {
  561. struct dentry *file;
  562. if (!connector->hdmi.funcs ||
  563. !connector->hdmi.funcs->audio.write_infoframe)
  564. return 0;
  565. file = debugfs_create_file("audio", 0400, parent, connector, &audio_infoframe_fops);
  566. if (IS_ERR(file))
  567. return PTR_ERR(file);
  568. return 0;
  569. }
  570. #define DEFINE_INFOFRAME_FILE(_f) \
  571. static ssize_t _f##_read_infoframe(struct file *filp, \
  572. char __user *ubuf, \
  573. size_t count, \
  574. loff_t *ppos) \
  575. { \
  576. struct drm_connector_hdmi_infoframe *infoframe; \
  577. struct drm_connector_state *conn_state; \
  578. struct drm_connector *connector; \
  579. union hdmi_infoframe *frame; \
  580. struct drm_device *dev; \
  581. u8 buf[HDMI_INFOFRAME_SIZE(MAX)]; \
  582. ssize_t len = 0; \
  583. \
  584. connector = filp->private_data; \
  585. dev = connector->dev; \
  586. \
  587. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); \
  588. \
  589. conn_state = connector->state; \
  590. infoframe = &conn_state->hdmi.infoframes._f; \
  591. if (!infoframe->set) \
  592. goto out; \
  593. \
  594. frame = &infoframe->data; \
  595. len = hdmi_infoframe_pack(frame, buf, sizeof(buf)); \
  596. if (len < 0) \
  597. goto out; \
  598. \
  599. len = simple_read_from_buffer(ubuf, count, ppos, buf, len); \
  600. \
  601. out: \
  602. drm_modeset_unlock(&dev->mode_config.connection_mutex); \
  603. return len; \
  604. } \
  605. \
  606. static const struct file_operations _f##_infoframe_fops = { \
  607. .owner = THIS_MODULE, \
  608. .open = simple_open, \
  609. .read = _f##_read_infoframe, \
  610. }; \
  611. \
  612. static int create_hdmi_## _f ## _infoframe_file(struct drm_connector *connector, \
  613. struct dentry *parent) \
  614. { \
  615. struct dentry *file; \
  616. \
  617. if (!connector->hdmi.funcs || \
  618. !connector->hdmi.funcs->_f.write_infoframe) \
  619. return 0; \
  620. file = debugfs_create_file(#_f, 0400, parent, connector, &_f ## _infoframe_fops); \
  621. if (IS_ERR(file)) \
  622. return PTR_ERR(file); \
  623. \
  624. return 0; \
  625. }
  626. DEFINE_INFOFRAME_FILE(avi);
  627. DEFINE_INFOFRAME_FILE(hdmi);
  628. DEFINE_INFOFRAME_FILE(hdr_drm);
  629. DEFINE_INFOFRAME_FILE(spd);
  630. static int create_hdmi_infoframe_files(struct drm_connector *connector,
  631. struct dentry *parent)
  632. {
  633. int ret;
  634. ret = create_hdmi_audio_infoframe_file(connector, parent);
  635. if (ret)
  636. return ret;
  637. ret = create_hdmi_avi_infoframe_file(connector, parent);
  638. if (ret)
  639. return ret;
  640. ret = create_hdmi_hdmi_infoframe_file(connector, parent);
  641. if (ret)
  642. return ret;
  643. ret = create_hdmi_hdr_drm_infoframe_file(connector, parent);
  644. if (ret)
  645. return ret;
  646. ret = create_hdmi_spd_infoframe_file(connector, parent);
  647. if (ret)
  648. return ret;
  649. return 0;
  650. }
  651. static void hdmi_debugfs_add(struct drm_connector *connector)
  652. {
  653. struct dentry *dir;
  654. if (!(connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  655. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB))
  656. return;
  657. dir = debugfs_create_dir("infoframes", connector->debugfs_entry);
  658. if (IS_ERR(dir))
  659. return;
  660. create_hdmi_infoframe_files(connector, dir);
  661. }
  662. void drm_debugfs_connector_add(struct drm_connector *connector)
  663. {
  664. struct drm_device *dev = connector->dev;
  665. struct dentry *root;
  666. if (!dev->debugfs_root)
  667. return;
  668. root = debugfs_create_dir(connector->name, dev->debugfs_root);
  669. connector->debugfs_entry = root;
  670. /* force */
  671. debugfs_create_file("force", 0644, root, connector,
  672. &drm_connector_fops);
  673. /* edid */
  674. debugfs_create_file("edid_override", 0644, root, connector,
  675. &drm_edid_fops);
  676. /* vrr range */
  677. debugfs_create_file("vrr_range", 0444, root, connector,
  678. &vrr_range_fops);
  679. /* max bpc */
  680. debugfs_create_file("output_bpc", 0444, root, connector,
  681. &output_bpc_fops);
  682. hdmi_debugfs_add(connector);
  683. if (connector->funcs->debugfs_init)
  684. connector->funcs->debugfs_init(connector, root);
  685. }
  686. void drm_debugfs_connector_remove(struct drm_connector *connector)
  687. {
  688. if (!connector->debugfs_entry)
  689. return;
  690. debugfs_remove_recursive(connector->debugfs_entry);
  691. connector->debugfs_entry = NULL;
  692. }
  693. void drm_debugfs_crtc_add(struct drm_crtc *crtc)
  694. {
  695. struct drm_device *dev = crtc->dev;
  696. struct dentry *root;
  697. char *name;
  698. name = kasprintf(GFP_KERNEL, "crtc-%d", crtc->index);
  699. if (!name)
  700. return;
  701. root = debugfs_create_dir(name, dev->debugfs_root);
  702. kfree(name);
  703. crtc->debugfs_entry = root;
  704. drm_debugfs_crtc_crc_add(crtc);
  705. }
  706. void drm_debugfs_crtc_remove(struct drm_crtc *crtc)
  707. {
  708. debugfs_remove_recursive(crtc->debugfs_entry);
  709. crtc->debugfs_entry = NULL;
  710. }
  711. void drm_debugfs_encoder_add(struct drm_encoder *encoder)
  712. {
  713. struct drm_minor *minor = encoder->dev->primary;
  714. struct dentry *root;
  715. char *name;
  716. name = kasprintf(GFP_KERNEL, "encoder-%d", encoder->index);
  717. if (!name)
  718. return;
  719. root = debugfs_create_dir(name, minor->debugfs_root);
  720. kfree(name);
  721. encoder->debugfs_entry = root;
  722. drm_bridge_debugfs_encoder_params(root, encoder);
  723. if (encoder->funcs && encoder->funcs->debugfs_init)
  724. encoder->funcs->debugfs_init(encoder, root);
  725. }
  726. void drm_debugfs_encoder_remove(struct drm_encoder *encoder)
  727. {
  728. debugfs_remove_recursive(encoder->debugfs_entry);
  729. encoder->debugfs_entry = NULL;
  730. }