xref: /openbmc/linux/arch/x86/kernel/kdebugfs.c (revision f15cbe6f1a4b4d9df59142fc8e4abb973302cf44)
1 /*
2  * Architecture specific debugfs files
3  *
4  * Copyright (C) 2007, Intel Corp.
5  *	Huang Ying <ying.huang@intel.com>
6  *
7  * This file is released under the GPLv2.
8  */
9 #include <linux/debugfs.h>
10 #include <linux/uaccess.h>
11 #include <linux/stat.h>
12 #include <linux/init.h>
13 #include <linux/io.h>
14 #include <linux/mm.h>
15 #include <linux/module.h>
16 
17 #include <asm/setup.h>
18 
19 struct dentry *arch_debugfs_dir;
20 EXPORT_SYMBOL(arch_debugfs_dir);
21 
22 #ifdef CONFIG_DEBUG_BOOT_PARAMS
23 struct setup_data_node {
24 	u64 paddr;
25 	u32 type;
26 	u32 len;
27 };
28 
29 static ssize_t
30 setup_data_read(struct file *file, char __user *user_buf, size_t count,
31 		loff_t *ppos)
32 {
33 	struct setup_data_node *node = file->private_data;
34 	unsigned long remain;
35 	loff_t pos = *ppos;
36 	struct page *pg;
37 	void *p;
38 	u64 pa;
39 
40 	if (pos < 0)
41 		return -EINVAL;
42 	if (pos >= node->len)
43 		return 0;
44 
45 	if (count > node->len - pos)
46 		count = node->len - pos;
47 	pa = node->paddr + sizeof(struct setup_data) + pos;
48 	pg = pfn_to_page((pa + count - 1) >> PAGE_SHIFT);
49 	if (PageHighMem(pg)) {
50 		p = ioremap_cache(pa, count);
51 		if (!p)
52 			return -ENXIO;
53 	} else {
54 		p = __va(pa);
55 	}
56 
57 	remain = copy_to_user(user_buf, p, count);
58 
59 	if (PageHighMem(pg))
60 		iounmap(p);
61 
62 	if (remain)
63 		return -EFAULT;
64 
65 	*ppos = pos + count;
66 
67 	return count;
68 }
69 
70 static int setup_data_open(struct inode *inode, struct file *file)
71 {
72 	file->private_data = inode->i_private;
73 	return 0;
74 }
75 
76 static const struct file_operations fops_setup_data = {
77 	.read =		setup_data_read,
78 	.open =		setup_data_open,
79 };
80 
81 static int __init
82 create_setup_data_node(struct dentry *parent, int no,
83 		       struct setup_data_node *node)
84 {
85 	struct dentry *d, *type, *data;
86 	char buf[16];
87 	int error;
88 
89 	sprintf(buf, "%d", no);
90 	d = debugfs_create_dir(buf, parent);
91 	if (!d) {
92 		error = -ENOMEM;
93 		goto err_return;
94 	}
95 	type = debugfs_create_x32("type", S_IRUGO, d, &node->type);
96 	if (!type) {
97 		error = -ENOMEM;
98 		goto err_dir;
99 	}
100 	data = debugfs_create_file("data", S_IRUGO, d, node, &fops_setup_data);
101 	if (!data) {
102 		error = -ENOMEM;
103 		goto err_type;
104 	}
105 	return 0;
106 
107 err_type:
108 	debugfs_remove(type);
109 err_dir:
110 	debugfs_remove(d);
111 err_return:
112 	return error;
113 }
114 
115 static int __init create_setup_data_nodes(struct dentry *parent)
116 {
117 	struct setup_data_node *node;
118 	struct setup_data *data;
119 	int error, no = 0;
120 	struct dentry *d;
121 	struct page *pg;
122 	u64 pa_data;
123 
124 	d = debugfs_create_dir("setup_data", parent);
125 	if (!d) {
126 		error = -ENOMEM;
127 		goto err_return;
128 	}
129 
130 	pa_data = boot_params.hdr.setup_data;
131 
132 	while (pa_data) {
133 		node = kmalloc(sizeof(*node), GFP_KERNEL);
134 		if (!node) {
135 			error = -ENOMEM;
136 			goto err_dir;
137 		}
138 		pg = pfn_to_page((pa_data+sizeof(*data)-1) >> PAGE_SHIFT);
139 		if (PageHighMem(pg)) {
140 			data = ioremap_cache(pa_data, sizeof(*data));
141 			if (!data) {
142 				error = -ENXIO;
143 				goto err_dir;
144 			}
145 		} else {
146 			data = __va(pa_data);
147 		}
148 
149 		node->paddr = pa_data;
150 		node->type = data->type;
151 		node->len = data->len;
152 		error = create_setup_data_node(d, no, node);
153 		pa_data = data->next;
154 
155 		if (PageHighMem(pg))
156 			iounmap(data);
157 		if (error)
158 			goto err_dir;
159 		no++;
160 	}
161 	return 0;
162 
163 err_dir:
164 	debugfs_remove(d);
165 err_return:
166 	return error;
167 }
168 
169 static struct debugfs_blob_wrapper boot_params_blob = {
170 	.data		= &boot_params,
171 	.size		= sizeof(boot_params),
172 };
173 
174 static int __init boot_params_kdebugfs_init(void)
175 {
176 	struct dentry *dbp, *version, *data;
177 	int error;
178 
179 	dbp = debugfs_create_dir("boot_params", NULL);
180 	if (!dbp) {
181 		error = -ENOMEM;
182 		goto err_return;
183 	}
184 	version = debugfs_create_x16("version", S_IRUGO, dbp,
185 				     &boot_params.hdr.version);
186 	if (!version) {
187 		error = -ENOMEM;
188 		goto err_dir;
189 	}
190 	data = debugfs_create_blob("data", S_IRUGO, dbp,
191 				   &boot_params_blob);
192 	if (!data) {
193 		error = -ENOMEM;
194 		goto err_version;
195 	}
196 	error = create_setup_data_nodes(dbp);
197 	if (error)
198 		goto err_data;
199 	return 0;
200 
201 err_data:
202 	debugfs_remove(data);
203 err_version:
204 	debugfs_remove(version);
205 err_dir:
206 	debugfs_remove(dbp);
207 err_return:
208 	return error;
209 }
210 #endif
211 
212 static int __init arch_kdebugfs_init(void)
213 {
214 	int error = 0;
215 
216 	arch_debugfs_dir = debugfs_create_dir("x86", NULL);
217 	if (!arch_debugfs_dir)
218 		return -ENOMEM;
219 
220 #ifdef CONFIG_DEBUG_BOOT_PARAMS
221 	error = boot_params_kdebugfs_init();
222 #endif
223 
224 	return error;
225 }
226 arch_initcall(arch_kdebugfs_init);
227