xref: /openbmc/linux/arch/x86/kernel/kdebugfs.c (revision b3c72fc9a78e74161f9d05ef7191706060628f8c)
155716d26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26d7d7433SHuang, Ying /*
36d7d7433SHuang, Ying  * Architecture specific debugfs files
46d7d7433SHuang, Ying  *
56d7d7433SHuang, Ying  * Copyright (C) 2007, Intel Corp.
66d7d7433SHuang, Ying  *	Huang Ying <ying.huang@intel.com>
76d7d7433SHuang, Ying  */
86d7d7433SHuang, Ying #include <linux/debugfs.h>
9c14b2adfSHuang, Ying #include <linux/uaccess.h>
10186f4360SPaul Gortmaker #include <linux/export.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
126d7d7433SHuang, Ying #include <linux/init.h>
13390cd85cSJaswinder Singh Rajput #include <linux/stat.h>
14c14b2adfSHuang, Ying #include <linux/io.h>
15c14b2adfSHuang, Ying #include <linux/mm.h>
166d7d7433SHuang, Ying 
176d7d7433SHuang, Ying #include <asm/setup.h>
186d7d7433SHuang, Ying 
19ae79cdaaSvenkatesh.pallipadi@intel.com struct dentry *arch_debugfs_dir;
20ae79cdaaSvenkatesh.pallipadi@intel.com EXPORT_SYMBOL(arch_debugfs_dir);
21ae79cdaaSvenkatesh.pallipadi@intel.com 
226d7d7433SHuang, Ying #ifdef CONFIG_DEBUG_BOOT_PARAMS
23c14b2adfSHuang, Ying struct setup_data_node {
24c14b2adfSHuang, Ying 	u64 paddr;
25c14b2adfSHuang, Ying 	u32 type;
26c14b2adfSHuang, Ying 	u32 len;
27c14b2adfSHuang, Ying };
28c14b2adfSHuang, Ying 
29390cd85cSJaswinder Singh Rajput static ssize_t setup_data_read(struct file *file, char __user *user_buf,
30390cd85cSJaswinder Singh Rajput 			       size_t count, loff_t *ppos)
31c14b2adfSHuang, Ying {
32c14b2adfSHuang, Ying 	struct setup_data_node *node = file->private_data;
33c14b2adfSHuang, Ying 	unsigned long remain;
34c14b2adfSHuang, Ying 	loff_t pos = *ppos;
35c14b2adfSHuang, Ying 	void *p;
36c14b2adfSHuang, Ying 	u64 pa;
37c14b2adfSHuang, Ying 
38c14b2adfSHuang, Ying 	if (pos < 0)
39c14b2adfSHuang, Ying 		return -EINVAL;
40390cd85cSJaswinder Singh Rajput 
41c14b2adfSHuang, Ying 	if (pos >= node->len)
42c14b2adfSHuang, Ying 		return 0;
43c14b2adfSHuang, Ying 
44c14b2adfSHuang, Ying 	if (count > node->len - pos)
45c14b2adfSHuang, Ying 		count = node->len - pos;
46390cd85cSJaswinder Singh Rajput 
47*b3c72fc9SDaniel Kiper 	pa = node->paddr + pos;
48*b3c72fc9SDaniel Kiper 
49*b3c72fc9SDaniel Kiper 	/* Is it direct data or invalid indirect one? */
50*b3c72fc9SDaniel Kiper 	if (!(node->type & SETUP_INDIRECT) || node->type == SETUP_INDIRECT)
51*b3c72fc9SDaniel Kiper 		pa += sizeof(struct setup_data);
52*b3c72fc9SDaniel Kiper 
53f7750a79STom Lendacky 	p = memremap(pa, count, MEMREMAP_WB);
54c14b2adfSHuang, Ying 	if (!p)
55f7750a79STom Lendacky 		return -ENOMEM;
56c14b2adfSHuang, Ying 
57c14b2adfSHuang, Ying 	remain = copy_to_user(user_buf, p, count);
58c14b2adfSHuang, Ying 
59f7750a79STom Lendacky 	memunmap(p);
60c14b2adfSHuang, Ying 
61c14b2adfSHuang, Ying 	if (remain)
62c14b2adfSHuang, Ying 		return -EFAULT;
63c14b2adfSHuang, Ying 
64c14b2adfSHuang, Ying 	*ppos = pos + count;
65c14b2adfSHuang, Ying 
66c14b2adfSHuang, Ying 	return count;
67c14b2adfSHuang, Ying }
68c14b2adfSHuang, Ying 
69c14b2adfSHuang, Ying static const struct file_operations fops_setup_data = {
70c14b2adfSHuang, Ying 	.read		= setup_data_read,
71234e3405SStephen Boyd 	.open		= simple_open,
726038f373SArnd Bergmann 	.llseek		= default_llseek,
73c14b2adfSHuang, Ying };
74c14b2adfSHuang, Ying 
750fc811e5SGreg Kroah-Hartman static void __init
76c14b2adfSHuang, Ying create_setup_data_node(struct dentry *parent, int no,
77c14b2adfSHuang, Ying 		       struct setup_data_node *node)
78c14b2adfSHuang, Ying {
790fc811e5SGreg Kroah-Hartman 	struct dentry *d;
80c14b2adfSHuang, Ying 	char buf[16];
81c14b2adfSHuang, Ying 
82c14b2adfSHuang, Ying 	sprintf(buf, "%d", no);
83c14b2adfSHuang, Ying 	d = debugfs_create_dir(buf, parent);
84390cd85cSJaswinder Singh Rajput 
850fc811e5SGreg Kroah-Hartman 	debugfs_create_x32("type", S_IRUGO, d, &node->type);
860fc811e5SGreg Kroah-Hartman 	debugfs_create_file("data", S_IRUGO, d, node, &fops_setup_data);
87c14b2adfSHuang, Ying }
88c14b2adfSHuang, Ying 
89c14b2adfSHuang, Ying static int __init create_setup_data_nodes(struct dentry *parent)
90c14b2adfSHuang, Ying {
91c14b2adfSHuang, Ying 	struct setup_data_node *node;
92c14b2adfSHuang, Ying 	struct setup_data *data;
9341fb433bSJulia Lawall 	int error;
94c14b2adfSHuang, Ying 	struct dentry *d;
95c14b2adfSHuang, Ying 	u64 pa_data;
96390cd85cSJaswinder Singh Rajput 	int no = 0;
97c14b2adfSHuang, Ying 
98c14b2adfSHuang, Ying 	d = debugfs_create_dir("setup_data", parent);
99c14b2adfSHuang, Ying 
100c14b2adfSHuang, Ying 	pa_data = boot_params.hdr.setup_data;
101c14b2adfSHuang, Ying 
102c14b2adfSHuang, Ying 	while (pa_data) {
103c14b2adfSHuang, Ying 		node = kmalloc(sizeof(*node), GFP_KERNEL);
10441fb433bSJulia Lawall 		if (!node) {
10541fb433bSJulia Lawall 			error = -ENOMEM;
106c14b2adfSHuang, Ying 			goto err_dir;
10741fb433bSJulia Lawall 		}
108390cd85cSJaswinder Singh Rajput 
109f7750a79STom Lendacky 		data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
110c14b2adfSHuang, Ying 		if (!data) {
111f461a1d8SJulia Lawall 			kfree(node);
112f7750a79STom Lendacky 			error = -ENOMEM;
113c14b2adfSHuang, Ying 			goto err_dir;
114c14b2adfSHuang, Ying 		}
115c14b2adfSHuang, Ying 
116*b3c72fc9SDaniel Kiper 		if (data->type == SETUP_INDIRECT &&
117*b3c72fc9SDaniel Kiper 		    ((struct setup_indirect *)data->data)->type != SETUP_INDIRECT) {
118*b3c72fc9SDaniel Kiper 			node->paddr = ((struct setup_indirect *)data->data)->addr;
119*b3c72fc9SDaniel Kiper 			node->type  = ((struct setup_indirect *)data->data)->type;
120*b3c72fc9SDaniel Kiper 			node->len   = ((struct setup_indirect *)data->data)->len;
121*b3c72fc9SDaniel Kiper 		} else {
122c14b2adfSHuang, Ying 			node->paddr = pa_data;
123c14b2adfSHuang, Ying 			node->type  = data->type;
124c14b2adfSHuang, Ying 			node->len   = data->len;
125*b3c72fc9SDaniel Kiper 		}
126*b3c72fc9SDaniel Kiper 
1270fc811e5SGreg Kroah-Hartman 		create_setup_data_node(d, no, node);
128c14b2adfSHuang, Ying 		pa_data = data->next;
129c14b2adfSHuang, Ying 
130f7750a79STom Lendacky 		memunmap(data);
131c14b2adfSHuang, Ying 		no++;
132c14b2adfSHuang, Ying 	}
133390cd85cSJaswinder Singh Rajput 
134c14b2adfSHuang, Ying 	return 0;
135c14b2adfSHuang, Ying 
136c14b2adfSHuang, Ying err_dir:
1370fc811e5SGreg Kroah-Hartman 	debugfs_remove_recursive(d);
138c14b2adfSHuang, Ying 	return error;
139c14b2adfSHuang, Ying }
140c14b2adfSHuang, Ying 
1416d7d7433SHuang, Ying static struct debugfs_blob_wrapper boot_params_blob = {
1426d7d7433SHuang, Ying 	.data		= &boot_params,
1436d7d7433SHuang, Ying 	.size		= sizeof(boot_params),
1446d7d7433SHuang, Ying };
1456d7d7433SHuang, Ying 
1466d7d7433SHuang, Ying static int __init boot_params_kdebugfs_init(void)
1476d7d7433SHuang, Ying {
1480fc811e5SGreg Kroah-Hartman 	struct dentry *dbp;
1490fc811e5SGreg Kroah-Hartman 	int error;
1506d7d7433SHuang, Ying 
15110bce841SBorislav Petkov 	dbp = debugfs_create_dir("boot_params", arch_debugfs_dir);
152390cd85cSJaswinder Singh Rajput 
1530fc811e5SGreg Kroah-Hartman 	debugfs_create_x16("version", S_IRUGO, dbp, &boot_params.hdr.version);
1540fc811e5SGreg Kroah-Hartman 	debugfs_create_blob("data", S_IRUGO, dbp, &boot_params_blob);
155390cd85cSJaswinder Singh Rajput 
156c14b2adfSHuang, Ying 	error = create_setup_data_nodes(dbp);
157c14b2adfSHuang, Ying 	if (error)
1580fc811e5SGreg Kroah-Hartman 		debugfs_remove_recursive(dbp);
159390cd85cSJaswinder Singh Rajput 
1606d7d7433SHuang, Ying 	return error;
1616d7d7433SHuang, Ying }
162390cd85cSJaswinder Singh Rajput #endif /* CONFIG_DEBUG_BOOT_PARAMS */
1636d7d7433SHuang, Ying 
1646d7d7433SHuang, Ying static int __init arch_kdebugfs_init(void)
1656d7d7433SHuang, Ying {
1666d7d7433SHuang, Ying 	int error = 0;
1676d7d7433SHuang, Ying 
168ae79cdaaSvenkatesh.pallipadi@intel.com 	arch_debugfs_dir = debugfs_create_dir("x86", NULL);
169ae79cdaaSvenkatesh.pallipadi@intel.com 
1706d7d7433SHuang, Ying #ifdef CONFIG_DEBUG_BOOT_PARAMS
1716d7d7433SHuang, Ying 	error = boot_params_kdebugfs_init();
1726d7d7433SHuang, Ying #endif
1736d7d7433SHuang, Ying 
1746d7d7433SHuang, Ying 	return error;
1756d7d7433SHuang, Ying }
1766d7d7433SHuang, Ying arch_initcall(arch_kdebugfs_init);
177