Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a35377

Browse files
committed
Merge linux-6.12-trunk/fpga/configfs into linux-6.12-mchp+fpga
Signed-off-by: Conor Dooley <[email protected]>
2 parents ea5af36 + 1e35d67 commit 5a35377

File tree

4 files changed

+336
-0
lines changed

4 files changed

+336
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Howto use the configfs overlay interface.
2+
3+
A device-tree configfs entry is created in /config/device-tree/overlays
4+
and and it is manipulated using standard file system I/O.
5+
Note that this is a debug level interface, for use by developers and
6+
not necessarily something accessed by normal users due to the
7+
security implications of having direct access to the kernel's device tree.
8+
9+
* To create an overlay you mkdir the directory:
10+
11+
# mkdir /config/device-tree/overlays/foo
12+
13+
* Either you echo the overlay firmware file to the path property file.
14+
15+
# echo foo.dtbo >/config/device-tree/overlays/foo/path
16+
17+
* Or you cat the contents of the overlay to the dtbo file
18+
19+
# cat foo.dtbo >/config/device-tree/overlays/foo/dtbo
20+
21+
The overlay file will be applied, and devices will be created/destroyed
22+
as required.
23+
24+
To remove it simply rmdir the directory.
25+
26+
# rmdir /config/device-tree/overlays/foo
27+
28+
The rationalle of the dual interface (firmware & direct copy) is that each is
29+
better suited to different use patterns. The firmware interface is what's
30+
intended to be used by hardware managers in the kernel, while the copy interface
31+
make sense for developers (since it avoids problems with namespaces).

drivers/of/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,16 @@ config OF_OVERLAY_KUNIT_TEST
120120
config OF_NUMA
121121
bool
122122

123+
124+
config OF_CONFIGFS
125+
bool "Device Tree Overlay ConfigFS interface"
126+
select CONFIGFS_FS
127+
depends on OF_OVERLAY
128+
help
129+
Select this option to enable simple user-space driven DT overlay
130+
interface to support device tree manipulated at runtime.
131+
Say Y here to include this support.
132+
133+
If unsure, say N.
134+
123135
endif # OF

drivers/of/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22
obj-y = base.o cpu.o device.o module.o platform.o property.o
33
obj-$(CONFIG_OF_KOBJ) += kobj.o
4+
obj-$(CONFIG_OF_CONFIGFS) += configfs.o
45
obj-$(CONFIG_OF_DYNAMIC) += dynamic.o
56
obj-$(CONFIG_OF_FLATTREE) += fdt.o empty_root.dtb.o
67
obj-$(CONFIG_OF_EARLY_FLATTREE) += fdt_address.o

