1d87f36a0SRajneesh Bhardwaj // SPDX-License-Identifier: GPL-2.0 OR MIT
2851a645eSFelix Kuehling /*
3d87f36a0SRajneesh Bhardwaj  * Copyright 2016-2022 Advanced Micro Devices, Inc.
4851a645eSFelix Kuehling  *
5851a645eSFelix Kuehling  * Permission is hereby granted, free of charge, to any person obtaining a
6851a645eSFelix Kuehling  * copy of this software and associated documentation files (the "Software"),
7851a645eSFelix Kuehling  * to deal in the Software without restriction, including without limitation
8851a645eSFelix Kuehling  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9851a645eSFelix Kuehling  * and/or sell copies of the Software, and to permit persons to whom the
10851a645eSFelix Kuehling  * Software is furnished to do so, subject to the following conditions:
11851a645eSFelix Kuehling  *
12851a645eSFelix Kuehling  * The above copyright notice and this permission notice shall be included in
13851a645eSFelix Kuehling  * all copies or substantial portions of the Software.
14851a645eSFelix Kuehling  *
15851a645eSFelix Kuehling  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16851a645eSFelix Kuehling  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17851a645eSFelix Kuehling  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18851a645eSFelix Kuehling  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19851a645eSFelix Kuehling  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20851a645eSFelix Kuehling  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21851a645eSFelix Kuehling  * OTHER DEALINGS IN THE SOFTWARE.
22851a645eSFelix Kuehling  */
23851a645eSFelix Kuehling 
24851a645eSFelix Kuehling #include <linux/debugfs.h>
25a29ec470SShaoyun Liu #include <linux/uaccess.h>
26a29ec470SShaoyun Liu 
27851a645eSFelix Kuehling #include "kfd_priv.h"
28851a645eSFelix Kuehling 
29851a645eSFelix Kuehling static struct dentry *debugfs_root;
30851a645eSFelix Kuehling 
kfd_debugfs_open(struct inode * inode,struct file * file)31851a645eSFelix Kuehling static int kfd_debugfs_open(struct inode *inode, struct file *file)
32851a645eSFelix Kuehling {
33851a645eSFelix Kuehling 	int (*show)(struct seq_file *, void *) = inode->i_private;
34851a645eSFelix Kuehling 
35851a645eSFelix Kuehling 	return single_open(file, show, NULL);
36851a645eSFelix Kuehling }
kfd_debugfs_hang_hws_read(struct seq_file * m,void * data)37d7361021SQu Huang static int kfd_debugfs_hang_hws_read(struct seq_file *m, void *data)
38d7361021SQu Huang {
392243f493SRajneesh Bhardwaj 	seq_puts(m, "echo gpu_id > hang_hws\n");
40d7361021SQu Huang 	return 0;
41d7361021SQu Huang }
42851a645eSFelix Kuehling 
kfd_debugfs_hang_hws_write(struct file * file,const char __user * user_buf,size_t size,loff_t * ppos)43a29ec470SShaoyun Liu static ssize_t kfd_debugfs_hang_hws_write(struct file *file,
44a29ec470SShaoyun Liu 	const char __user *user_buf, size_t size, loff_t *ppos)
45a29ec470SShaoyun Liu {
46*8dc1db31SMukul Joshi 	struct kfd_node *dev;
47a29ec470SShaoyun Liu 	char tmp[16];
48a29ec470SShaoyun Liu 	uint32_t gpu_id;
49a29ec470SShaoyun Liu 	int ret = -EINVAL;
50a29ec470SShaoyun Liu 
51a29ec470SShaoyun Liu 	memset(tmp, 0, 16);
52a29ec470SShaoyun Liu 	if (size >= 16) {
53a29ec470SShaoyun Liu 		pr_err("Invalid input for gpu id.\n");
54a29ec470SShaoyun Liu 		goto out;
55a29ec470SShaoyun Liu 	}
56a29ec470SShaoyun Liu 	if (copy_from_user(tmp, user_buf, size)) {
57a29ec470SShaoyun Liu 		ret = -EFAULT;
58a29ec470SShaoyun Liu 		goto out;
59a29ec470SShaoyun Liu 	}
60a29ec470SShaoyun Liu 	if (kstrtoint(tmp, 10, &gpu_id)) {
61a29ec470SShaoyun Liu 		pr_err("Invalid input for gpu id.\n");
62a29ec470SShaoyun Liu 		goto out;
63a29ec470SShaoyun Liu 	}
64a29ec470SShaoyun Liu 	dev = kfd_device_by_id(gpu_id);
65a29ec470SShaoyun Liu 	if (dev) {
66a29ec470SShaoyun Liu 		kfd_debugfs_hang_hws(dev);
67a29ec470SShaoyun Liu 		ret = size;
68a29ec470SShaoyun Liu 	} else
69a29ec470SShaoyun Liu 		pr_err("Cannot find device %d.\n", gpu_id);
70a29ec470SShaoyun Liu 
71a29ec470SShaoyun Liu out:
72a29ec470SShaoyun Liu 	return ret;
73a29ec470SShaoyun Liu }
74a29ec470SShaoyun Liu 
75851a645eSFelix Kuehling static const struct file_operations kfd_debugfs_fops = {
76851a645eSFelix Kuehling 	.owner = THIS_MODULE,
77851a645eSFelix Kuehling 	.open = kfd_debugfs_open,
78851a645eSFelix Kuehling 	.read = seq_read,
79851a645eSFelix Kuehling 	.llseek = seq_lseek,
80851a645eSFelix Kuehling 	.release = single_release,
81851a645eSFelix Kuehling };
82851a645eSFelix Kuehling 
83a29ec470SShaoyun Liu static const struct file_operations kfd_debugfs_hang_hws_fops = {
84a29ec470SShaoyun Liu 	.owner = THIS_MODULE,
85a29ec470SShaoyun Liu 	.open = kfd_debugfs_open,
86a29ec470SShaoyun Liu 	.read = seq_read,
87a29ec470SShaoyun Liu 	.write = kfd_debugfs_hang_hws_write,
88a29ec470SShaoyun Liu 	.llseek = seq_lseek,
89a29ec470SShaoyun Liu 	.release = single_release,
90a29ec470SShaoyun Liu };
91a29ec470SShaoyun Liu 
kfd_debugfs_init(void)92851a645eSFelix Kuehling void kfd_debugfs_init(void)
93851a645eSFelix Kuehling {
94851a645eSFelix Kuehling 	debugfs_root = debugfs_create_dir("kfd", NULL);
95851a645eSFelix Kuehling 
96641d3003SGreg Kroah-Hartman 	debugfs_create_file("mqds", S_IFREG | 0444, debugfs_root,
97641d3003SGreg Kroah-Hartman 			    kfd_debugfs_mqds_by_process, &kfd_debugfs_fops);
98641d3003SGreg Kroah-Hartman 	debugfs_create_file("hqds", S_IFREG | 0444, debugfs_root,
99641d3003SGreg Kroah-Hartman 			    kfd_debugfs_hqds_by_device, &kfd_debugfs_fops);
100641d3003SGreg Kroah-Hartman 	debugfs_create_file("rls", S_IFREG | 0444, debugfs_root,
101641d3003SGreg Kroah-Hartman 			    kfd_debugfs_rls_by_device, &kfd_debugfs_fops);
1022bdac179SFelix Kuehling 	debugfs_create_file("hang_hws", S_IFREG | 0200, debugfs_root,
103d7361021SQu Huang 			    kfd_debugfs_hang_hws_read, &kfd_debugfs_hang_hws_fops);
1043d2af401SAlex Sierra 	debugfs_create_file("mem_limit", S_IFREG | 0200, debugfs_root,
1053d2af401SAlex Sierra 			    kfd_debugfs_kfd_mem_limits, &kfd_debugfs_fops);
106851a645eSFelix Kuehling }
107851a645eSFelix Kuehling 
kfd_debugfs_fini(void)108851a645eSFelix Kuehling void kfd_debugfs_fini(void)
109851a645eSFelix Kuehling {
110851a645eSFelix Kuehling 	debugfs_remove_recursive(debugfs_root);
111851a645eSFelix Kuehling }
112