c1dcvs_scmi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/scmi_protocol.h>
  7. #include <linux/scmi_c1dcvs.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/err.h>
  12. #include <linux/errno.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/cpu.h>
  16. #include <linux/scmi_c1dcvs.h>
  17. #include <linux/slab.h>
  18. static struct kobject c1dcvs_kobj;
  19. static struct scmi_protocol_handle *ph;
  20. static const struct scmi_c1dcvs_vendor_ops *ops;
  21. static unsigned int user_c1dcvs_en;
  22. static unsigned int kernel_c1dcvs_en;
  23. static DEFINE_MUTEX(c1dcvs_lock);
  24. struct qcom_c1dcvs_attr {
  25. struct attribute attr;
  26. ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
  27. char *buf);
  28. ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
  29. const char *buf, size_t count);
  30. };
  31. #define to_c1dcvs_attr(_attr) \
  32. container_of(_attr, struct qcom_c1dcvs_attr, attr)
  33. #define C1DCVS_ATTR_RW(_name) \
  34. static struct qcom_c1dcvs_attr _name = \
  35. __ATTR(_name, 0644, show_##_name, store_##_name) \
  36. #define store_c1dcvs_attr(name) \
  37. static ssize_t store_##name(struct kobject *kobj, \
  38. struct attribute *attr, const char *buf,\
  39. size_t count) \
  40. { \
  41. unsigned int var; \
  42. int ret; \
  43. \
  44. if (!ops) \
  45. return -ENODEV; \
  46. \
  47. ret = kstrtouint(buf, 10, &var); \
  48. if (ret < 0) \
  49. return ret; \
  50. \
  51. ret = ops->set_##name(ph, &var); \
  52. return ((ret < 0) ? ret : count); \
  53. } \
  54. #define show_c1dcvs_attr(name) \
  55. static ssize_t show_##name(struct kobject *kobj, \
  56. struct attribute *attr, char *buf) \
  57. { \
  58. unsigned int var; \
  59. int ret; \
  60. \
  61. if (!ops) \
  62. return -ENODEV; \
  63. \
  64. ret = ops->get_##name(ph, &var); \
  65. if (ret < 0) \
  66. return ret; \
  67. \
  68. return scnprintf(buf, PAGE_SIZE, "%lu\n", le32_to_cpu(var)); \
  69. } \
  70. /*
  71. * Must hold c1dcvs_lock before calling this function
  72. */
  73. static int update_enable_c1dcvs(void)
  74. {
  75. unsigned int enable = min(user_c1dcvs_en, kernel_c1dcvs_en);
  76. if (!ops)
  77. return -ENODEV;
  78. return ops->set_enable_c1dcvs(ph, &enable);
  79. }
  80. static ssize_t store_enable_c1dcvs(struct kobject *kobj,
  81. struct attribute *attr, const char *buf,
  82. size_t count)
  83. {
  84. unsigned int var;
  85. int ret;
  86. if (!ops)
  87. return -ENODEV;
  88. ret = kstrtouint(buf, 10, &var);
  89. if (ret < 0)
  90. return ret;
  91. mutex_lock(&c1dcvs_lock);
  92. user_c1dcvs_en = var;
  93. ret = update_enable_c1dcvs();
  94. mutex_unlock(&c1dcvs_lock);
  95. return ((ret < 0) ? ret : count);
  96. }
  97. int c1dcvs_enable(bool enable)
  98. {
  99. unsigned int data = enable ? 1 : 0;
  100. int ret;
  101. if (!ops)
  102. return -EPROBE_DEFER;
  103. mutex_lock(&c1dcvs_lock);
  104. kernel_c1dcvs_en = data;
  105. ret = update_enable_c1dcvs();
  106. mutex_unlock(&c1dcvs_lock);
  107. return ret;
  108. }
  109. EXPORT_SYMBOL(c1dcvs_enable);
  110. store_c1dcvs_attr(enable_trace);
  111. show_c1dcvs_attr(enable_trace);
  112. C1DCVS_ATTR_RW(enable_trace);
  113. store_c1dcvs_attr(hysteresis);
  114. show_c1dcvs_attr(hysteresis);
  115. C1DCVS_ATTR_RW(hysteresis);
  116. show_c1dcvs_attr(enable_c1dcvs);
  117. C1DCVS_ATTR_RW(enable_c1dcvs);
  118. #define store_c1dcvs_thresh(name) \
  119. static ssize_t store_##name(struct kobject *kobj, \
  120. struct attribute *attr, const char *buf,\
  121. size_t count) \
  122. { \
  123. int ret, i = 0; \
  124. char *s = kstrdup(buf, GFP_KERNEL); \
  125. unsigned int msg[2] = {0}; \
  126. char *str, *s_orig = s; \
  127. \
  128. while (((str = strsep(&s, " ")) != NULL) && i < 2) { \
  129. ret = kstrtouint(str, 10, &msg[i]); \
  130. if (ret < 0) { \
  131. pr_err("Invalid value :%d\n", ret); \
  132. goto out; \
  133. } \
  134. i++; \
  135. } \
  136. \
  137. pr_info("Input threshold :%lu for cluster :%lu\n", msg[1], msg[0]);\
  138. ret = ops->set_##name(ph, msg); \
  139. out: \
  140. kfree(s_orig); \
  141. return ((ret < 0) ? ret : count); \
  142. } \
  143. #define show_c1dcvs_thresh(name) \
  144. static ssize_t show_##name(struct kobject *kobj, \
  145. struct attribute *attr, char *buf) \
  146. { \
  147. unsigned int *vars = NULL; \
  148. int i, ret, tot = 0; \
  149. \
  150. if (!ops) \
  151. return -ENODEV; \
  152. \
  153. vars = kcalloc(num_possible_cpus(), sizeof(unsigned int), GFP_KERNEL);\
  154. if (!vars) \
  155. return -ENOMEM; \
  156. \
  157. ret = ops->get_##name(ph, vars); \
  158. if (ret < 0) { \
  159. kfree(vars); \
  160. return ret; \
  161. } \
  162. \
  163. for (i = 0; i < num_possible_cpus(); i++) { \
  164. vars[i] = le32_to_cpu(vars[i]); \
  165. if (!vars[i]) \
  166. break; \
  167. tot += scnprintf(buf + tot, PAGE_SIZE - tot, "%lu\t", vars[i]);\
  168. } \
  169. tot += scnprintf(buf + tot, PAGE_SIZE - tot, "\n"); \
  170. \
  171. kfree(vars); \
  172. return tot; \
  173. } \
  174. store_c1dcvs_thresh(ipc_thresh);
  175. show_c1dcvs_thresh(ipc_thresh);
  176. C1DCVS_ATTR_RW(ipc_thresh);
  177. store_c1dcvs_thresh(efreq_thresh);
  178. show_c1dcvs_thresh(efreq_thresh);
  179. C1DCVS_ATTR_RW(efreq_thresh);
  180. static struct attribute *c1dcvs_settings_attrs[] = {
  181. &enable_c1dcvs.attr,
  182. &enable_trace.attr,
  183. &ipc_thresh.attr,
  184. &efreq_thresh.attr,
  185. &hysteresis.attr,
  186. NULL,
  187. };
  188. ATTRIBUTE_GROUPS(c1dcvs_settings);
  189. static ssize_t attr_show(struct kobject *kobj, struct attribute *attr,
  190. char *buf)
  191. {
  192. struct qcom_c1dcvs_attr *c1dcvs_attr = to_c1dcvs_attr(attr);
  193. ssize_t ret = -EIO;
  194. if (c1dcvs_attr->show)
  195. ret = c1dcvs_attr->show(kobj, attr, buf);
  196. return ret;
  197. }
  198. static ssize_t attr_store(struct kobject *kobj, struct attribute *attr,
  199. const char *buf, size_t count)
  200. {
  201. struct qcom_c1dcvs_attr *c1dcvs_attr = to_c1dcvs_attr(attr);
  202. ssize_t ret = -EIO;
  203. if (c1dcvs_attr->store)
  204. ret = c1dcvs_attr->store(kobj, attr, buf, count);
  205. return ret;
  206. }
  207. static const struct sysfs_ops c1dcvs_sysfs_ops = {
  208. .show = attr_show,
  209. .store = attr_store,
  210. };
  211. static struct kobj_type c1dcvs_settings_ktype = {
  212. .sysfs_ops = &c1dcvs_sysfs_ops,
  213. .default_groups = c1dcvs_settings_groups,
  214. };
  215. static int scmi_c1dcvs_probe(struct scmi_device *sdev)
  216. {
  217. int ret;
  218. if (!sdev)
  219. return -ENODEV;
  220. ops = sdev->handle->devm_protocol_get(sdev, SCMI_C1DCVS_PROTOCOL, &ph);
  221. if (IS_ERR(ops)) {
  222. ret = PTR_ERR(ops);
  223. ops = NULL;
  224. return ret;
  225. }
  226. ret = kobject_init_and_add(&c1dcvs_kobj, &c1dcvs_settings_ktype,
  227. &cpu_subsys.dev_root->kobj, "c1dcvs");
  228. if (ret < 0) {
  229. pr_err("failed to init c1 dcvs kobj: %d\n", ret);
  230. kobject_put(&c1dcvs_kobj);
  231. }
  232. user_c1dcvs_en = kernel_c1dcvs_en = 1;
  233. return 0;
  234. }
  235. static const struct scmi_device_id scmi_id_table[] = {
  236. { .protocol_id = SCMI_C1DCVS_PROTOCOL, .name = "scmi_c1dcvs_protocol" },
  237. { },
  238. };
  239. MODULE_DEVICE_TABLE(scmi, scmi_id_table);
  240. static struct scmi_driver scmi_c1dcvs_drv = {
  241. .name = "scmi-c1dcvs-driver",
  242. .probe = scmi_c1dcvs_probe,
  243. .id_table = scmi_id_table,
  244. };
  245. module_scmi_driver(scmi_c1dcvs_drv);
  246. MODULE_SOFTDEP("pre: c1dcvs_vendor");
  247. MODULE_DESCRIPTION("ARM SCMI C1DCVS driver");
  248. MODULE_LICENSE("GPL");