18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (C) 2009 Red Hat <bskeggs@redhat.com>
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
58c2ecf20Sopenharmony_ci * a copy of this software and associated documentation files (the
68c2ecf20Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
78c2ecf20Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
88c2ecf20Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
98c2ecf20Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
108c2ecf20Sopenharmony_ci * the following conditions:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the
138c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial
148c2ecf20Sopenharmony_ci * portions of the Software.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
178c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
188c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
198c2ecf20Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
208c2ecf20Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
218c2ecf20Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
228c2ecf20Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * Authors:
288c2ecf20Sopenharmony_ci *  Ben Skeggs <bskeggs@redhat.com>
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
328c2ecf20Sopenharmony_ci#include <nvif/class.h>
338c2ecf20Sopenharmony_ci#include <nvif/if0001.h>
348c2ecf20Sopenharmony_ci#include "nouveau_debugfs.h"
358c2ecf20Sopenharmony_ci#include "nouveau_drv.h"
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int
388c2ecf20Sopenharmony_cinouveau_debugfs_vbios_image(struct seq_file *m, void *data)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct drm_info_node *node = (struct drm_info_node *) m->private;
418c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(node->minor->dev);
428c2ecf20Sopenharmony_ci	int i;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	for (i = 0; i < drm->vbios.length; i++)
458c2ecf20Sopenharmony_ci		seq_printf(m, "%c", drm->vbios.data[i]);
468c2ecf20Sopenharmony_ci	return 0;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int
508c2ecf20Sopenharmony_cinouveau_debugfs_strap_peek(struct seq_file *m, void *data)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct drm_info_node *node = m->private;
538c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(node->minor->dev);
548c2ecf20Sopenharmony_ci	int ret;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	ret = pm_runtime_get_sync(drm->dev->dev);
578c2ecf20Sopenharmony_ci	if (ret < 0 && ret != -EACCES) {
588c2ecf20Sopenharmony_ci		pm_runtime_put_autosuspend(drm->dev->dev);
598c2ecf20Sopenharmony_ci		return ret;
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	seq_printf(m, "0x%08x\n",
638c2ecf20Sopenharmony_ci		   nvif_rd32(&drm->client.device.object, 0x101000));
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	pm_runtime_mark_last_busy(drm->dev->dev);
668c2ecf20Sopenharmony_ci	pm_runtime_put_autosuspend(drm->dev->dev);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	return 0;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic int
728c2ecf20Sopenharmony_cinouveau_debugfs_pstate_get(struct seq_file *m, void *data)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct drm_device *drm = m->private;
758c2ecf20Sopenharmony_ci	struct nouveau_debugfs *debugfs = nouveau_debugfs(drm);
768c2ecf20Sopenharmony_ci	struct nvif_object *ctrl = &debugfs->ctrl;
778c2ecf20Sopenharmony_ci	struct nvif_control_pstate_info_v0 info = {};
788c2ecf20Sopenharmony_ci	int ret, i;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (!debugfs)
818c2ecf20Sopenharmony_ci		return -ENODEV;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_INFO, &info, sizeof(info));
848c2ecf20Sopenharmony_ci	if (ret)
858c2ecf20Sopenharmony_ci		return ret;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	for (i = 0; i < info.count + 1; i++) {
888c2ecf20Sopenharmony_ci		const s32 state = i < info.count ? i :
898c2ecf20Sopenharmony_ci			NVIF_CONTROL_PSTATE_ATTR_V0_STATE_CURRENT;
908c2ecf20Sopenharmony_ci		struct nvif_control_pstate_attr_v0 attr = {
918c2ecf20Sopenharmony_ci			.state = state,
928c2ecf20Sopenharmony_ci			.index = 0,
938c2ecf20Sopenharmony_ci		};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci		ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_ATTR,
968c2ecf20Sopenharmony_ci				&attr, sizeof(attr));
978c2ecf20Sopenharmony_ci		if (ret)
988c2ecf20Sopenharmony_ci			return ret;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci		if (i < info.count)
1018c2ecf20Sopenharmony_ci			seq_printf(m, "%02x:", attr.state);
1028c2ecf20Sopenharmony_ci		else
1038c2ecf20Sopenharmony_ci			seq_printf(m, "%s:", info.pwrsrc == 0 ? "DC" :
1048c2ecf20Sopenharmony_ci					     info.pwrsrc == 1 ? "AC" : "--");
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci		attr.index = 0;
1078c2ecf20Sopenharmony_ci		do {
1088c2ecf20Sopenharmony_ci			attr.state = state;
1098c2ecf20Sopenharmony_ci			ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_ATTR,
1108c2ecf20Sopenharmony_ci					&attr, sizeof(attr));
1118c2ecf20Sopenharmony_ci			if (ret)
1128c2ecf20Sopenharmony_ci				return ret;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci			seq_printf(m, " %s %d", attr.name, attr.min);
1158c2ecf20Sopenharmony_ci			if (attr.min != attr.max)
1168c2ecf20Sopenharmony_ci				seq_printf(m, "-%d", attr.max);
1178c2ecf20Sopenharmony_ci			seq_printf(m, " %s", attr.unit);
1188c2ecf20Sopenharmony_ci		} while (attr.index);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci		if (state >= 0) {
1218c2ecf20Sopenharmony_ci			if (info.ustate_ac == state)
1228c2ecf20Sopenharmony_ci				seq_printf(m, " AC");
1238c2ecf20Sopenharmony_ci			if (info.ustate_dc == state)
1248c2ecf20Sopenharmony_ci				seq_printf(m, " DC");
1258c2ecf20Sopenharmony_ci			if (info.pstate == state)
1268c2ecf20Sopenharmony_ci				seq_printf(m, " *");
1278c2ecf20Sopenharmony_ci		} else {
1288c2ecf20Sopenharmony_ci			if (info.ustate_ac < -1)
1298c2ecf20Sopenharmony_ci				seq_printf(m, " AC");
1308c2ecf20Sopenharmony_ci			if (info.ustate_dc < -1)
1318c2ecf20Sopenharmony_ci				seq_printf(m, " DC");
1328c2ecf20Sopenharmony_ci		}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		seq_printf(m, "\n");
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	return 0;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic ssize_t
1418c2ecf20Sopenharmony_cinouveau_debugfs_pstate_set(struct file *file, const char __user *ubuf,
1428c2ecf20Sopenharmony_ci			   size_t len, loff_t *offp)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	struct seq_file *m = file->private_data;
1458c2ecf20Sopenharmony_ci	struct drm_device *drm = m->private;
1468c2ecf20Sopenharmony_ci	struct nouveau_debugfs *debugfs = nouveau_debugfs(drm);
1478c2ecf20Sopenharmony_ci	struct nvif_object *ctrl = &debugfs->ctrl;
1488c2ecf20Sopenharmony_ci	struct nvif_control_pstate_user_v0 args = { .pwrsrc = -EINVAL };
1498c2ecf20Sopenharmony_ci	char buf[32] = {}, *tmp, *cur = buf;
1508c2ecf20Sopenharmony_ci	long value, ret;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (!debugfs)
1538c2ecf20Sopenharmony_ci		return -ENODEV;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	if (len >= sizeof(buf))
1568c2ecf20Sopenharmony_ci		return -EINVAL;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	if (copy_from_user(buf, ubuf, len))
1598c2ecf20Sopenharmony_ci		return -EFAULT;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	if ((tmp = strchr(buf, '\n')))
1628c2ecf20Sopenharmony_ci		*tmp = '\0';
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	if (!strncasecmp(cur, "dc:", 3)) {
1658c2ecf20Sopenharmony_ci		args.pwrsrc = 0;
1668c2ecf20Sopenharmony_ci		cur += 3;
1678c2ecf20Sopenharmony_ci	} else
1688c2ecf20Sopenharmony_ci	if (!strncasecmp(cur, "ac:", 3)) {
1698c2ecf20Sopenharmony_ci		args.pwrsrc = 1;
1708c2ecf20Sopenharmony_ci		cur += 3;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (!strcasecmp(cur, "none"))
1748c2ecf20Sopenharmony_ci		args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_UNKNOWN;
1758c2ecf20Sopenharmony_ci	else
1768c2ecf20Sopenharmony_ci	if (!strcasecmp(cur, "auto"))
1778c2ecf20Sopenharmony_ci		args.ustate = NVIF_CONTROL_PSTATE_USER_V0_STATE_PERFMON;
1788c2ecf20Sopenharmony_ci	else {
1798c2ecf20Sopenharmony_ci		ret = kstrtol(cur, 16, &value);
1808c2ecf20Sopenharmony_ci		if (ret)
1818c2ecf20Sopenharmony_ci			return ret;
1828c2ecf20Sopenharmony_ci		args.ustate = value;
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	ret = pm_runtime_get_sync(drm->dev);
1868c2ecf20Sopenharmony_ci	if (ret < 0 && ret != -EACCES) {
1878c2ecf20Sopenharmony_ci		pm_runtime_put_autosuspend(drm->dev);
1888c2ecf20Sopenharmony_ci		return ret;
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	ret = nvif_mthd(ctrl, NVIF_CONTROL_PSTATE_USER, &args, sizeof(args));
1928c2ecf20Sopenharmony_ci	pm_runtime_put_autosuspend(drm->dev);
1938c2ecf20Sopenharmony_ci	if (ret < 0)
1948c2ecf20Sopenharmony_ci		return ret;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	return len;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic int
2008c2ecf20Sopenharmony_cinouveau_debugfs_pstate_open(struct inode *inode, struct file *file)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	return single_open(file, nouveau_debugfs_pstate_get, inode->i_private);
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic const struct file_operations nouveau_pstate_fops = {
2068c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
2078c2ecf20Sopenharmony_ci	.open = nouveau_debugfs_pstate_open,
2088c2ecf20Sopenharmony_ci	.read = seq_read,
2098c2ecf20Sopenharmony_ci	.write = nouveau_debugfs_pstate_set,
2108c2ecf20Sopenharmony_ci	.release = single_release,
2118c2ecf20Sopenharmony_ci};
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic struct drm_info_list nouveau_debugfs_list[] = {
2148c2ecf20Sopenharmony_ci	{ "vbios.rom",  nouveau_debugfs_vbios_image, 0, NULL },
2158c2ecf20Sopenharmony_ci	{ "strap_peek", nouveau_debugfs_strap_peek, 0, NULL },
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci#define NOUVEAU_DEBUGFS_ENTRIES ARRAY_SIZE(nouveau_debugfs_list)
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic const struct nouveau_debugfs_files {
2208c2ecf20Sopenharmony_ci	const char *name;
2218c2ecf20Sopenharmony_ci	const struct file_operations *fops;
2228c2ecf20Sopenharmony_ci} nouveau_debugfs_files[] = {
2238c2ecf20Sopenharmony_ci	{"pstate", &nouveau_pstate_fops},
2248c2ecf20Sopenharmony_ci};
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_civoid
2278c2ecf20Sopenharmony_cinouveau_drm_debugfs_init(struct drm_minor *minor)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	struct nouveau_drm *drm = nouveau_drm(minor->dev);
2308c2ecf20Sopenharmony_ci	struct dentry *dentry;
2318c2ecf20Sopenharmony_ci	int i;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(nouveau_debugfs_files); i++) {
2348c2ecf20Sopenharmony_ci		debugfs_create_file(nouveau_debugfs_files[i].name,
2358c2ecf20Sopenharmony_ci				    S_IRUGO | S_IWUSR,
2368c2ecf20Sopenharmony_ci				    minor->debugfs_root, minor->dev,
2378c2ecf20Sopenharmony_ci				    nouveau_debugfs_files[i].fops);
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	drm_debugfs_create_files(nouveau_debugfs_list,
2418c2ecf20Sopenharmony_ci				 NOUVEAU_DEBUGFS_ENTRIES,
2428c2ecf20Sopenharmony_ci				 minor->debugfs_root, minor);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	/* Set the size of the vbios since we know it, and it's confusing to
2458c2ecf20Sopenharmony_ci	 * userspace if it wants to seek() but the file has a length of 0
2468c2ecf20Sopenharmony_ci	 */
2478c2ecf20Sopenharmony_ci	dentry = debugfs_lookup("vbios.rom", minor->debugfs_root);
2488c2ecf20Sopenharmony_ci	if (!dentry)
2498c2ecf20Sopenharmony_ci		return;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	d_inode(dentry)->i_size = drm->vbios.length;
2528c2ecf20Sopenharmony_ci	dput(dentry);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ciint
2568c2ecf20Sopenharmony_cinouveau_debugfs_init(struct nouveau_drm *drm)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	int ret;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	drm->debugfs = kzalloc(sizeof(*drm->debugfs), GFP_KERNEL);
2618c2ecf20Sopenharmony_ci	if (!drm->debugfs)
2628c2ecf20Sopenharmony_ci		return -ENOMEM;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	ret = nvif_object_ctor(&drm->client.device.object, "debugfsCtrl", 0,
2658c2ecf20Sopenharmony_ci			       NVIF_CLASS_CONTROL, NULL, 0,
2668c2ecf20Sopenharmony_ci			       &drm->debugfs->ctrl);
2678c2ecf20Sopenharmony_ci	if (ret)
2688c2ecf20Sopenharmony_ci		return ret;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	return 0;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_civoid
2748c2ecf20Sopenharmony_cinouveau_debugfs_fini(struct nouveau_drm *drm)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	if (drm->debugfs && drm->debugfs->ctrl.priv)
2778c2ecf20Sopenharmony_ci		nvif_object_dtor(&drm->debugfs->ctrl);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	kfree(drm->debugfs);
2808c2ecf20Sopenharmony_ci	drm->debugfs = NULL;
2818c2ecf20Sopenharmony_ci}
282