blob: 139363af5c887fd8d8165e009747db7855a0d91d [file] [log] [blame]
Grant Likely53a42092011-12-12 09:25:57 -07001/*
2 * Self tests for device tree subsystem
3 */
4
Grant Likelycabb7d52013-02-12 21:19:37 +00005#define pr_fmt(fmt) "### dt-test ### " fmt
Grant Likely53a42092011-12-12 09:25:57 -07006
7#include <linux/clk.h>
8#include <linux/err.h>
9#include <linux/errno.h>
Grant Likely841ec212014-10-02 13:09:15 +010010#include <linux/hashtable.h>
Grant Likely53a42092011-12-12 09:25:57 -070011#include <linux/module.h>
12#include <linux/of.h>
Gaurav Minochaae9304c2014-07-16 23:09:39 -070013#include <linux/of_fdt.h>
Grant Likelya9f10ca2013-10-11 22:04:23 +010014#include <linux/of_irq.h>
Rob Herring82c0f582014-04-23 17:57:40 -050015#include <linux/of_platform.h>
Grant Likely53a42092011-12-12 09:25:57 -070016#include <linux/list.h>
17#include <linux/mutex.h>
18#include <linux/slab.h>
19#include <linux/device.h>
Pantelis Antoniou177d2712014-10-28 22:35:59 +020020#include <linux/platform_device.h>
21#include <linux/of_platform.h>
Grant Likely53a42092011-12-12 09:25:57 -070022
Pantelis Antoniou69843392014-07-04 19:58:47 +030023#include "of_private.h"
24
Grant Likelya9f10ca2013-10-11 22:04:23 +010025static struct selftest_results {
26 int passed;
27 int failed;
28} selftest_results;
29
Grant Likely2eb46da2014-10-02 14:36:46 +010030#define NO_OF_NODES 3
Gaurav Minochaae9304c2014-07-16 23:09:39 -070031static struct device_node *nodes[NO_OF_NODES];
32static int last_node_index;
Gaurav Minochab951f9d2014-07-26 12:48:50 -070033static bool selftest_live_tree;
Gaurav Minochaae9304c2014-07-16 23:09:39 -070034
Grant Likely851da972014-11-04 13:14:13 +000035#define selftest(result, fmt, ...) ({ \
36 bool failed = !(result); \
37 if (failed) { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010038 selftest_results.failed++; \
39 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000040 } else { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010041 selftest_results.passed++; \
42 pr_debug("pass %s():%i\n", __func__, __LINE__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000043 } \
Grant Likely851da972014-11-04 13:14:13 +000044 failed; \
45})
Grant Likely53a42092011-12-12 09:25:57 -070046
Grant Likelyae91ff72014-03-14 13:53:10 +000047static void __init of_selftest_find_node_by_name(void)
48{
49 struct device_node *np;
Leif Lindholm75c28c02014-11-28 11:34:28 +000050 const char *options;
Grant Likelyae91ff72014-03-14 13:53:10 +000051
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);
Leif Lindholm75c28c02014-11-28 11:34:28 +000091
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);
Grant Likelyae91ff72014-03-14 13:53:10 +0000120}
121
Grant Likely7e66c5c2013-11-15 17:19:09 +0000122static 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
Grant Likelyf2051d62014-10-01 17:40:22 +0100183static int __init of_selftest_check_node_linkage(struct device_node *np)
184{
Grant Likely5063e252014-10-03 16:28:27 +0100185 struct device_node *child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100186 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
Grant Likelyf2051d62014-10-01 17:40:22 +0100195 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
204static void __init of_selftest_check_tree_linkage(void)
205{
206 struct device_node *np;
207 int allnode_count = 0, child_count;
208
Grant Likely5063e252014-10-03 16:28:27 +0100209 if (!of_root)
Grant Likelyf2051d62014-10-01 17:40:22 +0100210 return;
211
212 for_each_of_allnodes(np)
213 allnode_count++;
Grant Likely5063e252014-10-03 16:28:27 +0100214 child_count = of_selftest_check_node_linkage(of_root);
Grant Likelyf2051d62014-10-01 17:40:22 +0100215
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
Grant Likely841ec212014-10-02 13:09:15 +0100222struct node_hash {
223 struct hlist_node node;
224 struct device_node *np;
225};
226
Grant Likely2118f4b2014-10-07 11:30:31 +0100227static DEFINE_HASHTABLE(phandle_ht, 8);
Grant Likely841ec212014-10-02 13:09:15 +0100228static 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;
Grant Likely841ec212014-10-02 13:09:15 +0100234
Grant Likely841ec212014-10-02 13:09:15 +0100235 for_each_of_allnodes(np) {
236 if (!np->phandle)
237 continue;
238
Grant Likely2118f4b2014-10-07 11:30:31 +0100239 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100240 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;
Grant Likely2118f4b2014-10-07 11:30:31 +0100253 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100254 phandle_count++;
255 }
256 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
257 dup_count, phandle_count);
258
259 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100260 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100261 hash_del(&nh->node);
262 kfree(nh);
263 }
264}
265
Grant Likely53a42092011-12-12 09:25:57 -0700266static void __init of_selftest_parse_phandle_with_args(void)
267{
268 struct device_node *np;
269 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000270 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700271
Grant Likely53a42092011-12-12 09:25:57 -0700272 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
Grant Likelybd69f732013-02-10 22:57:21 +0000278 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
Grant Likelyf7f951c2013-02-12 17:41:22 +0000281 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700282 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:
Grant Likelycabb7d52013-02-12 21:19:37 +0000325 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700326 break;
327 default:
328 passed = false;
329 }
330
Grant Likelycabb7d52013-02-12 21:19:37 +0000331 selftest(passed, "index %i - data error on node %s rc=%i\n",
332 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700333 }
334
335 /* Check for missing list property */
336 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
337 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000338 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000339 rc = of_count_phandle_with_args(np, "phandle-list-missing",
340 "#phandle-cells");
341 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700342
343 /* Check for missing cells property */
344 rc = of_parse_phandle_with_args(np, "phandle-list",
345 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000346 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000347 rc = of_count_phandle_with_args(np, "phandle-list",
348 "#phandle-cells-missing");
349 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700350
351 /* Check for bad phandle in list */
352 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
353 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000354 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000355 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);
Grant Likely53a42092011-12-12 09:25:57 -0700358
359 /* Check for incorrectly formed argument list */
360 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
361 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000362 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000363 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);
Grant Likely53a42092011-12-12 09:25:57 -0700366}
367
Grant Likelya87fa1d2014-11-03 15:15:35 +0000368static void __init of_selftest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700369{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000370 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700371 struct device_node *np;
372 int rc;
373
Grant Likely7aff0fe2011-12-12 09:25:58 -0700374 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");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000387 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700388 rc = of_property_match_string(np, "missing-property", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000389 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700390 rc = of_property_match_string(np, "empty-property", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000391 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700392 rc = of_property_match_string(np, "unterminated-string", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000393 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]);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700447}
448
Pantelis Antoniou69843392014-07-04 19:58:47 +0300449#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))
453static 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
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300474static 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;
Grant Likelye5179582014-11-17 22:31:32 +0000480 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300481 struct of_changeset chgset;
482
483 of_changeset_init(&chgset);
Grant Likelye5179582014-11-17 22:31:32 +0000484 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300485 selftest(n1, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000486 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300487 selftest(n2, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000488 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300489 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
Grant Likelye5179582014-11-17 22:31:32 +0000516 /* 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);
Markus Elfringc46ca3c2014-12-02 13:54:00 +0100519 of_node_put(np);
Grant Likelye5179582014-11-17 22:31:32 +0000520
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300521 mutex_lock(&of_mutex);
522 selftest(!of_changeset_revert(&chgset), "revert failed\n");
523 mutex_unlock(&of_mutex);
524
525 of_changeset_destroy(&chgset);
526#endif
527}
528
Grant Likelya9f10ca2013-10-11 22:04:23 +0100529static void __init of_selftest_parse_interrupts(void)
530{
531 struct device_node *np;
532 struct of_phandle_args args;
533 int i, rc;
534
535 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
536 if (!np) {
537 pr_err("missing testcase data\n");
538 return;
539 }
540
541 for (i = 0; i < 4; i++) {
542 bool passed = true;
543 args.args_count = 0;
544 rc = of_irq_parse_one(np, i, &args);
545
546 passed &= !rc;
547 passed &= (args.args_count == 1);
548 passed &= (args.args[0] == (i + 1));
549
550 selftest(passed, "index %i - data error on node %s rc=%i\n",
551 i, args.np->full_name, rc);
552 }
553 of_node_put(np);
554
555 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
556 if (!np) {
557 pr_err("missing testcase data\n");
558 return;
559 }
560
561 for (i = 0; i < 4; i++) {
562 bool passed = true;
563 args.args_count = 0;
564 rc = of_irq_parse_one(np, i, &args);
565
566 /* Test the values from tests-phandle.dtsi */
567 switch (i) {
568 case 0:
569 passed &= !rc;
570 passed &= (args.args_count == 1);
571 passed &= (args.args[0] == 9);
572 break;
573 case 1:
574 passed &= !rc;
575 passed &= (args.args_count == 3);
576 passed &= (args.args[0] == 10);
577 passed &= (args.args[1] == 11);
578 passed &= (args.args[2] == 12);
579 break;
580 case 2:
581 passed &= !rc;
582 passed &= (args.args_count == 2);
583 passed &= (args.args[0] == 13);
584 passed &= (args.args[1] == 14);
585 break;
586 case 3:
587 passed &= !rc;
588 passed &= (args.args_count == 2);
589 passed &= (args.args[0] == 15);
590 passed &= (args.args[1] == 16);
591 break;
592 default:
593 passed = false;
594 }
595 selftest(passed, "index %i - data error on node %s rc=%i\n",
596 i, args.np->full_name, rc);
597 }
598 of_node_put(np);
599}
600
Grant Likely79d97012013-09-19 16:47:37 -0500601static void __init of_selftest_parse_interrupts_extended(void)
602{
603 struct device_node *np;
604 struct of_phandle_args args;
605 int i, rc;
606
607 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
608 if (!np) {
609 pr_err("missing testcase data\n");
610 return;
611 }
612
613 for (i = 0; i < 7; i++) {
614 bool passed = true;
615 rc = of_irq_parse_one(np, i, &args);
616
617 /* Test the values from tests-phandle.dtsi */
618 switch (i) {
619 case 0:
620 passed &= !rc;
621 passed &= (args.args_count == 1);
622 passed &= (args.args[0] == 1);
623 break;
624 case 1:
625 passed &= !rc;
626 passed &= (args.args_count == 3);
627 passed &= (args.args[0] == 2);
628 passed &= (args.args[1] == 3);
629 passed &= (args.args[2] == 4);
630 break;
631 case 2:
632 passed &= !rc;
633 passed &= (args.args_count == 2);
634 passed &= (args.args[0] == 5);
635 passed &= (args.args[1] == 6);
636 break;
637 case 3:
638 passed &= !rc;
639 passed &= (args.args_count == 1);
640 passed &= (args.args[0] == 9);
641 break;
642 case 4:
643 passed &= !rc;
644 passed &= (args.args_count == 3);
645 passed &= (args.args[0] == 10);
646 passed &= (args.args[1] == 11);
647 passed &= (args.args[2] == 12);
648 break;
649 case 5:
650 passed &= !rc;
651 passed &= (args.args_count == 2);
652 passed &= (args.args[0] == 13);
653 passed &= (args.args[1] == 14);
654 break;
655 case 6:
656 passed &= !rc;
657 passed &= (args.args_count == 1);
658 passed &= (args.args[0] == 15);
659 break;
660 default:
661 passed = false;
662 }
663
664 selftest(passed, "index %i - data error on node %s rc=%i\n",
665 i, args.np->full_name, rc);
666 }
667 of_node_put(np);
668}
669
Grant Likely1f42e5d2014-02-18 21:38:55 +0000670static struct of_device_id match_node_table[] = {
671 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
672 { .data = "B", .type = "type1", }, /* followed by type alone */
673
674 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
675 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
676 { .data = "Cc", .name = "name2", .type = "type2", },
677
678 { .data = "E", .compatible = "compat3" },
679 { .data = "G", .compatible = "compat2", },
680 { .data = "H", .compatible = "compat2", .name = "name5", },
681 { .data = "I", .compatible = "compat2", .type = "type1", },
682 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
683 { .data = "K", .compatible = "compat2", .name = "name9", },
684 {}
685};
686
687static struct {
688 const char *path;
689 const char *data;
690} match_node_tests[] = {
691 { .path = "/testcase-data/match-node/name0", .data = "A", },
692 { .path = "/testcase-data/match-node/name1", .data = "B", },
693 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
694 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
695 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
696 { .path = "/testcase-data/match-node/name3", .data = "E", },
697 { .path = "/testcase-data/match-node/name4", .data = "G", },
698 { .path = "/testcase-data/match-node/name5", .data = "H", },
699 { .path = "/testcase-data/match-node/name6", .data = "G", },
700 { .path = "/testcase-data/match-node/name7", .data = "I", },
701 { .path = "/testcase-data/match-node/name8", .data = "J", },
702 { .path = "/testcase-data/match-node/name9", .data = "K", },
703};
704
705static void __init of_selftest_match_node(void)
706{
707 struct device_node *np;
708 const struct of_device_id *match;
709 int i;
710
711 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
712 np = of_find_node_by_path(match_node_tests[i].path);
713 if (!np) {
714 selftest(0, "missing testcase node %s\n",
715 match_node_tests[i].path);
716 continue;
717 }
718
719 match = of_match_node(match_node_table, np);
720 if (!match) {
721 selftest(0, "%s didn't match anything\n",
722 match_node_tests[i].path);
723 continue;
724 }
725
726 if (strcmp(match->data, match_node_tests[i].data) != 0) {
727 selftest(0, "%s got wrong match. expected %s, got %s\n",
728 match_node_tests[i].path, match_node_tests[i].data,
729 (const char *)match->data);
730 continue;
731 }
732 selftest(1, "passed");
733 }
734}
735
Grant Likely851da972014-11-04 13:14:13 +0000736struct device test_bus = {
737 .init_name = "unittest-bus",
738};
Rob Herring82c0f582014-04-23 17:57:40 -0500739static void __init of_selftest_platform_populate(void)
740{
Grant Likely851da972014-11-04 13:14:13 +0000741 int irq, rc;
742 struct device_node *np, *child, *grandchild;
Rob Herring82c0f582014-04-23 17:57:40 -0500743 struct platform_device *pdev;
Rob Herringfb2caa52014-05-13 10:07:54 -0500744 struct of_device_id match[] = {
745 { .compatible = "test-device", },
746 {}
747 };
Rob Herring82c0f582014-04-23 17:57:40 -0500748
749 np = of_find_node_by_path("/testcase-data");
750 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
751
752 /* Test that a missing irq domain returns -EPROBE_DEFER */
753 np = of_find_node_by_path("/testcase-data/testcase-device1");
754 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500755 selftest(pdev, "device 1 creation failed\n");
756
Rob Herring82c0f582014-04-23 17:57:40 -0500757 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500758 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500759
760 /* Test that a parsing failure does not return -EPROBE_DEFER */
761 np = of_find_node_by_path("/testcase-data/testcase-device2");
762 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500763 selftest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500764 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500765 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500766
Grant Likely851da972014-11-04 13:14:13 +0000767 if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"),
768 "No testcase data in device tree\n"));
Rob Herringfb2caa52014-05-13 10:07:54 -0500769 return;
Grant Likely851da972014-11-04 13:14:13 +0000770
771 if (selftest(!(rc = device_register(&test_bus)),
772 "testbus registration failed; rc=%i\n", rc));
773 return;
Rob Herringfb2caa52014-05-13 10:07:54 -0500774
775 for_each_child_of_node(np, child) {
Grant Likely851da972014-11-04 13:14:13 +0000776 of_platform_populate(child, match, NULL, &test_bus);
Rob Herringfb2caa52014-05-13 10:07:54 -0500777 for_each_child_of_node(child, grandchild)
778 selftest(of_find_device_by_node(grandchild),
779 "Could not create device for node '%s'\n",
780 grandchild->name);
781 }
Grant Likely851da972014-11-04 13:14:13 +0000782
783 of_platform_depopulate(&test_bus);
784 for_each_child_of_node(np, child) {
785 for_each_child_of_node(child, grandchild)
786 selftest(!of_find_device_by_node(grandchild),
787 "device didn't get destroyed '%s'\n",
788 grandchild->name);
789 }
790
791 device_unregister(&test_bus);
792 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -0500793}
794
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700795/**
796 * update_node_properties - adds the properties
797 * of np into dup node (present in live tree) and
798 * updates parent of children of np to dup.
799 *
800 * @np: node already present in live tree
801 * @dup: node present in live tree to be updated
802 */
803static void update_node_properties(struct device_node *np,
804 struct device_node *dup)
805{
806 struct property *prop;
807 struct device_node *child;
808
809 for_each_property_of_node(np, prop)
810 of_add_property(dup, prop);
811
812 for_each_child_of_node(np, child)
813 child->parent = dup;
814}
815
816/**
817 * attach_node_and_children - attaches nodes
818 * and its children to live tree
819 *
820 * @np: Node to attach to live tree
821 */
822static int attach_node_and_children(struct device_node *np)
823{
Grant Likely5063e252014-10-03 16:28:27 +0100824 struct device_node *next, *dup, *child;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800825 unsigned long flags;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700826
Grant Likely5063e252014-10-03 16:28:27 +0100827 dup = of_find_node_by_path(np->full_name);
828 if (dup) {
829 update_node_properties(np, dup);
830 return 0;
831 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700832
Grant Likely5063e252014-10-03 16:28:27 +0100833 /* Children of the root need to be remembered for removal */
834 if (np->parent == of_root) {
Grant Likelye66c98c2014-10-01 16:57:07 +0100835 if (WARN_ON(last_node_index >= NO_OF_NODES))
836 return -EINVAL;
Grant Likely5063e252014-10-03 16:28:27 +0100837 nodes[last_node_index++] = np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700838 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700839
Grant Likely5063e252014-10-03 16:28:27 +0100840 child = np->child;
841 np->child = NULL;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800842
843 mutex_lock(&of_mutex);
844 raw_spin_lock_irqsave(&devtree_lock, flags);
845 np->sibling = np->parent->child;
846 np->parent->child = np;
847 of_node_clear_flag(np, OF_DETACHED);
848 raw_spin_unlock_irqrestore(&devtree_lock, flags);
849
850 __of_attach_node_sysfs(np);
851 mutex_unlock(&of_mutex);
852
Grant Likely5063e252014-10-03 16:28:27 +0100853 while (child) {
854 next = child->sibling;
855 attach_node_and_children(child);
856 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700857 }
858
859 return 0;
860}
861
862/**
863 * selftest_data_add - Reads, copies data from
864 * linked tree and attaches it to the live tree
865 */
866static int __init selftest_data_add(void)
867{
868 void *selftest_data;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700869 struct device_node *selftest_data_node, *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700870 extern uint8_t __dtb_testcases_begin[];
871 extern uint8_t __dtb_testcases_end[];
872 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +0100873 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700874
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700875 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700876 pr_warn("%s: No testcase data to attach; not running tests\n",
877 __func__);
878 return -ENODATA;
879 }
880
881 /* creating copy */
882 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
883
884 if (!selftest_data) {
885 pr_warn("%s: Failed to allocate memory for selftest_data; "
886 "not running tests\n", __func__);
887 return -ENOMEM;
888 }
889 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700890 if (!selftest_data_node) {
891 pr_warn("%s: No tree to attach; not running tests\n", __func__);
892 return -ENODATA;
893 }
Grant Likely2eb46da2014-10-02 14:36:46 +0100894 of_node_set_flag(selftest_data_node, OF_DETACHED);
895 rc = of_resolve_phandles(selftest_data_node);
896 if (rc) {
897 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
898 return -EINVAL;
899 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700900
Grant Likely5063e252014-10-03 16:28:27 +0100901 if (!of_root) {
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700902 /* enabling flag for removing nodes */
903 selftest_live_tree = true;
Grant Likely5063e252014-10-03 16:28:27 +0100904 of_root = selftest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700905
906 for_each_of_allnodes(np)
907 __of_attach_node_sysfs(np);
908 of_aliases = of_find_node_by_path("/aliases");
909 of_chosen = of_find_node_by_path("/chosen");
910 return 0;
911 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700912
913 /* attach the sub-tree to live tree */
Grant Likely5063e252014-10-03 16:28:27 +0100914 np = selftest_data_node->child;
915 while (np) {
916 struct device_node *next = np->sibling;
917 np->parent = of_root;
918 attach_node_and_children(np);
919 np = next;
920 }
921 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700922}
923
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200924#ifdef CONFIG_OF_OVERLAY
925
926static int selftest_probe(struct platform_device *pdev)
927{
928 struct device *dev = &pdev->dev;
929 struct device_node *np = dev->of_node;
930
931 if (np == NULL) {
932 dev_err(dev, "No OF data for device\n");
933 return -EINVAL;
934
935 }
936
937 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
938 return 0;
939}
940
941static int selftest_remove(struct platform_device *pdev)
942{
943 struct device *dev = &pdev->dev;
944 struct device_node *np = dev->of_node;
945
946 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
947 return 0;
948}
949
950static struct of_device_id selftest_match[] = {
951 { .compatible = "selftest", },
952 {},
953};
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200954
955static struct platform_driver selftest_driver = {
956 .probe = selftest_probe,
957 .remove = selftest_remove,
958 .driver = {
959 .name = "selftest",
960 .owner = THIS_MODULE,
961 .of_match_table = of_match_ptr(selftest_match),
962 },
963};
964
965/* get the platform device instantiated at the path */
966static struct platform_device *of_path_to_platform_device(const char *path)
967{
968 struct device_node *np;
969 struct platform_device *pdev;
970
971 np = of_find_node_by_path(path);
972 if (np == NULL)
973 return NULL;
974
975 pdev = of_find_device_by_node(np);
976 of_node_put(np);
977
978 return pdev;
979}
980
981/* find out if a platform device exists at that path */
982static int of_path_platform_device_exists(const char *path)
983{
984 struct platform_device *pdev;
985
986 pdev = of_path_to_platform_device(path);
987 platform_device_put(pdev);
988 return pdev != NULL;
989}
990
991static const char *selftest_path(int nr)
992{
993 static char buf[256];
994
995 snprintf(buf, sizeof(buf) - 1,
996 "/testcase-data/overlay-node/test-bus/test-selftest%d", nr);
997 buf[sizeof(buf) - 1] = '\0';
998
999 return buf;
1000}
1001
1002static const char *overlay_path(int nr)
1003{
1004 static char buf[256];
1005
1006 snprintf(buf, sizeof(buf) - 1,
1007 "/testcase-data/overlay%d", nr);
1008 buf[sizeof(buf) - 1] = '\0';
1009
1010 return buf;
1011}
1012
1013static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1014
1015static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr,
1016 int *overlay_id)
1017{
1018 struct device_node *np = NULL;
1019 int ret, id = -1;
1020
1021 np = of_find_node_by_path(overlay_path(overlay_nr));
1022 if (np == NULL) {
1023 selftest(0, "could not find overlay node @\"%s\"\n",
1024 overlay_path(overlay_nr));
1025 ret = -EINVAL;
1026 goto out;
1027 }
1028
1029 ret = of_overlay_create(np);
1030 if (ret < 0) {
1031 selftest(0, "could not create overlay from \"%s\"\n",
1032 overlay_path(overlay_nr));
1033 goto out;
1034 }
1035 id = ret;
1036
1037 ret = 0;
1038
1039out:
1040 of_node_put(np);
1041
1042 if (overlay_id)
1043 *overlay_id = id;
1044
1045 return ret;
1046}
1047
1048/* apply an overlay while checking before and after states */
1049static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
1050 int before, int after)
1051{
1052 int ret;
1053
1054 /* selftest device must not be in before state */
1055 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1056 != before) {
1057 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1058 overlay_path(overlay_nr),
1059 selftest_path(selftest_nr),
1060 !before ? "enabled" : "disabled");
1061 return -EINVAL;
1062 }
1063
1064 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL);
1065 if (ret != 0) {
1066 /* of_selftest_apply_overlay already called selftest() */
1067 return ret;
1068 }
1069
1070 /* selftest device must be to set to after state */
1071 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1072 != after) {
1073 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1074 overlay_path(overlay_nr),
1075 selftest_path(selftest_nr),
1076 !after ? "enabled" : "disabled");
1077 return -EINVAL;
1078 }
1079
1080 return 0;
1081}
1082
1083/* apply an overlay and then revert it while checking before, after states */
1084static int of_selftest_apply_revert_overlay_check(int overlay_nr,
1085 int selftest_nr, int before, int after)
1086{
1087 int ret, ov_id;
1088
1089 /* selftest device must be in before state */
1090 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1091 != before) {
1092 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1093 overlay_path(overlay_nr),
1094 selftest_path(selftest_nr),
1095 !before ? "enabled" : "disabled");
1096 return -EINVAL;
1097 }
1098
1099 /* apply the overlay */
1100 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id);
1101 if (ret != 0) {
1102 /* of_selftest_apply_overlay already called selftest() */
1103 return ret;
1104 }
1105
1106 /* selftest device must be in after state */
1107 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1108 != after) {
1109 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1110 overlay_path(overlay_nr),
1111 selftest_path(selftest_nr),
1112 !after ? "enabled" : "disabled");
1113 return -EINVAL;
1114 }
1115
1116 ret = of_overlay_destroy(ov_id);
1117 if (ret != 0) {
1118 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1119 overlay_path(overlay_nr),
1120 selftest_path(selftest_nr));
1121 return ret;
1122 }
1123
1124 /* selftest device must be again in before state */
1125 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1126 != before) {
1127 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1128 overlay_path(overlay_nr),
1129 selftest_path(selftest_nr),
1130 !before ? "enabled" : "disabled");
1131 return -EINVAL;
1132 }
1133
1134 return 0;
1135}
1136
1137/* test activation of device */
1138static void of_selftest_overlay_0(void)
1139{
1140 int ret;
1141
1142 /* device should enable */
1143 ret = of_selftest_apply_overlay_check(0, 0, 0, 1);
1144 if (ret != 0)
1145 return;
1146
1147 selftest(1, "overlay test %d passed\n", 0);
1148}
1149
1150/* test deactivation of device */
1151static void of_selftest_overlay_1(void)
1152{
1153 int ret;
1154
1155 /* device should disable */
1156 ret = of_selftest_apply_overlay_check(1, 1, 1, 0);
1157 if (ret != 0)
1158 return;
1159
1160 selftest(1, "overlay test %d passed\n", 1);
1161}
1162
1163/* test activation of device */
1164static void of_selftest_overlay_2(void)
1165{
1166 int ret;
1167
1168 /* device should enable */
1169 ret = of_selftest_apply_overlay_check(2, 2, 0, 1);
1170 if (ret != 0)
1171 return;
1172
1173 selftest(1, "overlay test %d passed\n", 2);
1174}
1175
1176/* test deactivation of device */
1177static void of_selftest_overlay_3(void)
1178{
1179 int ret;
1180
1181 /* device should disable */
1182 ret = of_selftest_apply_overlay_check(3, 3, 1, 0);
1183 if (ret != 0)
1184 return;
1185
1186 selftest(1, "overlay test %d passed\n", 3);
1187}
1188
1189/* test activation of a full device node */
1190static void of_selftest_overlay_4(void)
1191{
1192 int ret;
1193
1194 /* device should disable */
1195 ret = of_selftest_apply_overlay_check(4, 4, 0, 1);
1196 if (ret != 0)
1197 return;
1198
1199 selftest(1, "overlay test %d passed\n", 4);
1200}
1201
1202/* test overlay apply/revert sequence */
1203static void of_selftest_overlay_5(void)
1204{
1205 int ret;
1206
1207 /* device should disable */
1208 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1);
1209 if (ret != 0)
1210 return;
1211
1212 selftest(1, "overlay test %d passed\n", 5);
1213}
1214
1215/* test overlay application in sequence */
1216static void of_selftest_overlay_6(void)
1217{
1218 struct device_node *np;
1219 int ret, i, ov_id[2];
1220 int overlay_nr = 6, selftest_nr = 6;
1221 int before = 0, after = 1;
1222
1223 /* selftest device must be in before state */
1224 for (i = 0; i < 2; i++) {
1225 if (of_path_platform_device_exists(
1226 selftest_path(selftest_nr + i))
1227 != before) {
1228 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1229 overlay_path(overlay_nr + i),
1230 selftest_path(selftest_nr + i),
1231 !before ? "enabled" : "disabled");
1232 return;
1233 }
1234 }
1235
1236 /* apply the overlays */
1237 for (i = 0; i < 2; i++) {
1238
1239 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1240 if (np == NULL) {
1241 selftest(0, "could not find overlay node @\"%s\"\n",
1242 overlay_path(overlay_nr + i));
1243 return;
1244 }
1245
1246 ret = of_overlay_create(np);
1247 if (ret < 0) {
1248 selftest(0, "could not create overlay from \"%s\"\n",
1249 overlay_path(overlay_nr + i));
1250 return;
1251 }
1252 ov_id[i] = ret;
1253 }
1254
1255 for (i = 0; i < 2; i++) {
1256 /* selftest device must be in after state */
1257 if (of_path_platform_device_exists(
1258 selftest_path(selftest_nr + i))
1259 != after) {
1260 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1261 overlay_path(overlay_nr + i),
1262 selftest_path(selftest_nr + i),
1263 !after ? "enabled" : "disabled");
1264 return;
1265 }
1266 }
1267
1268 for (i = 1; i >= 0; i--) {
1269 ret = of_overlay_destroy(ov_id[i]);
1270 if (ret != 0) {
1271 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1272 overlay_path(overlay_nr + i),
1273 selftest_path(selftest_nr + i));
1274 return;
1275 }
1276 }
1277
1278 for (i = 0; i < 2; i++) {
1279 /* selftest device must be again in before state */
1280 if (of_path_platform_device_exists(
1281 selftest_path(selftest_nr + i))
1282 != before) {
1283 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1284 overlay_path(overlay_nr + i),
1285 selftest_path(selftest_nr + i),
1286 !before ? "enabled" : "disabled");
1287 return;
1288 }
1289 }
1290
1291 selftest(1, "overlay test %d passed\n", 6);
1292}
1293
1294/* test overlay application in sequence */
1295static void of_selftest_overlay_8(void)
1296{
1297 struct device_node *np;
1298 int ret, i, ov_id[2];
1299 int overlay_nr = 8, selftest_nr = 8;
1300
1301 /* we don't care about device state in this test */
1302
1303 /* apply the overlays */
1304 for (i = 0; i < 2; i++) {
1305
1306 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1307 if (np == NULL) {
1308 selftest(0, "could not find overlay node @\"%s\"\n",
1309 overlay_path(overlay_nr + i));
1310 return;
1311 }
1312
1313 ret = of_overlay_create(np);
1314 if (ret < 0) {
1315 selftest(0, "could not create overlay from \"%s\"\n",
1316 overlay_path(overlay_nr + i));
1317 return;
1318 }
1319 ov_id[i] = ret;
1320 }
1321
1322 /* now try to remove first overlay (it should fail) */
1323 ret = of_overlay_destroy(ov_id[0]);
1324 if (ret == 0) {
1325 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1326 overlay_path(overlay_nr + 0),
1327 selftest_path(selftest_nr));
1328 return;
1329 }
1330
1331 /* removing them in order should work */
1332 for (i = 1; i >= 0; i--) {
1333 ret = of_overlay_destroy(ov_id[i]);
1334 if (ret != 0) {
1335 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1336 overlay_path(overlay_nr + i),
1337 selftest_path(selftest_nr));
1338 return;
1339 }
1340 }
1341
1342 selftest(1, "overlay test %d passed\n", 8);
1343}
1344
1345static void __init of_selftest_overlay(void)
1346{
1347 struct device_node *bus_np = NULL;
1348 int ret;
1349
1350 ret = platform_driver_register(&selftest_driver);
1351 if (ret != 0) {
1352 selftest(0, "could not register selftest driver\n");
1353 goto out;
1354 }
1355
1356 bus_np = of_find_node_by_path(bus_path);
1357 if (bus_np == NULL) {
1358 selftest(0, "could not find bus_path \"%s\"\n", bus_path);
1359 goto out;
1360 }
1361
1362 ret = of_platform_populate(bus_np, of_default_bus_match_table,
1363 NULL, NULL);
1364 if (ret != 0) {
1365 selftest(0, "could not populate bus @ \"%s\"\n", bus_path);
1366 goto out;
1367 }
1368
1369 if (!of_path_platform_device_exists(selftest_path(100))) {
1370 selftest(0, "could not find selftest0 @ \"%s\"\n",
1371 selftest_path(100));
1372 goto out;
1373 }
1374
1375 if (of_path_platform_device_exists(selftest_path(101))) {
1376 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1377 selftest_path(101));
1378 goto out;
1379 }
1380
1381 selftest(1, "basic infrastructure of overlays passed");
1382
1383 /* tests in sequence */
1384 of_selftest_overlay_0();
1385 of_selftest_overlay_1();
1386 of_selftest_overlay_2();
1387 of_selftest_overlay_3();
1388 of_selftest_overlay_4();
1389 of_selftest_overlay_5();
1390 of_selftest_overlay_6();
1391 of_selftest_overlay_8();
1392
1393out:
1394 of_node_put(bus_np);
1395}
1396
1397#else
1398static inline void __init of_selftest_overlay(void) { }
1399#endif
1400
Grant Likely53a42092011-12-12 09:25:57 -07001401static int __init of_selftest(void)
1402{
1403 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001404 int res;
1405
1406 /* adding data for selftest */
1407 res = selftest_data_add();
1408 if (res)
1409 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00001410 if (!of_aliases)
1411 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07001412
1413 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1414 if (!np) {
1415 pr_info("No testcase data in device tree; not running tests\n");
1416 return 0;
1417 }
1418 of_node_put(np);
1419
1420 pr_info("start of selftest - you will see error messages\n");
Grant Likelyf2051d62014-10-01 17:40:22 +01001421 of_selftest_check_tree_linkage();
Grant Likely841ec212014-10-02 13:09:15 +01001422 of_selftest_check_phandles();
Grant Likelyae91ff72014-03-14 13:53:10 +00001423 of_selftest_find_node_by_name();
Grant Likely7e66c5c2013-11-15 17:19:09 +00001424 of_selftest_dynamic();
Grant Likely53a42092011-12-12 09:25:57 -07001425 of_selftest_parse_phandle_with_args();
Grant Likelya87fa1d2014-11-03 15:15:35 +00001426 of_selftest_property_string();
Pantelis Antoniou69843392014-07-04 19:58:47 +03001427 of_selftest_property_copy();
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001428 of_selftest_changeset();
Grant Likelya9f10ca2013-10-11 22:04:23 +01001429 of_selftest_parse_interrupts();
Grant Likely79d97012013-09-19 16:47:37 -05001430 of_selftest_parse_interrupts_extended();
Grant Likely1f42e5d2014-02-18 21:38:55 +00001431 of_selftest_match_node();
Rob Herring82c0f582014-04-23 17:57:40 -05001432 of_selftest_platform_populate();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001433 of_selftest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001434
Grant Likelyf2051d62014-10-01 17:40:22 +01001435 /* Double check linkage after removing testcase data */
1436 of_selftest_check_tree_linkage();
1437
1438 pr_info("end of selftest - %i passed, %i failed\n",
1439 selftest_results.passed, selftest_results.failed);
1440
Grant Likely53a42092011-12-12 09:25:57 -07001441 return 0;
1442}
1443late_initcall(of_selftest);