drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / arch / x86 / kernel / ksysfs.c
blob257892fcefa794803d8eaf2d3d1810ebb278957b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Architecture specific sysfs attributes in /sys/kernel
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 */
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>
20 #include <asm/setup.h>
22 static ssize_t version_show(struct kobject *kobj,
23 struct kobj_attribute *attr, char *buf)
25 return sprintf(buf, "0x%04x\n", boot_params.hdr.version);
28 static struct kobj_attribute boot_params_version_attr = __ATTR_RO(version);
30 static 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)
34 memcpy(buf, (void *)&boot_params + off, count);
35 return count;
38 static struct bin_attribute boot_params_data_attr = {
39 .attr = {
40 .name = "data",
41 .mode = S_IRUGO,
43 .read = boot_params_data_read,
44 .size = sizeof(boot_params),
47 static struct attribute *boot_params_version_attrs[] = {
48 &boot_params_version_attr.attr,
49 NULL,
52 static struct bin_attribute *boot_params_data_attrs[] = {
53 &boot_params_data_attr,
54 NULL,
57 static const struct attribute_group boot_params_attr_group = {
58 .attrs = boot_params_version_attrs,
59 .bin_attrs = boot_params_data_attrs,
62 static int kobj_to_setup_data_nr(struct kobject *kobj, int *nr)
64 const char *name;
66 name = kobject_name(kobj);
67 return kstrtoint(name, 10, nr);
70 static int get_setup_data_paddr(int nr, u64 *paddr)
72 int i = 0;
73 struct setup_data *data;
74 u64 pa_data = boot_params.hdr.setup_data;
76 while (pa_data) {
77 if (nr == i) {
78 *paddr = pa_data;
79 return 0;
81 data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
82 if (!data)
83 return -ENOMEM;
85 pa_data = data->next;
86 memunmap(data);
87 i++;
89 return -EINVAL;
92 static int __init get_setup_data_size(int nr, size_t *size)
94 u64 pa_data = boot_params.hdr.setup_data, pa_next;
95 struct setup_indirect *indirect;
96 struct setup_data *data;
97 int i = 0;
98 u32 len;
100 while (pa_data) {
101 data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
102 if (!data)
103 return -ENOMEM;
104 pa_next = data->next;
106 if (nr == i) {
107 if (data->type == SETUP_INDIRECT) {
108 len = sizeof(*data) + data->len;
109 memunmap(data);
110 data = memremap(pa_data, len, MEMREMAP_WB);
111 if (!data)
112 return -ENOMEM;
114 indirect = (struct setup_indirect *)data->data;
116 if (indirect->type != SETUP_INDIRECT)
117 *size = indirect->len;
118 else
119 *size = data->len;
120 } else {
121 *size = data->len;
124 memunmap(data);
125 return 0;
128 pa_data = pa_next;
129 memunmap(data);
130 i++;
132 return -EINVAL;
135 static ssize_t type_show(struct kobject *kobj,
136 struct kobj_attribute *attr, char *buf)
138 struct setup_indirect *indirect;
139 struct setup_data *data;
140 int nr, ret;
141 u64 paddr;
142 u32 len;
144 ret = kobj_to_setup_data_nr(kobj, &nr);
145 if (ret)
146 return ret;
148 ret = get_setup_data_paddr(nr, &paddr);
149 if (ret)
150 return ret;
151 data = memremap(paddr, sizeof(*data), MEMREMAP_WB);
152 if (!data)
153 return -ENOMEM;
155 if (data->type == SETUP_INDIRECT) {
156 len = sizeof(*data) + data->len;
157 memunmap(data);
158 data = memremap(paddr, len, MEMREMAP_WB);
159 if (!data)
160 return -ENOMEM;
162 indirect = (struct setup_indirect *)data->data;
164 ret = sprintf(buf, "0x%x\n", indirect->type);
165 } else {
166 ret = sprintf(buf, "0x%x\n", data->type);
169 memunmap(data);
170 return ret;
173 static ssize_t setup_data_data_read(struct file *fp,
174 struct kobject *kobj,
175 struct bin_attribute *bin_attr,
176 char *buf,
177 loff_t off, size_t count)
179 struct setup_indirect *indirect;
180 struct setup_data *data;
181 int nr, ret = 0;
182 u64 paddr, len;
183 void *p;
185 ret = kobj_to_setup_data_nr(kobj, &nr);
186 if (ret)
187 return ret;
189 ret = get_setup_data_paddr(nr, &paddr);
190 if (ret)
191 return ret;
192 data = memremap(paddr, sizeof(*data), MEMREMAP_WB);
193 if (!data)
194 return -ENOMEM;
196 if (data->type == SETUP_INDIRECT) {
197 len = sizeof(*data) + data->len;
198 memunmap(data);
199 data = memremap(paddr, len, MEMREMAP_WB);
200 if (!data)
201 return -ENOMEM;
203 indirect = (struct setup_indirect *)data->data;
205 if (indirect->type != SETUP_INDIRECT) {
206 paddr = indirect->addr;
207 len = indirect->len;
208 } else {
210 * Even though this is technically undefined, return
211 * the data as though it is a normal setup_data struct.
212 * This will at least allow it to be inspected.
214 paddr += sizeof(*data);
215 len = data->len;
217 } else {
218 paddr += sizeof(*data);
219 len = data->len;
222 if (off > len) {
223 ret = -EINVAL;
224 goto out;
227 if (count > len - off)
228 count = len - off;
230 if (!count)
231 goto out;
233 ret = count;
234 p = memremap(paddr, len, MEMREMAP_WB);
235 if (!p) {
236 ret = -ENOMEM;
237 goto out;
239 memcpy(buf, p + off, count);
240 memunmap(p);
241 out:
242 memunmap(data);
243 return ret;
246 static struct kobj_attribute type_attr = __ATTR_RO(type);
248 static struct bin_attribute data_attr __ro_after_init = {
249 .attr = {
250 .name = "data",
251 .mode = S_IRUGO,
253 .read = setup_data_data_read,
256 static struct attribute *setup_data_type_attrs[] = {
257 &type_attr.attr,
258 NULL,
261 static struct bin_attribute *setup_data_data_attrs[] = {
262 &data_attr,
263 NULL,
266 static const struct attribute_group setup_data_attr_group = {
267 .attrs = setup_data_type_attrs,
268 .bin_attrs = setup_data_data_attrs,
271 static int __init create_setup_data_node(struct kobject *parent,
272 struct kobject **kobjp, int nr)
274 int ret = 0;
275 size_t size;
276 struct kobject *kobj;
277 char name[16]; /* should be enough for setup_data nodes numbers */
278 snprintf(name, 16, "%d", nr);
280 kobj = kobject_create_and_add(name, parent);
281 if (!kobj)
282 return -ENOMEM;
284 ret = get_setup_data_size(nr, &size);
285 if (ret)
286 goto out_kobj;
288 data_attr.size = size;
289 ret = sysfs_create_group(kobj, &setup_data_attr_group);
290 if (ret)
291 goto out_kobj;
292 *kobjp = kobj;
294 return 0;
295 out_kobj:
296 kobject_put(kobj);
297 return ret;
300 static void __init cleanup_setup_data_node(struct kobject *kobj)
302 sysfs_remove_group(kobj, &setup_data_attr_group);
303 kobject_put(kobj);
306 static int __init get_setup_data_total_num(u64 pa_data, int *nr)
308 int ret = 0;
309 struct setup_data *data;
311 *nr = 0;
312 while (pa_data) {
313 *nr += 1;
314 data = memremap(pa_data, sizeof(*data), MEMREMAP_WB);
315 if (!data) {
316 ret = -ENOMEM;
317 goto out;
319 pa_data = data->next;
320 memunmap(data);
323 out:
324 return ret;
327 static int __init create_setup_data_nodes(struct kobject *parent)
329 struct kobject *setup_data_kobj, **kobjp;
330 u64 pa_data;
331 int i, j, nr, ret = 0;
333 pa_data = boot_params.hdr.setup_data;
334 if (!pa_data)
335 return 0;
337 setup_data_kobj = kobject_create_and_add("setup_data", parent);
338 if (!setup_data_kobj) {
339 ret = -ENOMEM;
340 goto out;
343 ret = get_setup_data_total_num(pa_data, &nr);
344 if (ret)
345 goto out_setup_data_kobj;
347 kobjp = kmalloc_array(nr, sizeof(*kobjp), GFP_KERNEL);
348 if (!kobjp) {
349 ret = -ENOMEM;
350 goto out_setup_data_kobj;
353 for (i = 0; i < nr; i++) {
354 ret = create_setup_data_node(setup_data_kobj, kobjp + i, i);
355 if (ret)
356 goto out_clean_nodes;
359 kfree(kobjp);
360 return 0;
362 out_clean_nodes:
363 for (j = i - 1; j >= 0; j--)
364 cleanup_setup_data_node(*(kobjp + j));
365 kfree(kobjp);
366 out_setup_data_kobj:
367 kobject_put(setup_data_kobj);
368 out:
369 return ret;
372 static int __init boot_params_ksysfs_init(void)
374 int ret;
375 struct kobject *boot_params_kobj;
377 boot_params_kobj = kobject_create_and_add("boot_params",
378 kernel_kobj);
379 if (!boot_params_kobj) {
380 ret = -ENOMEM;
381 goto out;
384 ret = sysfs_create_group(boot_params_kobj, &boot_params_attr_group);
385 if (ret)
386 goto out_boot_params_kobj;
388 ret = create_setup_data_nodes(boot_params_kobj);
389 if (ret)
390 goto out_create_group;
392 return 0;
393 out_create_group:
394 sysfs_remove_group(boot_params_kobj, &boot_params_attr_group);
395 out_boot_params_kobj:
396 kobject_put(boot_params_kobj);
397 out:
398 return ret;
401 arch_initcall(boot_params_ksysfs_init);