]>
Commit | Line | Data |
---|---|---|
53a42093 GL |
1 | /* |
2 | * Self tests for device tree subsystem | |
3 | */ | |
4 | ||
cabb7d5b | 5 | #define pr_fmt(fmt) "### dt-test ### " fmt |
53a42093 GL |
6 | |
7 | #include <linux/clk.h> | |
8 | #include <linux/err.h> | |
9 | #include <linux/errno.h> | |
841ec213 | 10 | #include <linux/hashtable.h> |
53a42093 GL |
11 | #include <linux/module.h> |
12 | #include <linux/of.h> | |
ae9304c9 | 13 | #include <linux/of_fdt.h> |
a9f10ca7 | 14 | #include <linux/of_irq.h> |
82c0f589 | 15 | #include <linux/of_platform.h> |
53a42093 GL |
16 | #include <linux/list.h> |
17 | #include <linux/mutex.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/device.h> | |
177d271c PA |
20 | #include <linux/platform_device.h> |
21 | #include <linux/of_platform.h> | |
53a42093 | 22 | |
69843396 PA |
23 | #include "of_private.h" |
24 | ||
a9f10ca7 GL |
25 | static struct selftest_results { |
26 | int passed; | |
27 | int failed; | |
28 | } selftest_results; | |
29 | ||
2eb46da2 | 30 | #define NO_OF_NODES 3 |
ae9304c9 GM |
31 | static struct device_node *nodes[NO_OF_NODES]; |
32 | static int last_node_index; | |
b951f9dc | 33 | static bool selftest_live_tree; |
ae9304c9 | 34 | |
851da976 GL |
35 | #define selftest(result, fmt, ...) ({ \ |
36 | bool failed = !(result); \ | |
37 | if (failed) { \ | |
a9f10ca7 GL |
38 | selftest_results.failed++; \ |
39 | pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \ | |
cabb7d5b | 40 | } else { \ |
a9f10ca7 GL |
41 | selftest_results.passed++; \ |
42 | pr_debug("pass %s():%i\n", __func__, __LINE__); \ | |
cabb7d5b | 43 | } \ |
851da976 GL |
44 | failed; \ |
45 | }) | |
53a42093 | 46 | |
ae91ff72 GL |
47 | static void __init of_selftest_find_node_by_name(void) |
48 | { | |
49 | struct device_node *np; | |
75c28c09 | 50 | const char *options; |
ae91ff72 GL |
51 | |
52 | np = of_find_node_by_path("/testcase-data"); | |
53 | selftest(np && !strcmp("/testcase-data", np->full_name), | |
54 | "find /testcase-data failed\n"); | |
55 | of_node_put(np); | |
56 | ||
57 | /* Test if trailing '/' works */ | |
58 | np = of_find_node_by_path("/testcase-data/"); | |
59 | selftest(!np, "trailing '/' on /testcase-data/ should fail\n"); | |
60 | ||
61 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); | |
62 | selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name), | |
63 | "find /testcase-data/phandle-tests/consumer-a failed\n"); | |
64 | of_node_put(np); | |
65 | ||
66 | np = of_find_node_by_path("testcase-alias"); | |
67 | selftest(np && !strcmp("/testcase-data", np->full_name), | |
68 | "find testcase-alias failed\n"); | |
69 | of_node_put(np); | |
70 | ||
71 | /* Test if trailing '/' works on aliases */ | |
72 | np = of_find_node_by_path("testcase-alias/"); | |
73 | selftest(!np, "trailing '/' on testcase-alias/ should fail\n"); | |
74 | ||
75 | np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a"); | |
76 | selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name), | |
77 | "find testcase-alias/phandle-tests/consumer-a failed\n"); | |
78 | of_node_put(np); | |
79 | ||
80 | np = of_find_node_by_path("/testcase-data/missing-path"); | |
81 | selftest(!np, "non-existent path returned node %s\n", np->full_name); | |
82 | of_node_put(np); | |
83 | ||
84 | np = of_find_node_by_path("missing-alias"); | |
85 | selftest(!np, "non-existent alias returned node %s\n", np->full_name); | |
86 | of_node_put(np); | |
87 | ||
88 | np = of_find_node_by_path("testcase-alias/missing-path"); | |
89 | selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name); | |
90 | of_node_put(np); | |
75c28c09 LL |
91 | |
92 | np = of_find_node_opts_by_path("/testcase-data:testoption", &options); | |
93 | selftest(np && !strcmp("testoption", options), | |
94 | "option path test failed\n"); | |
95 | of_node_put(np); | |
96 | ||
97 | np = of_find_node_opts_by_path("/testcase-data:testoption", NULL); | |
98 | selftest(np, "NULL option path test failed\n"); | |
99 | of_node_put(np); | |
100 | ||
101 | np = of_find_node_opts_by_path("testcase-alias:testaliasoption", | |
102 | &options); | |
103 | selftest(np && !strcmp("testaliasoption", options), | |
104 | "option alias path test failed\n"); | |
105 | of_node_put(np); | |
106 | ||
107 | np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL); | |
108 | selftest(np, "NULL option alias path test failed\n"); | |
109 | of_node_put(np); | |
110 | ||
111 | options = "testoption"; | |
112 | np = of_find_node_opts_by_path("testcase-alias", &options); | |
113 | selftest(np && !options, "option clearing test failed\n"); | |
114 | of_node_put(np); | |
115 | ||
116 | options = "testoption"; | |
117 | np = of_find_node_opts_by_path("/", &options); | |
118 | selftest(np && !options, "option clearing root node test failed\n"); | |
119 | of_node_put(np); | |
ae91ff72 GL |
120 | } |
121 | ||
7e66c5c7 GL |
122 | static void __init of_selftest_dynamic(void) |
123 | { | |
124 | struct device_node *np; | |
125 | struct property *prop; | |
126 | ||
127 | np = of_find_node_by_path("/testcase-data"); | |
128 | if (!np) { | |
129 | pr_err("missing testcase data\n"); | |
130 | return; | |
131 | } | |
132 | ||
133 | /* Array of 4 properties for the purpose of testing */ | |
134 | prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL); | |
135 | if (!prop) { | |
136 | selftest(0, "kzalloc() failed\n"); | |
137 | return; | |
138 | } | |
139 | ||
140 | /* Add a new property - should pass*/ | |
141 | prop->name = "new-property"; | |
142 | prop->value = "new-property-data"; | |
143 | prop->length = strlen(prop->value); | |
144 | selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n"); | |
145 | ||
146 | /* Try to add an existing property - should fail */ | |
147 | prop++; | |
148 | prop->name = "new-property"; | |
149 | prop->value = "new-property-data-should-fail"; | |
150 | prop->length = strlen(prop->value); | |
151 | selftest(of_add_property(np, prop) != 0, | |
152 | "Adding an existing property should have failed\n"); | |
153 | ||
154 | /* Try to modify an existing property - should pass */ | |
155 | prop->value = "modify-property-data-should-pass"; | |
156 | prop->length = strlen(prop->value); | |
157 | selftest(of_update_property(np, prop) == 0, | |
158 | "Updating an existing property should have passed\n"); | |
159 | ||
160 | /* Try to modify non-existent property - should pass*/ | |
161 | prop++; | |
162 | prop->name = "modify-property"; | |
163 | prop->value = "modify-missing-property-data-should-pass"; | |
164 | prop->length = strlen(prop->value); | |
165 | selftest(of_update_property(np, prop) == 0, | |
166 | "Updating a missing property should have passed\n"); | |
167 | ||
168 | /* Remove property - should pass */ | |
169 | selftest(of_remove_property(np, prop) == 0, | |
170 | "Removing a property should have passed\n"); | |
171 | ||
172 | /* Adding very large property - should pass */ | |
173 | prop++; | |
174 | prop->name = "large-property-PAGE_SIZEx8"; | |
175 | prop->length = PAGE_SIZE * 8; | |
176 | prop->value = kzalloc(prop->length, GFP_KERNEL); | |
177 | selftest(prop->value != NULL, "Unable to allocate large buffer\n"); | |
178 | if (prop->value) | |
179 | selftest(of_add_property(np, prop) == 0, | |
180 | "Adding a large property should have passed\n"); | |
181 | } | |
182 | ||
f2051d6a GL |
183 | static int __init of_selftest_check_node_linkage(struct device_node *np) |
184 | { | |
5063e25a | 185 | struct device_node *child; |
f2051d6a GL |
186 | int count = 0, rc; |
187 | ||
188 | for_each_child_of_node(np, child) { | |
189 | if (child->parent != np) { | |
190 | pr_err("Child node %s links to wrong parent %s\n", | |
191 | child->name, np->name); | |
192 | return -EINVAL; | |
193 | } | |
194 | ||
f2051d6a GL |
195 | rc = of_selftest_check_node_linkage(child); |
196 | if (rc < 0) | |
197 | return rc; | |
198 | count += rc; | |
199 | } | |
200 | ||
201 | return count + 1; | |
202 | } | |
203 | ||
204 | static void __init of_selftest_check_tree_linkage(void) | |
205 | { | |
206 | struct device_node *np; | |
207 | int allnode_count = 0, child_count; | |
208 | ||
5063e25a | 209 | if (!of_root) |
f2051d6a GL |
210 | return; |
211 | ||
212 | for_each_of_allnodes(np) | |
213 | allnode_count++; | |
5063e25a | 214 | child_count = of_selftest_check_node_linkage(of_root); |
f2051d6a GL |
215 | |
216 | selftest(child_count > 0, "Device node data structure is corrupted\n"); | |
217 | selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match" | |
218 | "sibling lists size (%i)\n", allnode_count, child_count); | |
219 | pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count); | |
220 | } | |
221 | ||
841ec213 GL |
222 | struct node_hash { |
223 | struct hlist_node node; | |
224 | struct device_node *np; | |
225 | }; | |
226 | ||
2118f4b8 | 227 | static DEFINE_HASHTABLE(phandle_ht, 8); |
841ec213 GL |
228 | static void __init of_selftest_check_phandles(void) |
229 | { | |
230 | struct device_node *np; | |
231 | struct node_hash *nh; | |
232 | struct hlist_node *tmp; | |
233 | int i, dup_count = 0, phandle_count = 0; | |
841ec213 | 234 | |
841ec213 GL |
235 | for_each_of_allnodes(np) { |
236 | if (!np->phandle) | |
237 | continue; | |
238 | ||
2118f4b8 | 239 | hash_for_each_possible(phandle_ht, nh, node, np->phandle) { |
841ec213 GL |
240 | if (nh->np->phandle == np->phandle) { |
241 | pr_info("Duplicate phandle! %i used by %s and %s\n", | |
242 | np->phandle, nh->np->full_name, np->full_name); | |
243 | dup_count++; | |
244 | break; | |
245 | } | |
246 | } | |
247 | ||
248 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); | |
249 | if (WARN_ON(!nh)) | |
250 | return; | |
251 | ||
252 | nh->np = np; | |
2118f4b8 | 253 | hash_add(phandle_ht, &nh->node, np->phandle); |
841ec213 GL |
254 | phandle_count++; |
255 | } | |
256 | selftest(dup_count == 0, "Found %i duplicates in %i phandles\n", | |
257 | dup_count, phandle_count); | |
258 | ||
259 | /* Clean up */ | |
2118f4b8 | 260 | hash_for_each_safe(phandle_ht, i, tmp, nh, node) { |
841ec213 GL |
261 | hash_del(&nh->node); |
262 | kfree(nh); | |
263 | } | |
264 | } | |
265 | ||
53a42093 GL |
266 | static void __init of_selftest_parse_phandle_with_args(void) |
267 | { | |
268 | struct device_node *np; | |
269 | struct of_phandle_args args; | |
cabb7d5b | 270 | int i, rc; |
53a42093 | 271 | |
53a42093 GL |
272 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
273 | if (!np) { | |
274 | pr_err("missing testcase data\n"); | |
275 | return; | |
276 | } | |
277 | ||
bd69f73f GL |
278 | rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells"); |
279 | selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc); | |
280 | ||
f7f951c2 | 281 | for (i = 0; i < 8; i++) { |
53a42093 GL |
282 | bool passed = true; |
283 | rc = of_parse_phandle_with_args(np, "phandle-list", | |
284 | "#phandle-cells", i, &args); | |
285 | ||
286 | /* Test the values from tests-phandle.dtsi */ | |
287 | switch (i) { | |
288 | case 0: | |
289 | passed &= !rc; | |
290 | passed &= (args.args_count == 1); | |
291 | passed &= (args.args[0] == (i + 1)); | |
292 | break; | |
293 | case 1: | |
294 | passed &= !rc; | |
295 | passed &= (args.args_count == 2); | |
296 | passed &= (args.args[0] == (i + 1)); | |
297 | passed &= (args.args[1] == 0); | |
298 | break; | |
299 | case 2: | |
300 | passed &= (rc == -ENOENT); | |
301 | break; | |
302 | case 3: | |
303 | passed &= !rc; | |
304 | passed &= (args.args_count == 3); | |
305 | passed &= (args.args[0] == (i + 1)); | |
306 | passed &= (args.args[1] == 4); | |
307 | passed &= (args.args[2] == 3); | |
308 | break; | |
309 | case 4: | |
310 | passed &= !rc; | |
311 | passed &= (args.args_count == 2); | |
312 | passed &= (args.args[0] == (i + 1)); | |
313 | passed &= (args.args[1] == 100); | |
314 | break; | |
315 | case 5: | |
316 | passed &= !rc; | |
317 | passed &= (args.args_count == 0); | |
318 | break; | |
319 | case 6: | |
320 | passed &= !rc; | |
321 | passed &= (args.args_count == 1); | |
322 | passed &= (args.args[0] == (i + 1)); | |
323 | break; | |
324 | case 7: | |
cabb7d5b | 325 | passed &= (rc == -ENOENT); |
53a42093 GL |
326 | break; |
327 | default: | |
328 | passed = false; | |
329 | } | |
330 | ||
cabb7d5b GL |
331 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
332 | i, args.np->full_name, rc); | |
53a42093 GL |
333 | } |
334 | ||
335 | /* Check for missing list property */ | |
336 | rc = of_parse_phandle_with_args(np, "phandle-list-missing", | |
337 | "#phandle-cells", 0, &args); | |
cabb7d5b | 338 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
bd69f73f GL |
339 | rc = of_count_phandle_with_args(np, "phandle-list-missing", |
340 | "#phandle-cells"); | |
341 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); | |
53a42093 GL |
342 | |
343 | /* Check for missing cells property */ | |
344 | rc = of_parse_phandle_with_args(np, "phandle-list", | |
345 | "#phandle-cells-missing", 0, &args); | |
cabb7d5b | 346 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
bd69f73f GL |
347 | rc = of_count_phandle_with_args(np, "phandle-list", |
348 | "#phandle-cells-missing"); | |
349 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); | |
53a42093 GL |
350 | |
351 | /* Check for bad phandle in list */ | |
352 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle", | |
353 | "#phandle-cells", 0, &args); | |
cabb7d5b | 354 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
bd69f73f GL |
355 | rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle", |
356 | "#phandle-cells"); | |
357 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); | |
53a42093 GL |
358 | |
359 | /* Check for incorrectly formed argument list */ | |
360 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-args", | |
361 | "#phandle-cells", 1, &args); | |
cabb7d5b | 362 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
bd69f73f GL |
363 | rc = of_count_phandle_with_args(np, "phandle-list-bad-args", |
364 | "#phandle-cells"); | |
365 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); | |
53a42093 GL |
366 | } |
367 | ||
a87fa1d8 | 368 | static void __init of_selftest_property_string(void) |
7aff0fe3 | 369 | { |
a87fa1d8 | 370 | const char *strings[4]; |
7aff0fe3 GL |
371 | struct device_node *np; |
372 | int rc; | |
373 | ||
7aff0fe3 GL |
374 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
375 | if (!np) { | |
376 | pr_err("No testcase data in device tree\n"); | |
377 | return; | |
378 | } | |
379 | ||
380 | rc = of_property_match_string(np, "phandle-list-names", "first"); | |
381 | selftest(rc == 0, "first expected:0 got:%i\n", rc); | |
382 | rc = of_property_match_string(np, "phandle-list-names", "second"); | |
383 | selftest(rc == 1, "second expected:0 got:%i\n", rc); | |
384 | rc = of_property_match_string(np, "phandle-list-names", "third"); | |
385 | selftest(rc == 2, "third expected:0 got:%i\n", rc); | |
386 | rc = of_property_match_string(np, "phandle-list-names", "fourth"); | |
a87fa1d8 | 387 | selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc); |
7aff0fe3 | 388 | rc = of_property_match_string(np, "missing-property", "blah"); |
a87fa1d8 | 389 | selftest(rc == -EINVAL, "missing property; rc=%i\n", rc); |
7aff0fe3 | 390 | rc = of_property_match_string(np, "empty-property", "blah"); |
a87fa1d8 | 391 | selftest(rc == -ENODATA, "empty property; rc=%i\n", rc); |
7aff0fe3 | 392 | rc = of_property_match_string(np, "unterminated-string", "blah"); |
a87fa1d8 GL |
393 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
394 | ||
395 | /* of_property_count_strings() tests */ | |
396 | rc = of_property_count_strings(np, "string-property"); | |
397 | selftest(rc == 1, "Incorrect string count; rc=%i\n", rc); | |
398 | rc = of_property_count_strings(np, "phandle-list-names"); | |
399 | selftest(rc == 3, "Incorrect string count; rc=%i\n", rc); | |
400 | rc = of_property_count_strings(np, "unterminated-string"); | |
401 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); | |
402 | rc = of_property_count_strings(np, "unterminated-string-list"); | |
403 | selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); | |
404 | ||
405 | /* of_property_read_string_index() tests */ | |
406 | rc = of_property_read_string_index(np, "string-property", 0, strings); | |
407 | selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc); | |
408 | strings[0] = NULL; | |
409 | rc = of_property_read_string_index(np, "string-property", 1, strings); | |
410 | selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); | |
411 | rc = of_property_read_string_index(np, "phandle-list-names", 0, strings); | |
412 | selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); | |
413 | rc = of_property_read_string_index(np, "phandle-list-names", 1, strings); | |
414 | selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc); | |
415 | rc = of_property_read_string_index(np, "phandle-list-names", 2, strings); | |
416 | selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc); | |
417 | strings[0] = NULL; | |
418 | rc = of_property_read_string_index(np, "phandle-list-names", 3, strings); | |
419 | selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); | |
420 | strings[0] = NULL; | |
421 | rc = of_property_read_string_index(np, "unterminated-string", 0, strings); | |
422 | selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); | |
423 | rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings); | |
424 | selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); | |
425 | strings[0] = NULL; | |
426 | rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */ | |
427 | selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); | |
428 | strings[1] = NULL; | |
429 | ||
430 | /* of_property_read_string_array() tests */ | |
431 | rc = of_property_read_string_array(np, "string-property", strings, 4); | |
432 | selftest(rc == 1, "Incorrect string count; rc=%i\n", rc); | |
433 | rc = of_property_read_string_array(np, "phandle-list-names", strings, 4); | |
434 | selftest(rc == 3, "Incorrect string count; rc=%i\n", rc); | |
435 | rc = of_property_read_string_array(np, "unterminated-string", strings, 4); | |
436 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); | |
437 | /* -- An incorrectly formed string should cause a failure */ | |
438 | rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4); | |
439 | selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); | |
440 | /* -- parsing the correctly formed strings should still work: */ | |
441 | strings[2] = NULL; | |
442 | rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2); | |
443 | selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc); | |
444 | strings[1] = NULL; | |
445 | rc = of_property_read_string_array(np, "phandle-list-names", strings, 1); | |
446 | selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]); | |
7aff0fe3 GL |
447 | } |
448 | ||
69843396 PA |
449 | #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \ |
450 | (p1)->value && (p2)->value && \ | |
451 | !memcmp((p1)->value, (p2)->value, (p1)->length) && \ | |
452 | !strcmp((p1)->name, (p2)->name)) | |
453 | static void __init of_selftest_property_copy(void) | |
454 | { | |
455 | #ifdef CONFIG_OF_DYNAMIC | |
456 | struct property p1 = { .name = "p1", .length = 0, .value = "" }; | |
457 | struct property p2 = { .name = "p2", .length = 5, .value = "abcd" }; | |
458 | struct property *new; | |
459 | ||
460 | new = __of_prop_dup(&p1, GFP_KERNEL); | |
461 | selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n"); | |
462 | kfree(new->value); | |
463 | kfree(new->name); | |
464 | kfree(new); | |
465 | ||
466 | new = __of_prop_dup(&p2, GFP_KERNEL); | |
467 | selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n"); | |
468 | kfree(new->value); | |
469 | kfree(new->name); | |
470 | kfree(new); | |
471 | #endif | |
472 | } | |
473 | ||
201c910b PA |
474 | static void __init of_selftest_changeset(void) |
475 | { | |
476 | #ifdef CONFIG_OF_DYNAMIC | |
477 | struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" }; | |
478 | struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" }; | |
479 | struct property *ppremove; | |
e5179581 | 480 | struct device_node *n1, *n2, *n21, *nremove, *parent, *np; |
201c910b PA |
481 | struct of_changeset chgset; |
482 | ||
483 | of_changeset_init(&chgset); | |
e5179581 | 484 | n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1"); |
201c910b | 485 | selftest(n1, "testcase setup failure\n"); |
e5179581 | 486 | n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2"); |
201c910b | 487 | selftest(n2, "testcase setup failure\n"); |
e5179581 | 488 | n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21"); |
201c910b PA |
489 | selftest(n21, "testcase setup failure %p\n", n21); |
490 | nremove = of_find_node_by_path("/testcase-data/changeset/node-remove"); | |
491 | selftest(nremove, "testcase setup failure\n"); | |
492 | ppadd = __of_prop_dup(&padd, GFP_KERNEL); | |
493 | selftest(ppadd, "testcase setup failure\n"); | |
494 | ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL); | |
495 | selftest(ppupdate, "testcase setup failure\n"); | |
496 | parent = nremove->parent; | |
497 | n1->parent = parent; | |
498 | n2->parent = parent; | |
499 | n21->parent = n2; | |
500 | n2->child = n21; | |
501 | ppremove = of_find_property(parent, "prop-remove", NULL); | |
502 | selftest(ppremove, "failed to find removal prop"); | |
503 | ||
504 | of_changeset_init(&chgset); | |
505 | selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n"); | |
506 | selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n"); | |
507 | selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n"); | |
508 | selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n"); | |
509 | selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n"); | |
510 | selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n"); | |
511 | selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n"); | |
512 | mutex_lock(&of_mutex); | |
513 | selftest(!of_changeset_apply(&chgset), "apply failed\n"); | |
514 | mutex_unlock(&of_mutex); | |
515 | ||
e5179581 GL |
516 | /* Make sure node names are constructed correctly */ |
517 | selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")), | |
518 | "'%s' not added\n", n21->full_name); | |
519 | if (np) | |
520 | of_node_put(np); | |
521 | ||
201c910b PA |
522 | mutex_lock(&of_mutex); |
523 | selftest(!of_changeset_revert(&chgset), "revert failed\n"); | |
524 | mutex_unlock(&of_mutex); | |
525 | ||
526 | of_changeset_destroy(&chgset); | |
527 | #endif | |
528 | } | |
529 | ||
a9f10ca7 GL |
530 | static void __init of_selftest_parse_interrupts(void) |
531 | { | |
532 | struct device_node *np; | |
533 | struct of_phandle_args args; | |
534 | int i, rc; | |
535 | ||
536 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts0"); | |
537 | if (!np) { | |
538 | pr_err("missing testcase data\n"); | |
539 | return; | |
540 | } | |
541 | ||
542 | for (i = 0; i < 4; i++) { | |
543 | bool passed = true; | |
544 | args.args_count = 0; | |
545 | rc = of_irq_parse_one(np, i, &args); | |
546 | ||
547 | passed &= !rc; | |
548 | passed &= (args.args_count == 1); | |
549 | passed &= (args.args[0] == (i + 1)); | |
550 | ||
551 | selftest(passed, "index %i - data error on node %s rc=%i\n", | |
552 | i, args.np->full_name, rc); | |
553 | } | |
554 | of_node_put(np); | |
555 | ||
556 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts1"); | |
557 | if (!np) { | |
558 | pr_err("missing testcase data\n"); | |
559 | return; | |
560 | } | |
561 | ||
562 | for (i = 0; i < 4; i++) { | |
563 | bool passed = true; | |
564 | args.args_count = 0; | |
565 | rc = of_irq_parse_one(np, i, &args); | |
566 | ||
567 | /* Test the values from tests-phandle.dtsi */ | |
568 | switch (i) { | |
569 | case 0: | |
570 | passed &= !rc; | |
571 | passed &= (args.args_count == 1); | |
572 | passed &= (args.args[0] == 9); | |
573 | break; | |
574 | case 1: | |
575 | passed &= !rc; | |
576 | passed &= (args.args_count == 3); | |
577 | passed &= (args.args[0] == 10); | |
578 | passed &= (args.args[1] == 11); | |
579 | passed &= (args.args[2] == 12); | |
580 | break; | |
581 | case 2: | |
582 | passed &= !rc; | |
583 | passed &= (args.args_count == 2); | |
584 | passed &= (args.args[0] == 13); | |
585 | passed &= (args.args[1] == 14); | |
586 | break; | |
587 | case 3: | |
588 | passed &= !rc; | |
589 | passed &= (args.args_count == 2); | |
590 | passed &= (args.args[0] == 15); | |
591 | passed &= (args.args[1] == 16); | |
592 | break; | |
593 | default: | |
594 | passed = false; | |
595 | } | |
596 | selftest(passed, "index %i - data error on node %s rc=%i\n", | |
597 | i, args.np->full_name, rc); | |
598 | } | |
599 | of_node_put(np); | |
600 | } | |
601 | ||
79d97015 GL |
602 | static void __init of_selftest_parse_interrupts_extended(void) |
603 | { | |
604 | struct device_node *np; | |
605 | struct of_phandle_args args; | |
606 | int i, rc; | |
607 | ||
608 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0"); | |
609 | if (!np) { | |
610 | pr_err("missing testcase data\n"); | |
611 | return; | |
612 | } | |
613 | ||
614 | for (i = 0; i < 7; i++) { | |
615 | bool passed = true; | |
616 | rc = of_irq_parse_one(np, i, &args); | |
617 | ||
618 | /* Test the values from tests-phandle.dtsi */ | |
619 | switch (i) { | |
620 | case 0: | |
621 | passed &= !rc; | |
622 | passed &= (args.args_count == 1); | |
623 | passed &= (args.args[0] == 1); | |
624 | break; | |
625 | case 1: | |
626 | passed &= !rc; | |
627 | passed &= (args.args_count == 3); | |
628 | passed &= (args.args[0] == 2); | |
629 | passed &= (args.args[1] == 3); | |
630 | passed &= (args.args[2] == 4); | |
631 | break; | |
632 | case 2: | |
633 | passed &= !rc; | |
634 | passed &= (args.args_count == 2); | |
635 | passed &= (args.args[0] == 5); | |
636 | passed &= (args.args[1] == 6); | |
637 | break; | |
638 | case 3: | |
639 | passed &= !rc; | |
640 | passed &= (args.args_count == 1); | |
641 | passed &= (args.args[0] == 9); | |
642 | break; | |
643 | case 4: | |
644 | passed &= !rc; | |
645 | passed &= (args.args_count == 3); | |
646 | passed &= (args.args[0] == 10); | |
647 | passed &= (args.args[1] == 11); | |
648 | passed &= (args.args[2] == 12); | |
649 | break; | |
650 | case 5: | |
651 | passed &= !rc; | |
652 | passed &= (args.args_count == 2); | |
653 | passed &= (args.args[0] == 13); | |
654 | passed &= (args.args[1] == 14); | |
655 | break; | |
656 | case 6: | |
657 | passed &= !rc; | |
658 | passed &= (args.args_count == 1); | |
659 | passed &= (args.args[0] == 15); | |
660 | break; | |
661 | default: | |
662 | passed = false; | |
663 | } | |
664 | ||
665 | selftest(passed, "index %i - data error on node %s rc=%i\n", | |
666 | i, args.np->full_name, rc); | |
667 | } | |
668 | of_node_put(np); | |
669 | } | |
670 | ||
1f42e5dd GL |
671 | static struct of_device_id match_node_table[] = { |
672 | { .data = "A", .name = "name0", }, /* Name alone is lowest priority */ | |
673 | { .data = "B", .type = "type1", }, /* followed by type alone */ | |
674 | ||
675 | { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */ | |
676 | { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */ | |
677 | { .data = "Cc", .name = "name2", .type = "type2", }, | |
678 | ||
679 | { .data = "E", .compatible = "compat3" }, | |
680 | { .data = "G", .compatible = "compat2", }, | |
681 | { .data = "H", .compatible = "compat2", .name = "name5", }, | |
682 | { .data = "I", .compatible = "compat2", .type = "type1", }, | |
683 | { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", }, | |
684 | { .data = "K", .compatible = "compat2", .name = "name9", }, | |
685 | {} | |
686 | }; | |
687 | ||
688 | static struct { | |
689 | const char *path; | |
690 | const char *data; | |
691 | } match_node_tests[] = { | |
692 | { .path = "/testcase-data/match-node/name0", .data = "A", }, | |
693 | { .path = "/testcase-data/match-node/name1", .data = "B", }, | |
694 | { .path = "/testcase-data/match-node/a/name2", .data = "Ca", }, | |
695 | { .path = "/testcase-data/match-node/b/name2", .data = "Cb", }, | |
696 | { .path = "/testcase-data/match-node/c/name2", .data = "Cc", }, | |
697 | { .path = "/testcase-data/match-node/name3", .data = "E", }, | |
698 | { .path = "/testcase-data/match-node/name4", .data = "G", }, | |
699 | { .path = "/testcase-data/match-node/name5", .data = "H", }, | |
700 | { .path = "/testcase-data/match-node/name6", .data = "G", }, | |
701 | { .path = "/testcase-data/match-node/name7", .data = "I", }, | |
702 | { .path = "/testcase-data/match-node/name8", .data = "J", }, | |
703 | { .path = "/testcase-data/match-node/name9", .data = "K", }, | |
704 | }; | |
705 | ||
706 | static void __init of_selftest_match_node(void) | |
707 | { | |
708 | struct device_node *np; | |
709 | const struct of_device_id *match; | |
710 | int i; | |
711 | ||
712 | for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) { | |
713 | np = of_find_node_by_path(match_node_tests[i].path); | |
714 | if (!np) { | |
715 | selftest(0, "missing testcase node %s\n", | |
716 | match_node_tests[i].path); | |
717 | continue; | |
718 | } | |
719 | ||
720 | match = of_match_node(match_node_table, np); | |
721 | if (!match) { | |
722 | selftest(0, "%s didn't match anything\n", | |
723 | match_node_tests[i].path); | |
724 | continue; | |
725 | } | |
726 | ||
727 | if (strcmp(match->data, match_node_tests[i].data) != 0) { | |
728 | selftest(0, "%s got wrong match. expected %s, got %s\n", | |
729 | match_node_tests[i].path, match_node_tests[i].data, | |
730 | (const char *)match->data); | |
731 | continue; | |
732 | } | |
733 | selftest(1, "passed"); | |
734 | } | |
735 | } | |
736 | ||
851da976 GL |
737 | struct device test_bus = { |
738 | .init_name = "unittest-bus", | |
739 | }; | |
82c0f589 RH |
740 | static void __init of_selftest_platform_populate(void) |
741 | { | |
851da976 GL |
742 | int irq, rc; |
743 | struct device_node *np, *child, *grandchild; | |
82c0f589 | 744 | struct platform_device *pdev; |
fb2caa50 RH |
745 | struct of_device_id match[] = { |
746 | { .compatible = "test-device", }, | |
747 | {} | |
748 | }; | |
82c0f589 RH |
749 | |
750 | np = of_find_node_by_path("/testcase-data"); | |
751 | of_platform_populate(np, of_default_bus_match_table, NULL, NULL); | |
752 | ||
753 | /* Test that a missing irq domain returns -EPROBE_DEFER */ | |
754 | np = of_find_node_by_path("/testcase-data/testcase-device1"); | |
755 | pdev = of_find_device_by_node(np); | |
7d1cdc89 RH |
756 | selftest(pdev, "device 1 creation failed\n"); |
757 | ||
82c0f589 | 758 | irq = platform_get_irq(pdev, 0); |
7d1cdc89 | 759 | selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq); |
82c0f589 RH |
760 | |
761 | /* Test that a parsing failure does not return -EPROBE_DEFER */ | |
762 | np = of_find_node_by_path("/testcase-data/testcase-device2"); | |
763 | pdev = of_find_device_by_node(np); | |
7d1cdc89 | 764 | selftest(pdev, "device 2 creation failed\n"); |
82c0f589 | 765 | irq = platform_get_irq(pdev, 0); |
7d1cdc89 | 766 | selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq); |
82c0f589 | 767 | |
851da976 GL |
768 | if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"), |
769 | "No testcase data in device tree\n")); | |
770 | return; | |
771 | ||
772 | if (selftest(!(rc = device_register(&test_bus)), | |
773 | "testbus registration failed; rc=%i\n", rc)); | |
fb2caa50 | 774 | return; |
fb2caa50 RH |
775 | |
776 | for_each_child_of_node(np, child) { | |
851da976 | 777 | of_platform_populate(child, match, NULL, &test_bus); |
fb2caa50 RH |
778 | for_each_child_of_node(child, grandchild) |
779 | selftest(of_find_device_by_node(grandchild), | |
780 | "Could not create device for node '%s'\n", | |
781 | grandchild->name); | |
782 | } | |
851da976 GL |
783 | |
784 | of_platform_depopulate(&test_bus); | |
785 | for_each_child_of_node(np, child) { | |
786 | for_each_child_of_node(child, grandchild) | |
787 | selftest(!of_find_device_by_node(grandchild), | |
788 | "device didn't get destroyed '%s'\n", | |
789 | grandchild->name); | |
790 | } | |
791 | ||
792 | device_unregister(&test_bus); | |
793 | of_node_put(np); | |
82c0f589 RH |
794 | } |
795 | ||
ae9304c9 GM |
796 | /** |
797 | * update_node_properties - adds the properties | |
798 | * of np into dup node (present in live tree) and | |
799 | * updates parent of children of np to dup. | |
800 | * | |
801 | * @np: node already present in live tree | |
802 | * @dup: node present in live tree to be updated | |
803 | */ | |
804 | static void update_node_properties(struct device_node *np, | |
805 | struct device_node *dup) | |
806 | { | |
807 | struct property *prop; | |
808 | struct device_node *child; | |
809 | ||
810 | for_each_property_of_node(np, prop) | |
811 | of_add_property(dup, prop); | |
812 | ||
813 | for_each_child_of_node(np, child) | |
814 | child->parent = dup; | |
815 | } | |
816 | ||
817 | /** | |
818 | * attach_node_and_children - attaches nodes | |
819 | * and its children to live tree | |
820 | * | |
821 | * @np: Node to attach to live tree | |
822 | */ | |
823 | static int attach_node_and_children(struct device_node *np) | |
824 | { | |
5063e25a | 825 | struct device_node *next, *dup, *child; |
ae9304c9 | 826 | |
5063e25a GL |
827 | dup = of_find_node_by_path(np->full_name); |
828 | if (dup) { | |
829 | update_node_properties(np, dup); | |
830 | return 0; | |
831 | } | |
ae9304c9 | 832 | |
5063e25a GL |
833 | /* Children of the root need to be remembered for removal */ |
834 | if (np->parent == of_root) { | |
e66c98c7 GL |
835 | if (WARN_ON(last_node_index >= NO_OF_NODES)) |
836 | return -EINVAL; | |
5063e25a | 837 | nodes[last_node_index++] = np; |
ae9304c9 | 838 | } |
ae9304c9 | 839 | |
5063e25a GL |
840 | child = np->child; |
841 | np->child = NULL; | |
842 | np->sibling = NULL; | |
843 | of_attach_node(np); | |
844 | while (child) { | |
845 | next = child->sibling; | |
846 | attach_node_and_children(child); | |
847 | child = next; | |
ae9304c9 GM |
848 | } |
849 | ||
850 | return 0; | |
851 | } | |
852 | ||
853 | /** | |
854 | * selftest_data_add - Reads, copies data from | |
855 | * linked tree and attaches it to the live tree | |
856 | */ | |
857 | static int __init selftest_data_add(void) | |
858 | { | |
859 | void *selftest_data; | |
b951f9dc | 860 | struct device_node *selftest_data_node, *np; |
ae9304c9 GM |
861 | extern uint8_t __dtb_testcases_begin[]; |
862 | extern uint8_t __dtb_testcases_end[]; | |
863 | const int size = __dtb_testcases_end - __dtb_testcases_begin; | |
2eb46da2 | 864 | int rc; |
ae9304c9 | 865 | |
b951f9dc | 866 | if (!size) { |
ae9304c9 GM |
867 | pr_warn("%s: No testcase data to attach; not running tests\n", |
868 | __func__); | |
869 | return -ENODATA; | |
870 | } | |
871 | ||
872 | /* creating copy */ | |
873 | selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL); | |
874 | ||
875 | if (!selftest_data) { | |
876 | pr_warn("%s: Failed to allocate memory for selftest_data; " | |
877 | "not running tests\n", __func__); | |
878 | return -ENOMEM; | |
879 | } | |
880 | of_fdt_unflatten_tree(selftest_data, &selftest_data_node); | |
b951f9dc GM |
881 | if (!selftest_data_node) { |
882 | pr_warn("%s: No tree to attach; not running tests\n", __func__); | |
883 | return -ENODATA; | |
884 | } | |
2eb46da2 GL |
885 | of_node_set_flag(selftest_data_node, OF_DETACHED); |
886 | rc = of_resolve_phandles(selftest_data_node); | |
887 | if (rc) { | |
888 | pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc); | |
889 | return -EINVAL; | |
890 | } | |
b951f9dc | 891 | |
5063e25a | 892 | if (!of_root) { |
b951f9dc GM |
893 | /* enabling flag for removing nodes */ |
894 | selftest_live_tree = true; | |
5063e25a | 895 | of_root = selftest_data_node; |
b951f9dc GM |
896 | |
897 | for_each_of_allnodes(np) | |
898 | __of_attach_node_sysfs(np); | |
899 | of_aliases = of_find_node_by_path("/aliases"); | |
900 | of_chosen = of_find_node_by_path("/chosen"); | |
901 | return 0; | |
902 | } | |
ae9304c9 GM |
903 | |
904 | /* attach the sub-tree to live tree */ | |
5063e25a GL |
905 | np = selftest_data_node->child; |
906 | while (np) { | |
907 | struct device_node *next = np->sibling; | |
908 | np->parent = of_root; | |
909 | attach_node_and_children(np); | |
910 | np = next; | |
911 | } | |
912 | return 0; | |
ae9304c9 GM |
913 | } |
914 | ||
915 | /** | |
916 | * detach_node_and_children - detaches node | |
917 | * and its children from live tree | |
918 | * | |
919 | * @np: Node to detach from live tree | |
920 | */ | |
921 | static void detach_node_and_children(struct device_node *np) | |
922 | { | |
923 | while (np->child) | |
924 | detach_node_and_children(np->child); | |
ae9304c9 GM |
925 | of_detach_node(np); |
926 | } | |
927 | ||
928 | /** | |
929 | * selftest_data_remove - removes the selftest data | |
930 | * nodes from the live tree | |
931 | */ | |
932 | static void selftest_data_remove(void) | |
933 | { | |
934 | struct device_node *np; | |
935 | struct property *prop; | |
936 | ||
b951f9dc GM |
937 | if (selftest_live_tree) { |
938 | of_node_put(of_aliases); | |
939 | of_node_put(of_chosen); | |
940 | of_aliases = NULL; | |
941 | of_chosen = NULL; | |
5063e25a | 942 | for_each_child_of_node(of_root, np) |
b951f9dc | 943 | detach_node_and_children(np); |
5063e25a GL |
944 | __of_detach_node_sysfs(of_root); |
945 | of_root = NULL; | |
b951f9dc GM |
946 | return; |
947 | } | |
948 | ||
c1a2086e | 949 | while (last_node_index-- > 0) { |
ae9304c9 GM |
950 | if (nodes[last_node_index]) { |
951 | np = of_find_node_by_path(nodes[last_node_index]->full_name); | |
788ec2fc GL |
952 | if (np == nodes[last_node_index]) { |
953 | if (of_aliases == np) { | |
954 | of_node_put(of_aliases); | |
955 | of_aliases = NULL; | |
956 | } | |
e66c98c7 | 957 | detach_node_and_children(np); |
ae9304c9 GM |
958 | } else { |
959 | for_each_property_of_node(np, prop) { | |
960 | if (strcmp(prop->name, "testcase-alias") == 0) | |
961 | of_remove_property(np, prop); | |
962 | } | |
963 | } | |
964 | } | |
ae9304c9 GM |
965 | } |
966 | } | |
967 | ||
177d271c PA |
968 | #ifdef CONFIG_OF_OVERLAY |
969 | ||
970 | static int selftest_probe(struct platform_device *pdev) | |
971 | { | |
972 | struct device *dev = &pdev->dev; | |
973 | struct device_node *np = dev->of_node; | |
974 | ||
975 | if (np == NULL) { | |
976 | dev_err(dev, "No OF data for device\n"); | |
977 | return -EINVAL; | |
978 | ||
979 | } | |
980 | ||
981 | dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); | |
982 | return 0; | |
983 | } | |
984 | ||
985 | static int selftest_remove(struct platform_device *pdev) | |
986 | { | |
987 | struct device *dev = &pdev->dev; | |
988 | struct device_node *np = dev->of_node; | |
989 | ||
990 | dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); | |
991 | return 0; | |
992 | } | |
993 | ||
994 | static struct of_device_id selftest_match[] = { | |
995 | { .compatible = "selftest", }, | |
996 | {}, | |
997 | }; | |
177d271c PA |
998 | |
999 | static struct platform_driver selftest_driver = { | |
1000 | .probe = selftest_probe, | |
1001 | .remove = selftest_remove, | |
1002 | .driver = { | |
1003 | .name = "selftest", | |
1004 | .owner = THIS_MODULE, | |
1005 | .of_match_table = of_match_ptr(selftest_match), | |
1006 | }, | |
1007 | }; | |
1008 | ||
1009 | /* get the platform device instantiated at the path */ | |
1010 | static struct platform_device *of_path_to_platform_device(const char *path) | |
1011 | { | |
1012 | struct device_node *np; | |
1013 | struct platform_device *pdev; | |
1014 | ||
1015 | np = of_find_node_by_path(path); | |
1016 | if (np == NULL) | |
1017 | return NULL; | |
1018 | ||
1019 | pdev = of_find_device_by_node(np); | |
1020 | of_node_put(np); | |
1021 | ||
1022 | return pdev; | |
1023 | } | |
1024 | ||
1025 | /* find out if a platform device exists at that path */ | |
1026 | static int of_path_platform_device_exists(const char *path) | |
1027 | { | |
1028 | struct platform_device *pdev; | |
1029 | ||
1030 | pdev = of_path_to_platform_device(path); | |
1031 | platform_device_put(pdev); | |
1032 | return pdev != NULL; | |
1033 | } | |
1034 | ||
1035 | static const char *selftest_path(int nr) | |
1036 | { | |
1037 | static char buf[256]; | |
1038 | ||
1039 | snprintf(buf, sizeof(buf) - 1, | |
1040 | "/testcase-data/overlay-node/test-bus/test-selftest%d", nr); | |
1041 | buf[sizeof(buf) - 1] = '\0'; | |
1042 | ||
1043 | return buf; | |
1044 | } | |
1045 | ||
1046 | static const char *overlay_path(int nr) | |
1047 | { | |
1048 | static char buf[256]; | |
1049 | ||
1050 | snprintf(buf, sizeof(buf) - 1, | |
1051 | "/testcase-data/overlay%d", nr); | |
1052 | buf[sizeof(buf) - 1] = '\0'; | |
1053 | ||
1054 | return buf; | |
1055 | } | |
1056 | ||
1057 | static const char *bus_path = "/testcase-data/overlay-node/test-bus"; | |
1058 | ||
1059 | static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr, | |
1060 | int *overlay_id) | |
1061 | { | |
1062 | struct device_node *np = NULL; | |
1063 | int ret, id = -1; | |
1064 | ||
1065 | np = of_find_node_by_path(overlay_path(overlay_nr)); | |
1066 | if (np == NULL) { | |
1067 | selftest(0, "could not find overlay node @\"%s\"\n", | |
1068 | overlay_path(overlay_nr)); | |
1069 | ret = -EINVAL; | |
1070 | goto out; | |
1071 | } | |
1072 | ||
1073 | ret = of_overlay_create(np); | |
1074 | if (ret < 0) { | |
1075 | selftest(0, "could not create overlay from \"%s\"\n", | |
1076 | overlay_path(overlay_nr)); | |
1077 | goto out; | |
1078 | } | |
1079 | id = ret; | |
1080 | ||
1081 | ret = 0; | |
1082 | ||
1083 | out: | |
1084 | of_node_put(np); | |
1085 | ||
1086 | if (overlay_id) | |
1087 | *overlay_id = id; | |
1088 | ||
1089 | return ret; | |
1090 | } | |
1091 | ||
1092 | /* apply an overlay while checking before and after states */ | |
1093 | static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr, | |
1094 | int before, int after) | |
1095 | { | |
1096 | int ret; | |
1097 | ||
1098 | /* selftest device must not be in before state */ | |
1099 | if (of_path_platform_device_exists(selftest_path(selftest_nr)) | |
1100 | != before) { | |
1101 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", | |
1102 | overlay_path(overlay_nr), | |
1103 | selftest_path(selftest_nr), | |
1104 | !before ? "enabled" : "disabled"); | |
1105 | return -EINVAL; | |
1106 | } | |
1107 | ||
1108 | ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL); | |
1109 | if (ret != 0) { | |
1110 | /* of_selftest_apply_overlay already called selftest() */ | |
1111 | return ret; | |
1112 | } | |
1113 | ||
1114 | /* selftest device must be to set to after state */ | |
1115 | if (of_path_platform_device_exists(selftest_path(selftest_nr)) | |
1116 | != after) { | |
1117 | selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", | |
1118 | overlay_path(overlay_nr), | |
1119 | selftest_path(selftest_nr), | |
1120 | !after ? "enabled" : "disabled"); | |
1121 | return -EINVAL; | |
1122 | } | |
1123 | ||
1124 | return 0; | |
1125 | } | |
1126 | ||
1127 | /* apply an overlay and then revert it while checking before, after states */ | |
1128 | static int of_selftest_apply_revert_overlay_check(int overlay_nr, | |
1129 | int selftest_nr, int before, int after) | |
1130 | { | |
1131 | int ret, ov_id; | |
1132 | ||
1133 | /* selftest device must be in before state */ | |
1134 | if (of_path_platform_device_exists(selftest_path(selftest_nr)) | |
1135 | != before) { | |
1136 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", | |
1137 | overlay_path(overlay_nr), | |
1138 | selftest_path(selftest_nr), | |
1139 | !before ? "enabled" : "disabled"); | |
1140 | return -EINVAL; | |
1141 | } | |
1142 | ||
1143 | /* apply the overlay */ | |
1144 | ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id); | |
1145 | if (ret != 0) { | |
1146 | /* of_selftest_apply_overlay already called selftest() */ | |
1147 | return ret; | |
1148 | } | |
1149 | ||
1150 | /* selftest device must be in after state */ | |
1151 | if (of_path_platform_device_exists(selftest_path(selftest_nr)) | |
1152 | != after) { | |
1153 | selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", | |
1154 | overlay_path(overlay_nr), | |
1155 | selftest_path(selftest_nr), | |
1156 | !after ? "enabled" : "disabled"); | |
1157 | return -EINVAL; | |
1158 | } | |
1159 | ||
1160 | ret = of_overlay_destroy(ov_id); | |
1161 | if (ret != 0) { | |
1162 | selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n", | |
1163 | overlay_path(overlay_nr), | |
1164 | selftest_path(selftest_nr)); | |
1165 | return ret; | |
1166 | } | |
1167 | ||
1168 | /* selftest device must be again in before state */ | |
1169 | if (of_path_platform_device_exists(selftest_path(selftest_nr)) | |
1170 | != before) { | |
1171 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", | |
1172 | overlay_path(overlay_nr), | |
1173 | selftest_path(selftest_nr), | |
1174 | !before ? "enabled" : "disabled"); | |
1175 | return -EINVAL; | |
1176 | } | |
1177 | ||
1178 | return 0; | |
1179 | } | |
1180 | ||
1181 | /* test activation of device */ | |
1182 | static void of_selftest_overlay_0(void) | |
1183 | { | |
1184 | int ret; | |
1185 | ||
1186 | /* device should enable */ | |
1187 | ret = of_selftest_apply_overlay_check(0, 0, 0, 1); | |
1188 | if (ret != 0) | |
1189 | return; | |
1190 | ||
1191 | selftest(1, "overlay test %d passed\n", 0); | |
1192 | } | |
1193 | ||
1194 | /* test deactivation of device */ | |
1195 | static void of_selftest_overlay_1(void) | |
1196 | { | |
1197 | int ret; | |
1198 | ||
1199 | /* device should disable */ | |
1200 | ret = of_selftest_apply_overlay_check(1, 1, 1, 0); | |
1201 | if (ret != 0) | |
1202 | return; | |
1203 | ||
1204 | selftest(1, "overlay test %d passed\n", 1); | |
1205 | } | |
1206 | ||
1207 | /* test activation of device */ | |
1208 | static void of_selftest_overlay_2(void) | |
1209 | { | |
1210 | int ret; | |
1211 | ||
1212 | /* device should enable */ | |
1213 | ret = of_selftest_apply_overlay_check(2, 2, 0, 1); | |
1214 | if (ret != 0) | |
1215 | return; | |
1216 | ||
1217 | selftest(1, "overlay test %d passed\n", 2); | |
1218 | } | |
1219 | ||
1220 | /* test deactivation of device */ | |
1221 | static void of_selftest_overlay_3(void) | |
1222 | { | |
1223 | int ret; | |
1224 | ||
1225 | /* device should disable */ | |
1226 | ret = of_selftest_apply_overlay_check(3, 3, 1, 0); | |
1227 | if (ret != 0) | |
1228 | return; | |
1229 | ||
1230 | selftest(1, "overlay test %d passed\n", 3); | |
1231 | } | |
1232 | ||
1233 | /* test activation of a full device node */ | |
1234 | static void of_selftest_overlay_4(void) | |
1235 | { | |
1236 | int ret; | |
1237 | ||
1238 | /* device should disable */ | |
1239 | ret = of_selftest_apply_overlay_check(4, 4, 0, 1); | |
1240 | if (ret != 0) | |
1241 | return; | |
1242 | ||
1243 | selftest(1, "overlay test %d passed\n", 4); | |
1244 | } | |
1245 | ||
1246 | /* test overlay apply/revert sequence */ | |
1247 | static void of_selftest_overlay_5(void) | |
1248 | { | |
1249 | int ret; | |
1250 | ||
1251 | /* device should disable */ | |
1252 | ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1); | |
1253 | if (ret != 0) | |
1254 | return; | |
1255 | ||
1256 | selftest(1, "overlay test %d passed\n", 5); | |
1257 | } | |
1258 | ||
1259 | /* test overlay application in sequence */ | |
1260 | static void of_selftest_overlay_6(void) | |
1261 | { | |
1262 | struct device_node *np; | |
1263 | int ret, i, ov_id[2]; | |
1264 | int overlay_nr = 6, selftest_nr = 6; | |
1265 | int before = 0, after = 1; | |
1266 | ||
1267 | /* selftest device must be in before state */ | |
1268 | for (i = 0; i < 2; i++) { | |
1269 | if (of_path_platform_device_exists( | |
1270 | selftest_path(selftest_nr + i)) | |
1271 | != before) { | |
1272 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", | |
1273 | overlay_path(overlay_nr + i), | |
1274 | selftest_path(selftest_nr + i), | |
1275 | !before ? "enabled" : "disabled"); | |
1276 | return; | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | /* apply the overlays */ | |
1281 | for (i = 0; i < 2; i++) { | |
1282 | ||
1283 | np = of_find_node_by_path(overlay_path(overlay_nr + i)); | |
1284 | if (np == NULL) { | |
1285 | selftest(0, "could not find overlay node @\"%s\"\n", | |
1286 | overlay_path(overlay_nr + i)); | |
1287 | return; | |
1288 | } | |
1289 | ||
1290 | ret = of_overlay_create(np); | |
1291 | if (ret < 0) { | |
1292 | selftest(0, "could not create overlay from \"%s\"\n", | |
1293 | overlay_path(overlay_nr + i)); | |
1294 | return; | |
1295 | } | |
1296 | ov_id[i] = ret; | |
1297 | } | |
1298 | ||
1299 | for (i = 0; i < 2; i++) { | |
1300 | /* selftest device must be in after state */ | |
1301 | if (of_path_platform_device_exists( | |
1302 | selftest_path(selftest_nr + i)) | |
1303 | != after) { | |
1304 | selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n", | |
1305 | overlay_path(overlay_nr + i), | |
1306 | selftest_path(selftest_nr + i), | |
1307 | !after ? "enabled" : "disabled"); | |
1308 | return; | |
1309 | } | |
1310 | } | |
1311 | ||
1312 | for (i = 1; i >= 0; i--) { | |
1313 | ret = of_overlay_destroy(ov_id[i]); | |
1314 | if (ret != 0) { | |
1315 | selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n", | |
1316 | overlay_path(overlay_nr + i), | |
1317 | selftest_path(selftest_nr + i)); | |
1318 | return; | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | for (i = 0; i < 2; i++) { | |
1323 | /* selftest device must be again in before state */ | |
1324 | if (of_path_platform_device_exists( | |
1325 | selftest_path(selftest_nr + i)) | |
1326 | != before) { | |
1327 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", | |
1328 | overlay_path(overlay_nr + i), | |
1329 | selftest_path(selftest_nr + i), | |
1330 | !before ? "enabled" : "disabled"); | |
1331 | return; | |
1332 | } | |
1333 | } | |
1334 | ||
1335 | selftest(1, "overlay test %d passed\n", 6); | |
1336 | } | |
1337 | ||
1338 | /* test overlay application in sequence */ | |
1339 | static void of_selftest_overlay_8(void) | |
1340 | { | |
1341 | struct device_node *np; | |
1342 | int ret, i, ov_id[2]; | |
1343 | int overlay_nr = 8, selftest_nr = 8; | |
1344 | ||
1345 | /* we don't care about device state in this test */ | |
1346 | ||
1347 | /* apply the overlays */ | |
1348 | for (i = 0; i < 2; i++) { | |
1349 | ||
1350 | np = of_find_node_by_path(overlay_path(overlay_nr + i)); | |
1351 | if (np == NULL) { | |
1352 | selftest(0, "could not find overlay node @\"%s\"\n", | |
1353 | overlay_path(overlay_nr + i)); | |
1354 | return; | |
1355 | } | |
1356 | ||
1357 | ret = of_overlay_create(np); | |
1358 | if (ret < 0) { | |
1359 | selftest(0, "could not create overlay from \"%s\"\n", | |
1360 | overlay_path(overlay_nr + i)); | |
1361 | return; | |
1362 | } | |
1363 | ov_id[i] = ret; | |
1364 | } | |
1365 | ||
1366 | /* now try to remove first overlay (it should fail) */ | |
1367 | ret = of_overlay_destroy(ov_id[0]); | |
1368 | if (ret == 0) { | |
1369 | selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n", | |
1370 | overlay_path(overlay_nr + 0), | |
1371 | selftest_path(selftest_nr)); | |
1372 | return; | |
1373 | } | |
1374 | ||
1375 | /* removing them in order should work */ | |
1376 | for (i = 1; i >= 0; i--) { | |
1377 | ret = of_overlay_destroy(ov_id[i]); | |
1378 | if (ret != 0) { | |
1379 | selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n", | |
1380 | overlay_path(overlay_nr + i), | |
1381 | selftest_path(selftest_nr)); | |
1382 | return; | |
1383 | } | |
1384 | } | |
1385 | ||
1386 | selftest(1, "overlay test %d passed\n", 8); | |
1387 | } | |
1388 | ||
1389 | static void __init of_selftest_overlay(void) | |
1390 | { | |
1391 | struct device_node *bus_np = NULL; | |
1392 | int ret; | |
1393 | ||
1394 | ret = platform_driver_register(&selftest_driver); | |
1395 | if (ret != 0) { | |
1396 | selftest(0, "could not register selftest driver\n"); | |
1397 | goto out; | |
1398 | } | |
1399 | ||
1400 | bus_np = of_find_node_by_path(bus_path); | |
1401 | if (bus_np == NULL) { | |
1402 | selftest(0, "could not find bus_path \"%s\"\n", bus_path); | |
1403 | goto out; | |
1404 | } | |
1405 | ||
1406 | ret = of_platform_populate(bus_np, of_default_bus_match_table, | |
1407 | NULL, NULL); | |
1408 | if (ret != 0) { | |
1409 | selftest(0, "could not populate bus @ \"%s\"\n", bus_path); | |
1410 | goto out; | |
1411 | } | |
1412 | ||
1413 | if (!of_path_platform_device_exists(selftest_path(100))) { | |
1414 | selftest(0, "could not find selftest0 @ \"%s\"\n", | |
1415 | selftest_path(100)); | |
1416 | goto out; | |
1417 | } | |
1418 | ||
1419 | if (of_path_platform_device_exists(selftest_path(101))) { | |
1420 | selftest(0, "selftest1 @ \"%s\" should not exist\n", | |
1421 | selftest_path(101)); | |
1422 | goto out; | |
1423 | } | |
1424 | ||
1425 | selftest(1, "basic infrastructure of overlays passed"); | |
1426 | ||
1427 | /* tests in sequence */ | |
1428 | of_selftest_overlay_0(); | |
1429 | of_selftest_overlay_1(); | |
1430 | of_selftest_overlay_2(); | |
1431 | of_selftest_overlay_3(); | |
1432 | of_selftest_overlay_4(); | |
1433 | of_selftest_overlay_5(); | |
1434 | of_selftest_overlay_6(); | |
1435 | of_selftest_overlay_8(); | |
1436 | ||
1437 | out: | |
1438 | of_node_put(bus_np); | |
1439 | } | |
1440 | ||
1441 | #else | |
1442 | static inline void __init of_selftest_overlay(void) { } | |
1443 | #endif | |
1444 | ||
53a42093 GL |
1445 | static int __init of_selftest(void) |
1446 | { | |
1447 | struct device_node *np; | |
ae9304c9 GM |
1448 | int res; |
1449 | ||
1450 | /* adding data for selftest */ | |
1451 | res = selftest_data_add(); | |
1452 | if (res) | |
1453 | return res; | |
788ec2fc GL |
1454 | if (!of_aliases) |
1455 | of_aliases = of_find_node_by_path("/aliases"); | |
53a42093 GL |
1456 | |
1457 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); | |
1458 | if (!np) { | |
1459 | pr_info("No testcase data in device tree; not running tests\n"); | |
1460 | return 0; | |
1461 | } | |
1462 | of_node_put(np); | |
1463 | ||
1464 | pr_info("start of selftest - you will see error messages\n"); | |
f2051d6a | 1465 | of_selftest_check_tree_linkage(); |
841ec213 | 1466 | of_selftest_check_phandles(); |
ae91ff72 | 1467 | of_selftest_find_node_by_name(); |
7e66c5c7 | 1468 | of_selftest_dynamic(); |
53a42093 | 1469 | of_selftest_parse_phandle_with_args(); |
a87fa1d8 | 1470 | of_selftest_property_string(); |
69843396 | 1471 | of_selftest_property_copy(); |
201c910b | 1472 | of_selftest_changeset(); |
a9f10ca7 | 1473 | of_selftest_parse_interrupts(); |
79d97015 | 1474 | of_selftest_parse_interrupts_extended(); |
1f42e5dd | 1475 | of_selftest_match_node(); |
82c0f589 | 1476 | of_selftest_platform_populate(); |
177d271c | 1477 | of_selftest_overlay(); |
ae9304c9 GM |
1478 | |
1479 | /* removing selftest data from live tree */ | |
1480 | selftest_data_remove(); | |
1481 | ||
f2051d6a GL |
1482 | /* Double check linkage after removing testcase data */ |
1483 | of_selftest_check_tree_linkage(); | |
1484 | ||
1485 | pr_info("end of selftest - %i passed, %i failed\n", | |
1486 | selftest_results.passed, selftest_results.failed); | |
1487 | ||
53a42093 GL |
1488 | return 0; |
1489 | } | |
1490 | late_initcall(of_selftest); |