blob: bf7d99317a947f206506888f19462f4a859e9754 [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>
20
Pantelis Antoniou69843392014-07-04 19:58:47 +030021#include "of_private.h"
22
Grant Likelya9f10ca2013-10-11 22:04:23 +010023static struct selftest_results {
24 int passed;
25 int failed;
26} selftest_results;
27
Grant Likely2eb46da2014-10-02 14:36:46 +010028#define NO_OF_NODES 3
Gaurav Minochaae9304c2014-07-16 23:09:39 -070029static struct device_node *nodes[NO_OF_NODES];
30static int last_node_index;
Gaurav Minochab951f9d2014-07-26 12:48:50 -070031static bool selftest_live_tree;
Gaurav Minochaae9304c2014-07-16 23:09:39 -070032
Grant Likely53a42092011-12-12 09:25:57 -070033#define selftest(result, fmt, ...) { \
Grant Likelycabb7d52013-02-12 21:19:37 +000034 if (!(result)) { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010035 selftest_results.failed++; \
36 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000037 } else { \
Grant Likelya9f10ca2013-10-11 22:04:23 +010038 selftest_results.passed++; \
39 pr_debug("pass %s():%i\n", __func__, __LINE__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000040 } \
Grant Likely53a42092011-12-12 09:25:57 -070041}
42
Grant Likelyae91ff72014-03-14 13:53:10 +000043static void __init of_selftest_find_node_by_name(void)
44{
45 struct device_node *np;
46
47 np = of_find_node_by_path("/testcase-data");
48 selftest(np && !strcmp("/testcase-data", np->full_name),
49 "find /testcase-data failed\n");
50 of_node_put(np);
51
52 /* Test if trailing '/' works */
53 np = of_find_node_by_path("/testcase-data/");
54 selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
55
56 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
57 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
58 "find /testcase-data/phandle-tests/consumer-a failed\n");
59 of_node_put(np);
60
61 np = of_find_node_by_path("testcase-alias");
62 selftest(np && !strcmp("/testcase-data", np->full_name),
63 "find testcase-alias failed\n");
64 of_node_put(np);
65
66 /* Test if trailing '/' works on aliases */
67 np = of_find_node_by_path("testcase-alias/");
68 selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
69
70 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
71 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
72 "find testcase-alias/phandle-tests/consumer-a failed\n");
73 of_node_put(np);
74
75 np = of_find_node_by_path("/testcase-data/missing-path");
76 selftest(!np, "non-existent path returned node %s\n", np->full_name);
77 of_node_put(np);
78
79 np = of_find_node_by_path("missing-alias");
80 selftest(!np, "non-existent alias returned node %s\n", np->full_name);
81 of_node_put(np);
82
83 np = of_find_node_by_path("testcase-alias/missing-path");
84 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
85 of_node_put(np);
86}
87
Grant Likely7e66c5c2013-11-15 17:19:09 +000088static void __init of_selftest_dynamic(void)
89{
90 struct device_node *np;
91 struct property *prop;
92
93 np = of_find_node_by_path("/testcase-data");
94 if (!np) {
95 pr_err("missing testcase data\n");
96 return;
97 }
98
99 /* Array of 4 properties for the purpose of testing */
100 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
101 if (!prop) {
102 selftest(0, "kzalloc() failed\n");
103 return;
104 }
105
106 /* Add a new property - should pass*/
107 prop->name = "new-property";
108 prop->value = "new-property-data";
109 prop->length = strlen(prop->value);
110 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
111
112 /* Try to add an existing property - should fail */
113 prop++;
114 prop->name = "new-property";
115 prop->value = "new-property-data-should-fail";
116 prop->length = strlen(prop->value);
117 selftest(of_add_property(np, prop) != 0,
118 "Adding an existing property should have failed\n");
119
120 /* Try to modify an existing property - should pass */
121 prop->value = "modify-property-data-should-pass";
122 prop->length = strlen(prop->value);
123 selftest(of_update_property(np, prop) == 0,
124 "Updating an existing property should have passed\n");
125
126 /* Try to modify non-existent property - should pass*/
127 prop++;
128 prop->name = "modify-property";
129 prop->value = "modify-missing-property-data-should-pass";
130 prop->length = strlen(prop->value);
131 selftest(of_update_property(np, prop) == 0,
132 "Updating a missing property should have passed\n");
133
134 /* Remove property - should pass */
135 selftest(of_remove_property(np, prop) == 0,
136 "Removing a property should have passed\n");
137
138 /* Adding very large property - should pass */
139 prop++;
140 prop->name = "large-property-PAGE_SIZEx8";
141 prop->length = PAGE_SIZE * 8;
142 prop->value = kzalloc(prop->length, GFP_KERNEL);
143 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
144 if (prop->value)
145 selftest(of_add_property(np, prop) == 0,
146 "Adding a large property should have passed\n");
147}
148
Grant Likelyf2051d62014-10-01 17:40:22 +0100149static int __init of_selftest_check_node_linkage(struct device_node *np)
150{
Grant Likely5063e252014-10-03 16:28:27 +0100151 struct device_node *child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100152 int count = 0, rc;
153
154 for_each_child_of_node(np, child) {
155 if (child->parent != np) {
156 pr_err("Child node %s links to wrong parent %s\n",
157 child->name, np->name);
158 return -EINVAL;
159 }
160
Grant Likelyf2051d62014-10-01 17:40:22 +0100161 rc = of_selftest_check_node_linkage(child);
162 if (rc < 0)
163 return rc;
164 count += rc;
165 }
166
167 return count + 1;
168}
169
170static void __init of_selftest_check_tree_linkage(void)
171{
172 struct device_node *np;
173 int allnode_count = 0, child_count;
174
Grant Likely5063e252014-10-03 16:28:27 +0100175 if (!of_root)
Grant Likelyf2051d62014-10-01 17:40:22 +0100176 return;
177
178 for_each_of_allnodes(np)
179 allnode_count++;
Grant Likely5063e252014-10-03 16:28:27 +0100180 child_count = of_selftest_check_node_linkage(of_root);
Grant Likelyf2051d62014-10-01 17:40:22 +0100181
182 selftest(child_count > 0, "Device node data structure is corrupted\n");
183 selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
184 "sibling lists size (%i)\n", allnode_count, child_count);
185 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
186}
187
Grant Likely841ec212014-10-02 13:09:15 +0100188struct node_hash {
189 struct hlist_node node;
190 struct device_node *np;
191};
192
Grant Likely2118f4b2014-10-07 11:30:31 +0100193static DEFINE_HASHTABLE(phandle_ht, 8);
Grant Likely841ec212014-10-02 13:09:15 +0100194static void __init of_selftest_check_phandles(void)
195{
196 struct device_node *np;
197 struct node_hash *nh;
198 struct hlist_node *tmp;
199 int i, dup_count = 0, phandle_count = 0;
Grant Likely841ec212014-10-02 13:09:15 +0100200
Grant Likely841ec212014-10-02 13:09:15 +0100201 for_each_of_allnodes(np) {
202 if (!np->phandle)
203 continue;
204
Grant Likely2118f4b2014-10-07 11:30:31 +0100205 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100206 if (nh->np->phandle == np->phandle) {
207 pr_info("Duplicate phandle! %i used by %s and %s\n",
208 np->phandle, nh->np->full_name, np->full_name);
209 dup_count++;
210 break;
211 }
212 }
213
214 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
215 if (WARN_ON(!nh))
216 return;
217
218 nh->np = np;
Grant Likely2118f4b2014-10-07 11:30:31 +0100219 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100220 phandle_count++;
221 }
222 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
223 dup_count, phandle_count);
224
225 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100226 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100227 hash_del(&nh->node);
228 kfree(nh);
229 }
230}
231
Grant Likely53a42092011-12-12 09:25:57 -0700232static void __init of_selftest_parse_phandle_with_args(void)
233{
234 struct device_node *np;
235 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000236 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700237
Grant Likely53a42092011-12-12 09:25:57 -0700238 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
239 if (!np) {
240 pr_err("missing testcase data\n");
241 return;
242 }
243
Grant Likelybd69f732013-02-10 22:57:21 +0000244 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
245 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
246
Grant Likelyf7f951c2013-02-12 17:41:22 +0000247 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700248 bool passed = true;
249 rc = of_parse_phandle_with_args(np, "phandle-list",
250 "#phandle-cells", i, &args);
251
252 /* Test the values from tests-phandle.dtsi */
253 switch (i) {
254 case 0:
255 passed &= !rc;
256 passed &= (args.args_count == 1);
257 passed &= (args.args[0] == (i + 1));
258 break;
259 case 1:
260 passed &= !rc;
261 passed &= (args.args_count == 2);
262 passed &= (args.args[0] == (i + 1));
263 passed &= (args.args[1] == 0);
264 break;
265 case 2:
266 passed &= (rc == -ENOENT);
267 break;
268 case 3:
269 passed &= !rc;
270 passed &= (args.args_count == 3);
271 passed &= (args.args[0] == (i + 1));
272 passed &= (args.args[1] == 4);
273 passed &= (args.args[2] == 3);
274 break;
275 case 4:
276 passed &= !rc;
277 passed &= (args.args_count == 2);
278 passed &= (args.args[0] == (i + 1));
279 passed &= (args.args[1] == 100);
280 break;
281 case 5:
282 passed &= !rc;
283 passed &= (args.args_count == 0);
284 break;
285 case 6:
286 passed &= !rc;
287 passed &= (args.args_count == 1);
288 passed &= (args.args[0] == (i + 1));
289 break;
290 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000291 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700292 break;
293 default:
294 passed = false;
295 }
296
Grant Likelycabb7d52013-02-12 21:19:37 +0000297 selftest(passed, "index %i - data error on node %s rc=%i\n",
298 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700299 }
300
301 /* Check for missing list property */
302 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
303 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000304 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000305 rc = of_count_phandle_with_args(np, "phandle-list-missing",
306 "#phandle-cells");
307 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700308
309 /* Check for missing cells property */
310 rc = of_parse_phandle_with_args(np, "phandle-list",
311 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000312 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000313 rc = of_count_phandle_with_args(np, "phandle-list",
314 "#phandle-cells-missing");
315 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700316
317 /* Check for bad phandle in list */
318 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
319 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000320 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000321 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
322 "#phandle-cells");
323 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700324
325 /* Check for incorrectly formed argument list */
326 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
327 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000328 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000329 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
330 "#phandle-cells");
331 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700332}
333
Grant Likelya87fa1d2014-11-03 15:15:35 +0000334static void __init of_selftest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700335{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000336 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700337 struct device_node *np;
338 int rc;
339
Grant Likely7aff0fe2011-12-12 09:25:58 -0700340 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
341 if (!np) {
342 pr_err("No testcase data in device tree\n");
343 return;
344 }
345
346 rc = of_property_match_string(np, "phandle-list-names", "first");
347 selftest(rc == 0, "first expected:0 got:%i\n", rc);
348 rc = of_property_match_string(np, "phandle-list-names", "second");
349 selftest(rc == 1, "second expected:0 got:%i\n", rc);
350 rc = of_property_match_string(np, "phandle-list-names", "third");
351 selftest(rc == 2, "third expected:0 got:%i\n", rc);
352 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000353 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700354 rc = of_property_match_string(np, "missing-property", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000355 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700356 rc = of_property_match_string(np, "empty-property", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000357 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700358 rc = of_property_match_string(np, "unterminated-string", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000359 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
360
361 /* of_property_count_strings() tests */
362 rc = of_property_count_strings(np, "string-property");
363 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
364 rc = of_property_count_strings(np, "phandle-list-names");
365 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
366 rc = of_property_count_strings(np, "unterminated-string");
367 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
368 rc = of_property_count_strings(np, "unterminated-string-list");
369 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
370
371 /* of_property_read_string_index() tests */
372 rc = of_property_read_string_index(np, "string-property", 0, strings);
373 selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
374 strings[0] = NULL;
375 rc = of_property_read_string_index(np, "string-property", 1, strings);
376 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
377 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
378 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
379 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
380 selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
381 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
382 selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
383 strings[0] = NULL;
384 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
385 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
386 strings[0] = NULL;
387 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
388 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
389 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
390 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
391 strings[0] = NULL;
392 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
393 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
394 strings[1] = NULL;
395
396 /* of_property_read_string_array() tests */
397 rc = of_property_read_string_array(np, "string-property", strings, 4);
398 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
399 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
400 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
401 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
402 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
403 /* -- An incorrectly formed string should cause a failure */
404 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
405 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
406 /* -- parsing the correctly formed strings should still work: */
407 strings[2] = NULL;
408 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
409 selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
410 strings[1] = NULL;
411 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
412 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 -0700413}
414
Pantelis Antoniou69843392014-07-04 19:58:47 +0300415#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
416 (p1)->value && (p2)->value && \
417 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
418 !strcmp((p1)->name, (p2)->name))
419static void __init of_selftest_property_copy(void)
420{
421#ifdef CONFIG_OF_DYNAMIC
422 struct property p1 = { .name = "p1", .length = 0, .value = "" };
423 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
424 struct property *new;
425
426 new = __of_prop_dup(&p1, GFP_KERNEL);
427 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
428 kfree(new->value);
429 kfree(new->name);
430 kfree(new);
431
432 new = __of_prop_dup(&p2, GFP_KERNEL);
433 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
434 kfree(new->value);
435 kfree(new->name);
436 kfree(new);
437#endif
438}
439
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300440static void __init of_selftest_changeset(void)
441{
442#ifdef CONFIG_OF_DYNAMIC
443 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
444 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
445 struct property *ppremove;
446 struct device_node *n1, *n2, *n21, *nremove, *parent;
447 struct of_changeset chgset;
448
449 of_changeset_init(&chgset);
450 n1 = __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL);
451 selftest(n1, "testcase setup failure\n");
452 n2 = __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL);
453 selftest(n2, "testcase setup failure\n");
454 n21 = __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL);
455 selftest(n21, "testcase setup failure %p\n", n21);
456 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
457 selftest(nremove, "testcase setup failure\n");
458 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
459 selftest(ppadd, "testcase setup failure\n");
460 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
461 selftest(ppupdate, "testcase setup failure\n");
462 parent = nremove->parent;
463 n1->parent = parent;
464 n2->parent = parent;
465 n21->parent = n2;
466 n2->child = n21;
467 ppremove = of_find_property(parent, "prop-remove", NULL);
468 selftest(ppremove, "failed to find removal prop");
469
470 of_changeset_init(&chgset);
471 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
472 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
473 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
474 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
475 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
476 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
477 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
478 mutex_lock(&of_mutex);
479 selftest(!of_changeset_apply(&chgset), "apply failed\n");
480 mutex_unlock(&of_mutex);
481
482 mutex_lock(&of_mutex);
483 selftest(!of_changeset_revert(&chgset), "revert failed\n");
484 mutex_unlock(&of_mutex);
485
486 of_changeset_destroy(&chgset);
487#endif
488}
489
Grant Likelya9f10ca2013-10-11 22:04:23 +0100490static void __init of_selftest_parse_interrupts(void)
491{
492 struct device_node *np;
493 struct of_phandle_args args;
494 int i, rc;
495
496 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
497 if (!np) {
498 pr_err("missing testcase data\n");
499 return;
500 }
501
502 for (i = 0; i < 4; i++) {
503 bool passed = true;
504 args.args_count = 0;
505 rc = of_irq_parse_one(np, i, &args);
506
507 passed &= !rc;
508 passed &= (args.args_count == 1);
509 passed &= (args.args[0] == (i + 1));
510
511 selftest(passed, "index %i - data error on node %s rc=%i\n",
512 i, args.np->full_name, rc);
513 }
514 of_node_put(np);
515
516 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
517 if (!np) {
518 pr_err("missing testcase data\n");
519 return;
520 }
521
522 for (i = 0; i < 4; i++) {
523 bool passed = true;
524 args.args_count = 0;
525 rc = of_irq_parse_one(np, i, &args);
526
527 /* Test the values from tests-phandle.dtsi */
528 switch (i) {
529 case 0:
530 passed &= !rc;
531 passed &= (args.args_count == 1);
532 passed &= (args.args[0] == 9);
533 break;
534 case 1:
535 passed &= !rc;
536 passed &= (args.args_count == 3);
537 passed &= (args.args[0] == 10);
538 passed &= (args.args[1] == 11);
539 passed &= (args.args[2] == 12);
540 break;
541 case 2:
542 passed &= !rc;
543 passed &= (args.args_count == 2);
544 passed &= (args.args[0] == 13);
545 passed &= (args.args[1] == 14);
546 break;
547 case 3:
548 passed &= !rc;
549 passed &= (args.args_count == 2);
550 passed &= (args.args[0] == 15);
551 passed &= (args.args[1] == 16);
552 break;
553 default:
554 passed = false;
555 }
556 selftest(passed, "index %i - data error on node %s rc=%i\n",
557 i, args.np->full_name, rc);
558 }
559 of_node_put(np);
560}
561
Grant Likely79d97012013-09-19 16:47:37 -0500562static void __init of_selftest_parse_interrupts_extended(void)
563{
564 struct device_node *np;
565 struct of_phandle_args args;
566 int i, rc;
567
568 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
569 if (!np) {
570 pr_err("missing testcase data\n");
571 return;
572 }
573
574 for (i = 0; i < 7; i++) {
575 bool passed = true;
576 rc = of_irq_parse_one(np, i, &args);
577
578 /* Test the values from tests-phandle.dtsi */
579 switch (i) {
580 case 0:
581 passed &= !rc;
582 passed &= (args.args_count == 1);
583 passed &= (args.args[0] == 1);
584 break;
585 case 1:
586 passed &= !rc;
587 passed &= (args.args_count == 3);
588 passed &= (args.args[0] == 2);
589 passed &= (args.args[1] == 3);
590 passed &= (args.args[2] == 4);
591 break;
592 case 2:
593 passed &= !rc;
594 passed &= (args.args_count == 2);
595 passed &= (args.args[0] == 5);
596 passed &= (args.args[1] == 6);
597 break;
598 case 3:
599 passed &= !rc;
600 passed &= (args.args_count == 1);
601 passed &= (args.args[0] == 9);
602 break;
603 case 4:
604 passed &= !rc;
605 passed &= (args.args_count == 3);
606 passed &= (args.args[0] == 10);
607 passed &= (args.args[1] == 11);
608 passed &= (args.args[2] == 12);
609 break;
610 case 5:
611 passed &= !rc;
612 passed &= (args.args_count == 2);
613 passed &= (args.args[0] == 13);
614 passed &= (args.args[1] == 14);
615 break;
616 case 6:
617 passed &= !rc;
618 passed &= (args.args_count == 1);
619 passed &= (args.args[0] == 15);
620 break;
621 default:
622 passed = false;
623 }
624
625 selftest(passed, "index %i - data error on node %s rc=%i\n",
626 i, args.np->full_name, rc);
627 }
628 of_node_put(np);
629}
630
Grant Likely1f42e5d2014-02-18 21:38:55 +0000631static struct of_device_id match_node_table[] = {
632 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
633 { .data = "B", .type = "type1", }, /* followed by type alone */
634
635 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
636 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
637 { .data = "Cc", .name = "name2", .type = "type2", },
638
639 { .data = "E", .compatible = "compat3" },
640 { .data = "G", .compatible = "compat2", },
641 { .data = "H", .compatible = "compat2", .name = "name5", },
642 { .data = "I", .compatible = "compat2", .type = "type1", },
643 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
644 { .data = "K", .compatible = "compat2", .name = "name9", },
645 {}
646};
647
648static struct {
649 const char *path;
650 const char *data;
651} match_node_tests[] = {
652 { .path = "/testcase-data/match-node/name0", .data = "A", },
653 { .path = "/testcase-data/match-node/name1", .data = "B", },
654 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
655 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
656 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
657 { .path = "/testcase-data/match-node/name3", .data = "E", },
658 { .path = "/testcase-data/match-node/name4", .data = "G", },
659 { .path = "/testcase-data/match-node/name5", .data = "H", },
660 { .path = "/testcase-data/match-node/name6", .data = "G", },
661 { .path = "/testcase-data/match-node/name7", .data = "I", },
662 { .path = "/testcase-data/match-node/name8", .data = "J", },
663 { .path = "/testcase-data/match-node/name9", .data = "K", },
664};
665
666static void __init of_selftest_match_node(void)
667{
668 struct device_node *np;
669 const struct of_device_id *match;
670 int i;
671
672 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
673 np = of_find_node_by_path(match_node_tests[i].path);
674 if (!np) {
675 selftest(0, "missing testcase node %s\n",
676 match_node_tests[i].path);
677 continue;
678 }
679
680 match = of_match_node(match_node_table, np);
681 if (!match) {
682 selftest(0, "%s didn't match anything\n",
683 match_node_tests[i].path);
684 continue;
685 }
686
687 if (strcmp(match->data, match_node_tests[i].data) != 0) {
688 selftest(0, "%s got wrong match. expected %s, got %s\n",
689 match_node_tests[i].path, match_node_tests[i].data,
690 (const char *)match->data);
691 continue;
692 }
693 selftest(1, "passed");
694 }
695}
696
Rob Herring82c0f582014-04-23 17:57:40 -0500697static void __init of_selftest_platform_populate(void)
698{
699 int irq;
Rob Herringfb2caa52014-05-13 10:07:54 -0500700 struct device_node *np, *child;
Rob Herring82c0f582014-04-23 17:57:40 -0500701 struct platform_device *pdev;
Rob Herringfb2caa52014-05-13 10:07:54 -0500702 struct of_device_id match[] = {
703 { .compatible = "test-device", },
704 {}
705 };
Rob Herring82c0f582014-04-23 17:57:40 -0500706
707 np = of_find_node_by_path("/testcase-data");
708 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
709
710 /* Test that a missing irq domain returns -EPROBE_DEFER */
711 np = of_find_node_by_path("/testcase-data/testcase-device1");
712 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500713 selftest(pdev, "device 1 creation failed\n");
714
Rob Herring82c0f582014-04-23 17:57:40 -0500715 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500716 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500717
718 /* Test that a parsing failure does not return -EPROBE_DEFER */
719 np = of_find_node_by_path("/testcase-data/testcase-device2");
720 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500721 selftest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500722 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500723 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500724
Rob Herringfb2caa52014-05-13 10:07:54 -0500725 np = of_find_node_by_path("/testcase-data/platform-tests");
726 if (!np) {
727 pr_err("No testcase data in device tree\n");
728 return;
729 }
730
731 for_each_child_of_node(np, child) {
732 struct device_node *grandchild;
733 of_platform_populate(child, match, NULL, NULL);
734 for_each_child_of_node(child, grandchild)
735 selftest(of_find_device_by_node(grandchild),
736 "Could not create device for node '%s'\n",
737 grandchild->name);
738 }
Rob Herring82c0f582014-04-23 17:57:40 -0500739}
740
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700741/**
742 * update_node_properties - adds the properties
743 * of np into dup node (present in live tree) and
744 * updates parent of children of np to dup.
745 *
746 * @np: node already present in live tree
747 * @dup: node present in live tree to be updated
748 */
749static void update_node_properties(struct device_node *np,
750 struct device_node *dup)
751{
752 struct property *prop;
753 struct device_node *child;
754
755 for_each_property_of_node(np, prop)
756 of_add_property(dup, prop);
757
758 for_each_child_of_node(np, child)
759 child->parent = dup;
760}
761
762/**
763 * attach_node_and_children - attaches nodes
764 * and its children to live tree
765 *
766 * @np: Node to attach to live tree
767 */
768static int attach_node_and_children(struct device_node *np)
769{
Grant Likely5063e252014-10-03 16:28:27 +0100770 struct device_node *next, *dup, *child;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700771
Grant Likely5063e252014-10-03 16:28:27 +0100772 dup = of_find_node_by_path(np->full_name);
773 if (dup) {
774 update_node_properties(np, dup);
775 return 0;
776 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700777
Grant Likely5063e252014-10-03 16:28:27 +0100778 /* Children of the root need to be remembered for removal */
779 if (np->parent == of_root) {
Grant Likelye66c98c2014-10-01 16:57:07 +0100780 if (WARN_ON(last_node_index >= NO_OF_NODES))
781 return -EINVAL;
Grant Likely5063e252014-10-03 16:28:27 +0100782 nodes[last_node_index++] = np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700783 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700784
Grant Likely5063e252014-10-03 16:28:27 +0100785 child = np->child;
786 np->child = NULL;
787 np->sibling = NULL;
788 of_attach_node(np);
789 while (child) {
790 next = child->sibling;
791 attach_node_and_children(child);
792 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700793 }
794
795 return 0;
796}
797
798/**
799 * selftest_data_add - Reads, copies data from
800 * linked tree and attaches it to the live tree
801 */
802static int __init selftest_data_add(void)
803{
804 void *selftest_data;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700805 struct device_node *selftest_data_node, *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700806 extern uint8_t __dtb_testcases_begin[];
807 extern uint8_t __dtb_testcases_end[];
808 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +0100809 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700810
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700811 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700812 pr_warn("%s: No testcase data to attach; not running tests\n",
813 __func__);
814 return -ENODATA;
815 }
816
817 /* creating copy */
818 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
819
820 if (!selftest_data) {
821 pr_warn("%s: Failed to allocate memory for selftest_data; "
822 "not running tests\n", __func__);
823 return -ENOMEM;
824 }
825 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700826 if (!selftest_data_node) {
827 pr_warn("%s: No tree to attach; not running tests\n", __func__);
828 return -ENODATA;
829 }
Grant Likely2eb46da2014-10-02 14:36:46 +0100830 of_node_set_flag(selftest_data_node, OF_DETACHED);
831 rc = of_resolve_phandles(selftest_data_node);
832 if (rc) {
833 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
834 return -EINVAL;
835 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700836
Grant Likely5063e252014-10-03 16:28:27 +0100837 if (!of_root) {
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700838 /* enabling flag for removing nodes */
839 selftest_live_tree = true;
Grant Likely5063e252014-10-03 16:28:27 +0100840 of_root = selftest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700841
842 for_each_of_allnodes(np)
843 __of_attach_node_sysfs(np);
844 of_aliases = of_find_node_by_path("/aliases");
845 of_chosen = of_find_node_by_path("/chosen");
846 return 0;
847 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700848
849 /* attach the sub-tree to live tree */
Grant Likely5063e252014-10-03 16:28:27 +0100850 np = selftest_data_node->child;
851 while (np) {
852 struct device_node *next = np->sibling;
853 np->parent = of_root;
854 attach_node_and_children(np);
855 np = next;
856 }
857 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700858}
859
860/**
861 * detach_node_and_children - detaches node
862 * and its children from live tree
863 *
864 * @np: Node to detach from live tree
865 */
866static void detach_node_and_children(struct device_node *np)
867{
868 while (np->child)
869 detach_node_and_children(np->child);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700870 of_detach_node(np);
871}
872
873/**
874 * selftest_data_remove - removes the selftest data
875 * nodes from the live tree
876 */
877static void selftest_data_remove(void)
878{
879 struct device_node *np;
880 struct property *prop;
881
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700882 if (selftest_live_tree) {
883 of_node_put(of_aliases);
884 of_node_put(of_chosen);
885 of_aliases = NULL;
886 of_chosen = NULL;
Grant Likely5063e252014-10-03 16:28:27 +0100887 for_each_child_of_node(of_root, np)
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700888 detach_node_and_children(np);
Grant Likely5063e252014-10-03 16:28:27 +0100889 __of_detach_node_sysfs(of_root);
890 of_root = NULL;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700891 return;
892 }
893
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700894 while (last_node_index >= 0) {
895 if (nodes[last_node_index]) {
896 np = of_find_node_by_path(nodes[last_node_index]->full_name);
897 if (strcmp(np->full_name, "/aliases") != 0) {
Grant Likelye66c98c2014-10-01 16:57:07 +0100898 detach_node_and_children(np);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700899 } else {
900 for_each_property_of_node(np, prop) {
901 if (strcmp(prop->name, "testcase-alias") == 0)
902 of_remove_property(np, prop);
903 }
904 }
905 }
906 last_node_index--;
907 }
908}
909
Grant Likely53a42092011-12-12 09:25:57 -0700910static int __init of_selftest(void)
911{
912 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700913 int res;
914
915 /* adding data for selftest */
916 res = selftest_data_add();
917 if (res)
918 return res;
Grant Likely53a42092011-12-12 09:25:57 -0700919
920 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
921 if (!np) {
922 pr_info("No testcase data in device tree; not running tests\n");
923 return 0;
924 }
925 of_node_put(np);
926
927 pr_info("start of selftest - you will see error messages\n");
Grant Likelyf2051d62014-10-01 17:40:22 +0100928 of_selftest_check_tree_linkage();
Grant Likely841ec212014-10-02 13:09:15 +0100929 of_selftest_check_phandles();
Grant Likelyae91ff72014-03-14 13:53:10 +0000930 of_selftest_find_node_by_name();
Grant Likely7e66c5c2013-11-15 17:19:09 +0000931 of_selftest_dynamic();
Grant Likely53a42092011-12-12 09:25:57 -0700932 of_selftest_parse_phandle_with_args();
Grant Likelya87fa1d2014-11-03 15:15:35 +0000933 of_selftest_property_string();
Pantelis Antoniou69843392014-07-04 19:58:47 +0300934 of_selftest_property_copy();
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300935 of_selftest_changeset();
Grant Likelya9f10ca2013-10-11 22:04:23 +0100936 of_selftest_parse_interrupts();
Grant Likely79d97012013-09-19 16:47:37 -0500937 of_selftest_parse_interrupts_extended();
Grant Likely1f42e5d2014-02-18 21:38:55 +0000938 of_selftest_match_node();
Rob Herring82c0f582014-04-23 17:57:40 -0500939 of_selftest_platform_populate();
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700940
941 /* removing selftest data from live tree */
942 selftest_data_remove();
943
Grant Likelyf2051d62014-10-01 17:40:22 +0100944 /* Double check linkage after removing testcase data */
945 of_selftest_check_tree_linkage();
946
947 pr_info("end of selftest - %i passed, %i failed\n",
948 selftest_results.passed, selftest_results.failed);
949
Grant Likely53a42092011-12-12 09:25:57 -0700950 return 0;
951}
952late_initcall(of_selftest);