1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2016-2022 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <linux/debugfs.h>
25 #include <linux/uaccess.h>
26 
27 #include "kfd_priv.h"
28 
29 static struct dentry *debugfs_root;
30 
31 static int kfd_debugfs_open(struct inode *inode, struct file *file)
32 {
33 	int (*show)(struct seq_file *, void *) = inode->i_private;
34 
35 	return single_open(file, show, NULL);
36 }
37 static int kfd_debugfs_hang_hws_read(struct seq_file *m, void *data)
38 {
39 	seq_puts(m, "echo gpu_id > hang_hws\n");
40 	return 0;
41 }
42 
43 static ssize_t kfd_debugfs_hang_hws_write(struct file *file,
44 	const char __user *user_buf, size_t size, loff_t *ppos)
45 {
46 	struct kfd_dev *dev;
47 	char tmp[16];
48 	uint32_t gpu_id;
49 	int ret = -EINVAL;
50 
51 	memset(tmp, 0, 16);
52 	if (size >= 16) {
53 		pr_err("Invalid input for gpu id.\n");
54 		goto out;
55 	}
56 	if (copy_from_user(tmp, user_buf, size)) {
57 		ret = -EFAULT;
58 		goto out;
59 	}
60 	if (kstrtoint(tmp, 10, &gpu_id)) {
61 		pr_err("Invalid input for gpu id.\n");
62 		goto out;
63 	}
64 	dev = kfd_device_by_id(gpu_id);
65 	if (dev) {
66 		kfd_debugfs_hang_hws(dev);
67 		ret = size;
68 	} else
69 		pr_err("Cannot find device %d.\n", gpu_id);
70 
71 out:
72 	return ret;
73 }
74 
75 static const struct file_operations kfd_debugfs_fops = {
76 	.owner = THIS_MODULE,
77 	.open = kfd_debugfs_open,
78 	.read = seq_read,
79 	.llseek = seq_lseek,
80 	.release = single_release,
81 };
82 
83 static const struct file_operations kfd_debugfs_hang_hws_fops = {
84 	.owner = THIS_MODULE,
85 	.open = kfd_debugfs_open,
86 	.read = seq_read,
87 	.write = kfd_debugfs_hang_hws_write,
88 	.llseek = seq_lseek,
89 	.release = single_release,
90 };
91 
92 void kfd_debugfs_init(void)
93 {
94 	debugfs_root = debugfs_create_dir("kfd", NULL);
95 
96 	debugfs_create_file("mqds", S_IFREG | 0444, debugfs_root,
97 			    kfd_debugfs_mqds_by_process, &kfd_debugfs_fops);
98 	debugfs_create_file("hqds", S_IFREG | 0444, debugfs_root,
99 			    kfd_debugfs_hqds_by_device, &kfd_debugfs_fops);
100 	debugfs_create_file("rls", S_IFREG | 0444, debugfs_root,
101 			    kfd_debugfs_rls_by_device, &kfd_debugfs_fops);
102 	debugfs_create_file("hang_hws", S_IFREG | 0200, debugfs_root,
103 			    kfd_debugfs_hang_hws_read, &kfd_debugfs_hang_hws_fops);
104 	debugfs_create_file("mem_limit", S_IFREG | 0200, debugfs_root,
105 			    kfd_debugfs_kfd_mem_limits, &kfd_debugfs_fops);
106 }
107 
108 void kfd_debugfs_fini(void)
109 {
110 	debugfs_remove_recursive(debugfs_root);
111 }
112