nouveau_debugfs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2009 Red Hat <bskeggs@redhat.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. /*
  26. * Authors:
  27. * Ben Skeggs <bskeggs@redhat.com>
  28. */
  29. #include <linux/debugfs.h>
  30. #include <nvif/class.h>
  31. #include <nvif/if0001.h>
  32. #include "nouveau_debugfs.h"
  33. #include "nouveau_drv.h"
  34. static int
  35. nouveau_debugfs_vbios_image(struct seq_file *m, void *data)
  36. {
  37. struct drm_info_node *node = (struct drm_info_node *) m->private;
  38. struct nouveau_drm *drm = nouveau_drm(node->minor->dev);
  39. int i;
  40. for (i = 0; i < drm->vbios.length; i++)
  41. seq_printf(m, "%c", drm->vbios.data[i]);
  42. return 0;
  43. }
  44. static int
  45. nouveau_debugfs_pstate_get(struct seq_file *m, void *data)
  46. {
  47. struct drm_info_node *node = (struct drm_info_node *) m->private;
  48. struct nouveau_debugfs *debugfs = nouveau_debugfs(node->minor->dev);
  49. struct nvif_object *ctrl = &debugfs->ctrl;
  50. struct nvif_control_pstate_info_v0 info = {};
  51. int ret, i;
  52. if (!debugfs)
  53. return -ENODEV;
  54. ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_INFO, &info, sizeof(info));
  55. if (ret)
  56. return ret;
  57. for (i = 0; i < info.count + 1; i++) {
  58. const s32 state = i < info.count ? i :
  59. NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT;
  60. struct nvif_control_pstate_attr_v0 attr = {
  61. .state = state,
  62. .index = 0,
  63. };
  64. ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_ATTR,
  65. &attr, sizeof(attr));
  66. if (ret)
  67. return ret;
  68. if (i < info.count)
  69. seq_printf(m, "%02x:", attr.state);
  70. else
  71. seq_printf(m, "%s:", info.pwrsrc == 0 ? "DC" :
  72. info.pwrsrc == 1 ? "AC" : "--");
  73. attr.index = 0;
  74. do {
  75. attr.state = state;
  76. ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_ATTR,
  77. &attr, sizeof(attr));
  78. if (ret)
  79. return ret;
  80. seq_printf(m, " %s %d", attr.name, attr.min);
  81. if (attr.min != attr.max)
  82. seq_printf(m, "-%d", attr.max);
  83. seq_printf(m, " %s", attr.unit);
  84. } while (attr.index);
  85. if (state >= 0) {
  86. if (info.ustate_ac == state)
  87. seq_printf(m, " AC");
  88. if (info.ustate_dc == state)
  89. seq_printf(m, " DC");
  90. if (info.pstate == state)
  91. seq_printf(m, " *");
  92. } else {
  93. if (info.ustate_ac < -1)
  94. seq_printf(m, " AC");
  95. if (info.ustate_dc < -1)
  96. seq_printf(m, " DC");
  97. }
  98. seq_printf(m, "\n");
  99. }
  100. return 0;
  101. }
  102. static ssize_t
  103. nouveau_debugfs_pstate_set(struct file *file, const char __user *ubuf,
  104. size_t len, loff_t *offp)
  105. {
  106. struct seq_file *m = file->private_data;
  107. struct drm_info_node *node = (struct drm_info_node *) m->private;
  108. struct nouveau_debugfs *debugfs = nouveau_debugfs(node->minor->dev);
  109. struct nvif_object *ctrl = &debugfs->ctrl;
  110. struct nvif_control_pstate_user_v0 args = { .pwrsrc = -EINVAL };
  111. char buf[32] = {}, *tmp, *cur = buf;
  112. long value, ret;
  113. if (!debugfs)
  114. return -ENODEV;
  115. if (len >= sizeof(buf))
  116. return -EINVAL;
  117. if (copy_from_user(buf, ubuf, len))
  118. return -EFAULT;
  119. if ((tmp = strchr(buf, '\n')))
  120. *tmp = '\0';
  121. if (!strncasecmp(cur, "dc:", 3)) {
  122. args.pwrsrc = 0;
  123. cur += 3;
  124. } else
  125. if (!strncasecmp(cur, "ac:", 3)) {
  126. args.pwrsrc = 1;
  127. cur += 3;
  128. }
  129. if (!strcasecmp(cur, "none"))
  130. args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_UNKNOWN;
  131. else
  132. if (!strcasecmp(cur, "auto"))
  133. args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_PERFMON;
  134. else {
  135. ret = kstrtol(cur, 16, &value);
  136. if (ret)
  137. return ret;
  138. args.ustate = value;
  139. }
  140. ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_USER, &args, sizeof(args));
  141. if (ret < 0)
  142. return ret;
  143. return len;
  144. }
  145. static int
  146. nouveau_debugfs_pstate_open(struct inode *inode, struct file *file)
  147. {
  148. return single_open(file, nouveau_debugfs_pstate_get, inode->i_private);
  149. }
  150. static const struct file_operations nouveau_pstate_fops = {
  151. .owner = THIS_MODULE,
  152. .open = nouveau_debugfs_pstate_open,
  153. .read = seq_read,
  154. .write = nouveau_debugfs_pstate_set,
  155. };
  156. static struct drm_info_list nouveau_debugfs_list[] = {
  157. { "vbios.rom", nouveau_debugfs_vbios_image, 0, NULL },
  158. };
  159. #define NOUVEAU_DEBUGFS_ENTRIES ARRAY_SIZE(nouveau_debugfs_list)
  160. static const struct nouveau_debugfs_files {
  161. const char *name;
  162. const struct file_operations *fops;
  163. } nouveau_debugfs_files[] = {
  164. {"pstate", &nouveau_pstate_fops},
  165. };
  166. static int
  167. nouveau_debugfs_create_file(struct drm_minor *minor,
  168. const struct nouveau_debugfs_files *ndf)
  169. {
  170. struct drm_info_node *node;
  171. node = kmalloc(sizeof(*node), GFP_KERNEL);
  172. if (node == NULL)
  173. return -ENOMEM;
  174. node->minor = minor;
  175. node->info_ent = (const void *)ndf->fops;
  176. node->dent = debugfs_create_file(ndf->name, S_IRUGO | S_IWUSR,
  177. minor->debugfs_root, node, ndf->fops);
  178. if (!node->dent) {
  179. kfree(node);
  180. return -ENOMEM;
  181. }
  182. mutex_lock(&minor->debugfs_lock);
  183. list_add(&node->list, &minor->debugfs_list);
  184. mutex_unlock(&minor->debugfs_lock);
  185. return 0;
  186. }
  187. int
  188. nouveau_drm_debugfs_init(struct drm_minor *minor)
  189. {
  190. int i, ret;
  191. for (i = 0; i < ARRAY_SIZE(nouveau_debugfs_files); i++) {
  192. ret = nouveau_debugfs_create_file(minor,
  193. &nouveau_debugfs_files[i]);
  194. if (ret)
  195. return ret;
  196. }
  197. return drm_debugfs_create_files(nouveau_debugfs_list,
  198. NOUVEAU_DEBUGFS_ENTRIES,
  199. minor->debugfs_root, minor);
  200. }
  201. void
  202. nouveau_drm_debugfs_cleanup(struct drm_minor *minor)
  203. {
  204. int i;
  205. drm_debugfs_remove_files(nouveau_debugfs_list, NOUVEAU_DEBUGFS_ENTRIES,
  206. minor);
  207. for (i = 0; i < ARRAY_SIZE(nouveau_debugfs_files); i++) {
  208. drm_debugfs_remove_files((struct drm_info_list *)
  209. nouveau_debugfs_files[i].fops,
  210. 1, minor);
  211. }
  212. }
  213. int
  214. nouveau_debugfs_init(struct nouveau_drm *drm)
  215. {
  216. int ret;
  217. drm->debugfs = kzalloc(sizeof(*drm->debugfs), GFP_KERNEL);
  218. if (!drm->debugfs)
  219. return -ENOMEM;
  220. ret = nvif_object_init(&drm->device.object, 0, NVIF_CLASS_CONTROL,
  221. NULL, 0, &drm->debugfs->ctrl);
  222. if (ret)
  223. return ret;
  224. return 0;
  225. }
  226. void
  227. nouveau_debugfs_fini(struct nouveau_drm *drm)
  228. {
  229. if (drm->debugfs && drm->debugfs->ctrl.priv)
  230. nvif_object_fini(&drm->debugfs->ctrl);
  231. kfree(drm->debugfs);
  232. drm->debugfs = NULL;
  233. }