vas-sysfs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2022-23 IBM Corp.
  4. */
  5. #define pr_fmt(fmt) "vas: " fmt
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/miscdevice.h>
  9. #include <linux/kobject.h>
  10. #include <linux/slab.h>
  11. #include <linux/mm.h>
  12. #include "vas.h"
  13. #ifdef CONFIG_SYSFS
  14. static struct kobject *pseries_vas_kobj;
  15. static struct kobject *gzip_caps_kobj;
  16. struct vas_caps_entry {
  17. struct kobject kobj;
  18. struct vas_cop_feat_caps *caps;
  19. };
  20. #define to_caps_entry(entry) container_of(entry, struct vas_caps_entry, kobj)
  21. /*
  22. * This function is used to get the notification from the drmgr when
  23. * QoS credits are changed.
  24. */
  25. static ssize_t update_total_credits_store(struct vas_cop_feat_caps *caps,
  26. const char *buf, size_t count)
  27. {
  28. int err;
  29. u16 creds;
  30. err = kstrtou16(buf, 0, &creds);
  31. /*
  32. * The user space interface from the management console
  33. * notifies OS with the new QoS credits and then the
  34. * hypervisor. So OS has to use this new credits value
  35. * and reconfigure VAS windows (close or reopen depends
  36. * on the credits available) instead of depending on VAS
  37. * QoS capabilities from the hypervisor.
  38. */
  39. if (!err)
  40. err = vas_reconfig_capabilties(caps->win_type, creds);
  41. if (err)
  42. return -EINVAL;
  43. pr_info("Set QoS total credits %u\n", creds);
  44. return count;
  45. }
  46. #define sysfs_caps_entry_read(_name) \
  47. static ssize_t _name##_show(struct vas_cop_feat_caps *caps, char *buf) \
  48. { \
  49. return sprintf(buf, "%d\n", atomic_read(&caps->_name)); \
  50. }
  51. struct vas_sysfs_entry {
  52. struct attribute attr;
  53. ssize_t (*show)(struct vas_cop_feat_caps *, char *);
  54. ssize_t (*store)(struct vas_cop_feat_caps *, const char *, size_t);
  55. };
  56. #define VAS_ATTR_RO(_name) \
  57. sysfs_caps_entry_read(_name); \
  58. static struct vas_sysfs_entry _name##_attribute = __ATTR(_name, \
  59. 0444, _name##_show, NULL);
  60. /*
  61. * Create sysfs interface:
  62. * /sys/devices/virtual/misc/vas/vas0/gzip/default_capabilities
  63. * This directory contains the following VAS GZIP capabilities
  64. * for the default credit type.
  65. * /sys/devices/virtual/misc/vas/vas0/gzip/default_capabilities/nr_total_credits
  66. * Total number of default credits assigned to the LPAR which
  67. * can be changed with DLPAR operation.
  68. * /sys/devices/virtual/misc/vas/vas0/gzip/default_capabilities/nr_used_credits
  69. * Number of credits used by the user space. One credit will
  70. * be assigned for each window open.
  71. *
  72. * /sys/devices/virtual/misc/vas/vas0/gzip/qos_capabilities
  73. * This directory contains the following VAS GZIP capabilities
  74. * for the Quality of Service (QoS) credit type.
  75. * /sys/devices/virtual/misc/vas/vas0/gzip/qos_capabilities/nr_total_credits
  76. * Total number of QoS credits assigned to the LPAR. The user
  77. * has to define this value using HMC interface. It can be
  78. * changed dynamically by the user.
  79. * /sys/devices/virtual/misc/vas/vas0/gzip/qos_capabilities/nr_used_credits
  80. * Number of credits used by the user space.
  81. * /sys/devices/virtual/misc/vas/vas0/gzip/qos_capabilities/update_total_credits
  82. * Update total QoS credits dynamically
  83. */
  84. VAS_ATTR_RO(nr_total_credits);
  85. VAS_ATTR_RO(nr_used_credits);
  86. static struct vas_sysfs_entry update_total_credits_attribute =
  87. __ATTR(update_total_credits, 0200, NULL, update_total_credits_store);
  88. static struct attribute *vas_def_capab_attrs[] = {
  89. &nr_total_credits_attribute.attr,
  90. &nr_used_credits_attribute.attr,
  91. NULL,
  92. };
  93. ATTRIBUTE_GROUPS(vas_def_capab);
  94. static struct attribute *vas_qos_capab_attrs[] = {
  95. &nr_total_credits_attribute.attr,
  96. &nr_used_credits_attribute.attr,
  97. &update_total_credits_attribute.attr,
  98. NULL,
  99. };
  100. ATTRIBUTE_GROUPS(vas_qos_capab);
  101. static ssize_t vas_type_show(struct kobject *kobj, struct attribute *attr,
  102. char *buf)
  103. {
  104. struct vas_caps_entry *centry;
  105. struct vas_cop_feat_caps *caps;
  106. struct vas_sysfs_entry *entry;
  107. centry = to_caps_entry(kobj);
  108. caps = centry->caps;
  109. entry = container_of(attr, struct vas_sysfs_entry, attr);
  110. if (!entry->show)
  111. return -EIO;
  112. return entry->show(caps, buf);
  113. }
  114. static ssize_t vas_type_store(struct kobject *kobj, struct attribute *attr,
  115. const char *buf, size_t count)
  116. {
  117. struct vas_caps_entry *centry;
  118. struct vas_cop_feat_caps *caps;
  119. struct vas_sysfs_entry *entry;
  120. centry = to_caps_entry(kobj);
  121. caps = centry->caps;
  122. entry = container_of(attr, struct vas_sysfs_entry, attr);
  123. if (!entry->store)
  124. return -EIO;
  125. return entry->store(caps, buf, count);
  126. }
  127. static void vas_type_release(struct kobject *kobj)
  128. {
  129. struct vas_caps_entry *centry = to_caps_entry(kobj);
  130. kfree(centry);
  131. }
  132. static const struct sysfs_ops vas_sysfs_ops = {
  133. .show = vas_type_show,
  134. .store = vas_type_store,
  135. };
  136. static struct kobj_type vas_def_attr_type = {
  137. .release = vas_type_release,
  138. .sysfs_ops = &vas_sysfs_ops,
  139. .default_groups = vas_def_capab_groups,
  140. };
  141. static struct kobj_type vas_qos_attr_type = {
  142. .release = vas_type_release,
  143. .sysfs_ops = &vas_sysfs_ops,
  144. .default_groups = vas_qos_capab_groups,
  145. };
  146. static char *vas_caps_kobj_name(struct vas_caps_entry *centry,
  147. struct kobject **kobj)
  148. {
  149. struct vas_cop_feat_caps *caps = centry->caps;
  150. if (caps->descriptor == VAS_GZIP_QOS_CAPABILITIES) {
  151. kobject_init(&centry->kobj, &vas_qos_attr_type);
  152. *kobj = gzip_caps_kobj;
  153. return "qos_capabilities";
  154. } else if (caps->descriptor == VAS_GZIP_DEFAULT_CAPABILITIES) {
  155. kobject_init(&centry->kobj, &vas_def_attr_type);
  156. *kobj = gzip_caps_kobj;
  157. return "default_capabilities";
  158. } else
  159. return "Unknown";
  160. }
  161. /*
  162. * Add feature specific capability dir entry.
  163. * Ex: VDefGzip or VQosGzip
  164. */
  165. int sysfs_add_vas_caps(struct vas_cop_feat_caps *caps)
  166. {
  167. struct vas_caps_entry *centry;
  168. struct kobject *kobj = NULL;
  169. int ret = 0;
  170. char *name;
  171. centry = kzalloc(sizeof(*centry), GFP_KERNEL);
  172. if (!centry)
  173. return -ENOMEM;
  174. centry->caps = caps;
  175. name = vas_caps_kobj_name(centry, &kobj);
  176. if (kobj) {
  177. ret = kobject_add(&centry->kobj, kobj, "%s", name);
  178. if (ret) {
  179. pr_err("VAS: sysfs kobject add / event failed %d\n",
  180. ret);
  181. kobject_put(&centry->kobj);
  182. }
  183. }
  184. return ret;
  185. }
  186. static struct miscdevice vas_miscdev = {
  187. .minor = MISC_DYNAMIC_MINOR,
  188. .name = "vas",
  189. };
  190. /*
  191. * Add VAS and VasCaps (overall capabilities) dir entries.
  192. */
  193. int __init sysfs_pseries_vas_init(struct vas_all_caps *vas_caps)
  194. {
  195. int ret;
  196. ret = misc_register(&vas_miscdev);
  197. if (ret < 0) {
  198. pr_err("%s: register vas misc device failed\n", __func__);
  199. return ret;
  200. }
  201. /*
  202. * The hypervisor does not expose multiple VAS instances, but can
  203. * see multiple VAS instances on PowerNV. So create 'vas0' directory
  204. * on pseries.
  205. */
  206. pseries_vas_kobj = kobject_create_and_add("vas0",
  207. &vas_miscdev.this_device->kobj);
  208. if (!pseries_vas_kobj) {
  209. misc_deregister(&vas_miscdev);
  210. pr_err("Failed to create VAS sysfs entry\n");
  211. return -ENOMEM;
  212. }
  213. if ((vas_caps->feat_type & VAS_GZIP_QOS_FEAT_BIT) ||
  214. (vas_caps->feat_type & VAS_GZIP_DEF_FEAT_BIT)) {
  215. gzip_caps_kobj = kobject_create_and_add("gzip",
  216. pseries_vas_kobj);
  217. if (!gzip_caps_kobj) {
  218. pr_err("Failed to create VAS GZIP capability entry\n");
  219. kobject_put(pseries_vas_kobj);
  220. misc_deregister(&vas_miscdev);
  221. return -ENOMEM;
  222. }
  223. }
  224. return 0;
  225. }
  226. #else
  227. int sysfs_add_vas_caps(struct vas_cop_feat_caps *caps)
  228. {
  229. return 0;
  230. }
  231. int __init sysfs_pseries_vas_init(struct vas_all_caps *vas_caps)
  232. {
  233. return 0;
  234. }
  235. #endif