blob: 7a7ae07d592f066a4c99c8626dfb78ed3a682bdf [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;
50
51 np = of_find_node_by_path("/testcase-data");
52 selftest(np && !strcmp("/testcase-data", np->full_name),
53 "find /testcase-data failed\n");
54 of_node_put(np);
55
56 /* Test if trailing '/' works */
57 np = of_find_node_by_path("/testcase-data/");
58 selftest(!np, "trailing '/' on /testcase-data/ should fail\n");
59
60 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
61 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
62 "find /testcase-data/phandle-tests/consumer-a failed\n");
63 of_node_put(np);
64
65 np = of_find_node_by_path("testcase-alias");
66 selftest(np && !strcmp("/testcase-data", np->full_name),
67 "find testcase-alias failed\n");
68 of_node_put(np);
69
70 /* Test if trailing '/' works on aliases */
71 np = of_find_node_by_path("testcase-alias/");
72 selftest(!np, "trailing '/' on testcase-alias/ should fail\n");
73
74 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
75 selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name),
76 "find testcase-alias/phandle-tests/consumer-a failed\n");
77 of_node_put(np);
78
79 np = of_find_node_by_path("/testcase-data/missing-path");
80 selftest(!np, "non-existent path returned node %s\n", np->full_name);
81 of_node_put(np);
82
83 np = of_find_node_by_path("missing-alias");
84 selftest(!np, "non-existent alias returned node %s\n", np->full_name);
85 of_node_put(np);
86
87 np = of_find_node_by_path("testcase-alias/missing-path");
88 selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name);
89 of_node_put(np);
90}
91
Grant Likely7e66c5c2013-11-15 17:19:09 +000092static void __init of_selftest_dynamic(void)
93{
94 struct device_node *np;
95 struct property *prop;
96
97 np = of_find_node_by_path("/testcase-data");
98 if (!np) {
99 pr_err("missing testcase data\n");
100 return;
101 }
102
103 /* Array of 4 properties for the purpose of testing */
104 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
105 if (!prop) {
106 selftest(0, "kzalloc() failed\n");
107 return;
108 }
109
110 /* Add a new property - should pass*/
111 prop->name = "new-property";
112 prop->value = "new-property-data";
113 prop->length = strlen(prop->value);
114 selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
115
116 /* Try to add an existing property - should fail */
117 prop++;
118 prop->name = "new-property";
119 prop->value = "new-property-data-should-fail";
120 prop->length = strlen(prop->value);
121 selftest(of_add_property(np, prop) != 0,
122 "Adding an existing property should have failed\n");
123
124 /* Try to modify an existing property - should pass */
125 prop->value = "modify-property-data-should-pass";
126 prop->length = strlen(prop->value);
127 selftest(of_update_property(np, prop) == 0,
128 "Updating an existing property should have passed\n");
129
130 /* Try to modify non-existent property - should pass*/
131 prop++;
132 prop->name = "modify-property";
133 prop->value = "modify-missing-property-data-should-pass";
134 prop->length = strlen(prop->value);
135 selftest(of_update_property(np, prop) == 0,
136 "Updating a missing property should have passed\n");
137
138 /* Remove property - should pass */
139 selftest(of_remove_property(np, prop) == 0,
140 "Removing a property should have passed\n");
141
142 /* Adding very large property - should pass */
143 prop++;
144 prop->name = "large-property-PAGE_SIZEx8";
145 prop->length = PAGE_SIZE * 8;
146 prop->value = kzalloc(prop->length, GFP_KERNEL);
147 selftest(prop->value != NULL, "Unable to allocate large buffer\n");
148 if (prop->value)
149 selftest(of_add_property(np, prop) == 0,
150 "Adding a large property should have passed\n");
151}
152
Grant Likelyf2051d62014-10-01 17:40:22 +0100153static int __init of_selftest_check_node_linkage(struct device_node *np)
154{
Grant Likely5063e252014-10-03 16:28:27 +0100155 struct device_node *child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100156 int count = 0, rc;
157
158 for_each_child_of_node(np, child) {
159 if (child->parent != np) {
160 pr_err("Child node %s links to wrong parent %s\n",
161 child->name, np->name);
162 return -EINVAL;
163 }
164
Grant Likelyf2051d62014-10-01 17:40:22 +0100165 rc = of_selftest_check_node_linkage(child);
166 if (rc < 0)
167 return rc;
168 count += rc;
169 }
170
171 return count + 1;
172}
173
174static void __init of_selftest_check_tree_linkage(void)
175{
176 struct device_node *np;
177 int allnode_count = 0, child_count;
178
Grant Likely5063e252014-10-03 16:28:27 +0100179 if (!of_root)
Grant Likelyf2051d62014-10-01 17:40:22 +0100180 return;
181
182 for_each_of_allnodes(np)
183 allnode_count++;
Grant Likely5063e252014-10-03 16:28:27 +0100184 child_count = of_selftest_check_node_linkage(of_root);
Grant Likelyf2051d62014-10-01 17:40:22 +0100185
186 selftest(child_count > 0, "Device node data structure is corrupted\n");
187 selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match"
188 "sibling lists size (%i)\n", allnode_count, child_count);
189 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
190}
191
Grant Likely841ec212014-10-02 13:09:15 +0100192struct node_hash {
193 struct hlist_node node;
194 struct device_node *np;
195};
196
Grant Likely2118f4b2014-10-07 11:30:31 +0100197static DEFINE_HASHTABLE(phandle_ht, 8);
Grant Likely841ec212014-10-02 13:09:15 +0100198static void __init of_selftest_check_phandles(void)
199{
200 struct device_node *np;
201 struct node_hash *nh;
202 struct hlist_node *tmp;
203 int i, dup_count = 0, phandle_count = 0;
Grant Likely841ec212014-10-02 13:09:15 +0100204
Grant Likely841ec212014-10-02 13:09:15 +0100205 for_each_of_allnodes(np) {
206 if (!np->phandle)
207 continue;
208
Grant Likely2118f4b2014-10-07 11:30:31 +0100209 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100210 if (nh->np->phandle == np->phandle) {
211 pr_info("Duplicate phandle! %i used by %s and %s\n",
212 np->phandle, nh->np->full_name, np->full_name);
213 dup_count++;
214 break;
215 }
216 }
217
218 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
219 if (WARN_ON(!nh))
220 return;
221
222 nh->np = np;
Grant Likely2118f4b2014-10-07 11:30:31 +0100223 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100224 phandle_count++;
225 }
226 selftest(dup_count == 0, "Found %i duplicates in %i phandles\n",
227 dup_count, phandle_count);
228
229 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100230 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100231 hash_del(&nh->node);
232 kfree(nh);
233 }
234}
235
Grant Likely53a42092011-12-12 09:25:57 -0700236static void __init of_selftest_parse_phandle_with_args(void)
237{
238 struct device_node *np;
239 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000240 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700241
Grant Likely53a42092011-12-12 09:25:57 -0700242 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
243 if (!np) {
244 pr_err("missing testcase data\n");
245 return;
246 }
247
Grant Likelybd69f732013-02-10 22:57:21 +0000248 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
249 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
250
Grant Likelyf7f951c2013-02-12 17:41:22 +0000251 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700252 bool passed = true;
253 rc = of_parse_phandle_with_args(np, "phandle-list",
254 "#phandle-cells", i, &args);
255
256 /* Test the values from tests-phandle.dtsi */
257 switch (i) {
258 case 0:
259 passed &= !rc;
260 passed &= (args.args_count == 1);
261 passed &= (args.args[0] == (i + 1));
262 break;
263 case 1:
264 passed &= !rc;
265 passed &= (args.args_count == 2);
266 passed &= (args.args[0] == (i + 1));
267 passed &= (args.args[1] == 0);
268 break;
269 case 2:
270 passed &= (rc == -ENOENT);
271 break;
272 case 3:
273 passed &= !rc;
274 passed &= (args.args_count == 3);
275 passed &= (args.args[0] == (i + 1));
276 passed &= (args.args[1] == 4);
277 passed &= (args.args[2] == 3);
278 break;
279 case 4:
280 passed &= !rc;
281 passed &= (args.args_count == 2);
282 passed &= (args.args[0] == (i + 1));
283 passed &= (args.args[1] == 100);
284 break;
285 case 5:
286 passed &= !rc;
287 passed &= (args.args_count == 0);
288 break;
289 case 6:
290 passed &= !rc;
291 passed &= (args.args_count == 1);
292 passed &= (args.args[0] == (i + 1));
293 break;
294 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000295 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700296 break;
297 default:
298 passed = false;
299 }
300
Grant Likelycabb7d52013-02-12 21:19:37 +0000301 selftest(passed, "index %i - data error on node %s rc=%i\n",
302 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700303 }
304
305 /* Check for missing list property */
306 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
307 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000308 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000309 rc = of_count_phandle_with_args(np, "phandle-list-missing",
310 "#phandle-cells");
311 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700312
313 /* Check for missing cells property */
314 rc = of_parse_phandle_with_args(np, "phandle-list",
315 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000316 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000317 rc = of_count_phandle_with_args(np, "phandle-list",
318 "#phandle-cells-missing");
319 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700320
321 /* Check for bad phandle in list */
322 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
323 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000324 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000325 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
326 "#phandle-cells");
327 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700328
329 /* Check for incorrectly formed argument list */
330 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
331 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000332 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000333 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
334 "#phandle-cells");
335 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700336}
337
Grant Likelya87fa1d2014-11-03 15:15:35 +0000338static void __init of_selftest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700339{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000340 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700341 struct device_node *np;
342 int rc;
343
Grant Likely7aff0fe2011-12-12 09:25:58 -0700344 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
345 if (!np) {
346 pr_err("No testcase data in device tree\n");
347 return;
348 }
349
350 rc = of_property_match_string(np, "phandle-list-names", "first");
351 selftest(rc == 0, "first expected:0 got:%i\n", rc);
352 rc = of_property_match_string(np, "phandle-list-names", "second");
353 selftest(rc == 1, "second expected:0 got:%i\n", rc);
354 rc = of_property_match_string(np, "phandle-list-names", "third");
355 selftest(rc == 2, "third expected:0 got:%i\n", rc);
356 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000357 selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700358 rc = of_property_match_string(np, "missing-property", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000359 selftest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700360 rc = of_property_match_string(np, "empty-property", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000361 selftest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700362 rc = of_property_match_string(np, "unterminated-string", "blah");
Grant Likelya87fa1d2014-11-03 15:15:35 +0000363 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
364
365 /* of_property_count_strings() tests */
366 rc = of_property_count_strings(np, "string-property");
367 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
368 rc = of_property_count_strings(np, "phandle-list-names");
369 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
370 rc = of_property_count_strings(np, "unterminated-string");
371 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
372 rc = of_property_count_strings(np, "unterminated-string-list");
373 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
374
375 /* of_property_read_string_index() tests */
376 rc = of_property_read_string_index(np, "string-property", 0, strings);
377 selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
378 strings[0] = NULL;
379 rc = of_property_read_string_index(np, "string-property", 1, strings);
380 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
381 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
382 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
383 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
384 selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
385 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
386 selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
387 strings[0] = NULL;
388 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
389 selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
390 strings[0] = NULL;
391 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
392 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
393 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
394 selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
395 strings[0] = NULL;
396 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
397 selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
398 strings[1] = NULL;
399
400 /* of_property_read_string_array() tests */
401 rc = of_property_read_string_array(np, "string-property", strings, 4);
402 selftest(rc == 1, "Incorrect string count; rc=%i\n", rc);
403 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
404 selftest(rc == 3, "Incorrect string count; rc=%i\n", rc);
405 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
406 selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
407 /* -- An incorrectly formed string should cause a failure */
408 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
409 selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
410 /* -- parsing the correctly formed strings should still work: */
411 strings[2] = NULL;
412 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
413 selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
414 strings[1] = NULL;
415 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
416 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 -0700417}
418
Pantelis Antoniou69843392014-07-04 19:58:47 +0300419#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
420 (p1)->value && (p2)->value && \
421 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
422 !strcmp((p1)->name, (p2)->name))
423static void __init of_selftest_property_copy(void)
424{
425#ifdef CONFIG_OF_DYNAMIC
426 struct property p1 = { .name = "p1", .length = 0, .value = "" };
427 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
428 struct property *new;
429
430 new = __of_prop_dup(&p1, GFP_KERNEL);
431 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
432 kfree(new->value);
433 kfree(new->name);
434 kfree(new);
435
436 new = __of_prop_dup(&p2, GFP_KERNEL);
437 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
438 kfree(new->value);
439 kfree(new->name);
440 kfree(new);
441#endif
442}
443
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300444static void __init of_selftest_changeset(void)
445{
446#ifdef CONFIG_OF_DYNAMIC
447 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
448 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
449 struct property *ppremove;
Grant Likelye5179582014-11-17 22:31:32 +0000450 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300451 struct of_changeset chgset;
452
453 of_changeset_init(&chgset);
Grant Likelye5179582014-11-17 22:31:32 +0000454 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300455 selftest(n1, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000456 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300457 selftest(n2, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000458 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300459 selftest(n21, "testcase setup failure %p\n", n21);
460 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
461 selftest(nremove, "testcase setup failure\n");
462 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
463 selftest(ppadd, "testcase setup failure\n");
464 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
465 selftest(ppupdate, "testcase setup failure\n");
466 parent = nremove->parent;
467 n1->parent = parent;
468 n2->parent = parent;
469 n21->parent = n2;
470 n2->child = n21;
471 ppremove = of_find_property(parent, "prop-remove", NULL);
472 selftest(ppremove, "failed to find removal prop");
473
474 of_changeset_init(&chgset);
475 selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
476 selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
477 selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
478 selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
479 selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
480 selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
481 selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
482 mutex_lock(&of_mutex);
483 selftest(!of_changeset_apply(&chgset), "apply failed\n");
484 mutex_unlock(&of_mutex);
485
Grant Likelye5179582014-11-17 22:31:32 +0000486 /* Make sure node names are constructed correctly */
487 selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
488 "'%s' not added\n", n21->full_name);
489 if (np)
490 of_node_put(np);
491
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300492 mutex_lock(&of_mutex);
493 selftest(!of_changeset_revert(&chgset), "revert failed\n");
494 mutex_unlock(&of_mutex);
495
496 of_changeset_destroy(&chgset);
497#endif
498}
499
Grant Likelya9f10ca2013-10-11 22:04:23 +0100500static void __init of_selftest_parse_interrupts(void)
501{
502 struct device_node *np;
503 struct of_phandle_args args;
504 int i, rc;
505
506 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
507 if (!np) {
508 pr_err("missing testcase data\n");
509 return;
510 }
511
512 for (i = 0; i < 4; i++) {
513 bool passed = true;
514 args.args_count = 0;
515 rc = of_irq_parse_one(np, i, &args);
516
517 passed &= !rc;
518 passed &= (args.args_count == 1);
519 passed &= (args.args[0] == (i + 1));
520
521 selftest(passed, "index %i - data error on node %s rc=%i\n",
522 i, args.np->full_name, rc);
523 }
524 of_node_put(np);
525
526 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
527 if (!np) {
528 pr_err("missing testcase data\n");
529 return;
530 }
531
532 for (i = 0; i < 4; i++) {
533 bool passed = true;
534 args.args_count = 0;
535 rc = of_irq_parse_one(np, i, &args);
536
537 /* Test the values from tests-phandle.dtsi */
538 switch (i) {
539 case 0:
540 passed &= !rc;
541 passed &= (args.args_count == 1);
542 passed &= (args.args[0] == 9);
543 break;
544 case 1:
545 passed &= !rc;
546 passed &= (args.args_count == 3);
547 passed &= (args.args[0] == 10);
548 passed &= (args.args[1] == 11);
549 passed &= (args.args[2] == 12);
550 break;
551 case 2:
552 passed &= !rc;
553 passed &= (args.args_count == 2);
554 passed &= (args.args[0] == 13);
555 passed &= (args.args[1] == 14);
556 break;
557 case 3:
558 passed &= !rc;
559 passed &= (args.args_count == 2);
560 passed &= (args.args[0] == 15);
561 passed &= (args.args[1] == 16);
562 break;
563 default:
564 passed = false;
565 }
566 selftest(passed, "index %i - data error on node %s rc=%i\n",
567 i, args.np->full_name, rc);
568 }
569 of_node_put(np);
570}
571
Grant Likely79d97012013-09-19 16:47:37 -0500572static void __init of_selftest_parse_interrupts_extended(void)
573{
574 struct device_node *np;
575 struct of_phandle_args args;
576 int i, rc;
577
578 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
579 if (!np) {
580 pr_err("missing testcase data\n");
581 return;
582 }
583
584 for (i = 0; i < 7; i++) {
585 bool passed = true;
586 rc = of_irq_parse_one(np, i, &args);
587
588 /* Test the values from tests-phandle.dtsi */
589 switch (i) {
590 case 0:
591 passed &= !rc;
592 passed &= (args.args_count == 1);
593 passed &= (args.args[0] == 1);
594 break;
595 case 1:
596 passed &= !rc;
597 passed &= (args.args_count == 3);
598 passed &= (args.args[0] == 2);
599 passed &= (args.args[1] == 3);
600 passed &= (args.args[2] == 4);
601 break;
602 case 2:
603 passed &= !rc;
604 passed &= (args.args_count == 2);
605 passed &= (args.args[0] == 5);
606 passed &= (args.args[1] == 6);
607 break;
608 case 3:
609 passed &= !rc;
610 passed &= (args.args_count == 1);
611 passed &= (args.args[0] == 9);
612 break;
613 case 4:
614 passed &= !rc;
615 passed &= (args.args_count == 3);
616 passed &= (args.args[0] == 10);
617 passed &= (args.args[1] == 11);
618 passed &= (args.args[2] == 12);
619 break;
620 case 5:
621 passed &= !rc;
622 passed &= (args.args_count == 2);
623 passed &= (args.args[0] == 13);
624 passed &= (args.args[1] == 14);
625 break;
626 case 6:
627 passed &= !rc;
628 passed &= (args.args_count == 1);
629 passed &= (args.args[0] == 15);
630 break;
631 default:
632 passed = false;
633 }
634
635 selftest(passed, "index %i - data error on node %s rc=%i\n",
636 i, args.np->full_name, rc);
637 }
638 of_node_put(np);
639}
640
Grant Likely1f42e5d2014-02-18 21:38:55 +0000641static struct of_device_id match_node_table[] = {
642 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
643 { .data = "B", .type = "type1", }, /* followed by type alone */
644
645 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
646 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
647 { .data = "Cc", .name = "name2", .type = "type2", },
648
649 { .data = "E", .compatible = "compat3" },
650 { .data = "G", .compatible = "compat2", },
651 { .data = "H", .compatible = "compat2", .name = "name5", },
652 { .data = "I", .compatible = "compat2", .type = "type1", },
653 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
654 { .data = "K", .compatible = "compat2", .name = "name9", },
655 {}
656};
657
658static struct {
659 const char *path;
660 const char *data;
661} match_node_tests[] = {
662 { .path = "/testcase-data/match-node/name0", .data = "A", },
663 { .path = "/testcase-data/match-node/name1", .data = "B", },
664 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
665 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
666 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
667 { .path = "/testcase-data/match-node/name3", .data = "E", },
668 { .path = "/testcase-data/match-node/name4", .data = "G", },
669 { .path = "/testcase-data/match-node/name5", .data = "H", },
670 { .path = "/testcase-data/match-node/name6", .data = "G", },
671 { .path = "/testcase-data/match-node/name7", .data = "I", },
672 { .path = "/testcase-data/match-node/name8", .data = "J", },
673 { .path = "/testcase-data/match-node/name9", .data = "K", },
674};
675
676static void __init of_selftest_match_node(void)
677{
678 struct device_node *np;
679 const struct of_device_id *match;
680 int i;
681
682 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
683 np = of_find_node_by_path(match_node_tests[i].path);
684 if (!np) {
685 selftest(0, "missing testcase node %s\n",
686 match_node_tests[i].path);
687 continue;
688 }
689
690 match = of_match_node(match_node_table, np);
691 if (!match) {
692 selftest(0, "%s didn't match anything\n",
693 match_node_tests[i].path);
694 continue;
695 }
696
697 if (strcmp(match->data, match_node_tests[i].data) != 0) {
698 selftest(0, "%s got wrong match. expected %s, got %s\n",
699 match_node_tests[i].path, match_node_tests[i].data,
700 (const char *)match->data);
701 continue;
702 }
703 selftest(1, "passed");
704 }
705}
706
Grant Likely851da972014-11-04 13:14:13 +0000707struct device test_bus = {
708 .init_name = "unittest-bus",
709};
Rob Herring82c0f582014-04-23 17:57:40 -0500710static void __init of_selftest_platform_populate(void)
711{
Grant Likely851da972014-11-04 13:14:13 +0000712 int irq, rc;
713 struct device_node *np, *child, *grandchild;
Rob Herring82c0f582014-04-23 17:57:40 -0500714 struct platform_device *pdev;
Rob Herringfb2caa52014-05-13 10:07:54 -0500715 struct of_device_id match[] = {
716 { .compatible = "test-device", },
717 {}
718 };
Rob Herring82c0f582014-04-23 17:57:40 -0500719
720 np = of_find_node_by_path("/testcase-data");
721 of_platform_populate(np, of_default_bus_match_table, NULL, NULL);
722
723 /* Test that a missing irq domain returns -EPROBE_DEFER */
724 np = of_find_node_by_path("/testcase-data/testcase-device1");
725 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500726 selftest(pdev, "device 1 creation failed\n");
727
Rob Herring82c0f582014-04-23 17:57:40 -0500728 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500729 selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500730
731 /* Test that a parsing failure does not return -EPROBE_DEFER */
732 np = of_find_node_by_path("/testcase-data/testcase-device2");
733 pdev = of_find_device_by_node(np);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500734 selftest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500735 irq = platform_get_irq(pdev, 0);
Rob Herring7d1cdc82014-05-13 10:07:29 -0500736 selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500737
Grant Likely851da972014-11-04 13:14:13 +0000738 if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"),
739 "No testcase data in device tree\n"));
Rob Herringfb2caa52014-05-13 10:07:54 -0500740 return;
Grant Likely851da972014-11-04 13:14:13 +0000741
742 if (selftest(!(rc = device_register(&test_bus)),
743 "testbus registration failed; rc=%i\n", rc));
744 return;
Rob Herringfb2caa52014-05-13 10:07:54 -0500745
746 for_each_child_of_node(np, child) {
Grant Likely851da972014-11-04 13:14:13 +0000747 of_platform_populate(child, match, NULL, &test_bus);
Rob Herringfb2caa52014-05-13 10:07:54 -0500748 for_each_child_of_node(child, grandchild)
749 selftest(of_find_device_by_node(grandchild),
750 "Could not create device for node '%s'\n",
751 grandchild->name);
752 }
Grant Likely851da972014-11-04 13:14:13 +0000753
754 of_platform_depopulate(&test_bus);
755 for_each_child_of_node(np, child) {
756 for_each_child_of_node(child, grandchild)
757 selftest(!of_find_device_by_node(grandchild),
758 "device didn't get destroyed '%s'\n",
759 grandchild->name);
760 }
761
762 device_unregister(&test_bus);
763 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -0500764}
765
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700766/**
767 * update_node_properties - adds the properties
768 * of np into dup node (present in live tree) and
769 * updates parent of children of np to dup.
770 *
771 * @np: node already present in live tree
772 * @dup: node present in live tree to be updated
773 */
774static void update_node_properties(struct device_node *np,
775 struct device_node *dup)
776{
777 struct property *prop;
778 struct device_node *child;
779
780 for_each_property_of_node(np, prop)
781 of_add_property(dup, prop);
782
783 for_each_child_of_node(np, child)
784 child->parent = dup;
785}
786
787/**
788 * attach_node_and_children - attaches nodes
789 * and its children to live tree
790 *
791 * @np: Node to attach to live tree
792 */
793static int attach_node_and_children(struct device_node *np)
794{
Grant Likely5063e252014-10-03 16:28:27 +0100795 struct device_node *next, *dup, *child;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700796
Grant Likely5063e252014-10-03 16:28:27 +0100797 dup = of_find_node_by_path(np->full_name);
798 if (dup) {
799 update_node_properties(np, dup);
800 return 0;
801 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700802
Grant Likely5063e252014-10-03 16:28:27 +0100803 /* Children of the root need to be remembered for removal */
804 if (np->parent == of_root) {
Grant Likelye66c98c2014-10-01 16:57:07 +0100805 if (WARN_ON(last_node_index >= NO_OF_NODES))
806 return -EINVAL;
Grant Likely5063e252014-10-03 16:28:27 +0100807 nodes[last_node_index++] = np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700808 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700809
Grant Likely5063e252014-10-03 16:28:27 +0100810 child = np->child;
811 np->child = NULL;
812 np->sibling = NULL;
813 of_attach_node(np);
814 while (child) {
815 next = child->sibling;
816 attach_node_and_children(child);
817 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700818 }
819
820 return 0;
821}
822
823/**
824 * selftest_data_add - Reads, copies data from
825 * linked tree and attaches it to the live tree
826 */
827static int __init selftest_data_add(void)
828{
829 void *selftest_data;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700830 struct device_node *selftest_data_node, *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700831 extern uint8_t __dtb_testcases_begin[];
832 extern uint8_t __dtb_testcases_end[];
833 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +0100834 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700835
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700836 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700837 pr_warn("%s: No testcase data to attach; not running tests\n",
838 __func__);
839 return -ENODATA;
840 }
841
842 /* creating copy */
843 selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
844
845 if (!selftest_data) {
846 pr_warn("%s: Failed to allocate memory for selftest_data; "
847 "not running tests\n", __func__);
848 return -ENOMEM;
849 }
850 of_fdt_unflatten_tree(selftest_data, &selftest_data_node);
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700851 if (!selftest_data_node) {
852 pr_warn("%s: No tree to attach; not running tests\n", __func__);
853 return -ENODATA;
854 }
Grant Likely2eb46da2014-10-02 14:36:46 +0100855 of_node_set_flag(selftest_data_node, OF_DETACHED);
856 rc = of_resolve_phandles(selftest_data_node);
857 if (rc) {
858 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
859 return -EINVAL;
860 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700861
Grant Likely5063e252014-10-03 16:28:27 +0100862 if (!of_root) {
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700863 /* enabling flag for removing nodes */
864 selftest_live_tree = true;
Grant Likely5063e252014-10-03 16:28:27 +0100865 of_root = selftest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700866
867 for_each_of_allnodes(np)
868 __of_attach_node_sysfs(np);
869 of_aliases = of_find_node_by_path("/aliases");
870 of_chosen = of_find_node_by_path("/chosen");
871 return 0;
872 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700873
874 /* attach the sub-tree to live tree */
Grant Likely5063e252014-10-03 16:28:27 +0100875 np = selftest_data_node->child;
876 while (np) {
877 struct device_node *next = np->sibling;
878 np->parent = of_root;
879 attach_node_and_children(np);
880 np = next;
881 }
882 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700883}
884
885/**
886 * detach_node_and_children - detaches node
887 * and its children from live tree
888 *
889 * @np: Node to detach from live tree
890 */
891static void detach_node_and_children(struct device_node *np)
892{
893 while (np->child)
894 detach_node_and_children(np->child);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700895 of_detach_node(np);
896}
897
898/**
899 * selftest_data_remove - removes the selftest data
900 * nodes from the live tree
901 */
902static void selftest_data_remove(void)
903{
904 struct device_node *np;
905 struct property *prop;
906
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700907 if (selftest_live_tree) {
908 of_node_put(of_aliases);
909 of_node_put(of_chosen);
910 of_aliases = NULL;
911 of_chosen = NULL;
Grant Likely5063e252014-10-03 16:28:27 +0100912 for_each_child_of_node(of_root, np)
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700913 detach_node_and_children(np);
Grant Likely5063e252014-10-03 16:28:27 +0100914 __of_detach_node_sysfs(of_root);
915 of_root = NULL;
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700916 return;
917 }
918
Grant Likelyc1a20862014-11-19 16:22:32 +0000919 while (last_node_index-- > 0) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700920 if (nodes[last_node_index]) {
921 np = of_find_node_by_path(nodes[last_node_index]->full_name);
Grant Likely788ec2f2014-11-19 17:13:44 +0000922 if (np == nodes[last_node_index]) {
923 if (of_aliases == np) {
924 of_node_put(of_aliases);
925 of_aliases = NULL;
926 }
Grant Likelye66c98c2014-10-01 16:57:07 +0100927 detach_node_and_children(np);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700928 } else {
929 for_each_property_of_node(np, prop) {
930 if (strcmp(prop->name, "testcase-alias") == 0)
931 of_remove_property(np, prop);
932 }
933 }
934 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700935 }
936}
937
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200938#ifdef CONFIG_OF_OVERLAY
939
940static int selftest_probe(struct platform_device *pdev)
941{
942 struct device *dev = &pdev->dev;
943 struct device_node *np = dev->of_node;
944
945 if (np == NULL) {
946 dev_err(dev, "No OF data for device\n");
947 return -EINVAL;
948
949 }
950
951 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
952 return 0;
953}
954
955static int selftest_remove(struct platform_device *pdev)
956{
957 struct device *dev = &pdev->dev;
958 struct device_node *np = dev->of_node;
959
960 dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name);
961 return 0;
962}
963
964static struct of_device_id selftest_match[] = {
965 { .compatible = "selftest", },
966 {},
967};
Pantelis Antoniou177d2712014-10-28 22:35:59 +0200968
969static struct platform_driver selftest_driver = {
970 .probe = selftest_probe,
971 .remove = selftest_remove,
972 .driver = {
973 .name = "selftest",
974 .owner = THIS_MODULE,
975 .of_match_table = of_match_ptr(selftest_match),
976 },
977};
978
979/* get the platform device instantiated at the path */
980static struct platform_device *of_path_to_platform_device(const char *path)
981{
982 struct device_node *np;
983 struct platform_device *pdev;
984
985 np = of_find_node_by_path(path);
986 if (np == NULL)
987 return NULL;
988
989 pdev = of_find_device_by_node(np);
990 of_node_put(np);
991
992 return pdev;
993}
994
995/* find out if a platform device exists at that path */
996static int of_path_platform_device_exists(const char *path)
997{
998 struct platform_device *pdev;
999
1000 pdev = of_path_to_platform_device(path);
1001 platform_device_put(pdev);
1002 return pdev != NULL;
1003}
1004
1005static const char *selftest_path(int nr)
1006{
1007 static char buf[256];
1008
1009 snprintf(buf, sizeof(buf) - 1,
1010 "/testcase-data/overlay-node/test-bus/test-selftest%d", nr);
1011 buf[sizeof(buf) - 1] = '\0';
1012
1013 return buf;
1014}
1015
1016static const char *overlay_path(int nr)
1017{
1018 static char buf[256];
1019
1020 snprintf(buf, sizeof(buf) - 1,
1021 "/testcase-data/overlay%d", nr);
1022 buf[sizeof(buf) - 1] = '\0';
1023
1024 return buf;
1025}
1026
1027static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1028
1029static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr,
1030 int *overlay_id)
1031{
1032 struct device_node *np = NULL;
1033 int ret, id = -1;
1034
1035 np = of_find_node_by_path(overlay_path(overlay_nr));
1036 if (np == NULL) {
1037 selftest(0, "could not find overlay node @\"%s\"\n",
1038 overlay_path(overlay_nr));
1039 ret = -EINVAL;
1040 goto out;
1041 }
1042
1043 ret = of_overlay_create(np);
1044 if (ret < 0) {
1045 selftest(0, "could not create overlay from \"%s\"\n",
1046 overlay_path(overlay_nr));
1047 goto out;
1048 }
1049 id = ret;
1050
1051 ret = 0;
1052
1053out:
1054 of_node_put(np);
1055
1056 if (overlay_id)
1057 *overlay_id = id;
1058
1059 return ret;
1060}
1061
1062/* apply an overlay while checking before and after states */
1063static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr,
1064 int before, int after)
1065{
1066 int ret;
1067
1068 /* selftest device must not be in before state */
1069 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1070 != before) {
1071 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1072 overlay_path(overlay_nr),
1073 selftest_path(selftest_nr),
1074 !before ? "enabled" : "disabled");
1075 return -EINVAL;
1076 }
1077
1078 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL);
1079 if (ret != 0) {
1080 /* of_selftest_apply_overlay already called selftest() */
1081 return ret;
1082 }
1083
1084 /* selftest device must be to set to after state */
1085 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1086 != after) {
1087 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1088 overlay_path(overlay_nr),
1089 selftest_path(selftest_nr),
1090 !after ? "enabled" : "disabled");
1091 return -EINVAL;
1092 }
1093
1094 return 0;
1095}
1096
1097/* apply an overlay and then revert it while checking before, after states */
1098static int of_selftest_apply_revert_overlay_check(int overlay_nr,
1099 int selftest_nr, int before, int after)
1100{
1101 int ret, ov_id;
1102
1103 /* selftest device must be in before state */
1104 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1105 != before) {
1106 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1107 overlay_path(overlay_nr),
1108 selftest_path(selftest_nr),
1109 !before ? "enabled" : "disabled");
1110 return -EINVAL;
1111 }
1112
1113 /* apply the overlay */
1114 ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id);
1115 if (ret != 0) {
1116 /* of_selftest_apply_overlay already called selftest() */
1117 return ret;
1118 }
1119
1120 /* selftest device must be in after state */
1121 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1122 != after) {
1123 selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
1124 overlay_path(overlay_nr),
1125 selftest_path(selftest_nr),
1126 !after ? "enabled" : "disabled");
1127 return -EINVAL;
1128 }
1129
1130 ret = of_overlay_destroy(ov_id);
1131 if (ret != 0) {
1132 selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
1133 overlay_path(overlay_nr),
1134 selftest_path(selftest_nr));
1135 return ret;
1136 }
1137
1138 /* selftest device must be again in before state */
1139 if (of_path_platform_device_exists(selftest_path(selftest_nr))
1140 != before) {
1141 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1142 overlay_path(overlay_nr),
1143 selftest_path(selftest_nr),
1144 !before ? "enabled" : "disabled");
1145 return -EINVAL;
1146 }
1147
1148 return 0;
1149}
1150
1151/* test activation of device */
1152static void of_selftest_overlay_0(void)
1153{
1154 int ret;
1155
1156 /* device should enable */
1157 ret = of_selftest_apply_overlay_check(0, 0, 0, 1);
1158 if (ret != 0)
1159 return;
1160
1161 selftest(1, "overlay test %d passed\n", 0);
1162}
1163
1164/* test deactivation of device */
1165static void of_selftest_overlay_1(void)
1166{
1167 int ret;
1168
1169 /* device should disable */
1170 ret = of_selftest_apply_overlay_check(1, 1, 1, 0);
1171 if (ret != 0)
1172 return;
1173
1174 selftest(1, "overlay test %d passed\n", 1);
1175}
1176
1177/* test activation of device */
1178static void of_selftest_overlay_2(void)
1179{
1180 int ret;
1181
1182 /* device should enable */
1183 ret = of_selftest_apply_overlay_check(2, 2, 0, 1);
1184 if (ret != 0)
1185 return;
1186
1187 selftest(1, "overlay test %d passed\n", 2);
1188}
1189
1190/* test deactivation of device */
1191static void of_selftest_overlay_3(void)
1192{
1193 int ret;
1194
1195 /* device should disable */
1196 ret = of_selftest_apply_overlay_check(3, 3, 1, 0);
1197 if (ret != 0)
1198 return;
1199
1200 selftest(1, "overlay test %d passed\n", 3);
1201}
1202
1203/* test activation of a full device node */
1204static void of_selftest_overlay_4(void)
1205{
1206 int ret;
1207
1208 /* device should disable */
1209 ret = of_selftest_apply_overlay_check(4, 4, 0, 1);
1210 if (ret != 0)
1211 return;
1212
1213 selftest(1, "overlay test %d passed\n", 4);
1214}
1215
1216/* test overlay apply/revert sequence */
1217static void of_selftest_overlay_5(void)
1218{
1219 int ret;
1220
1221 /* device should disable */
1222 ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1);
1223 if (ret != 0)
1224 return;
1225
1226 selftest(1, "overlay test %d passed\n", 5);
1227}
1228
1229/* test overlay application in sequence */
1230static void of_selftest_overlay_6(void)
1231{
1232 struct device_node *np;
1233 int ret, i, ov_id[2];
1234 int overlay_nr = 6, selftest_nr = 6;
1235 int before = 0, after = 1;
1236
1237 /* selftest device must be in before state */
1238 for (i = 0; i < 2; i++) {
1239 if (of_path_platform_device_exists(
1240 selftest_path(selftest_nr + i))
1241 != before) {
1242 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1243 overlay_path(overlay_nr + i),
1244 selftest_path(selftest_nr + i),
1245 !before ? "enabled" : "disabled");
1246 return;
1247 }
1248 }
1249
1250 /* apply the overlays */
1251 for (i = 0; i < 2; i++) {
1252
1253 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1254 if (np == NULL) {
1255 selftest(0, "could not find overlay node @\"%s\"\n",
1256 overlay_path(overlay_nr + i));
1257 return;
1258 }
1259
1260 ret = of_overlay_create(np);
1261 if (ret < 0) {
1262 selftest(0, "could not create overlay from \"%s\"\n",
1263 overlay_path(overlay_nr + i));
1264 return;
1265 }
1266 ov_id[i] = ret;
1267 }
1268
1269 for (i = 0; i < 2; i++) {
1270 /* selftest device must be in after state */
1271 if (of_path_platform_device_exists(
1272 selftest_path(selftest_nr + i))
1273 != after) {
1274 selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
1275 overlay_path(overlay_nr + i),
1276 selftest_path(selftest_nr + i),
1277 !after ? "enabled" : "disabled");
1278 return;
1279 }
1280 }
1281
1282 for (i = 1; i >= 0; i--) {
1283 ret = of_overlay_destroy(ov_id[i]);
1284 if (ret != 0) {
1285 selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
1286 overlay_path(overlay_nr + i),
1287 selftest_path(selftest_nr + i));
1288 return;
1289 }
1290 }
1291
1292 for (i = 0; i < 2; i++) {
1293 /* selftest device must be again in before state */
1294 if (of_path_platform_device_exists(
1295 selftest_path(selftest_nr + i))
1296 != before) {
1297 selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
1298 overlay_path(overlay_nr + i),
1299 selftest_path(selftest_nr + i),
1300 !before ? "enabled" : "disabled");
1301 return;
1302 }
1303 }
1304
1305 selftest(1, "overlay test %d passed\n", 6);
1306}
1307
1308/* test overlay application in sequence */
1309static void of_selftest_overlay_8(void)
1310{
1311 struct device_node *np;
1312 int ret, i, ov_id[2];
1313 int overlay_nr = 8, selftest_nr = 8;
1314
1315 /* we don't care about device state in this test */
1316
1317 /* apply the overlays */
1318 for (i = 0; i < 2; i++) {
1319
1320 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1321 if (np == NULL) {
1322 selftest(0, "could not find overlay node @\"%s\"\n",
1323 overlay_path(overlay_nr + i));
1324 return;
1325 }
1326
1327 ret = of_overlay_create(np);
1328 if (ret < 0) {
1329 selftest(0, "could not create overlay from \"%s\"\n",
1330 overlay_path(overlay_nr + i));
1331 return;
1332 }
1333 ov_id[i] = ret;
1334 }
1335
1336 /* now try to remove first overlay (it should fail) */
1337 ret = of_overlay_destroy(ov_id[0]);
1338 if (ret == 0) {
1339 selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
1340 overlay_path(overlay_nr + 0),
1341 selftest_path(selftest_nr));
1342 return;
1343 }
1344
1345 /* removing them in order should work */
1346 for (i = 1; i >= 0; i--) {
1347 ret = of_overlay_destroy(ov_id[i]);
1348 if (ret != 0) {
1349 selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
1350 overlay_path(overlay_nr + i),
1351 selftest_path(selftest_nr));
1352 return;
1353 }
1354 }
1355
1356 selftest(1, "overlay test %d passed\n", 8);
1357}
1358
1359static void __init of_selftest_overlay(void)
1360{
1361 struct device_node *bus_np = NULL;
1362 int ret;
1363
1364 ret = platform_driver_register(&selftest_driver);
1365 if (ret != 0) {
1366 selftest(0, "could not register selftest driver\n");
1367 goto out;
1368 }
1369
1370 bus_np = of_find_node_by_path(bus_path);
1371 if (bus_np == NULL) {
1372 selftest(0, "could not find bus_path \"%s\"\n", bus_path);
1373 goto out;
1374 }
1375
1376 ret = of_platform_populate(bus_np, of_default_bus_match_table,
1377 NULL, NULL);
1378 if (ret != 0) {
1379 selftest(0, "could not populate bus @ \"%s\"\n", bus_path);
1380 goto out;
1381 }
1382
1383 if (!of_path_platform_device_exists(selftest_path(100))) {
1384 selftest(0, "could not find selftest0 @ \"%s\"\n",
1385 selftest_path(100));
1386 goto out;
1387 }
1388
1389 if (of_path_platform_device_exists(selftest_path(101))) {
1390 selftest(0, "selftest1 @ \"%s\" should not exist\n",
1391 selftest_path(101));
1392 goto out;
1393 }
1394
1395 selftest(1, "basic infrastructure of overlays passed");
1396
1397 /* tests in sequence */
1398 of_selftest_overlay_0();
1399 of_selftest_overlay_1();
1400 of_selftest_overlay_2();
1401 of_selftest_overlay_3();
1402 of_selftest_overlay_4();
1403 of_selftest_overlay_5();
1404 of_selftest_overlay_6();
1405 of_selftest_overlay_8();
1406
1407out:
1408 of_node_put(bus_np);
1409}
1410
1411#else
1412static inline void __init of_selftest_overlay(void) { }
1413#endif
1414
Grant Likely53a42092011-12-12 09:25:57 -07001415static int __init of_selftest(void)
1416{
1417 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001418 int res;
1419
1420 /* adding data for selftest */
1421 res = selftest_data_add();
1422 if (res)
1423 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00001424 if (!of_aliases)
1425 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07001426
1427 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
1428 if (!np) {
1429 pr_info("No testcase data in device tree; not running tests\n");
1430 return 0;
1431 }
1432 of_node_put(np);
1433
1434 pr_info("start of selftest - you will see error messages\n");
Grant Likelyf2051d62014-10-01 17:40:22 +01001435 of_selftest_check_tree_linkage();
Grant Likely841ec212014-10-02 13:09:15 +01001436 of_selftest_check_phandles();
Grant Likelyae91ff72014-03-14 13:53:10 +00001437 of_selftest_find_node_by_name();
Grant Likely7e66c5c2013-11-15 17:19:09 +00001438 of_selftest_dynamic();
Grant Likely53a42092011-12-12 09:25:57 -07001439 of_selftest_parse_phandle_with_args();
Grant Likelya87fa1d2014-11-03 15:15:35 +00001440 of_selftest_property_string();
Pantelis Antoniou69843392014-07-04 19:58:47 +03001441 of_selftest_property_copy();
Pantelis Antoniou201c9102014-07-04 19:58:49 +03001442 of_selftest_changeset();
Grant Likelya9f10ca2013-10-11 22:04:23 +01001443 of_selftest_parse_interrupts();
Grant Likely79d97012013-09-19 16:47:37 -05001444 of_selftest_parse_interrupts_extended();
Grant Likely1f42e5d2014-02-18 21:38:55 +00001445 of_selftest_match_node();
Rob Herring82c0f582014-04-23 17:57:40 -05001446 of_selftest_platform_populate();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001447 of_selftest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001448
1449 /* removing selftest data from live tree */
1450 selftest_data_remove();
1451
Grant Likelyf2051d62014-10-01 17:40:22 +01001452 /* Double check linkage after removing testcase data */
1453 of_selftest_check_tree_linkage();
1454
1455 pr_info("end of selftest - %i passed, %i failed\n",
1456 selftest_results.passed, selftest_results.failed);
1457
Grant Likely53a42092011-12-12 09:25:57 -07001458 return 0;
1459}
1460late_initcall(of_selftest);