xref: /openbmc/linux/arch/x86/kernel/kdebugfs.c (revision f7750a79568788473c5e8092ee58a52248f34329)
16d7d7433SHuang, Ying /*
26d7d7433SHuang, Ying  * Architecture specific debugfs files
36d7d7433SHuang, Ying  *
46d7d7433SHuang, Ying  * Copyright (C) 2007, Intel Corp.
56d7d7433SHuang, Ying  *	Huang Ying <ying.huang@intel.com>
66d7d7433SHuang, Ying  *
76d7d7433SHuang, Ying  * This file is released under the GPLv2.
86d7d7433SHuang, Ying  */
96d7d7433SHuang, Ying #include <linux/debugfs.h>
10c14b2adfSHuang, Ying #include <linux/uaccess.h>
11186f4360SPaul Gortmaker #include <linux/export.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
136d7d7433SHuang, Ying #include <linux/init.h>
14390cd85cSJaswinder Singh Rajput #include <linux/stat.h>
15c14b2adfSHuang, Ying #include <linux/io.h>
16c14b2adfSHuang, Ying #include <linux/mm.h>
176d7d7433SHuang, Ying 
186d7d7433SHuang, Ying #include <asm/setup.h>
196d7d7433SHuang, Ying 
20ae79cdaaSvenkatesh.pallipadi@intel.com struct dentry *arch_debugfs_dir;
21ae79cdaaSvenkatesh.pallipadi@intel.com EXPORT_SYMBOL(arch_debugfs_dir);
22ae79cdaaSvenkatesh.pallipadi@intel.com 
236d7d7433SHuang, Ying #ifdef CONFIG_DEBUG_BOOT_PARAMS
24c14b2adfSHuang, Ying struct setup_data_node {
25c14b2adfSHuang, Ying 	u64 paddr;
26c14b2adfSHuang, Ying 	u32 type;
27c14b2adfSHuang, Ying 	u32 len;
28c14b2adfSHuang, Ying };
29c14b2adfSHuang, Ying 
30390cd85cSJaswinder Singh Rajput static ssize_t setup_data_read(struct file *file, char __user *user_buf,
31390cd85cSJaswinder Singh Rajput 			       size_t count, loff_t *ppos)
32c14b2adfSHuang, Ying {
33c14b2adfSHuang, Ying 	struct setup_data_node *node = file->private_data;
34c14b2adfSHuang, Ying 	unsigned long remain;
35c14b2adfSHuang, Ying 	loff_t pos = *ppos;
36c14b2adfSHuang, Ying 	void *p;
37c14b2adfSHuang, Ying 	u64 pa;
38c14b2adfSHuang, Ying 
39c14b2adfSHuang, Ying 	if (pos < 0)
40c14b2adfSHuang, Ying 		return -EINVAL;
41390cd85cSJaswinder Singh Rajput 
42c14b2adfSHuang, Ying 	if (pos >= node->len)
43c14b2adfSHuang, Ying 		return 0;
44c14b2adfSHuang, Ying 
45c14b2adfSHuang, Ying 	if (count > node->len - pos)
46c14b2adfSHuang, Ying 		count = node->len - pos;
47390cd85cSJaswinder Singh Rajput 
48c14b2adfSHuang, Ying 	pa = node->paddr + sizeof(struct setup_data) + pos;
49*f7750a79STom Lendacky 	p = memremap(pa, count, MEMREMAP_WB);
50c14b2adfSHuang, Ying 	if (!p)
51*f7750a79STom Lendacky 		return -ENOMEM;
52c14b2adfSHuang, Ying 
53c14b2adfSHuang, Ying 	remain = copy_to_user(user_buf, p, count);
54c14b2adfSHuang, Ying 
55*f7750a79STom Lendacky 	memunmap(p);
56c14b2adfSHuang, Ying 
57c14b2adfSHuang, Ying 	if (remain)
58c14b2adfSHuang, Ying 		return -EFAULT;
59c14b2adfSHuang, Ying 
60c14b2adfSHuang, Ying 	*ppos = pos + count;
61c14b2adfSHuang, Ying 
62c14b2adfSHuang, Ying 	return count;
63c14b2adfSHuang, Ying }
64c14b2adfSHuang, Ying 
65c14b2adfSHuang, Ying static const struct file_operations fops_setup_data = {
66c14b2adfSHuang, Ying 	.read		= setup_data_read,
67234e3405SStephen Boyd 	.open		= simple_open,
686038f373SArnd Bergmann 	.llseek		= default_llseek,
69c14b2adfSHuang, Ying };
70c14b2adfSHuang, Ying 
71c14b2adfSHuang, Ying static int __init
72c14b2adfSHuang, Ying create_setup_data_node(struct dentry *parent, int no,
73c14b2adfSHuang, Ying 		       struct setup_data_node *node)
74c14b2adfSHuang, Ying {
75c14b2adfSHuang, Ying 	struct dentry *d, *type, *data;
76c14b2adfSHuang, Ying 	char buf[16];
77c14b2adfSHuang, Ying 
78c14b2adfSHuang, Ying 	sprintf(buf, "%d", no);
79c14b2adfSHuang, Ying 	d = debugfs_create_dir(buf, parent);
80390cd85cSJaswinder Singh Rajput 	if (!d)
81390cd85cSJaswinder Singh Rajput 		return -ENOMEM;
82390cd85cSJaswinder Singh Rajput 
83c14b2adfSHuang, Ying 	type = debugfs_create_x32("type", S_IRUGO, d, &node->type);
84390cd85cSJaswinder Singh Rajput 	if (!type)
85c14b2adfSHuang, Ying 		goto err_dir;
86390cd85cSJaswinder Singh Rajput 
87c14b2adfSHuang, Ying 	data = debugfs_create_file("data", S_IRUGO, d, node, &fops_setup_data);
88390cd85cSJaswinder Singh Rajput 	if (!data)
89c14b2adfSHuang, Ying 		goto err_type;
90390cd85cSJaswinder Singh Rajput 
91c14b2adfSHuang, Ying 	return 0;
92c14b2adfSHuang, Ying 
93c14b2adfSHuang, Ying err_type:
94c14b2adfSHuang, Ying 	debugfs_remove(type);
95c14b2adfSHuang, Ying err_dir:
96c14b2adfSHuang, Ying 	debugfs_remove(d);
97390cd85cSJaswinder Singh Rajput 	return -ENOMEM;
98c14b2adfSHuang, Ying }
99c14b2adfSHuang, Ying 
100c14b2adfSHuang, Ying static int __init create_setup_data_nodes(struct dentry *parent)
101c14b2adfSHuang, Ying {
102c14b2adfSHuang, Ying 	struct setup_data_node *node;
103c14b2adfSHuang, Ying 	struct setup_data *data;
10441fb433bSJulia Lawall 	int error;
105c14b2adfSHuang, Ying 	struct dentry *d;
106c14b2adfSHuang, Ying 	u64 pa_data;
107390cd85cSJaswinder Singh Rajput 	int no = 0;
108c14b2adfSHuang, Ying 
109c14b2adfSHuang, Ying 	d = debugfs_create_dir("setup_data", parent);
110390cd85cSJaswinder Singh Rajput 	if (!d)
111390cd85cSJaswinder Singh Rajput 		return -ENOMEM;
112c14b2adfSHuang, Ying 
113c14b2adfSHuang, Ying 	pa_data = boot_params.hdr.setup_data;
114c14b2adfSHuang, Ying 
115c14b2adfSHuang, Ying 	while (pa_data) {
116c14b2adfSHuang, Ying 		node = kmalloc(sizeof(*node), GFP_KERNEL);
11741fb433bSJulia Lawall 		if (!node) {
11841fb433bSJulia Lawall 			error = -ENOMEM;
119c14b2adfSHuang, Ying 			goto err_dir;
12041fb433bSJulia Lawall 		}
121390cd85cSJaswinder Singh Rajput 
122*f7750a79STom Lendacky 		data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
123c14b2adfSHuang, Ying 		if (!data) {
124f461a1d8SJulia Lawall 			kfree(node);
125*f7750a79STom Lendacky 			error = -ENOMEM;
126c14b2adfSHuang, Ying 			goto err_dir;
127c14b2adfSHuang, Ying 		}
128c14b2adfSHuang, Ying 
129c14b2adfSHuang, Ying 		node->paddr = pa_data;
130c14b2adfSHuang, Ying 		node->type = data->type;
131c14b2adfSHuang, Ying 		node->len = data->len;
132c14b2adfSHuang, Ying 		error = create_setup_data_node(d, no, node);
133c14b2adfSHuang, Ying 		pa_data = data->next;
134c14b2adfSHuang, Ying 
135*f7750a79STom Lendacky 		memunmap(data);
136c14b2adfSHuang, Ying 		if (error)
137c14b2adfSHuang, Ying 			goto err_dir;
138c14b2adfSHuang, Ying 		no++;
139c14b2adfSHuang, Ying 	}
140390cd85cSJaswinder Singh Rajput 
141c14b2adfSHuang, Ying 	return 0;
142c14b2adfSHuang, Ying 
143c14b2adfSHuang, Ying err_dir:
144c14b2adfSHuang, Ying 	debugfs_remove(d);
145c14b2adfSHuang, Ying 	return error;
146c14b2adfSHuang, Ying }
147c14b2adfSHuang, Ying 
1486d7d7433SHuang, Ying static struct debugfs_blob_wrapper boot_params_blob = {
1496d7d7433SHuang, Ying 	.data		= &boot_params,
1506d7d7433SHuang, Ying 	.size		= sizeof(boot_params),
1516d7d7433SHuang, Ying };
1526d7d7433SHuang, Ying 
1536d7d7433SHuang, Ying static int __init boot_params_kdebugfs_init(void)
1546d7d7433SHuang, Ying {
1556d7d7433SHuang, Ying 	struct dentry *dbp, *version, *data;
156390cd85cSJaswinder Singh Rajput 	int error = -ENOMEM;
1576d7d7433SHuang, Ying 
15810bce841SBorislav Petkov 	dbp = debugfs_create_dir("boot_params", arch_debugfs_dir);
159390cd85cSJaswinder Singh Rajput 	if (!dbp)
160390cd85cSJaswinder Singh Rajput 		return -ENOMEM;
161390cd85cSJaswinder Singh Rajput 
1626d7d7433SHuang, Ying 	version = debugfs_create_x16("version", S_IRUGO, dbp,
1636d7d7433SHuang, Ying 				     &boot_params.hdr.version);
164390cd85cSJaswinder Singh Rajput 	if (!version)
1656d7d7433SHuang, Ying 		goto err_dir;
166390cd85cSJaswinder Singh Rajput 
1676d7d7433SHuang, Ying 	data = debugfs_create_blob("data", S_IRUGO, dbp,
1686d7d7433SHuang, Ying 				   &boot_params_blob);
169390cd85cSJaswinder Singh Rajput 	if (!data)
1706d7d7433SHuang, Ying 		goto err_version;
171390cd85cSJaswinder Singh Rajput 
172c14b2adfSHuang, Ying 	error = create_setup_data_nodes(dbp);
173c14b2adfSHuang, Ying 	if (error)
174c14b2adfSHuang, Ying 		goto err_data;
175390cd85cSJaswinder Singh Rajput 
1766d7d7433SHuang, Ying 	return 0;
177c14b2adfSHuang, Ying 
178c14b2adfSHuang, Ying err_data:
179c14b2adfSHuang, Ying 	debugfs_remove(data);
1806d7d7433SHuang, Ying err_version:
1816d7d7433SHuang, Ying 	debugfs_remove(version);
1826d7d7433SHuang, Ying err_dir:
1836d7d7433SHuang, Ying 	debugfs_remove(dbp);
1846d7d7433SHuang, Ying 	return error;
1856d7d7433SHuang, Ying }
186390cd85cSJaswinder Singh Rajput #endif /* CONFIG_DEBUG_BOOT_PARAMS */
1876d7d7433SHuang, Ying 
1886d7d7433SHuang, Ying static int __init arch_kdebugfs_init(void)
1896d7d7433SHuang, Ying {
1906d7d7433SHuang, Ying 	int error = 0;
1916d7d7433SHuang, Ying 
192ae79cdaaSvenkatesh.pallipadi@intel.com 	arch_debugfs_dir = debugfs_create_dir("x86", NULL);
193ae79cdaaSvenkatesh.pallipadi@intel.com 	if (!arch_debugfs_dir)
194ae79cdaaSvenkatesh.pallipadi@intel.com 		return -ENOMEM;
195ae79cdaaSvenkatesh.pallipadi@intel.com 
1966d7d7433SHuang, Ying #ifdef CONFIG_DEBUG_BOOT_PARAMS
1976d7d7433SHuang, Ying 	error = boot_params_kdebugfs_init();
1986d7d7433SHuang, Ying #endif
1996d7d7433SHuang, Ying 
2006d7d7433SHuang, Ying 	return error;
2016d7d7433SHuang, Ying }
2026d7d7433SHuang, Ying arch_initcall(arch_kdebugfs_init);
203