162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2016-2022 Advanced Micro Devices, Inc.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
662306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
762306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
862306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
962306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
1062306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1362306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1662306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1762306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1862306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1962306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2062306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2162306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include <linux/debugfs.h>
2562306a36Sopenharmony_ci#include <linux/uaccess.h>
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#include "kfd_priv.h"
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistatic struct dentry *debugfs_root;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistatic int kfd_debugfs_open(struct inode *inode, struct file *file)
3262306a36Sopenharmony_ci{
3362306a36Sopenharmony_ci	int (*show)(struct seq_file *, void *) = inode->i_private;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	return single_open(file, show, NULL);
3662306a36Sopenharmony_ci}
3762306a36Sopenharmony_cistatic int kfd_debugfs_hang_hws_read(struct seq_file *m, void *data)
3862306a36Sopenharmony_ci{
3962306a36Sopenharmony_ci	seq_puts(m, "echo gpu_id > hang_hws\n");
4062306a36Sopenharmony_ci	return 0;
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic ssize_t kfd_debugfs_hang_hws_write(struct file *file,
4462306a36Sopenharmony_ci	const char __user *user_buf, size_t size, loff_t *ppos)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	struct kfd_node *dev;
4762306a36Sopenharmony_ci	char tmp[16];
4862306a36Sopenharmony_ci	uint32_t gpu_id;
4962306a36Sopenharmony_ci	int ret = -EINVAL;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	memset(tmp, 0, 16);
5262306a36Sopenharmony_ci	if (size >= 16) {
5362306a36Sopenharmony_ci		pr_err("Invalid input for gpu id.\n");
5462306a36Sopenharmony_ci		goto out;
5562306a36Sopenharmony_ci	}
5662306a36Sopenharmony_ci	if (copy_from_user(tmp, user_buf, size)) {
5762306a36Sopenharmony_ci		ret = -EFAULT;
5862306a36Sopenharmony_ci		goto out;
5962306a36Sopenharmony_ci	}
6062306a36Sopenharmony_ci	if (kstrtoint(tmp, 10, &gpu_id)) {
6162306a36Sopenharmony_ci		pr_err("Invalid input for gpu id.\n");
6262306a36Sopenharmony_ci		goto out;
6362306a36Sopenharmony_ci	}
6462306a36Sopenharmony_ci	dev = kfd_device_by_id(gpu_id);
6562306a36Sopenharmony_ci	if (dev) {
6662306a36Sopenharmony_ci		kfd_debugfs_hang_hws(dev);
6762306a36Sopenharmony_ci		ret = size;
6862306a36Sopenharmony_ci	} else
6962306a36Sopenharmony_ci		pr_err("Cannot find device %d.\n", gpu_id);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciout:
7262306a36Sopenharmony_ci	return ret;
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistatic const struct file_operations kfd_debugfs_fops = {
7662306a36Sopenharmony_ci	.owner = THIS_MODULE,
7762306a36Sopenharmony_ci	.open = kfd_debugfs_open,
7862306a36Sopenharmony_ci	.read = seq_read,
7962306a36Sopenharmony_ci	.llseek = seq_lseek,
8062306a36Sopenharmony_ci	.release = single_release,
8162306a36Sopenharmony_ci};
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic const struct file_operations kfd_debugfs_hang_hws_fops = {
8462306a36Sopenharmony_ci	.owner = THIS_MODULE,
8562306a36Sopenharmony_ci	.open = kfd_debugfs_open,
8662306a36Sopenharmony_ci	.read = seq_read,
8762306a36Sopenharmony_ci	.write = kfd_debugfs_hang_hws_write,
8862306a36Sopenharmony_ci	.llseek = seq_lseek,
8962306a36Sopenharmony_ci	.release = single_release,
9062306a36Sopenharmony_ci};
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_civoid kfd_debugfs_init(void)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	debugfs_root = debugfs_create_dir("kfd", NULL);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	debugfs_create_file("mqds", S_IFREG | 0444, debugfs_root,
9762306a36Sopenharmony_ci			    kfd_debugfs_mqds_by_process, &kfd_debugfs_fops);
9862306a36Sopenharmony_ci	debugfs_create_file("hqds", S_IFREG | 0444, debugfs_root,
9962306a36Sopenharmony_ci			    kfd_debugfs_hqds_by_device, &kfd_debugfs_fops);
10062306a36Sopenharmony_ci	debugfs_create_file("rls", S_IFREG | 0444, debugfs_root,
10162306a36Sopenharmony_ci			    kfd_debugfs_rls_by_device, &kfd_debugfs_fops);
10262306a36Sopenharmony_ci	debugfs_create_file("hang_hws", S_IFREG | 0200, debugfs_root,
10362306a36Sopenharmony_ci			    kfd_debugfs_hang_hws_read, &kfd_debugfs_hang_hws_fops);
10462306a36Sopenharmony_ci	debugfs_create_file("mem_limit", S_IFREG | 0200, debugfs_root,
10562306a36Sopenharmony_ci			    kfd_debugfs_kfd_mem_limits, &kfd_debugfs_fops);
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_civoid kfd_debugfs_fini(void)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	debugfs_remove_recursive(debugfs_root);
11162306a36Sopenharmony_ci}
112