Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Architecture specific sysfs attributes in /sys/kernel
4 *
5 * Copyright (C) 2007, Intel Corp.
6 * Huang Ying <ying.huang@intel.com>
7 * Copyright (C) 2013, 2013 Red Hat, Inc.
8 * Dave Young <dyoung@redhat.com>
9 */
10
11#include <linux/kobject.h>
12#include <linux/string.h>
13#include <linux/sysfs.h>
14#include <linux/init.h>
15#include <linux/stat.h>
16#include <linux/slab.h>
17#include <linux/mm.h>
18#include <linux/io.h>
19
20#include <asm/setup.h>
21
22static ssize_t version_show(struct kobject *kobj,
23 struct kobj_attribute *attr, char *buf)
24{
25 return sprintf(buf, "0x%04x\n", boot_params.hdr.version);
26}
27
28static struct kobj_attribute boot_params_version_attr = __ATTR_RO(version);
29
30static ssize_t boot_params_data_read(struct file *fp, struct kobject *kobj,
31 struct bin_attribute *bin_attr,
32 char *buf, loff_t off, size_t count)
33{
34 memcpy(buf, (void *)&boot_params + off, count);
35 return count;
36}
37
38static struct bin_attribute boot_params_data_attr = {
39 .attr = {
40 .name = "data",
41 .mode = S_IRUGO,
42 },
43 .read = boot_params_data_read,
44 .size = sizeof(boot_params),
45};
46
47static struct attribute *boot_params_version_attrs[] = {
48 &boot_params_version_attr.attr,
49 NULL,
50};
51
52static struct bin_attribute *boot_params_data_attrs[] = {
53 &boot_params_data_attr,
54 NULL,
55};
56
57static const struct attribute_group boot_params_attr_group = {
58 .attrs = boot_params_version_attrs,
59 .bin_attrs = boot_params_data_attrs,
60};
61
62static int kobj_to_setup_data_nr(struct kobject *kobj, int *nr)
63{
64 const char *name;
65
66 name = kobject_name(kobj);
67 return kstrtoint(name, 10, nr);
68}
69
70static int get_setup_data_paddr(int nr, u64 *paddr)
71{
72 int i = 0;
73 struct setup_data *data;
74 u64 pa_data = boot_params.hdr.setup_data;
75
76 while (pa_data) {
77 if (nr == i) {
78 *paddr = pa_data;
79 return 0;
80 }
81 data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
82 if (!data)
83 return -ENOMEM;
84
85 pa_data = data->next;
86 memunmap(data);
87 i++;
88 }
89 return -EINVAL;
90}
91
92static int __init get_setup_data_size(int nr, size_t *size)
93{
94 int i = 0;
95 struct setup_data *data;
96 u64 pa_data = boot_params.hdr.setup_data;
97
98 while (pa_data) {
99 data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
100 if (!data)
101 return -ENOMEM;
102 if (nr == i) {
103 *size = data->len;
104 memunmap(data);
105 return 0;
106 }
107
108 pa_data = data->next;
109 memunmap(data);
110 i++;
111 }
112 return -EINVAL;
113}
114
115static ssize_t type_show(struct kobject *kobj,
116 struct kobj_attribute *attr, char *buf)
117{
118 int nr, ret;
119 u64 paddr;
120 struct setup_data *data;
121
122 ret = kobj_to_setup_data_nr(kobj, &nr);
123 if (ret)
124 return ret;
125
126 ret = get_setup_data_paddr(nr, &paddr);
127 if (ret)
128 return ret;
129 data = memremap(paddr, sizeof(*data), MEMREMAP_WB);
130 if (!data)
131 return -ENOMEM;
132
133 ret = sprintf(buf, "0x%x\n", data->type);
134 memunmap(data);
135 return ret;
136}
137
138static ssize_t setup_data_data_read(struct file *fp,
139 struct kobject *kobj,
140 struct bin_attribute *bin_attr,
141 char *buf,
142 loff_t off, size_t count)
143{
144 int nr, ret = 0;
145 u64 paddr;
146 struct setup_data *data;
147 void *p;
148
149 ret = kobj_to_setup_data_nr(kobj, &nr);
150 if (ret)
151 return ret;
152
153 ret = get_setup_data_paddr(nr, &paddr);
154 if (ret)
155 return ret;
156 data = memremap(paddr, sizeof(*data), MEMREMAP_WB);
157 if (!data)
158 return -ENOMEM;
159
160 if (off > data->len) {
161 ret = -EINVAL;
162 goto out;
163 }
164
165 if (count > data->len - off)
166 count = data->len - off;
167
168 if (!count)
169 goto out;
170
171 ret = count;
172 p = memremap(paddr + sizeof(*data), data->len, MEMREMAP_WB);
173 if (!p) {
174 ret = -ENOMEM;
175 goto out;
176 }
177 memcpy(buf, p + off, count);
178 memunmap(p);
179out:
180 memunmap(data);
181 return ret;
182}
183
184static struct kobj_attribute type_attr = __ATTR_RO(type);
185
186static struct bin_attribute data_attr __ro_after_init = {
187 .attr = {
188 .name = "data",
189 .mode = S_IRUGO,
190 },
191 .read = setup_data_data_read,
192};
193
194static struct attribute *setup_data_type_attrs[] = {
195 &type_attr.attr,
196 NULL,
197};
198
199static struct bin_attribute *setup_data_data_attrs[] = {
200 &data_attr,
201 NULL,
202};
203
204static const struct attribute_group setup_data_attr_group = {
205 .attrs = setup_data_type_attrs,
206 .bin_attrs = setup_data_data_attrs,
207};
208
209static int __init create_setup_data_node(struct kobject *parent,
210 struct kobject **kobjp, int nr)
211{
212 int ret = 0;
213 size_t size;
214 struct kobject *kobj;
215 char name[16]; /* should be enough for setup_data nodes numbers */
216 snprintf(name, 16, "%d", nr);
217
218 kobj = kobject_create_and_add(name, parent);
219 if (!kobj)
220 return -ENOMEM;
221
222 ret = get_setup_data_size(nr, &size);
223 if (ret)
224 goto out_kobj;
225
226 data_attr.size = size;
227 ret = sysfs_create_group(kobj, &setup_data_attr_group);
228 if (ret)
229 goto out_kobj;
230 *kobjp = kobj;
231
232 return 0;
233out_kobj:
234 kobject_put(kobj);
235 return ret;
236}
237
238static void __init cleanup_setup_data_node(struct kobject *kobj)
239{
240 sysfs_remove_group(kobj, &setup_data_attr_group);
241 kobject_put(kobj);
242}
243
244static int __init get_setup_data_total_num(u64 pa_data, int *nr)
245{
246 int ret = 0;
247 struct setup_data *data;
248
249 *nr = 0;
250 while (pa_data) {
251 *nr += 1;
252 data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
253 if (!data) {
254 ret = -ENOMEM;
255 goto out;
256 }
257 pa_data = data->next;
258 memunmap(data);
259 }
260
261out:
262 return ret;
263}
264
265static int __init create_setup_data_nodes(struct kobject *parent)
266{
267 struct kobject *setup_data_kobj, **kobjp;
268 u64 pa_data;
269 int i, j, nr, ret = 0;
270
271 pa_data = boot_params.hdr.setup_data;
272 if (!pa_data)
273 return 0;
274
275 setup_data_kobj = kobject_create_and_add("setup_data", parent);
276 if (!setup_data_kobj) {
277 ret = -ENOMEM;
278 goto out;
279 }
280
281 ret = get_setup_data_total_num(pa_data, &nr);
282 if (ret)
283 goto out_setup_data_kobj;
284
285 kobjp = kmalloc_array(nr, sizeof(*kobjp), GFP_KERNEL);
286 if (!kobjp) {
287 ret = -ENOMEM;
288 goto out_setup_data_kobj;
289 }
290
291 for (i = 0; i < nr; i++) {
292 ret = create_setup_data_node(setup_data_kobj, kobjp + i, i);
293 if (ret)
294 goto out_clean_nodes;
295 }
296
297 kfree(kobjp);
298 return 0;
299
300out_clean_nodes:
301 for (j = i - 1; j >= 0; j--)
302 cleanup_setup_data_node(*(kobjp + j));
303 kfree(kobjp);
304out_setup_data_kobj:
305 kobject_put(setup_data_kobj);
306out:
307 return ret;
308}
309
310static int __init boot_params_ksysfs_init(void)
311{
312 int ret;
313 struct kobject *boot_params_kobj;
314
315 boot_params_kobj = kobject_create_and_add("boot_params",
316 kernel_kobj);
317 if (!boot_params_kobj) {
318 ret = -ENOMEM;
319 goto out;
320 }
321
322 ret = sysfs_create_group(boot_params_kobj, &boot_params_attr_group);
323 if (ret)
324 goto out_boot_params_kobj;
325
326 ret = create_setup_data_nodes(boot_params_kobj);
327 if (ret)
328 goto out_create_group;
329
330 return 0;
331out_create_group:
332 sysfs_remove_group(boot_params_kobj, &boot_params_attr_group);
333out_boot_params_kobj:
334 kobject_put(boot_params_kobj);
335out:
336 return ret;
337}
338
339arch_initcall(boot_params_ksysfs_init);
1/*
2 * Architecture specific sysfs attributes in /sys/kernel
3 *
4 * Copyright (C) 2007, Intel Corp.
5 * Huang Ying <ying.huang@intel.com>
6 * Copyright (C) 2013, 2013 Red Hat, Inc.
7 * Dave Young <dyoung@redhat.com>
8 *
9 * This file is released under the GPLv2
10 */
11
12#include <linux/kobject.h>
13#include <linux/string.h>
14#include <linux/sysfs.h>
15#include <linux/init.h>
16#include <linux/stat.h>
17#include <linux/slab.h>
18#include <linux/mm.h>
19
20#include <asm/io.h>
21#include <asm/setup.h>
22
23static ssize_t version_show(struct kobject *kobj,
24 struct kobj_attribute *attr, char *buf)
25{
26 return sprintf(buf, "0x%04x\n", boot_params.hdr.version);
27}
28
29static struct kobj_attribute boot_params_version_attr = __ATTR_RO(version);
30
31static ssize_t boot_params_data_read(struct file *fp, struct kobject *kobj,
32 struct bin_attribute *bin_attr,
33 char *buf, loff_t off, size_t count)
34{
35 memcpy(buf, (void *)&boot_params + off, count);
36 return count;
37}
38
39static struct bin_attribute boot_params_data_attr = {
40 .attr = {
41 .name = "data",
42 .mode = S_IRUGO,
43 },
44 .read = boot_params_data_read,
45 .size = sizeof(boot_params),
46};
47
48static struct attribute *boot_params_version_attrs[] = {
49 &boot_params_version_attr.attr,
50 NULL,
51};
52
53static struct bin_attribute *boot_params_data_attrs[] = {
54 &boot_params_data_attr,
55 NULL,
56};
57
58static struct attribute_group boot_params_attr_group = {
59 .attrs = boot_params_version_attrs,
60 .bin_attrs = boot_params_data_attrs,
61};
62
63static int kobj_to_setup_data_nr(struct kobject *kobj, int *nr)
64{
65 const char *name;
66
67 name = kobject_name(kobj);
68 return kstrtoint(name, 10, nr);
69}
70
71static int get_setup_data_paddr(int nr, u64 *paddr)
72{
73 int i = 0;
74 struct setup_data *data;
75 u64 pa_data = boot_params.hdr.setup_data;
76
77 while (pa_data) {
78 if (nr == i) {
79 *paddr = pa_data;
80 return 0;
81 }
82 data = ioremap_cache(pa_data, sizeof(*data));
83 if (!data)
84 return -ENOMEM;
85
86 pa_data = data->next;
87 iounmap(data);
88 i++;
89 }
90 return -EINVAL;
91}
92
93static int __init get_setup_data_size(int nr, size_t *size)
94{
95 int i = 0;
96 struct setup_data *data;
97 u64 pa_data = boot_params.hdr.setup_data;
98
99 while (pa_data) {
100 data = ioremap_cache(pa_data, sizeof(*data));
101 if (!data)
102 return -ENOMEM;
103 if (nr == i) {
104 *size = data->len;
105 iounmap(data);
106 return 0;
107 }
108
109 pa_data = data->next;
110 iounmap(data);
111 i++;
112 }
113 return -EINVAL;
114}
115
116static ssize_t type_show(struct kobject *kobj,
117 struct kobj_attribute *attr, char *buf)
118{
119 int nr, ret;
120 u64 paddr;
121 struct setup_data *data;
122
123 ret = kobj_to_setup_data_nr(kobj, &nr);
124 if (ret)
125 return ret;
126
127 ret = get_setup_data_paddr(nr, &paddr);
128 if (ret)
129 return ret;
130 data = ioremap_cache(paddr, sizeof(*data));
131 if (!data)
132 return -ENOMEM;
133
134 ret = sprintf(buf, "0x%x\n", data->type);
135 iounmap(data);
136 return ret;
137}
138
139static ssize_t setup_data_data_read(struct file *fp,
140 struct kobject *kobj,
141 struct bin_attribute *bin_attr,
142 char *buf,
143 loff_t off, size_t count)
144{
145 int nr, ret = 0;
146 u64 paddr;
147 struct setup_data *data;
148 void *p;
149
150 ret = kobj_to_setup_data_nr(kobj, &nr);
151 if (ret)
152 return ret;
153
154 ret = get_setup_data_paddr(nr, &paddr);
155 if (ret)
156 return ret;
157 data = ioremap_cache(paddr, sizeof(*data));
158 if (!data)
159 return -ENOMEM;
160
161 if (off > data->len) {
162 ret = -EINVAL;
163 goto out;
164 }
165
166 if (count > data->len - off)
167 count = data->len - off;
168
169 if (!count)
170 goto out;
171
172 ret = count;
173 p = ioremap_cache(paddr + sizeof(*data), data->len);
174 if (!p) {
175 ret = -ENOMEM;
176 goto out;
177 }
178 memcpy(buf, p + off, count);
179 iounmap(p);
180out:
181 iounmap(data);
182 return ret;
183}
184
185static struct kobj_attribute type_attr = __ATTR_RO(type);
186
187static struct bin_attribute data_attr = {
188 .attr = {
189 .name = "data",
190 .mode = S_IRUGO,
191 },
192 .read = setup_data_data_read,
193};
194
195static struct attribute *setup_data_type_attrs[] = {
196 &type_attr.attr,
197 NULL,
198};
199
200static struct bin_attribute *setup_data_data_attrs[] = {
201 &data_attr,
202 NULL,
203};
204
205static struct attribute_group setup_data_attr_group = {
206 .attrs = setup_data_type_attrs,
207 .bin_attrs = setup_data_data_attrs,
208};
209
210static int __init create_setup_data_node(struct kobject *parent,
211 struct kobject **kobjp, int nr)
212{
213 int ret = 0;
214 size_t size;
215 struct kobject *kobj;
216 char name[16]; /* should be enough for setup_data nodes numbers */
217 snprintf(name, 16, "%d", nr);
218
219 kobj = kobject_create_and_add(name, parent);
220 if (!kobj)
221 return -ENOMEM;
222
223 ret = get_setup_data_size(nr, &size);
224 if (ret)
225 goto out_kobj;
226
227 data_attr.size = size;
228 ret = sysfs_create_group(kobj, &setup_data_attr_group);
229 if (ret)
230 goto out_kobj;
231 *kobjp = kobj;
232
233 return 0;
234out_kobj:
235 kobject_put(kobj);
236 return ret;
237}
238
239static void __init cleanup_setup_data_node(struct kobject *kobj)
240{
241 sysfs_remove_group(kobj, &setup_data_attr_group);
242 kobject_put(kobj);
243}
244
245static int __init get_setup_data_total_num(u64 pa_data, int *nr)
246{
247 int ret = 0;
248 struct setup_data *data;
249
250 *nr = 0;
251 while (pa_data) {
252 *nr += 1;
253 data = ioremap_cache(pa_data, sizeof(*data));
254 if (!data) {
255 ret = -ENOMEM;
256 goto out;
257 }
258 pa_data = data->next;
259 iounmap(data);
260 }
261
262out:
263 return ret;
264}
265
266static int __init create_setup_data_nodes(struct kobject *parent)
267{
268 struct kobject *setup_data_kobj, **kobjp;
269 u64 pa_data;
270 int i, j, nr, ret = 0;
271
272 pa_data = boot_params.hdr.setup_data;
273 if (!pa_data)
274 return 0;
275
276 setup_data_kobj = kobject_create_and_add("setup_data", parent);
277 if (!setup_data_kobj) {
278 ret = -ENOMEM;
279 goto out;
280 }
281
282 ret = get_setup_data_total_num(pa_data, &nr);
283 if (ret)
284 goto out_setup_data_kobj;
285
286 kobjp = kmalloc(sizeof(*kobjp) * nr, GFP_KERNEL);
287 if (!kobjp) {
288 ret = -ENOMEM;
289 goto out_setup_data_kobj;
290 }
291
292 for (i = 0; i < nr; i++) {
293 ret = create_setup_data_node(setup_data_kobj, kobjp + i, i);
294 if (ret)
295 goto out_clean_nodes;
296 }
297
298 kfree(kobjp);
299 return 0;
300
301out_clean_nodes:
302 for (j = i - 1; j > 0; j--)
303 cleanup_setup_data_node(*(kobjp + j));
304 kfree(kobjp);
305out_setup_data_kobj:
306 kobject_put(setup_data_kobj);
307out:
308 return ret;
309}
310
311static int __init boot_params_ksysfs_init(void)
312{
313 int ret;
314 struct kobject *boot_params_kobj;
315
316 boot_params_kobj = kobject_create_and_add("boot_params",
317 kernel_kobj);
318 if (!boot_params_kobj) {
319 ret = -ENOMEM;
320 goto out;
321 }
322
323 ret = sysfs_create_group(boot_params_kobj, &boot_params_attr_group);
324 if (ret)
325 goto out_boot_params_kobj;
326
327 ret = create_setup_data_nodes(boot_params_kobj);
328 if (ret)
329 goto out_create_group;
330
331 return 0;
332out_create_group:
333 sysfs_remove_group(boot_params_kobj, &boot_params_attr_group);
334out_boot_params_kobj:
335 kobject_put(boot_params_kobj);
336out:
337 return ret;
338}
339
340arch_initcall(boot_params_ksysfs_init);