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