nouveau_debugfs.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_strap_peek(struct seq_file *m, void *data)
  46. {
  47. struct drm_info_node *node = m->private;
  48. struct nouveau_drm *drm = nouveau_drm(node->minor->dev);
  49. int ret;
  50. ret = pm_runtime_get_sync(drm->dev->dev);
  51. if (ret < 0 && ret != -EACCES) {
  52. pm_runtime_put_autosuspend(drm->dev->dev);
  53. return ret;
  54. }
  55. seq_printf(m, "0x%08x\n",
  56. nvif_rd32(&drm->client.device.object, 0x101000));
  57. pm_runtime_mark_last_busy(drm->dev->dev);
  58. pm_runtime_put_autosuspend(drm->dev->dev);
  59. return 0;
  60. }
  61. static int
  62. nouveau_debugfs_pstate_get(struct seq_file *m, void *data)
  63. {
  64. struct drm_device *drm = m->private;
  65. struct nouveau_debugfs *debugfs = nouveau_debugfs(drm);
  66. struct nvif_object *ctrl = &debugfs->ctrl;
  67. struct nvif_control_pstate_info_v0 info = {};
  68. int ret, i;
  69. if (!debugfs)
  70. return -ENODEV;
  71. ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_INFO, &info, sizeof(info));
  72. if (ret)
  73. return ret;
  74. for (i = 0; i < info.count + 1; i++) {
  75. const s32 state = i < info.count ? i :
  76. NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT;
  77. struct nvif_control_pstate_attr_v0 attr = {
  78. .state = state,
  79. .index = 0,
  80. };
  81. ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_ATTR,
  82. &attr, sizeof(attr));
  83. if (ret)
  84. return ret;
  85. if (i < info.count)
  86. seq_printf(m, "%02x:", attr.state);
  87. else
  88. seq_printf(m, "%s:", info.pwrsrc == 0 ? "DC" :
  89. info.pwrsrc == 1 ? "AC" : "--");
  90. attr.index = 0;
  91. do {
  92. attr.state = state;
  93. ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_ATTR,
  94. &attr, sizeof(attr));
  95. if (ret)
  96. return ret;
  97. seq_printf(m, " %s %d", attr.name, attr.min);
  98. if (attr.min != attr.max)
  99. seq_printf(m, "-%d", attr.max);
  100. seq_printf(m, " %s", attr.unit);
  101. } while (attr.index);
  102. if (state >= 0) {
  103. if (info.ustate_ac == state)
  104. seq_printf(m, " AC");
  105. if (info.ustate_dc == state)
  106. seq_printf(m, " DC");
  107. if (info.pstate == state)
  108. seq_printf(m, " *");
  109. } else {
  110. if (info.ustate_ac < -1)
  111. seq_printf(m, " AC");
  112. if (info.ustate_dc < -1)
  113. seq_printf(m, " DC");
  114. }
  115. seq_printf(m, "\n");
  116. }
  117. return 0;
  118. }
  119. static ssize_t
  120. nouveau_debugfs_pstate_set(struct file *file, const char __user *ubuf,
  121. size_t len, loff_t *offp)
  122. {
  123. struct seq_file *m = file->private_data;
  124. struct drm_device *drm = m->private;
  125. struct nouveau_debugfs *debugfs = nouveau_debugfs(drm);
  126. struct nvif_object *ctrl = &debugfs->ctrl;
  127. struct nvif_control_pstate_user_v0 args = { .pwrsrc = -EINVAL };
  128. char buf[32] = {}, *tmp, *cur = buf;
  129. long value, ret;
  130. if (!debugfs)
  131. return -ENODEV;
  132. if (len >= sizeof(buf))
  133. return -EINVAL;
  134. if (copy_from_user(buf, ubuf, len))
  135. return -EFAULT;
  136. if ((tmp = strchr(buf, '\n')))
  137. *tmp = '\0';
  138. if (!strncasecmp(cur, "dc:", 3)) {
  139. args.pwrsrc = 0;
  140. cur += 3;
  141. } else
  142. if (!strncasecmp(cur, "ac:", 3)) {
  143. args.pwrsrc = 1;
  144. cur += 3;
  145. }
  146. if (!strcasecmp(cur, "none"))
  147. args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_UNKNOWN;
  148. else
  149. if (!strcasecmp(cur, "auto"))
  150. args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_PERFMON;
  151. else {
  152. ret = kstrtol(cur, 16, &value);
  153. if (ret)
  154. return ret;
  155. args.ustate = value;
  156. }
  157. ret = pm_runtime_get_sync(drm->dev);
  158. if (ret < 0 && ret != -EACCES) {
  159. pm_runtime_put_autosuspend(drm->dev);
  160. return ret;
  161. }
  162. ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_USER, &args, sizeof(args));
  163. pm_runtime_put_autosuspend(drm->dev);
  164. if (ret < 0)
  165. return ret;
  166. return len;
  167. }
  168. static int
  169. nouveau_debugfs_pstate_open(struct inode *inode, struct file *file)
  170. {
  171. return single_open(file, nouveau_debugfs_pstate_get, inode->i_private);
  172. }
  173. static const struct file_operations nouveau_pstate_fops = {
  174. .owner = THIS_MODULE,
  175. .open = nouveau_debugfs_pstate_open,
  176. .read = seq_read,
  177. .write = nouveau_debugfs_pstate_set,
  178. .release = single_release,
  179. };
  180. static struct drm_info_list nouveau_debugfs_list[] = {
  181. { "vbios.rom", nouveau_debugfs_vbios_image, 0, NULL },
  182. { "strap_peek", nouveau_debugfs_strap_peek, 0, NULL },
  183. };
  184. #define NOUVEAU_DEBUGFS_ENTRIES ARRAY_SIZE(nouveau_debugfs_list)
  185. static const struct nouveau_debugfs_files {
  186. const char *name;
  187. const struct file_operations *fops;
  188. } nouveau_debugfs_files[] = {
  189. {"pstate", &nouveau_pstate_fops},
  190. };
  191. void
  192. nouveau_drm_debugfs_init(struct drm_minor *minor)
  193. {
  194. struct nouveau_drm *drm = nouveau_drm(minor->dev);
  195. struct dentry *dentry;
  196. int i;
  197. for (i = 0; i < ARRAY_SIZE(nouveau_debugfs_files); i++) {
  198. debugfs_create_file(nouveau_debugfs_files[i].name,
  199. S_IRUGO | S_IWUSR,
  200. minor->debugfs_root, minor->dev,
  201. nouveau_debugfs_files[i].fops);
  202. }
  203. drm_debugfs_create_files(nouveau_debugfs_list,
  204. NOUVEAU_DEBUGFS_ENTRIES,
  205. minor->debugfs_root, minor);
  206. /* Set the size of the vbios since we know it, and it's confusing to
  207. * userspace if it wants to seek() but the file has a length of 0
  208. */
  209. dentry = debugfs_lookup("vbios.rom", minor->debugfs_root);
  210. if (!dentry)
  211. return;
  212. d_inode(dentry)->i_size = drm->vbios.length;
  213. dput(dentry);
  214. }
  215. int
  216. nouveau_debugfs_init(struct nouveau_drm *drm)
  217. {
  218. int ret;
  219. drm->debugfs = kzalloc(sizeof(*drm->debugfs), GFP_KERNEL);
  220. if (!drm->debugfs)
  221. return -ENOMEM;
  222. ret = nvif_object_ctor(&drm->client.device.object, "debugfsCtrl", 0,
  223. NVIF_CLASS_CONTROL, NULL, 0,
  224. &drm->debugfs->ctrl);
  225. if (ret)
  226. return ret;
  227. return 0;
  228. }
  229. void
  230. nouveau_debugfs_fini(struct nouveau_drm *drm)
  231. {
  232. if (drm->debugfs && drm->debugfs->ctrl.priv)
  233. nvif_object_dtor(&drm->debugfs->ctrl);
  234. kfree(drm->debugfs);
  235. drm->debugfs = NULL;
  236. }