diff options
author | Grant Likely <grant.likely@linaro.org> | 2014-06-26 10:40:48 -0400 |
---|---|---|
committer | Grant Likely <grant.likely@linaro.org> | 2014-07-07 08:59:45 -0400 |
commit | 6afc0dc381573559251de9a8259404f49e6aed14 (patch) | |
tree | ae2e323b3556a08aa1756f604770d94c963e4303 /drivers/of | |
parent | c05aba2bd5f9dd3f363611ff844513de1341ac60 (diff) |
of: Move CONFIG_OF_DYNAMIC code into a separate file
Split the dynamic device tree code into a separate file to make it
really clear what features CONFIF_OF_DYNAMIC add to the kernel. Without
CONFIG_OF_DYNAMIC only properties can be changed, and notifiers do not
get sent. Enabling it turns on reference counting, notifiers and the
ability to add and remove nodes.
v2: Moved of_node_release() into dynamic.c
Signed-off-by: Grant Likely <grant.likely@linaro.org>
Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
Cc: Rob Herring <robh+dt@kernel.org>
Diffstat (limited to 'drivers/of')
-rw-r--r-- | drivers/of/Makefile | 1 | ||||
-rw-r--r-- | drivers/of/base.c | 230 | ||||
-rw-r--r-- | drivers/of/dynamic.c | 216 | ||||
-rw-r--r-- | drivers/of/of_private.h | 18 |
4 files changed, 236 insertions, 229 deletions
diff --git a/drivers/of/Makefile b/drivers/of/Makefile index 099b1fb00af4..08e6c0f79806 100644 --- a/drivers/of/Makefile +++ b/drivers/of/Makefile | |||
@@ -1,4 +1,5 @@ | |||
1 | obj-y = base.o device.o platform.o | 1 | obj-y = base.o device.o platform.o |
2 | obj-$(CONFIG_OF_DYNAMIC) += dynamic.o | ||
2 | obj-$(CONFIG_OF_FLATTREE) += fdt.o | 3 | obj-$(CONFIG_OF_FLATTREE) += fdt.o |
3 | obj-$(CONFIG_OF_EARLY_FLATTREE) += fdt_address.o | 4 | obj-$(CONFIG_OF_EARLY_FLATTREE) += fdt_address.o |
4 | obj-$(CONFIG_OF_PROMTREE) += pdt.o | 5 | obj-$(CONFIG_OF_PROMTREE) += pdt.o |
diff --git a/drivers/of/base.c b/drivers/of/base.c index e48a1b90a392..0d8955605738 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c | |||
@@ -88,79 +88,7 @@ int __weak of_node_to_nid(struct device_node *np) | |||
88 | } | 88 | } |
89 | #endif | 89 | #endif |
90 | 90 | ||
91 | #if defined(CONFIG_OF_DYNAMIC) | 91 | #ifndef CONFIG_OF_DYNAMIC |
92 | /** | ||
93 | * of_node_get - Increment refcount of a node | ||
94 | * @node: Node to inc refcount, NULL is supported to | ||
95 | * simplify writing of callers | ||
96 | * | ||
97 | * Returns node. | ||
98 | */ | ||
99 | struct device_node *of_node_get(struct device_node *node) | ||
100 | { | ||
101 | if (node) | ||
102 | kobject_get(&node->kobj); | ||
103 | return node; | ||
104 | } | ||
105 | EXPORT_SYMBOL(of_node_get); | ||
106 | |||
107 | static inline struct device_node *kobj_to_device_node(struct kobject *kobj) | ||
108 | { | ||
109 | return container_of(kobj, struct device_node, kobj); | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * of_node_release - release a dynamically allocated node | ||
114 | * @kref: kref element of the node to be released | ||
115 | * | ||
116 | * In of_node_put() this function is passed to kref_put() | ||
117 | * as the destructor. | ||
118 | */ | ||
119 | static void of_node_release(struct kobject *kobj) | ||
120 | { | ||
121 | struct device_node *node = kobj_to_device_node(kobj); | ||
122 | struct property *prop = node->properties; | ||
123 | |||
124 | /* We should never be releasing nodes that haven't been detached. */ | ||
125 | if (!of_node_check_flag(node, OF_DETACHED)) { | ||
126 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); | ||
127 | dump_stack(); | ||
128 | return; | ||
129 | } | ||
130 | |||
131 | if (!of_node_check_flag(node, OF_DYNAMIC)) | ||
132 | return; | ||
133 | |||
134 | while (prop) { | ||
135 | struct property *next = prop->next; | ||
136 | kfree(prop->name); | ||
137 | kfree(prop->value); | ||
138 | kfree(prop); | ||
139 | prop = next; | ||
140 | |||
141 | if (!prop) { | ||
142 | prop = node->deadprops; | ||
143 | node->deadprops = NULL; | ||
144 | } | ||
145 | } | ||
146 | kfree(node->full_name); | ||
147 | kfree(node->data); | ||
148 | kfree(node); | ||
149 | } | ||
150 | |||
151 | /** | ||
152 | * of_node_put - Decrement refcount of a node | ||
153 | * @node: Node to dec refcount, NULL is supported to | ||
154 | * simplify writing of callers | ||
155 | * | ||
156 | */ | ||
157 | void of_node_put(struct device_node *node) | ||
158 | { | ||
159 | if (node) | ||
160 | kobject_put(&node->kobj); | ||
161 | } | ||
162 | EXPORT_SYMBOL(of_node_put); | ||
163 | #else | ||
164 | static void of_node_release(struct kobject *kobj) | 92 | static void of_node_release(struct kobject *kobj) |
165 | { | 93 | { |
166 | /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */ | 94 | /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */ |
@@ -264,25 +192,6 @@ int of_node_add(struct device_node *np) | |||
264 | return rc; | 192 | return rc; |
265 | } | 193 | } |
266 | 194 | ||
267 | #if defined(CONFIG_OF_DYNAMIC) | ||
268 | static void of_node_remove(struct device_node *np) | ||
269 | { | ||
270 | struct property *pp; | ||
271 | |||
272 | BUG_ON(!of_node_is_initialized(np)); | ||
273 | |||
274 | /* only remove properties if on sysfs */ | ||
275 | if (of_node_is_attached(np)) { | ||
276 | for_each_property_of_node(np, pp) | ||
277 | sysfs_remove_bin_file(&np->kobj, &pp->attr); | ||
278 | kobject_del(&np->kobj); | ||
279 | } | ||
280 | |||
281 | /* finally remove the kobj_init ref */ | ||
282 | of_node_put(np); | ||
283 | } | ||
284 | #endif | ||
285 | |||
286 | static int __init of_init(void) | 195 | static int __init of_init(void) |
287 | { | 196 | { |
288 | struct device_node *np; | 197 | struct device_node *np; |
@@ -1747,28 +1656,6 @@ int of_count_phandle_with_args(const struct device_node *np, const char *list_na | |||
1747 | } | 1656 | } |
1748 | EXPORT_SYMBOL(of_count_phandle_with_args); | 1657 | EXPORT_SYMBOL(of_count_phandle_with_args); |
1749 | 1658 | ||
1750 | #if defined(CONFIG_OF_DYNAMIC) | ||
1751 | static int of_property_notify(int action, struct device_node *np, | ||
1752 | struct property *prop) | ||
1753 | { | ||
1754 | struct of_prop_reconfig pr; | ||
1755 | |||
1756 | /* only call notifiers if the node is attached */ | ||
1757 | if (!of_node_is_attached(np)) | ||
1758 | return 0; | ||
1759 | |||
1760 | pr.dn = np; | ||
1761 | pr.prop = prop; | ||
1762 | return of_reconfig_notify(action, &pr); | ||
1763 | } | ||
1764 | #else | ||
1765 | static int of_property_notify(int action, struct device_node *np, | ||
1766 | struct property *prop) | ||
1767 | { | ||
1768 | return 0; | ||
1769 | } | ||
1770 | #endif | ||
1771 | |||
1772 | /** | 1659 | /** |
1773 | * __of_add_property - Add a property to a node without lock operations | 1660 | * __of_add_property - Add a property to a node without lock operations |
1774 | */ | 1661 | */ |
@@ -1915,121 +1802,6 @@ int of_update_property(struct device_node *np, struct property *newprop) | |||
1915 | return 0; | 1802 | return 0; |
1916 | } | 1803 | } |
1917 | 1804 | ||
1918 | #if defined(CONFIG_OF_DYNAMIC) | ||
1919 | /* | ||
1920 | * Support for dynamic device trees. | ||
1921 | * | ||
1922 | * On some platforms, the device tree can be manipulated at runtime. | ||
1923 | * The routines in this section support adding, removing and changing | ||
1924 | * device tree nodes. | ||
1925 | */ | ||
1926 | |||
1927 | static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain); | ||
1928 | |||
1929 | int of_reconfig_notifier_register(struct notifier_block *nb) | ||
1930 | { | ||
1931 | return blocking_notifier_chain_register(&of_reconfig_chain, nb); | ||
1932 | } | ||
1933 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_register); | ||
1934 | |||
1935 | int of_reconfig_notifier_unregister(struct notifier_block *nb) | ||
1936 | { | ||
1937 | return blocking_notifier_chain_unregister(&of_reconfig_chain, nb); | ||
1938 | } | ||
1939 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister); | ||
1940 | |||
1941 | int of_reconfig_notify(unsigned long action, void *p) | ||
1942 | { | ||
1943 | int rc; | ||
1944 | |||
1945 | rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); | ||
1946 | return notifier_to_errno(rc); | ||
1947 | } | ||
1948 | |||
1949 | /** | ||
1950 | * of_attach_node - Plug a device node into the tree and global list. | ||
1951 | */ | ||
1952 | int of_attach_node(struct device_node *np) | ||
1953 | { | ||
1954 | unsigned long flags; | ||
1955 | int rc; | ||
1956 | |||
1957 | rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np); | ||
1958 | if (rc) | ||
1959 | return rc; | ||
1960 | |||
1961 | raw_spin_lock_irqsave(&devtree_lock, flags); | ||
1962 | np->sibling = np->parent->child; | ||
1963 | np->allnext = np->parent->allnext; | ||
1964 | np->parent->allnext = np; | ||
1965 | np->parent->child = np; | ||
1966 | of_node_clear_flag(np, OF_DETACHED); | ||
1967 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | ||
1968 | |||
1969 | of_node_add(np); | ||
1970 | return 0; | ||
1971 | } | ||
1972 | |||
1973 | /** | ||
1974 | * of_detach_node - "Unplug" a node from the device tree. | ||
1975 | * | ||
1976 | * The caller must hold a reference to the node. The memory associated with | ||
1977 | * the node is not freed until its refcount goes to zero. | ||
1978 | */ | ||
1979 | int of_detach_node(struct device_node *np) | ||
1980 | { | ||
1981 | struct device_node *parent; | ||
1982 | unsigned long flags; | ||
1983 | int rc = 0; | ||
1984 | |||
1985 | rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np); | ||
1986 | if (rc) | ||
1987 | return rc; | ||
1988 | |||
1989 | raw_spin_lock_irqsave(&devtree_lock, flags); | ||
1990 | |||
1991 | if (of_node_check_flag(np, OF_DETACHED)) { | ||
1992 | /* someone already detached it */ | ||
1993 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | ||
1994 | return rc; | ||
1995 | } | ||
1996 | |||
1997 | parent = np->parent; | ||
1998 | if (!parent) { | ||
1999 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | ||
2000 | return rc; | ||
2001 | } | ||
2002 | |||
2003 | if (of_allnodes == np) | ||
2004 | of_allnodes = np->allnext; | ||
2005 | else { | ||
2006 | struct device_node *prev; | ||
2007 | for (prev = of_allnodes; | ||
2008 | prev->allnext != np; | ||
2009 | prev = prev->allnext) | ||
2010 | ; | ||
2011 | prev->allnext = np->allnext; | ||
2012 | } | ||
2013 | |||
2014 | if (parent->child == np) | ||
2015 | parent->child = np->sibling; | ||
2016 | else { | ||
2017 | struct device_node *prevsib; | ||
2018 | for (prevsib = np->parent->child; | ||
2019 | prevsib->sibling != np; | ||
2020 | prevsib = prevsib->sibling) | ||
2021 | ; | ||
2022 | prevsib->sibling = np->sibling; | ||
2023 | } | ||
2024 | |||
2025 | of_node_set_flag(np, OF_DETACHED); | ||
2026 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | ||
2027 | |||
2028 | of_node_remove(np); | ||
2029 | return rc; | ||
2030 | } | ||
2031 | #endif /* defined(CONFIG_OF_DYNAMIC) */ | ||
2032 | |||
2033 | static void of_alias_add(struct alias_prop *ap, struct device_node *np, | 1805 | static void of_alias_add(struct alias_prop *ap, struct device_node *np, |
2034 | int id, const char *stem, int stem_len) | 1806 | int id, const char *stem, int stem_len) |
2035 | { | 1807 | { |
diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c new file mode 100644 index 000000000000..125994330437 --- /dev/null +++ b/drivers/of/dynamic.c | |||
@@ -0,0 +1,216 @@ | |||
1 | /* | ||
2 | * Support for dynamic device trees. | ||
3 | * | ||
4 | * On some platforms, the device tree can be manipulated at runtime. | ||
5 | * The routines in this section support adding, removing and changing | ||
6 | * device tree nodes. | ||
7 | */ | ||
8 | |||
9 | #include <linux/of.h> | ||
10 | #include <linux/spinlock.h> | ||
11 | #include <linux/slab.h> | ||
12 | #include <linux/string.h> | ||
13 | #include <linux/proc_fs.h> | ||
14 | |||
15 | #include "of_private.h" | ||
16 | |||
17 | /** | ||
18 | * of_node_get() - Increment refcount of a node | ||
19 | * @node: Node to inc refcount, NULL is supported to simplify writing of | ||
20 | * callers | ||
21 | * | ||
22 | * Returns node. | ||
23 | */ | ||
24 | struct device_node *of_node_get(struct device_node *node) | ||
25 | { | ||
26 | if (node) | ||
27 | kobject_get(&node->kobj); | ||
28 | return node; | ||
29 | } | ||
30 | EXPORT_SYMBOL(of_node_get); | ||
31 | |||
32 | /** | ||
33 | * of_node_put() - Decrement refcount of a node | ||
34 | * @node: Node to dec refcount, NULL is supported to simplify writing of | ||
35 | * callers | ||
36 | */ | ||
37 | void of_node_put(struct device_node *node) | ||
38 | { | ||
39 | if (node) | ||
40 | kobject_put(&node->kobj); | ||
41 | } | ||
42 | EXPORT_SYMBOL(of_node_put); | ||
43 | |||
44 | static void of_node_remove(struct device_node *np) | ||
45 | { | ||
46 | struct property *pp; | ||
47 | |||
48 | BUG_ON(!of_node_is_initialized(np)); | ||
49 | |||
50 | /* only remove properties if on sysfs */ | ||
51 | if (of_node_is_attached(np)) { | ||
52 | for_each_property_of_node(np, pp) | ||
53 | sysfs_remove_bin_file(&np->kobj, &pp->attr); | ||
54 | kobject_del(&np->kobj); | ||
55 | } | ||
56 | |||
57 | /* finally remove the kobj_init ref */ | ||
58 | of_node_put(np); | ||
59 | } | ||
60 | |||
61 | static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain); | ||
62 | |||
63 | int of_reconfig_notifier_register(struct notifier_block *nb) | ||
64 | { | ||
65 | return blocking_notifier_chain_register(&of_reconfig_chain, nb); | ||
66 | } | ||
67 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_register); | ||
68 | |||
69 | int of_reconfig_notifier_unregister(struct notifier_block *nb) | ||
70 | { | ||
71 | return blocking_notifier_chain_unregister(&of_reconfig_chain, nb); | ||
72 | } | ||
73 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister); | ||
74 | |||
75 | int of_reconfig_notify(unsigned long action, void *p) | ||
76 | { | ||
77 | int rc; | ||
78 | |||
79 | rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); | ||
80 | return notifier_to_errno(rc); | ||
81 | } | ||
82 | |||
83 | int of_property_notify(int action, struct device_node *np, | ||
84 | struct property *prop) | ||
85 | { | ||
86 | struct of_prop_reconfig pr; | ||
87 | |||
88 | /* only call notifiers if the node is attached */ | ||
89 | if (!of_node_is_attached(np)) | ||
90 | return 0; | ||
91 | |||
92 | pr.dn = np; | ||
93 | pr.prop = prop; | ||
94 | return of_reconfig_notify(action, &pr); | ||
95 | } | ||
96 | |||
97 | /** | ||
98 | * of_attach_node() - Plug a device node into the tree and global list. | ||
99 | */ | ||
100 | int of_attach_node(struct device_node *np) | ||
101 | { | ||
102 | unsigned long flags; | ||
103 | int rc; | ||
104 | |||
105 | rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np); | ||
106 | if (rc) | ||
107 | return rc; | ||
108 | |||
109 | raw_spin_lock_irqsave(&devtree_lock, flags); | ||
110 | np->sibling = np->parent->child; | ||
111 | np->allnext = np->parent->allnext; | ||
112 | np->parent->allnext = np; | ||
113 | np->parent->child = np; | ||
114 | of_node_clear_flag(np, OF_DETACHED); | ||
115 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | ||
116 | |||
117 | of_node_add(np); | ||
118 | return 0; | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * of_detach_node() - "Unplug" a node from the device tree. | ||
123 | * | ||
124 | * The caller must hold a reference to the node. The memory associated with | ||
125 | * the node is not freed until its refcount goes to zero. | ||
126 | */ | ||
127 | int of_detach_node(struct device_node *np) | ||
128 | { | ||
129 | struct device_node *parent; | ||
130 | unsigned long flags; | ||
131 | int rc = 0; | ||
132 | |||
133 | rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np); | ||
134 | if (rc) | ||
135 | return rc; | ||
136 | |||
137 | raw_spin_lock_irqsave(&devtree_lock, flags); | ||
138 | |||
139 | if (of_node_check_flag(np, OF_DETACHED)) { | ||
140 | /* someone already detached it */ | ||
141 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | ||
142 | return rc; | ||
143 | } | ||
144 | |||
145 | parent = np->parent; | ||
146 | if (!parent) { | ||
147 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | ||
148 | return rc; | ||
149 | } | ||
150 | |||
151 | if (of_allnodes == np) | ||
152 | of_allnodes = np->allnext; | ||
153 | else { | ||
154 | struct device_node *prev; | ||
155 | for (prev = of_allnodes; | ||
156 | prev->allnext != np; | ||
157 | prev = prev->allnext) | ||
158 | ; | ||
159 | prev->allnext = np->allnext; | ||
160 | } | ||
161 | |||
162 | if (parent->child == np) | ||
163 | parent->child = np->sibling; | ||
164 | else { | ||
165 | struct device_node *prevsib; | ||
166 | for (prevsib = np->parent->child; | ||
167 | prevsib->sibling != np; | ||
168 | prevsib = prevsib->sibling) | ||
169 | ; | ||
170 | prevsib->sibling = np->sibling; | ||
171 | } | ||
172 | |||
173 | of_node_set_flag(np, OF_DETACHED); | ||
174 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | ||
175 | |||
176 | of_node_remove(np); | ||
177 | return rc; | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * of_node_release() - release a dynamically allocated node | ||
182 | * @kref: kref element of the node to be released | ||
183 | * | ||
184 | * In of_node_put() this function is passed to kref_put() as the destructor. | ||
185 | */ | ||
186 | void of_node_release(struct kobject *kobj) | ||
187 | { | ||
188 | struct device_node *node = kobj_to_device_node(kobj); | ||
189 | struct property *prop = node->properties; | ||
190 | |||
191 | /* We should never be releasing nodes that haven't been detached. */ | ||
192 | if (!of_node_check_flag(node, OF_DETACHED)) { | ||
193 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); | ||
194 | dump_stack(); | ||
195 | return; | ||
196 | } | ||
197 | |||
198 | if (!of_node_check_flag(node, OF_DYNAMIC)) | ||
199 | return; | ||
200 | |||
201 | while (prop) { | ||
202 | struct property *next = prop->next; | ||
203 | kfree(prop->name); | ||
204 | kfree(prop->value); | ||
205 | kfree(prop); | ||
206 | prop = next; | ||
207 | |||
208 | if (!prop) { | ||
209 | prop = node->deadprops; | ||
210 | node->deadprops = NULL; | ||
211 | } | ||
212 | } | ||
213 | kfree(node->full_name); | ||
214 | kfree(node->data); | ||
215 | kfree(node); | ||
216 | } | ||
diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h index fcc70e74dfe0..c270f2037779 100644 --- a/drivers/of/of_private.h +++ b/drivers/of/of_private.h | |||
@@ -33,4 +33,22 @@ struct alias_prop { | |||
33 | 33 | ||
34 | extern struct mutex of_mutex; | 34 | extern struct mutex of_mutex; |
35 | extern struct list_head aliases_lookup; | 35 | extern struct list_head aliases_lookup; |
36 | |||
37 | static inline struct device_node *kobj_to_device_node(struct kobject *kobj) | ||
38 | { | ||
39 | return container_of(kobj, struct device_node, kobj); | ||
40 | } | ||
41 | |||
42 | #if defined(CONFIG_OF_DYNAMIC) | ||
43 | extern int of_property_notify(int action, struct device_node *np, | ||
44 | struct property *prop); | ||
45 | extern void of_node_release(struct kobject *kobj); | ||
46 | #else /* CONFIG_OF_DYNAMIC */ | ||
47 | static inline int of_property_notify(int action, struct device_node *np, | ||
48 | struct property *prop) | ||
49 | { | ||
50 | return 0; | ||
51 | } | ||
52 | #endif /* CONFIG_OF_DYNAMIC */ | ||
53 | |||
36 | #endif /* _LINUX_OF_PRIVATE_H */ | 54 | #endif /* _LINUX_OF_PRIVATE_H */ |