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