drivers/of/configfs.c

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Configfs entries for device-tree
4+
*
5+
* Copyright (C) 2013 - Pantelis Antoniou <[email protected]>
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version
10+
* 2 of the License, or (at your option) any later version.
11+
*/
12+
#include <linux/ctype.h>
13+
#include <linux/cpu.h>
14+
#include <linux/module.h>
15+
#include <linux/of.h>
16+
#include <linux/of_fdt.h>
17+
#include <linux/spinlock.h>
18+
#include <linux/sizes.h>
19+
#include <linux/slab.h>
20+
#include <linux/proc_fs.h>
21+
#include <linux/configfs.h>
22+
#include <linux/types.h>
23+
#include <linux/stat.h>
24+
#include <linux/limits.h>
25+
#include <linux/file.h>
26+
#include <linux/vmalloc.h>
27+
#include <linux/firmware.h>
28+
29+
#include "of_private.h"
30+
31+
struct cfs_overlay_item {
32+
struct config_item item;
33+
34+
char path[PATH_MAX];
35+
36+
const struct firmware *fw;
37+
struct device_node *overlay;
38+
int ov_id;
39+
40+
void *dtbo;
41+
int dtbo_size;
42+
43+
void *mem;
44+
};
45+
46+
static DEFINE_MUTEX(overlay_lock);
47+
48+
static int create_overlay(struct cfs_overlay_item *overlay, void *blob)
49+
{
50+
int err;
51+
52+
/* FIXME */
53+
err = of_overlay_fdt_apply(blob, overlay->dtbo_size, &overlay->ov_id, NULL);
54+
if (err < 0) {
55+
pr_err("%s: Failed to create overlay (err=%d)\n",
56+
__func__, err);
57+
return err;
58+
}
59+
60+
return err;
61+
}
62+
63+
static inline struct cfs_overlay_item
64+
*to_cfs_overlay_item(struct config_item *item)
65+
{
66+
return item ? container_of(item, struct cfs_overlay_item, item) : NULL;
67+
}
68+
69+
static ssize_t cfs_overlay_item_path_show(struct config_item *item, char *page)
70+
{
71+
return sprintf(page, "%s\n", to_cfs_overlay_item(item)->path);
72+
}
73+
74+
static ssize_t cfs_overlay_item_path_store(struct config_item *item,
75+
const char *page, size_t count)
76+
{
77+
struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
78+
const char *p = page;
79+
char *s;
80+
int err;
81+
82+
/* if it's set do not allow changes */
83+
if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
84+
return -EPERM;
85+
86+
/* copy to path buffer (and make sure it's always zero terminated */
87+
count = snprintf(overlay->path, sizeof(overlay->path) - 1, "%s", p);
88+
overlay->path[sizeof(overlay->path) - 1] = '\0';
89+
90+
/* strip trailing newlines */
91+
s = overlay->path + strlen(overlay->path);
92+
while (s > overlay->path && *--s == '\n')
93+
*s = '\0';
94+
95+
pr_debug("%s: path is '%s'\n", __func__, overlay->path);
96+
97+
err = request_firmware(&overlay->fw, overlay->path, NULL);
98+
if (err != 0)
99+
goto out_err;
100+
101+
overlay->dtbo_size = overlay->fw->size;
102+
err = create_overlay(overlay, (void *)overlay->fw->data);
103+
if (err < 0)
104+
goto out_err;
105+
106+
return count;
107+
108+
out_err:
109+
110+
release_firmware(overlay->fw);
111+
overlay->fw = NULL;
112+
113+
overlay->path[0] = '\0';
114+
115+
return count;
116+
}
117+
118+
static ssize_t cfs_overlay_item_status_show(struct config_item *item,
119+
char *page)
120+
{
121+
return sprintf(page, "%s\n", to_cfs_overlay_item(item)->ov_id >= 0 ?
122+
"applied" : "unapplied");
123+
}
124+
125+
CONFIGFS_ATTR(cfs_overlay_item_, path);
126+
CONFIGFS_ATTR_RO(cfs_overlay_item_, status);
127+
128+
static struct configfs_attribute *cfs_overlay_attrs[] = {
129+
&cfs_overlay_item_attr_path,
130+
&cfs_overlay_item_attr_status,
131+
NULL,
132+
};
133+
134+
static ssize_t cfs_overlay_item_dtbo_read(struct config_item *item,
135+
void *buf, size_t max_count)
136+
{
137+
struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
138+
139+
pr_debug("%s: buf=%p max_count=%zu\n", __func__, buf, max_count);
140+
141+
if (!overlay->dtbo)
142+
return 0;
143+
144+
/* copy if buffer provided */
145+
if (buf) {
146+
/* the buffer must be large enough */
147+
if (overlay->dtbo_size > max_count)
148+
return -ENOSPC;
149+
150+
memcpy(buf, overlay->dtbo, overlay->dtbo_size);
151+
}
152+
153+
return overlay->dtbo_size;
154+
}
155+
156+
static ssize_t cfs_overlay_item_dtbo_write(struct config_item *item,
157+
const void *buf, size_t count)
158+
{
159+
struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
160+
int err;
161+
162+
/* if it's set do not allow changes */
163+
if (overlay->path[0] != '\0' || overlay->dtbo_size > 0)
164+
return -EPERM;
165+
166+
/* copy the contents */
167+
overlay->dtbo = kmemdup(buf, count, GFP_KERNEL);
168+
if (!overlay->dtbo)
169+
return -ENOMEM;
170+
171+
overlay->dtbo_size = count;
172+
173+
err = create_overlay(overlay, overlay->dtbo);
174+
if (err < 0)
175+
goto out_err;
176+
177+
return count;
178+
179+
out_err:
180+
kfree(overlay->dtbo);
181+
overlay->dtbo = NULL;
182+
overlay->dtbo_size = 0;
183+
184+
return err;
185+
}
186+
187+
CONFIGFS_BIN_ATTR(cfs_overlay_item_, dtbo, NULL, SZ_1M);
188+
189+
static struct configfs_bin_attribute *cfs_overlay_bin_attrs[] = {
190+
&cfs_overlay_item_attr_dtbo,
191+
NULL,
192+
};
193+
194+
static void cfs_overlay_release(struct config_item *item)
195+
{
196+
struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
197+
198+
if (overlay->ov_id >= 0)
199+
of_overlay_remove(&overlay->ov_id);
200+
if (overlay->fw)
201+
release_firmware(overlay->fw);
202+
/* kfree with NULL is safe */
203+
kfree(overlay->dtbo);
204+
kfree(overlay->mem);
205+
kfree(overlay);
206+
}
207+
208+
static struct configfs_item_operations cfs_overlay_item_ops = {
209+
.release = cfs_overlay_release,
210+
};
211+
212+
static struct config_item_type cfs_overlay_type = {
213+
.ct_item_ops = &cfs_overlay_item_ops,
214+
.ct_attrs = cfs_overlay_attrs,
215+
.ct_bin_attrs = cfs_overlay_bin_attrs,
216+
.ct_owner = THIS_MODULE,
217+
};
218+
219+
static struct config_item
220+
*cfs_overlay_group_make_item(struct config_group *group,
221+
const char *name)
222+
{
223+
struct cfs_overlay_item *overlay;
224+
225+
overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
226+
if (!overlay)
227+
return ERR_PTR(-ENOMEM);
228+
overlay->ov_id = -1;
229+
config_item_init_type_name(&overlay->item, name, &cfs_overlay_type);
230+
231+
return &overlay->item;
232+
}
233+
234+
static void cfs_overlay_group_drop_item(struct config_group *group,
235+
struct config_item *item)
236+
{
237+
struct cfs_overlay_item *overlay = to_cfs_overlay_item(item);
238+
239+
config_item_put(&overlay->item);
240+
}
241+
242+
static struct configfs_group_operations overlays_ops = {
243+
.make_item = cfs_overlay_group_make_item,
244+
.drop_item = cfs_overlay_group_drop_item,
245+
};
246+
247+
static struct config_item_type overlays_type = {
248+
.ct_group_ops = &overlays_ops,
249+
.ct_owner = THIS_MODULE,
250+
};
251+
252+
static struct configfs_group_operations of_cfs_ops = {
253+
/* empty - we don't allow anything to be created */
254+
};
255+
256+
static struct config_item_type of_cfs_type = {
257+
.ct_group_ops = &of_cfs_ops,
258+
.ct_owner = THIS_MODULE,
259+
};
260+
261+
struct config_group of_cfs_overlay_group;
262+
263+
static struct configfs_subsystem of_cfs_subsys = {
264+
.su_group = {
265+
.cg_item = {
266+
.ci_namebuf = "device-tree",
267+
.ci_type = &of_cfs_type,
268+
},
269+
},
270+
.su_mutex = __MUTEX_INITIALIZER(of_cfs_subsys.su_mutex),
271+
};
272+
273+
static int __init of_cfs_init(void)
274+
{
275+
int ret;
276+
277+
config_group_init(&of_cfs_subsys.su_group);
278+
config_group_init_type_name(&of_cfs_overlay_group, "overlays",
279+
&overlays_type);
280+
configfs_add_default_group(&of_cfs_overlay_group,
281+
&of_cfs_subsys.su_group);
282+
283+
ret = configfs_register_subsystem(&of_cfs_subsys);
284+
if (ret != 0) {
285+
pr_err("%s: failed to register subsys\n", __func__);
286+
goto out;
287+
}
288+
pr_debug("%s: OK\n", __func__);
289+
out:
290+
return ret;
291+
}
292+
late_initcall(of_cfs_init);

0 commit comments

Comments
 (0)