debugfs-client.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/debugfs.h>
  6. #include <linux/interconnect.h>
  7. #include <linux/platform_device.h>
  8. #include "internal.h"
  9. /*
  10. * This can be dangerous, therefore don't provide any real compile time
  11. * configuration option for this feature.
  12. * People who want to use this will need to modify the source code directly.
  13. */
  14. #undef INTERCONNECT_ALLOW_WRITE_DEBUGFS
  15. #if defined(INTERCONNECT_ALLOW_WRITE_DEBUGFS) && defined(CONFIG_DEBUG_FS)
  16. static LIST_HEAD(debugfs_paths);
  17. static DEFINE_MUTEX(debugfs_lock);
  18. static struct platform_device *pdev;
  19. static struct icc_path *cur_path;
  20. static char *src_node;
  21. static char *dst_node;
  22. static u32 avg_bw;
  23. static u32 peak_bw;
  24. static u32 tag;
  25. struct debugfs_path {
  26. const char *src;
  27. const char *dst;
  28. struct icc_path *path;
  29. struct list_head list;
  30. };
  31. static struct icc_path *get_path(const char *src, const char *dst)
  32. {
  33. struct debugfs_path *path;
  34. list_for_each_entry(path, &debugfs_paths, list) {
  35. if (!strcmp(path->src, src) && !strcmp(path->dst, dst))
  36. return path->path;
  37. }
  38. return NULL;
  39. }
  40. static int icc_get_set(void *data, u64 val)
  41. {
  42. struct debugfs_path *debugfs_path;
  43. char *src, *dst;
  44. int ret = 0;
  45. mutex_lock(&debugfs_lock);
  46. rcu_read_lock();
  47. src = rcu_dereference(src_node);
  48. dst = rcu_dereference(dst_node);
  49. /*
  50. * If we've already looked up a path, then use the existing one instead
  51. * of calling icc_get() again. This allows for updating previous BW
  52. * votes when "get" is written to multiple times for multiple paths.
  53. */
  54. cur_path = get_path(src, dst);
  55. if (cur_path) {
  56. rcu_read_unlock();
  57. goto out;
  58. }
  59. src = kstrdup(src, GFP_ATOMIC);
  60. dst = kstrdup(dst, GFP_ATOMIC);
  61. rcu_read_unlock();
  62. if (!src || !dst) {
  63. ret = -ENOMEM;
  64. goto err_free;
  65. }
  66. cur_path = icc_get(&pdev->dev, src, dst);
  67. if (IS_ERR(cur_path)) {
  68. ret = PTR_ERR(cur_path);
  69. goto err_free;
  70. }
  71. debugfs_path = kzalloc(sizeof(*debugfs_path), GFP_KERNEL);
  72. if (!debugfs_path) {
  73. ret = -ENOMEM;
  74. goto err_put;
  75. }
  76. debugfs_path->path = cur_path;
  77. debugfs_path->src = src;
  78. debugfs_path->dst = dst;
  79. list_add_tail(&debugfs_path->list, &debugfs_paths);
  80. goto out;
  81. err_put:
  82. icc_put(cur_path);
  83. err_free:
  84. kfree(src);
  85. kfree(dst);
  86. out:
  87. mutex_unlock(&debugfs_lock);
  88. return ret;
  89. }
  90. DEFINE_DEBUGFS_ATTRIBUTE(icc_get_fops, NULL, icc_get_set, "%llu\n");
  91. static int icc_commit_set(void *data, u64 val)
  92. {
  93. int ret;
  94. mutex_lock(&debugfs_lock);
  95. if (IS_ERR_OR_NULL(cur_path)) {
  96. ret = PTR_ERR(cur_path);
  97. goto out;
  98. }
  99. icc_set_tag(cur_path, tag);
  100. ret = icc_set_bw(cur_path, avg_bw, peak_bw);
  101. out:
  102. mutex_unlock(&debugfs_lock);
  103. return ret;
  104. }
  105. DEFINE_DEBUGFS_ATTRIBUTE(icc_commit_fops, NULL, icc_commit_set, "%llu\n");
  106. int icc_debugfs_client_init(struct dentry *icc_dir)
  107. {
  108. struct dentry *client_dir;
  109. int ret;
  110. pdev = platform_device_alloc("icc-debugfs-client", PLATFORM_DEVID_NONE);
  111. ret = platform_device_add(pdev);
  112. if (ret) {
  113. pr_err("%s: failed to add platform device: %d\n", __func__, ret);
  114. platform_device_put(pdev);
  115. return ret;
  116. }
  117. client_dir = debugfs_create_dir("test_client", icc_dir);
  118. debugfs_create_str("src_node", 0600, client_dir, &src_node);
  119. debugfs_create_str("dst_node", 0600, client_dir, &dst_node);
  120. debugfs_create_file("get", 0200, client_dir, NULL, &icc_get_fops);
  121. debugfs_create_u32("avg_bw", 0600, client_dir, &avg_bw);
  122. debugfs_create_u32("peak_bw", 0600, client_dir, &peak_bw);
  123. debugfs_create_u32("tag", 0600, client_dir, &tag);
  124. debugfs_create_file("commit", 0200, client_dir, NULL, &icc_commit_fops);
  125. return 0;
  126. }
  127. #else
  128. int icc_debugfs_client_init(struct dentry *icc_dir)
  129. {
  130. return 0;
  131. }
  132. #endif