nouveau_sysfs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2013 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs <bskeggs@redhat.com>
  23. */
  24. #include <nvif/os.h>
  25. #include <nvif/class.h>
  26. #include <nvif/ioctl.h>
  27. #include "nouveau_sysfs.h"
  28. MODULE_PARM_DESC(pstate, "enable sysfs pstate file, which will be moved in the future");
  29. int nouveau_pstate;
  30. module_param_named(pstate, nouveau_pstate, int, 0400);
  31. static inline struct drm_device *
  32. drm_device(struct device *d)
  33. {
  34. return dev_get_drvdata(d);
  35. }
  36. #define snappendf(p,r,f,a...) do { \
  37. snprintf(p, r, f, ##a); \
  38. r -= strlen(p); \
  39. p += strlen(p); \
  40. } while(0)
  41. static ssize_t
  42. nouveau_sysfs_pstate_get(struct device *d, struct device_attribute *a, char *b)
  43. {
  44. struct nouveau_sysfs *sysfs = nouveau_sysfs(drm_device(d));
  45. struct nvif_control_pstate_info_v0 info = {};
  46. size_t cnt = PAGE_SIZE;
  47. char *buf = b;
  48. int ret, i;
  49. ret = nvif_mthd(&sysfs->ctrl, NVIF_CONTROL_PSTATE_INFO,
  50. &info, sizeof(info));
  51. if (ret)
  52. return ret;
  53. for (i = 0; i < info.count + 1; i++) {
  54. const s32 state = i < info.count ? i :
  55. NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT;
  56. struct nvif_control_pstate_attr_v0 attr = {
  57. .state = state,
  58. .index = 0,
  59. };
  60. ret = nvif_mthd(&sysfs->ctrl, NVIF_CONTROL_PSTATE_ATTR,
  61. &attr, sizeof(attr));
  62. if (ret)
  63. return ret;
  64. if (i < info.count)
  65. snappendf(buf, cnt, "%02x:", attr.state);
  66. else
  67. snappendf(buf, cnt, "%s:", info.pwrsrc == 0 ? "DC" :
  68. info.pwrsrc == 1 ? "AC" :
  69. "--");
  70. attr.index = 0;
  71. do {
  72. attr.state = state;
  73. ret = nvif_mthd(&sysfs->ctrl,
  74. NVIF_CONTROL_PSTATE_ATTR,
  75. &attr, sizeof(attr));
  76. if (ret)
  77. return ret;
  78. snappendf(buf, cnt, " %s %d", attr.name, attr.min);
  79. if (attr.min != attr.max)
  80. snappendf(buf, cnt, "-%d", attr.max);
  81. snappendf(buf, cnt, " %s", attr.unit);
  82. } while (attr.index);
  83. if (state >= 0) {
  84. if (info.ustate_ac == state)
  85. snappendf(buf, cnt, " AC");
  86. if (info.ustate_dc == state)
  87. snappendf(buf, cnt, " DC");
  88. if (info.pstate == state)
  89. snappendf(buf, cnt, " *");
  90. } else {
  91. if (info.ustate_ac < -1)
  92. snappendf(buf, cnt, " AC");
  93. if (info.ustate_dc < -1)
  94. snappendf(buf, cnt, " DC");
  95. }
  96. snappendf(buf, cnt, "\n");
  97. }
  98. return strlen(b);
  99. }
  100. static ssize_t
  101. nouveau_sysfs_pstate_set(struct device *d, struct device_attribute *a,
  102. const char *buf, size_t count)
  103. {
  104. struct nouveau_sysfs *sysfs = nouveau_sysfs(drm_device(d));
  105. struct nvif_control_pstate_user_v0 args = { .pwrsrc = -EINVAL };
  106. long value, ret;
  107. char *tmp;
  108. if ((tmp = strchr(buf, '\n')))
  109. *tmp = '\0';
  110. if (!strncasecmp(buf, "dc:", 3)) {
  111. args.pwrsrc = 0;
  112. buf += 3;
  113. } else
  114. if (!strncasecmp(buf, "ac:", 3)) {
  115. args.pwrsrc = 1;
  116. buf += 3;
  117. }
  118. if (!strcasecmp(buf, "none"))
  119. args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_UNKNOWN;
  120. else
  121. if (!strcasecmp(buf, "auto"))
  122. args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_PERFMON;
  123. else {
  124. ret = kstrtol(buf, 16, &value);
  125. if (ret)
  126. return ret;
  127. args.ustate = value;
  128. }
  129. ret = nvif_mthd(&sysfs->ctrl, NVIF_CONTROL_PSTATE_USER,
  130. &args, sizeof(args));
  131. if (ret < 0)
  132. return ret;
  133. return count;
  134. }
  135. static DEVICE_ATTR(pstate, S_IRUGO | S_IWUSR,
  136. nouveau_sysfs_pstate_get, nouveau_sysfs_pstate_set);
  137. void
  138. nouveau_sysfs_fini(struct drm_device *dev)
  139. {
  140. struct nouveau_sysfs *sysfs = nouveau_sysfs(dev);
  141. struct nouveau_drm *drm = nouveau_drm(dev);
  142. struct nvif_device *device = &drm->device;
  143. if (sysfs && sysfs->ctrl.priv) {
  144. device_remove_file(nvxx_device(device)->dev, &dev_attr_pstate);
  145. nvif_object_fini(&sysfs->ctrl);
  146. }
  147. drm->sysfs = NULL;
  148. kfree(sysfs);
  149. }
  150. int
  151. nouveau_sysfs_init(struct drm_device *dev)
  152. {
  153. struct nouveau_drm *drm = nouveau_drm(dev);
  154. struct nvif_device *device = &drm->device;
  155. struct nouveau_sysfs *sysfs;
  156. int ret;
  157. if (!nouveau_pstate)
  158. return 0;
  159. sysfs = drm->sysfs = kzalloc(sizeof(*sysfs), GFP_KERNEL);
  160. if (!sysfs)
  161. return -ENOMEM;
  162. ret = nvif_object_init(&device->object, 0, NVIF_IOCTL_NEW_V0_CONTROL,
  163. NULL, 0, &sysfs->ctrl);
  164. if (ret == 0)
  165. device_create_file(nvxx_device(device)->dev, &dev_attr_pstate);
  166. return 0;
  167. }