Linux 4.1.18
[linux/fpc-iii.git] / arch / x86 / kernel / ksysfs.c
blobc2bedaea11f7b929709e64979e17e15ca4d8d60a
1 /*
2 * Architecture specific sysfs attributes in /sys/kernel
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>
9 * This file is released under the GPLv2
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>
20 #include <asm/io.h>
21 #include <asm/setup.h>
23 static ssize_t version_show(struct kobject *kobj,
24 struct kobj_attribute *attr, char *buf)
26 return sprintf(buf, "0x%04x\n", boot_params.hdr.version);
29 static struct kobj_attribute boot_params_version_attr = __ATTR_RO(version);
31 static 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)
35 memcpy(buf, (void *)&boot_params + off, count);
36 return count;
39 static struct bin_attribute boot_params_data_attr = {
40 .attr = {
41 .name = "data",
42 .mode = S_IRUGO,
44 .read = boot_params_data_read,
45 .size = sizeof(boot_params),
48 static struct attribute *boot_params_version_attrs[] = {
49 &boot_params_version_attr.attr,
50 NULL,
53 static struct bin_attribute *boot_params_data_attrs[] = {
54 &boot_params_data_attr,
55 NULL,
58 static struct attribute_group boot_params_attr_group = {
59 .attrs = boot_params_version_attrs,
60 .bin_attrs = boot_params_data_attrs,
63 static int kobj_to_setup_data_nr(struct kobject *kobj, int *nr)
65 const char *name;
67 name = kobject_name(kobj);
68 return kstrtoint(name, 10, nr);
71 static int get_setup_data_paddr(int nr, u64 *paddr)
73 int i = 0;
74 struct setup_data *data;
75 u64 pa_data = boot_params.hdr.setup_data;
77 while (pa_data) {
78 if (nr == i) {
79 *paddr = pa_data;
80 return 0;
82 data = ioremap_cache(pa_data, sizeof(*data));
83 if (!data)
84 return -ENOMEM;
86 pa_data = data->next;
87 iounmap(data);
88 i++;
90 return -EINVAL;
93 static int __init get_setup_data_size(int nr, size_t *size)
95 int i = 0;
96 struct setup_data *data;
97 u64 pa_data = boot_params.hdr.setup_data;
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;
109 pa_data = data->next;
110 iounmap(data);
111 i++;
113 return -EINVAL;
116 static ssize_t type_show(struct kobject *kobj,
117 struct kobj_attribute *attr, char *buf)
119 int nr, ret;
120 u64 paddr;
121 struct setup_data *data;
123 ret = kobj_to_setup_data_nr(kobj, &nr);
124 if (ret)
125 return ret;
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;
134 ret = sprintf(buf, "0x%x\n", data->type);
135 iounmap(data);
136 return ret;
139 static 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)
145 int nr, ret = 0;
146 u64 paddr;
147 struct setup_data *data;
148 void *p;
150 ret = kobj_to_setup_data_nr(kobj, &nr);
151 if (ret)
152 return ret;
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;
161 if (off > data->len) {
162 ret = -EINVAL;
163 goto out;
166 if (count > data->len - off)
167 count = data->len - off;
169 if (!count)
170 goto out;
172 ret = count;
173 p = ioremap_cache(paddr + sizeof(*data), data->len);
174 if (!p) {
175 ret = -ENOMEM;
176 goto out;
178 memcpy(buf, p + off, count);
179 iounmap(p);
180 out:
181 iounmap(data);
182 return ret;
185 static struct kobj_attribute type_attr = __ATTR_RO(type);
187 static struct bin_attribute data_attr = {
188 .attr = {
189 .name = "data",
190 .mode = S_IRUGO,
192 .read = setup_data_data_read,
195 static struct attribute *setup_data_type_attrs[] = {
196 &type_attr.attr,
197 NULL,
200 static struct bin_attribute *setup_data_data_attrs[] = {
201 &data_attr,
202 NULL,
205 static struct attribute_group setup_data_attr_group = {
206 .attrs = setup_data_type_attrs,
207 .bin_attrs = setup_data_data_attrs,
210 static int __init create_setup_data_node(struct kobject *parent,
211 struct kobject **kobjp, int nr)
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);
219 kobj = kobject_create_and_add(name, parent);
220 if (!kobj)
221 return -ENOMEM;
223 ret = get_setup_data_size(nr, &size);
224 if (ret)
225 goto out_kobj;
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;
233 return 0;
234 out_kobj:
235 kobject_put(kobj);
236 return ret;
239 static void __init cleanup_setup_data_node(struct kobject *kobj)
241 sysfs_remove_group(kobj, &setup_data_attr_group);
242 kobject_put(kobj);
245 static int __init get_setup_data_total_num(u64 pa_data, int *nr)
247 int ret = 0;
248 struct setup_data *data;
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;
258 pa_data = data->next;
259 iounmap(data);
262 out:
263 return ret;
266 static int __init create_setup_data_nodes(struct kobject *parent)
268 struct kobject *setup_data_kobj, **kobjp;
269 u64 pa_data;
270 int i, j, nr, ret = 0;
272 pa_data = boot_params.hdr.setup_data;
273 if (!pa_data)
274 return 0;
276 setup_data_kobj = kobject_create_and_add("setup_data", parent);
277 if (!setup_data_kobj) {
278 ret = -ENOMEM;
279 goto out;
282 ret = get_setup_data_total_num(pa_data, &nr);
283 if (ret)
284 goto out_setup_data_kobj;
286 kobjp = kmalloc(sizeof(*kobjp) * nr, GFP_KERNEL);
287 if (!kobjp) {
288 ret = -ENOMEM;
289 goto out_setup_data_kobj;
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;
298 kfree(kobjp);
299 return 0;
301 out_clean_nodes:
302 for (j = i - 1; j > 0; j--)
303 cleanup_setup_data_node(*(kobjp + j));
304 kfree(kobjp);
305 out_setup_data_kobj:
306 kobject_put(setup_data_kobj);
307 out:
308 return ret;
311 static int __init boot_params_ksysfs_init(void)
313 int ret;
314 struct kobject *boot_params_kobj;
316 boot_params_kobj = kobject_create_and_add("boot_params",
317 kernel_kobj);
318 if (!boot_params_kobj) {
319 ret = -ENOMEM;
320 goto out;
323 ret = sysfs_create_group(boot_params_kobj, &boot_params_attr_group);
324 if (ret)
325 goto out_boot_params_kobj;
327 ret = create_setup_data_nodes(boot_params_kobj);
328 if (ret)
329 goto out_create_group;
331 return 0;
332 out_create_group:
333 sysfs_remove_group(boot_params_kobj, &boot_params_attr_group);
334 out_boot_params_kobj:
335 kobject_put(boot_params_kobj);
336 out:
337 return ret;
340 arch_initcall(boot_params_ksysfs_init);