blob: 29a35cb1da64c070ff1b6a96a76ad54d7c4cb636 [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>
Frank Rowand81d0848f2017-04-25 17:09:54 -070011#include <linux/libfdt.h>
Grant Likely53a42092011-12-12 09:25:57 -070012#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>
Grant Likely53a42092011-12-12 09:25:57 -070021
Pantelis Antonioud5e75502015-01-12 19:02:49 +020022#include <linux/i2c.h>
23#include <linux/i2c-mux.h>
24
Pantelis Antoniou492a22a2015-04-07 22:23:49 +030025#include <linux/bitops.h>
26
Pantelis Antoniou69843392014-07-04 19:58:47 +030027#include "of_private.h"
28
Wang Long9697a552015-03-11 08:36:54 +000029static struct unittest_results {
Grant Likelya9f10ca2013-10-11 22:04:23 +010030 int passed;
31 int failed;
Wang Long9697a552015-03-11 08:36:54 +000032} unittest_results;
Grant Likelya9f10ca2013-10-11 22:04:23 +010033
Wang Long9697a552015-03-11 08:36:54 +000034#define unittest(result, fmt, ...) ({ \
Grant Likely851da972014-11-04 13:14:13 +000035 bool failed = !(result); \
36 if (failed) { \
Wang Long9697a552015-03-11 08:36:54 +000037 unittest_results.failed++; \
Grant Likelya9f10ca2013-10-11 22:04:23 +010038 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000039 } else { \
Wang Long9697a552015-03-11 08:36:54 +000040 unittest_results.passed++; \
Grant Likelya9f10ca2013-10-11 22:04:23 +010041 pr_debug("pass %s():%i\n", __func__, __LINE__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000042 } \
Grant Likely851da972014-11-04 13:14:13 +000043 failed; \
44})
Grant Likely53a42092011-12-12 09:25:57 -070045
Wang Long9697a552015-03-11 08:36:54 +000046static void __init of_unittest_find_node_by_name(void)
Grant Likelyae91ff72014-03-14 13:53:10 +000047{
48 struct device_node *np;
Rob Herring0d638a02017-06-01 15:50:55 -050049 const char *options, *name;
Grant Likelyae91ff72014-03-14 13:53:10 +000050
51 np = of_find_node_by_path("/testcase-data");
Rob Herring0d638a02017-06-01 15:50:55 -050052 name = kasprintf(GFP_KERNEL, "%pOF", np);
53 unittest(np && !strcmp("/testcase-data", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000054 "find /testcase-data failed\n");
55 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050056 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000057
58 /* Test if trailing '/' works */
59 np = of_find_node_by_path("/testcase-data/");
Wang Long9697a552015-03-11 08:36:54 +000060 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
Grant Likelyae91ff72014-03-14 13:53:10 +000061
62 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
Rob Herring0d638a02017-06-01 15:50:55 -050063 name = kasprintf(GFP_KERNEL, "%pOF", np);
64 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000065 "find /testcase-data/phandle-tests/consumer-a failed\n");
66 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050067 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000068
69 np = of_find_node_by_path("testcase-alias");
Rob Herring0d638a02017-06-01 15:50:55 -050070 name = kasprintf(GFP_KERNEL, "%pOF", np);
71 unittest(np && !strcmp("/testcase-data", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000072 "find testcase-alias failed\n");
73 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050074 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000075
76 /* Test if trailing '/' works on aliases */
77 np = of_find_node_by_path("testcase-alias/");
Wang Long9697a552015-03-11 08:36:54 +000078 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
Grant Likelyae91ff72014-03-14 13:53:10 +000079
80 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
Rob Herring0d638a02017-06-01 15:50:55 -050081 name = kasprintf(GFP_KERNEL, "%pOF", np);
82 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000083 "find testcase-alias/phandle-tests/consumer-a failed\n");
84 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050085 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000086
87 np = of_find_node_by_path("/testcase-data/missing-path");
Rob Herring0d638a02017-06-01 15:50:55 -050088 unittest(!np, "non-existent path returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +000089 of_node_put(np);
90
91 np = of_find_node_by_path("missing-alias");
Rob Herring0d638a02017-06-01 15:50:55 -050092 unittest(!np, "non-existent alias returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +000093 of_node_put(np);
94
95 np = of_find_node_by_path("testcase-alias/missing-path");
Rob Herring0d638a02017-06-01 15:50:55 -050096 unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +000097 of_node_put(np);
Leif Lindholm75c28c02014-11-28 11:34:28 +000098
99 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
Wang Long9697a552015-03-11 08:36:54 +0000100 unittest(np && !strcmp("testoption", options),
Leif Lindholm75c28c02014-11-28 11:34:28 +0000101 "option path test failed\n");
102 of_node_put(np);
103
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500104 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
Wang Long9697a552015-03-11 08:36:54 +0000105 unittest(np && !strcmp("test/option", options),
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500106 "option path test, subcase #1 failed\n");
107 of_node_put(np);
108
Brian Norris5ca1b0d2015-03-17 12:30:32 -0700109 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
Wang Long9697a552015-03-11 08:36:54 +0000110 unittest(np && !strcmp("test/option", options),
Brian Norris5ca1b0d2015-03-17 12:30:32 -0700111 "option path test, subcase #2 failed\n");
112 of_node_put(np);
113
Leif Lindholm75c28c02014-11-28 11:34:28 +0000114 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000115 unittest(np, "NULL option path test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000116 of_node_put(np);
117
118 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
119 &options);
Wang Long9697a552015-03-11 08:36:54 +0000120 unittest(np && !strcmp("testaliasoption", options),
Leif Lindholm75c28c02014-11-28 11:34:28 +0000121 "option alias path test failed\n");
122 of_node_put(np);
123
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500124 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
125 &options);
Wang Long9697a552015-03-11 08:36:54 +0000126 unittest(np && !strcmp("test/alias/option", options),
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500127 "option alias path test, subcase #1 failed\n");
128 of_node_put(np);
129
Leif Lindholm75c28c02014-11-28 11:34:28 +0000130 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000131 unittest(np, "NULL option alias path test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000132 of_node_put(np);
133
134 options = "testoption";
135 np = of_find_node_opts_by_path("testcase-alias", &options);
Wang Long9697a552015-03-11 08:36:54 +0000136 unittest(np && !options, "option clearing test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000137 of_node_put(np);
138
139 options = "testoption";
140 np = of_find_node_opts_by_path("/", &options);
Wang Long9697a552015-03-11 08:36:54 +0000141 unittest(np && !options, "option clearing root node test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000142 of_node_put(np);
Grant Likelyae91ff72014-03-14 13:53:10 +0000143}
144
Wang Long9697a552015-03-11 08:36:54 +0000145static void __init of_unittest_dynamic(void)
Grant Likely7e66c5c2013-11-15 17:19:09 +0000146{
147 struct device_node *np;
148 struct property *prop;
149
150 np = of_find_node_by_path("/testcase-data");
151 if (!np) {
152 pr_err("missing testcase data\n");
153 return;
154 }
155
156 /* Array of 4 properties for the purpose of testing */
157 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
158 if (!prop) {
Wang Long9697a552015-03-11 08:36:54 +0000159 unittest(0, "kzalloc() failed\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000160 return;
161 }
162
163 /* Add a new property - should pass*/
164 prop->name = "new-property";
165 prop->value = "new-property-data";
166 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000167 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000168
169 /* Try to add an existing property - should fail */
170 prop++;
171 prop->name = "new-property";
172 prop->value = "new-property-data-should-fail";
173 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000174 unittest(of_add_property(np, prop) != 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000175 "Adding an existing property should have failed\n");
176
177 /* Try to modify an existing property - should pass */
178 prop->value = "modify-property-data-should-pass";
179 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000180 unittest(of_update_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000181 "Updating an existing property should have passed\n");
182
183 /* Try to modify non-existent property - should pass*/
184 prop++;
185 prop->name = "modify-property";
186 prop->value = "modify-missing-property-data-should-pass";
187 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000188 unittest(of_update_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000189 "Updating a missing property should have passed\n");
190
191 /* Remove property - should pass */
Wang Long9697a552015-03-11 08:36:54 +0000192 unittest(of_remove_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000193 "Removing a property should have passed\n");
194
195 /* Adding very large property - should pass */
196 prop++;
197 prop->name = "large-property-PAGE_SIZEx8";
198 prop->length = PAGE_SIZE * 8;
199 prop->value = kzalloc(prop->length, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000200 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000201 if (prop->value)
Wang Long9697a552015-03-11 08:36:54 +0000202 unittest(of_add_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000203 "Adding a large property should have passed\n");
204}
205
Wang Long9697a552015-03-11 08:36:54 +0000206static int __init of_unittest_check_node_linkage(struct device_node *np)
Grant Likelyf2051d62014-10-01 17:40:22 +0100207{
Grant Likely5063e252014-10-03 16:28:27 +0100208 struct device_node *child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100209 int count = 0, rc;
210
211 for_each_child_of_node(np, child) {
212 if (child->parent != np) {
213 pr_err("Child node %s links to wrong parent %s\n",
214 child->name, np->name);
Julia Lawall855ff282015-10-22 11:02:50 +0200215 rc = -EINVAL;
216 goto put_child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100217 }
218
Wang Long9697a552015-03-11 08:36:54 +0000219 rc = of_unittest_check_node_linkage(child);
Grant Likelyf2051d62014-10-01 17:40:22 +0100220 if (rc < 0)
Julia Lawall855ff282015-10-22 11:02:50 +0200221 goto put_child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100222 count += rc;
223 }
224
225 return count + 1;
Julia Lawall855ff282015-10-22 11:02:50 +0200226put_child:
227 of_node_put(child);
228 return rc;
Grant Likelyf2051d62014-10-01 17:40:22 +0100229}
230
Wang Long9697a552015-03-11 08:36:54 +0000231static void __init of_unittest_check_tree_linkage(void)
Grant Likelyf2051d62014-10-01 17:40:22 +0100232{
233 struct device_node *np;
234 int allnode_count = 0, child_count;
235
Grant Likely5063e252014-10-03 16:28:27 +0100236 if (!of_root)
Grant Likelyf2051d62014-10-01 17:40:22 +0100237 return;
238
239 for_each_of_allnodes(np)
240 allnode_count++;
Wang Long9697a552015-03-11 08:36:54 +0000241 child_count = of_unittest_check_node_linkage(of_root);
Grant Likelyf2051d62014-10-01 17:40:22 +0100242
Wang Long9697a552015-03-11 08:36:54 +0000243 unittest(child_count > 0, "Device node data structure is corrupted\n");
Grant Likelya2166ca2015-03-29 08:59:58 +0100244 unittest(child_count == allnode_count,
Frank Rowanda6bb1212015-03-13 23:59:01 -0700245 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
246 allnode_count, child_count);
Grant Likelyf2051d62014-10-01 17:40:22 +0100247 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
248}
249
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200250static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
251 const char *expected)
252{
253 unsigned char buf[strlen(expected)+10];
254 int size, i;
255
256 /* Baseline; check conversion with a large size limit */
257 memset(buf, 0xff, sizeof(buf));
258 size = snprintf(buf, sizeof(buf) - 2, fmt, np);
259
260 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
261 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
262 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
263 fmt, expected, buf);
264
265 /* Make sure length limits work */
266 size++;
267 for (i = 0; i < 2; i++, size--) {
268 /* Clear the buffer, and make sure it works correctly still */
269 memset(buf, 0xff, sizeof(buf));
270 snprintf(buf, size+1, fmt, np);
271 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
272 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
273 size, fmt, expected, buf);
274 }
275}
276
277static void __init of_unittest_printf(void)
278{
279 struct device_node *np;
280 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
281 char phandle_str[16] = "";
282
283 np = of_find_node_by_path(full_name);
284 if (!np) {
285 unittest(np, "testcase data missing\n");
286 return;
287 }
288
289 num_to_str(phandle_str, sizeof(phandle_str), np->phandle);
290
291 of_unittest_printf_one(np, "%pOF", full_name);
292 of_unittest_printf_one(np, "%pOFf", full_name);
293 of_unittest_printf_one(np, "%pOFp", phandle_str);
294 of_unittest_printf_one(np, "%pOFP", "dev@100");
295 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
296 of_unittest_printf_one(np, "%10pOFP", " dev@100");
297 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
298 of_unittest_printf_one(of_root, "%pOFP", "/");
299 of_unittest_printf_one(np, "%pOFF", "----");
300 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
301 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
302 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
303 of_unittest_printf_one(np, "%pOFC",
304 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
305}
306
Grant Likely841ec212014-10-02 13:09:15 +0100307struct node_hash {
308 struct hlist_node node;
309 struct device_node *np;
310};
311
Grant Likely2118f4b2014-10-07 11:30:31 +0100312static DEFINE_HASHTABLE(phandle_ht, 8);
Wang Long9697a552015-03-11 08:36:54 +0000313static void __init of_unittest_check_phandles(void)
Grant Likely841ec212014-10-02 13:09:15 +0100314{
315 struct device_node *np;
316 struct node_hash *nh;
317 struct hlist_node *tmp;
318 int i, dup_count = 0, phandle_count = 0;
Grant Likely841ec212014-10-02 13:09:15 +0100319
Grant Likely841ec212014-10-02 13:09:15 +0100320 for_each_of_allnodes(np) {
321 if (!np->phandle)
322 continue;
323
Grant Likely2118f4b2014-10-07 11:30:31 +0100324 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100325 if (nh->np->phandle == np->phandle) {
Rob Herring0d638a02017-06-01 15:50:55 -0500326 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
327 np->phandle, nh->np, np);
Grant Likely841ec212014-10-02 13:09:15 +0100328 dup_count++;
329 break;
330 }
331 }
332
333 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
334 if (WARN_ON(!nh))
335 return;
336
337 nh->np = np;
Grant Likely2118f4b2014-10-07 11:30:31 +0100338 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100339 phandle_count++;
340 }
Wang Long9697a552015-03-11 08:36:54 +0000341 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
Grant Likely841ec212014-10-02 13:09:15 +0100342 dup_count, phandle_count);
343
344 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100345 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100346 hash_del(&nh->node);
347 kfree(nh);
348 }
349}
350
Wang Long9697a552015-03-11 08:36:54 +0000351static void __init of_unittest_parse_phandle_with_args(void)
Grant Likely53a42092011-12-12 09:25:57 -0700352{
353 struct device_node *np;
354 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000355 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700356
Grant Likely53a42092011-12-12 09:25:57 -0700357 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
358 if (!np) {
359 pr_err("missing testcase data\n");
360 return;
361 }
362
Grant Likelybd69f732013-02-10 22:57:21 +0000363 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000364 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000365
Grant Likelyf7f951c2013-02-12 17:41:22 +0000366 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700367 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700368
Grant Likely53a42092011-12-12 09:25:57 -0700369 rc = of_parse_phandle_with_args(np, "phandle-list",
370 "#phandle-cells", i, &args);
371
372 /* Test the values from tests-phandle.dtsi */
373 switch (i) {
374 case 0:
375 passed &= !rc;
376 passed &= (args.args_count == 1);
377 passed &= (args.args[0] == (i + 1));
378 break;
379 case 1:
380 passed &= !rc;
381 passed &= (args.args_count == 2);
382 passed &= (args.args[0] == (i + 1));
383 passed &= (args.args[1] == 0);
384 break;
385 case 2:
386 passed &= (rc == -ENOENT);
387 break;
388 case 3:
389 passed &= !rc;
390 passed &= (args.args_count == 3);
391 passed &= (args.args[0] == (i + 1));
392 passed &= (args.args[1] == 4);
393 passed &= (args.args[2] == 3);
394 break;
395 case 4:
396 passed &= !rc;
397 passed &= (args.args_count == 2);
398 passed &= (args.args[0] == (i + 1));
399 passed &= (args.args[1] == 100);
400 break;
401 case 5:
402 passed &= !rc;
403 passed &= (args.args_count == 0);
404 break;
405 case 6:
406 passed &= !rc;
407 passed &= (args.args_count == 1);
408 passed &= (args.args[0] == (i + 1));
409 break;
410 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000411 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700412 break;
413 default:
414 passed = false;
415 }
416
Rob Herring0d638a02017-06-01 15:50:55 -0500417 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
418 i, args.np, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700419 }
420
421 /* Check for missing list property */
422 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
423 "#phandle-cells", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000424 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000425 rc = of_count_phandle_with_args(np, "phandle-list-missing",
426 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000427 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700428
429 /* Check for missing cells property */
430 rc = of_parse_phandle_with_args(np, "phandle-list",
431 "#phandle-cells-missing", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000432 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000433 rc = of_count_phandle_with_args(np, "phandle-list",
434 "#phandle-cells-missing");
Wang Long9697a552015-03-11 08:36:54 +0000435 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700436
437 /* Check for bad phandle in list */
438 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
439 "#phandle-cells", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000440 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000441 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
442 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000443 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700444
445 /* Check for incorrectly formed argument list */
446 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
447 "#phandle-cells", 1, &args);
Wang Long9697a552015-03-11 08:36:54 +0000448 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000449 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
450 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000451 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700452}
453
Wang Long9697a552015-03-11 08:36:54 +0000454static void __init of_unittest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700455{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000456 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700457 struct device_node *np;
458 int rc;
459
Grant Likely7aff0fe2011-12-12 09:25:58 -0700460 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
461 if (!np) {
462 pr_err("No testcase data in device tree\n");
463 return;
464 }
465
466 rc = of_property_match_string(np, "phandle-list-names", "first");
Wang Long9697a552015-03-11 08:36:54 +0000467 unittest(rc == 0, "first expected:0 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700468 rc = of_property_match_string(np, "phandle-list-names", "second");
Wang Long9697a552015-03-11 08:36:54 +0000469 unittest(rc == 1, "second expected:1 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700470 rc = of_property_match_string(np, "phandle-list-names", "third");
Wang Long9697a552015-03-11 08:36:54 +0000471 unittest(rc == 2, "third expected:2 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700472 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Wang Long9697a552015-03-11 08:36:54 +0000473 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700474 rc = of_property_match_string(np, "missing-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000475 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700476 rc = of_property_match_string(np, "empty-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000477 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700478 rc = of_property_match_string(np, "unterminated-string", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000479 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000480
481 /* of_property_count_strings() tests */
482 rc = of_property_count_strings(np, "string-property");
Wang Long9697a552015-03-11 08:36:54 +0000483 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000484 rc = of_property_count_strings(np, "phandle-list-names");
Wang Long9697a552015-03-11 08:36:54 +0000485 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000486 rc = of_property_count_strings(np, "unterminated-string");
Wang Long9697a552015-03-11 08:36:54 +0000487 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000488 rc = of_property_count_strings(np, "unterminated-string-list");
Wang Long9697a552015-03-11 08:36:54 +0000489 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000490
491 /* of_property_read_string_index() tests */
492 rc = of_property_read_string_index(np, "string-property", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000493 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000494 strings[0] = NULL;
495 rc = of_property_read_string_index(np, "string-property", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000496 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000497 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000498 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000499 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000500 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000501 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
Wang Long9697a552015-03-11 08:36:54 +0000502 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000503 strings[0] = NULL;
504 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
Wang Long9697a552015-03-11 08:36:54 +0000505 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000506 strings[0] = NULL;
507 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000508 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000509 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000510 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000511 strings[0] = NULL;
512 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
Wang Long9697a552015-03-11 08:36:54 +0000513 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000514 strings[1] = NULL;
515
516 /* of_property_read_string_array() tests */
517 rc = of_property_read_string_array(np, "string-property", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000518 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000519 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000520 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000521 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000522 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000523 /* -- An incorrectly formed string should cause a failure */
524 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000525 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000526 /* -- parsing the correctly formed strings should still work: */
527 strings[2] = NULL;
528 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
Wang Long9697a552015-03-11 08:36:54 +0000529 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000530 strings[1] = NULL;
531 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
Wang Long9697a552015-03-11 08:36:54 +0000532 unittest(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 -0700533}
534
Pantelis Antoniou69843392014-07-04 19:58:47 +0300535#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
536 (p1)->value && (p2)->value && \
537 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
538 !strcmp((p1)->name, (p2)->name))
Wang Long9697a552015-03-11 08:36:54 +0000539static void __init of_unittest_property_copy(void)
Pantelis Antoniou69843392014-07-04 19:58:47 +0300540{
541#ifdef CONFIG_OF_DYNAMIC
542 struct property p1 = { .name = "p1", .length = 0, .value = "" };
543 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
544 struct property *new;
545
546 new = __of_prop_dup(&p1, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000547 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300548 kfree(new->value);
549 kfree(new->name);
550 kfree(new);
551
552 new = __of_prop_dup(&p2, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000553 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300554 kfree(new->value);
555 kfree(new->name);
556 kfree(new);
557#endif
558}
559
Wang Long9697a552015-03-11 08:36:54 +0000560static void __init of_unittest_changeset(void)
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300561{
562#ifdef CONFIG_OF_DYNAMIC
563 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
564 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
565 struct property *ppremove;
Grant Likelye5179582014-11-17 22:31:32 +0000566 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300567 struct of_changeset chgset;
568
Grant Likelye5179582014-11-17 22:31:32 +0000569 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
Wang Long9697a552015-03-11 08:36:54 +0000570 unittest(n1, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000571 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
Wang Long9697a552015-03-11 08:36:54 +0000572 unittest(n2, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000573 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
Wang Long9697a552015-03-11 08:36:54 +0000574 unittest(n21, "testcase setup failure %p\n", n21);
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300575 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
Wang Long9697a552015-03-11 08:36:54 +0000576 unittest(nremove, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300577 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000578 unittest(ppadd, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300579 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000580 unittest(ppupdate, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300581 parent = nremove->parent;
582 n1->parent = parent;
583 n2->parent = parent;
584 n21->parent = n2;
585 n2->child = n21;
586 ppremove = of_find_property(parent, "prop-remove", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000587 unittest(ppremove, "failed to find removal prop");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300588
589 of_changeset_init(&chgset);
Wang Long9697a552015-03-11 08:36:54 +0000590 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
591 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
592 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
593 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
594 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
595 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
596 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
Wang Long9697a552015-03-11 08:36:54 +0000597 unittest(!of_changeset_apply(&chgset), "apply failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300598
Grant Likelye5179582014-11-17 22:31:32 +0000599 /* Make sure node names are constructed correctly */
Wang Long9697a552015-03-11 08:36:54 +0000600 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
Rob Herring0d638a02017-06-01 15:50:55 -0500601 "'%pOF' not added\n", n21);
Markus Elfringc46ca3c2014-12-02 13:54:00 +0100602 of_node_put(np);
Grant Likelye5179582014-11-17 22:31:32 +0000603
Wang Long9697a552015-03-11 08:36:54 +0000604 unittest(!of_changeset_revert(&chgset), "revert failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300605
606 of_changeset_destroy(&chgset);
607#endif
608}
609
Wang Long9697a552015-03-11 08:36:54 +0000610static void __init of_unittest_parse_interrupts(void)
Grant Likelya9f10ca2013-10-11 22:04:23 +0100611{
612 struct device_node *np;
613 struct of_phandle_args args;
614 int i, rc;
615
616 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
617 if (!np) {
618 pr_err("missing testcase data\n");
619 return;
620 }
621
622 for (i = 0; i < 4; i++) {
623 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700624
Grant Likelya9f10ca2013-10-11 22:04:23 +0100625 args.args_count = 0;
626 rc = of_irq_parse_one(np, i, &args);
627
628 passed &= !rc;
629 passed &= (args.args_count == 1);
630 passed &= (args.args[0] == (i + 1));
631
Rob Herring0d638a02017-06-01 15:50:55 -0500632 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
633 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100634 }
635 of_node_put(np);
636
637 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
638 if (!np) {
639 pr_err("missing testcase data\n");
640 return;
641 }
642
643 for (i = 0; i < 4; i++) {
644 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700645
Grant Likelya9f10ca2013-10-11 22:04:23 +0100646 args.args_count = 0;
647 rc = of_irq_parse_one(np, i, &args);
648
649 /* Test the values from tests-phandle.dtsi */
650 switch (i) {
651 case 0:
652 passed &= !rc;
653 passed &= (args.args_count == 1);
654 passed &= (args.args[0] == 9);
655 break;
656 case 1:
657 passed &= !rc;
658 passed &= (args.args_count == 3);
659 passed &= (args.args[0] == 10);
660 passed &= (args.args[1] == 11);
661 passed &= (args.args[2] == 12);
662 break;
663 case 2:
664 passed &= !rc;
665 passed &= (args.args_count == 2);
666 passed &= (args.args[0] == 13);
667 passed &= (args.args[1] == 14);
668 break;
669 case 3:
670 passed &= !rc;
671 passed &= (args.args_count == 2);
672 passed &= (args.args[0] == 15);
673 passed &= (args.args[1] == 16);
674 break;
675 default:
676 passed = false;
677 }
Rob Herring0d638a02017-06-01 15:50:55 -0500678 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
679 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100680 }
681 of_node_put(np);
682}
683
Wang Long9697a552015-03-11 08:36:54 +0000684static void __init of_unittest_parse_interrupts_extended(void)
Grant Likely79d97012013-09-19 16:47:37 -0500685{
686 struct device_node *np;
687 struct of_phandle_args args;
688 int i, rc;
689
690 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
691 if (!np) {
692 pr_err("missing testcase data\n");
693 return;
694 }
695
696 for (i = 0; i < 7; i++) {
697 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700698
Grant Likely79d97012013-09-19 16:47:37 -0500699 rc = of_irq_parse_one(np, i, &args);
700
701 /* Test the values from tests-phandle.dtsi */
702 switch (i) {
703 case 0:
704 passed &= !rc;
705 passed &= (args.args_count == 1);
706 passed &= (args.args[0] == 1);
707 break;
708 case 1:
709 passed &= !rc;
710 passed &= (args.args_count == 3);
711 passed &= (args.args[0] == 2);
712 passed &= (args.args[1] == 3);
713 passed &= (args.args[2] == 4);
714 break;
715 case 2:
716 passed &= !rc;
717 passed &= (args.args_count == 2);
718 passed &= (args.args[0] == 5);
719 passed &= (args.args[1] == 6);
720 break;
721 case 3:
722 passed &= !rc;
723 passed &= (args.args_count == 1);
724 passed &= (args.args[0] == 9);
725 break;
726 case 4:
727 passed &= !rc;
728 passed &= (args.args_count == 3);
729 passed &= (args.args[0] == 10);
730 passed &= (args.args[1] == 11);
731 passed &= (args.args[2] == 12);
732 break;
733 case 5:
734 passed &= !rc;
735 passed &= (args.args_count == 2);
736 passed &= (args.args[0] == 13);
737 passed &= (args.args[1] == 14);
738 break;
739 case 6:
740 passed &= !rc;
741 passed &= (args.args_count == 1);
742 passed &= (args.args[0] == 15);
743 break;
744 default:
745 passed = false;
746 }
747
Rob Herring0d638a02017-06-01 15:50:55 -0500748 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
749 i, args.np, rc);
Grant Likely79d97012013-09-19 16:47:37 -0500750 }
751 of_node_put(np);
752}
753
Frank Rowandafaed7a2015-03-14 00:00:36 -0700754static const struct of_device_id match_node_table[] = {
Grant Likely1f42e5d2014-02-18 21:38:55 +0000755 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
756 { .data = "B", .type = "type1", }, /* followed by type alone */
757
758 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
759 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
760 { .data = "Cc", .name = "name2", .type = "type2", },
761
762 { .data = "E", .compatible = "compat3" },
763 { .data = "G", .compatible = "compat2", },
764 { .data = "H", .compatible = "compat2", .name = "name5", },
765 { .data = "I", .compatible = "compat2", .type = "type1", },
766 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
767 { .data = "K", .compatible = "compat2", .name = "name9", },
768 {}
769};
770
771static struct {
772 const char *path;
773 const char *data;
774} match_node_tests[] = {
775 { .path = "/testcase-data/match-node/name0", .data = "A", },
776 { .path = "/testcase-data/match-node/name1", .data = "B", },
777 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
778 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
779 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
780 { .path = "/testcase-data/match-node/name3", .data = "E", },
781 { .path = "/testcase-data/match-node/name4", .data = "G", },
782 { .path = "/testcase-data/match-node/name5", .data = "H", },
783 { .path = "/testcase-data/match-node/name6", .data = "G", },
784 { .path = "/testcase-data/match-node/name7", .data = "I", },
785 { .path = "/testcase-data/match-node/name8", .data = "J", },
786 { .path = "/testcase-data/match-node/name9", .data = "K", },
787};
788
Wang Long9697a552015-03-11 08:36:54 +0000789static void __init of_unittest_match_node(void)
Grant Likely1f42e5d2014-02-18 21:38:55 +0000790{
791 struct device_node *np;
792 const struct of_device_id *match;
793 int i;
794
795 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
796 np = of_find_node_by_path(match_node_tests[i].path);
797 if (!np) {
Wang Long9697a552015-03-11 08:36:54 +0000798 unittest(0, "missing testcase node %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000799 match_node_tests[i].path);
800 continue;
801 }
802
803 match = of_match_node(match_node_table, np);
804 if (!match) {
Wang Long9697a552015-03-11 08:36:54 +0000805 unittest(0, "%s didn't match anything\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000806 match_node_tests[i].path);
807 continue;
808 }
809
810 if (strcmp(match->data, match_node_tests[i].data) != 0) {
Wang Long9697a552015-03-11 08:36:54 +0000811 unittest(0, "%s got wrong match. expected %s, got %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000812 match_node_tests[i].path, match_node_tests[i].data,
813 (const char *)match->data);
814 continue;
815 }
Wang Long9697a552015-03-11 08:36:54 +0000816 unittest(1, "passed");
Grant Likely1f42e5d2014-02-18 21:38:55 +0000817 }
818}
819
Grant Likelyd2329fb2016-01-04 13:13:21 +0100820static struct resource test_bus_res = {
821 .start = 0xfffffff8,
822 .end = 0xfffffff9,
823 .flags = IORESOURCE_MEM,
824};
Grant Likely37791b62015-03-27 20:30:04 -0700825static const struct platform_device_info test_bus_info = {
826 .name = "unittest-bus",
Grant Likely851da972014-11-04 13:14:13 +0000827};
Wang Long9697a552015-03-11 08:36:54 +0000828static void __init of_unittest_platform_populate(void)
Rob Herring82c0f582014-04-23 17:57:40 -0500829{
Grant Likely851da972014-11-04 13:14:13 +0000830 int irq, rc;
831 struct device_node *np, *child, *grandchild;
Grant Likely37791b62015-03-27 20:30:04 -0700832 struct platform_device *pdev, *test_bus;
Frank Rowandafaed7a2015-03-14 00:00:36 -0700833 const struct of_device_id match[] = {
Rob Herringfb2caa52014-05-13 10:07:54 -0500834 { .compatible = "test-device", },
835 {}
836 };
Rob Herring82c0f582014-04-23 17:57:40 -0500837
838 np = of_find_node_by_path("/testcase-data");
Kefeng Wang146dedb2016-06-01 14:53:10 +0800839 of_platform_default_populate(np, NULL, NULL);
Rob Herring82c0f582014-04-23 17:57:40 -0500840
841 /* Test that a missing irq domain returns -EPROBE_DEFER */
842 np = of_find_node_by_path("/testcase-data/testcase-device1");
843 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +0000844 unittest(pdev, "device 1 creation failed\n");
Rob Herring7d1cdc82014-05-13 10:07:29 -0500845
Rob Herring82c0f582014-04-23 17:57:40 -0500846 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +0000847 unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500848
849 /* Test that a parsing failure does not return -EPROBE_DEFER */
850 np = of_find_node_by_path("/testcase-data/testcase-device2");
851 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +0000852 unittest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500853 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +0000854 unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500855
Frank Rowand716e1d42015-03-13 23:57:40 -0700856 np = of_find_node_by_path("/testcase-data/platform-tests");
Grant Likelya2166ca2015-03-29 08:59:58 +0100857 unittest(np, "No testcase data in device tree\n");
Frank Rowand716e1d42015-03-13 23:57:40 -0700858 if (!np)
Rob Herringfb2caa52014-05-13 10:07:54 -0500859 return;
Grant Likely851da972014-11-04 13:14:13 +0000860
Grant Likely37791b62015-03-27 20:30:04 -0700861 test_bus = platform_device_register_full(&test_bus_info);
862 rc = PTR_ERR_OR_ZERO(test_bus);
Grant Likelya2166ca2015-03-29 08:59:58 +0100863 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
Frank Rowand716e1d42015-03-13 23:57:40 -0700864 if (rc)
Grant Likely851da972014-11-04 13:14:13 +0000865 return;
Grant Likely37791b62015-03-27 20:30:04 -0700866 test_bus->dev.of_node = np;
Rob Herringfb2caa52014-05-13 10:07:54 -0500867
Grant Likelyd2329fb2016-01-04 13:13:21 +0100868 /*
869 * Add a dummy resource to the test bus node after it is
870 * registered to catch problems with un-inserted resources. The
871 * DT code doesn't insert the resources, and it has caused the
872 * kernel to oops in the past. This makes sure the same bug
873 * doesn't crop up again.
874 */
875 platform_device_add_resources(test_bus, &test_bus_res, 1);
876
Grant Likely37791b62015-03-27 20:30:04 -0700877 of_platform_populate(np, match, NULL, &test_bus->dev);
Rob Herringfb2caa52014-05-13 10:07:54 -0500878 for_each_child_of_node(np, child) {
Rob Herringfb2caa52014-05-13 10:07:54 -0500879 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +0000880 unittest(of_find_device_by_node(grandchild),
Rob Herringfb2caa52014-05-13 10:07:54 -0500881 "Could not create device for node '%s'\n",
882 grandchild->name);
883 }
Grant Likely851da972014-11-04 13:14:13 +0000884
Grant Likely37791b62015-03-27 20:30:04 -0700885 of_platform_depopulate(&test_bus->dev);
Grant Likely851da972014-11-04 13:14:13 +0000886 for_each_child_of_node(np, child) {
887 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +0000888 unittest(!of_find_device_by_node(grandchild),
Grant Likely851da972014-11-04 13:14:13 +0000889 "device didn't get destroyed '%s'\n",
890 grandchild->name);
891 }
892
Grant Likely37791b62015-03-27 20:30:04 -0700893 platform_device_unregister(test_bus);
Grant Likely851da972014-11-04 13:14:13 +0000894 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -0500895}
896
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700897/**
898 * update_node_properties - adds the properties
899 * of np into dup node (present in live tree) and
900 * updates parent of children of np to dup.
901 *
902 * @np: node already present in live tree
903 * @dup: node present in live tree to be updated
904 */
905static void update_node_properties(struct device_node *np,
906 struct device_node *dup)
907{
908 struct property *prop;
909 struct device_node *child;
910
911 for_each_property_of_node(np, prop)
912 of_add_property(dup, prop);
913
914 for_each_child_of_node(np, child)
915 child->parent = dup;
916}
917
918/**
919 * attach_node_and_children - attaches nodes
920 * and its children to live tree
921 *
922 * @np: Node to attach to live tree
923 */
924static int attach_node_and_children(struct device_node *np)
925{
Grant Likely5063e252014-10-03 16:28:27 +0100926 struct device_node *next, *dup, *child;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800927 unsigned long flags;
Rob Herring0d638a02017-06-01 15:50:55 -0500928 const char *full_name;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700929
Rob Herring0d638a02017-06-01 15:50:55 -0500930 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
931 dup = of_find_node_by_path(full_name);
932 kfree(full_name);
Grant Likely5063e252014-10-03 16:28:27 +0100933 if (dup) {
934 update_node_properties(np, dup);
935 return 0;
936 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700937
Grant Likely5063e252014-10-03 16:28:27 +0100938 child = np->child;
939 np->child = NULL;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800940
941 mutex_lock(&of_mutex);
942 raw_spin_lock_irqsave(&devtree_lock, flags);
943 np->sibling = np->parent->child;
944 np->parent->child = np;
945 of_node_clear_flag(np, OF_DETACHED);
946 raw_spin_unlock_irqrestore(&devtree_lock, flags);
947
948 __of_attach_node_sysfs(np);
949 mutex_unlock(&of_mutex);
950
Grant Likely5063e252014-10-03 16:28:27 +0100951 while (child) {
952 next = child->sibling;
953 attach_node_and_children(child);
954 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700955 }
956
957 return 0;
958}
959
960/**
Wang Long9697a552015-03-11 08:36:54 +0000961 * unittest_data_add - Reads, copies data from
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700962 * linked tree and attaches it to the live tree
963 */
Wang Long9697a552015-03-11 08:36:54 +0000964static int __init unittest_data_add(void)
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700965{
Wang Long9697a552015-03-11 08:36:54 +0000966 void *unittest_data;
967 struct device_node *unittest_data_node, *np;
Frank Rowandc8547112015-03-14 00:04:24 -0700968 /*
969 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
970 * created by cmd_dt_S_dtb in scripts/Makefile.lib
971 */
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700972 extern uint8_t __dtb_testcases_begin[];
973 extern uint8_t __dtb_testcases_end[];
974 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +0100975 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700976
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700977 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700978 pr_warn("%s: No testcase data to attach; not running tests\n",
979 __func__);
980 return -ENODATA;
981 }
982
983 /* creating copy */
Wang Long9697a552015-03-11 08:36:54 +0000984 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700985
Wang Long9697a552015-03-11 08:36:54 +0000986 if (!unittest_data) {
987 pr_warn("%s: Failed to allocate memory for unittest_data; "
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700988 "not running tests\n", __func__);
989 return -ENOMEM;
990 }
Gavin Shanc4263232016-05-03 23:22:50 +1000991 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
Wang Long9697a552015-03-11 08:36:54 +0000992 if (!unittest_data_node) {
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700993 pr_warn("%s: No tree to attach; not running tests\n", __func__);
994 return -ENODATA;
995 }
Wang Long9697a552015-03-11 08:36:54 +0000996 of_node_set_flag(unittest_data_node, OF_DETACHED);
997 rc = of_resolve_phandles(unittest_data_node);
Grant Likely2eb46da2014-10-02 14:36:46 +0100998 if (rc) {
999 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
1000 return -EINVAL;
1001 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001002
Grant Likely5063e252014-10-03 16:28:27 +01001003 if (!of_root) {
Wang Long9697a552015-03-11 08:36:54 +00001004 of_root = unittest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001005 for_each_of_allnodes(np)
1006 __of_attach_node_sysfs(np);
1007 of_aliases = of_find_node_by_path("/aliases");
1008 of_chosen = of_find_node_by_path("/chosen");
1009 return 0;
1010 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001011
1012 /* attach the sub-tree to live tree */
Wang Long9697a552015-03-11 08:36:54 +00001013 np = unittest_data_node->child;
Grant Likely5063e252014-10-03 16:28:27 +01001014 while (np) {
1015 struct device_node *next = np->sibling;
Frank Rowand3db316d2015-03-14 00:02:31 -07001016
Grant Likely5063e252014-10-03 16:28:27 +01001017 np->parent = of_root;
1018 attach_node_and_children(np);
1019 np = next;
1020 }
1021 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001022}
1023
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001024#ifdef CONFIG_OF_OVERLAY
1025
Wang Long9697a552015-03-11 08:36:54 +00001026static int unittest_probe(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001027{
1028 struct device *dev = &pdev->dev;
1029 struct device_node *np = dev->of_node;
1030
1031 if (np == NULL) {
1032 dev_err(dev, "No OF data for device\n");
1033 return -EINVAL;
1034
1035 }
1036
Rob Herring0d638a02017-06-01 15:50:55 -05001037 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001038
1039 of_platform_populate(np, NULL, NULL, &pdev->dev);
1040
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001041 return 0;
1042}
1043
Wang Long9697a552015-03-11 08:36:54 +00001044static int unittest_remove(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001045{
1046 struct device *dev = &pdev->dev;
1047 struct device_node *np = dev->of_node;
1048
Rob Herring0d638a02017-06-01 15:50:55 -05001049 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001050 return 0;
1051}
1052
Grant Likelya2166ca2015-03-29 08:59:58 +01001053static const struct of_device_id unittest_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001054 { .compatible = "unittest", },
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001055 {},
1056};
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001057
Wang Long9697a552015-03-11 08:36:54 +00001058static struct platform_driver unittest_driver = {
1059 .probe = unittest_probe,
1060 .remove = unittest_remove,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001061 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001062 .name = "unittest",
Wang Long9697a552015-03-11 08:36:54 +00001063 .of_match_table = of_match_ptr(unittest_match),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001064 },
1065};
1066
1067/* get the platform device instantiated at the path */
1068static struct platform_device *of_path_to_platform_device(const char *path)
1069{
1070 struct device_node *np;
1071 struct platform_device *pdev;
1072
1073 np = of_find_node_by_path(path);
1074 if (np == NULL)
1075 return NULL;
1076
1077 pdev = of_find_device_by_node(np);
1078 of_node_put(np);
1079
1080 return pdev;
1081}
1082
1083/* find out if a platform device exists at that path */
1084static int of_path_platform_device_exists(const char *path)
1085{
1086 struct platform_device *pdev;
1087
1088 pdev = of_path_to_platform_device(path);
1089 platform_device_put(pdev);
1090 return pdev != NULL;
1091}
1092
Arnd Bergmann4252de32015-03-04 20:49:47 +01001093#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001094
1095/* get the i2c client device instantiated at the path */
1096static struct i2c_client *of_path_to_i2c_client(const char *path)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001097{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001098 struct device_node *np;
1099 struct i2c_client *client;
1100
1101 np = of_find_node_by_path(path);
1102 if (np == NULL)
1103 return NULL;
1104
1105 client = of_find_i2c_device_by_node(np);
1106 of_node_put(np);
1107
1108 return client;
1109}
1110
1111/* find out if a i2c client device exists at that path */
1112static int of_path_i2c_client_exists(const char *path)
1113{
1114 struct i2c_client *client;
1115
1116 client = of_path_to_i2c_client(path);
1117 if (client)
1118 put_device(&client->dev);
1119 return client != NULL;
1120}
1121#else
1122static int of_path_i2c_client_exists(const char *path)
1123{
1124 return 0;
1125}
1126#endif
1127
1128enum overlay_type {
1129 PDEV_OVERLAY,
1130 I2C_OVERLAY
1131};
1132
1133static int of_path_device_type_exists(const char *path,
1134 enum overlay_type ovtype)
1135{
1136 switch (ovtype) {
1137 case PDEV_OVERLAY:
1138 return of_path_platform_device_exists(path);
1139 case I2C_OVERLAY:
1140 return of_path_i2c_client_exists(path);
1141 }
1142 return 0;
1143}
1144
Wang Long9697a552015-03-11 08:36:54 +00001145static const char *unittest_path(int nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001146{
1147 const char *base;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001148 static char buf[256];
1149
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001150 switch (ovtype) {
1151 case PDEV_OVERLAY:
1152 base = "/testcase-data/overlay-node/test-bus";
1153 break;
1154 case I2C_OVERLAY:
1155 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1156 break;
1157 default:
1158 buf[0] = '\0';
1159 return buf;
1160 }
Wang Long9697a552015-03-11 08:36:54 +00001161 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001162 buf[sizeof(buf) - 1] = '\0';
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001163 return buf;
1164}
1165
Wang Long9697a552015-03-11 08:36:54 +00001166static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001167{
1168 const char *path;
1169
Wang Long9697a552015-03-11 08:36:54 +00001170 path = unittest_path(unittest_nr, ovtype);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001171
1172 switch (ovtype) {
1173 case PDEV_OVERLAY:
1174 return of_path_platform_device_exists(path);
1175 case I2C_OVERLAY:
1176 return of_path_i2c_client_exists(path);
1177 }
1178 return 0;
1179}
1180
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001181static const char *overlay_path(int nr)
1182{
1183 static char buf[256];
1184
1185 snprintf(buf, sizeof(buf) - 1,
1186 "/testcase-data/overlay%d", nr);
1187 buf[sizeof(buf) - 1] = '\0';
1188
1189 return buf;
1190}
1191
1192static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1193
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001194/* it is guaranteed that overlay ids are assigned in sequence */
1195#define MAX_UNITTEST_OVERLAYS 256
1196static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1197static int overlay_first_id = -1;
1198
1199static void of_unittest_track_overlay(int id)
1200{
1201 if (overlay_first_id < 0)
1202 overlay_first_id = id;
1203 id -= overlay_first_id;
1204
1205 /* we shouldn't need that many */
1206 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1207 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1208}
1209
1210static void of_unittest_untrack_overlay(int id)
1211{
1212 if (overlay_first_id < 0)
1213 return;
1214 id -= overlay_first_id;
1215 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1216 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1217}
1218
1219static void of_unittest_destroy_tracked_overlays(void)
1220{
1221 int id, ret, defers;
1222
1223 if (overlay_first_id < 0)
1224 return;
1225
1226 /* try until no defers */
1227 do {
1228 defers = 0;
1229 /* remove in reverse order */
1230 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1231 if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1232 continue;
1233
1234 ret = of_overlay_destroy(id + overlay_first_id);
Sergey Senozhatsky815d74b2016-03-02 20:24:49 +09001235 if (ret == -ENODEV) {
1236 pr_warn("%s: no overlay to destroy for #%d\n",
1237 __func__, id + overlay_first_id);
1238 continue;
1239 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001240 if (ret != 0) {
1241 defers++;
1242 pr_warn("%s: overlay destroy failed for #%d\n",
1243 __func__, id + overlay_first_id);
1244 continue;
1245 }
1246
1247 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1248 }
1249 } while (defers > 0);
1250}
1251
Alexander Sverdlinca7b8962017-01-19 11:06:16 +01001252static int of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001253 int *overlay_id)
1254{
1255 struct device_node *np = NULL;
1256 int ret, id = -1;
1257
1258 np = of_find_node_by_path(overlay_path(overlay_nr));
1259 if (np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001260 unittest(0, "could not find overlay node @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001261 overlay_path(overlay_nr));
1262 ret = -EINVAL;
1263 goto out;
1264 }
1265
1266 ret = of_overlay_create(np);
1267 if (ret < 0) {
Wang Long9697a552015-03-11 08:36:54 +00001268 unittest(0, "could not create overlay from \"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001269 overlay_path(overlay_nr));
1270 goto out;
1271 }
1272 id = ret;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001273 of_unittest_track_overlay(id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001274
1275 ret = 0;
1276
1277out:
1278 of_node_put(np);
1279
1280 if (overlay_id)
1281 *overlay_id = id;
1282
1283 return ret;
1284}
1285
1286/* apply an overlay while checking before and after states */
Wang Long9697a552015-03-11 08:36:54 +00001287static int of_unittest_apply_overlay_check(int overlay_nr, int unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001288 int before, int after, enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001289{
1290 int ret;
1291
Wang Long9697a552015-03-11 08:36:54 +00001292 /* unittest device must not be in before state */
1293 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1294 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001295 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001296 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001297 !before ? "enabled" : "disabled");
1298 return -EINVAL;
1299 }
1300
Wang Long9697a552015-03-11 08:36:54 +00001301 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, NULL);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001302 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001303 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001304 return ret;
1305 }
1306
Wang Long9697a552015-03-11 08:36:54 +00001307 /* unittest device must be to set to after state */
1308 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1309 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001310 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001311 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001312 !after ? "enabled" : "disabled");
1313 return -EINVAL;
1314 }
1315
1316 return 0;
1317}
1318
1319/* apply an overlay and then revert it while checking before, after states */
Wang Long9697a552015-03-11 08:36:54 +00001320static int of_unittest_apply_revert_overlay_check(int overlay_nr,
1321 int unittest_nr, int before, int after,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001322 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001323{
1324 int ret, ov_id;
1325
Wang Long9697a552015-03-11 08:36:54 +00001326 /* unittest device must be in before state */
1327 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1328 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001329 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001330 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001331 !before ? "enabled" : "disabled");
1332 return -EINVAL;
1333 }
1334
1335 /* apply the overlay */
Wang Long9697a552015-03-11 08:36:54 +00001336 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ov_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001337 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001338 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001339 return ret;
1340 }
1341
Wang Long9697a552015-03-11 08:36:54 +00001342 /* unittest device must be in after state */
1343 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1344 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001345 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001346 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001347 !after ? "enabled" : "disabled");
1348 return -EINVAL;
1349 }
1350
1351 ret = of_overlay_destroy(ov_id);
1352 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001353 unittest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001354 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001355 unittest_path(unittest_nr, ovtype));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001356 return ret;
1357 }
1358
Wang Long9697a552015-03-11 08:36:54 +00001359 /* unittest device must be again in before state */
1360 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
1361 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001362 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001363 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001364 !before ? "enabled" : "disabled");
1365 return -EINVAL;
1366 }
1367
1368 return 0;
1369}
1370
1371/* test activation of device */
Wang Long9697a552015-03-11 08:36:54 +00001372static void of_unittest_overlay_0(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001373{
1374 int ret;
1375
1376 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001377 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001378 if (ret != 0)
1379 return;
1380
Wang Long9697a552015-03-11 08:36:54 +00001381 unittest(1, "overlay test %d passed\n", 0);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001382}
1383
1384/* test deactivation of device */
Wang Long9697a552015-03-11 08:36:54 +00001385static void of_unittest_overlay_1(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001386{
1387 int ret;
1388
1389 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001390 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001391 if (ret != 0)
1392 return;
1393
Wang Long9697a552015-03-11 08:36:54 +00001394 unittest(1, "overlay test %d passed\n", 1);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001395}
1396
1397/* test activation of device */
Wang Long9697a552015-03-11 08:36:54 +00001398static void of_unittest_overlay_2(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001399{
1400 int ret;
1401
1402 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001403 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001404 if (ret != 0)
1405 return;
1406
Wang Long9697a552015-03-11 08:36:54 +00001407 unittest(1, "overlay test %d passed\n", 2);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001408}
1409
1410/* test deactivation of device */
Wang Long9697a552015-03-11 08:36:54 +00001411static void of_unittest_overlay_3(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001412{
1413 int ret;
1414
1415 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001416 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001417 if (ret != 0)
1418 return;
1419
Wang Long9697a552015-03-11 08:36:54 +00001420 unittest(1, "overlay test %d passed\n", 3);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001421}
1422
1423/* test activation of a full device node */
Wang Long9697a552015-03-11 08:36:54 +00001424static void of_unittest_overlay_4(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001425{
1426 int ret;
1427
1428 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001429 ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001430 if (ret != 0)
1431 return;
1432
Wang Long9697a552015-03-11 08:36:54 +00001433 unittest(1, "overlay test %d passed\n", 4);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001434}
1435
1436/* test overlay apply/revert sequence */
Wang Long9697a552015-03-11 08:36:54 +00001437static void of_unittest_overlay_5(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001438{
1439 int ret;
1440
1441 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001442 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001443 if (ret != 0)
1444 return;
1445
Wang Long9697a552015-03-11 08:36:54 +00001446 unittest(1, "overlay test %d passed\n", 5);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001447}
1448
1449/* test overlay application in sequence */
Wang Long9697a552015-03-11 08:36:54 +00001450static void of_unittest_overlay_6(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001451{
1452 struct device_node *np;
1453 int ret, i, ov_id[2];
Wang Long9697a552015-03-11 08:36:54 +00001454 int overlay_nr = 6, unittest_nr = 6;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001455 int before = 0, after = 1;
1456
Wang Long9697a552015-03-11 08:36:54 +00001457 /* unittest device must be in before state */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001458 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001459 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001460 != before) {
Wang Long9697a552015-03-11 08:36:54 +00001461 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001462 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001463 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001464 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001465 !before ? "enabled" : "disabled");
1466 return;
1467 }
1468 }
1469
1470 /* apply the overlays */
1471 for (i = 0; i < 2; i++) {
1472
1473 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1474 if (np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001475 unittest(0, "could not find overlay node @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001476 overlay_path(overlay_nr + i));
1477 return;
1478 }
1479
1480 ret = of_overlay_create(np);
1481 if (ret < 0) {
Wang Long9697a552015-03-11 08:36:54 +00001482 unittest(0, "could not create overlay from \"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001483 overlay_path(overlay_nr + i));
1484 return;
1485 }
1486 ov_id[i] = ret;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001487 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001488 }
1489
1490 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001491 /* unittest device must be in after state */
1492 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001493 != after) {
Wang Long9697a552015-03-11 08:36:54 +00001494 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001495 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001496 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001497 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001498 !after ? "enabled" : "disabled");
1499 return;
1500 }
1501 }
1502
1503 for (i = 1; i >= 0; i--) {
1504 ret = of_overlay_destroy(ov_id[i]);
1505 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001506 unittest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001507 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001508 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001509 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001510 return;
1511 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001512 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001513 }
1514
1515 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001516 /* unittest device must be again in before state */
1517 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001518 != before) {
Wang Long9697a552015-03-11 08:36:54 +00001519 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001520 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001521 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001522 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001523 !before ? "enabled" : "disabled");
1524 return;
1525 }
1526 }
1527
Wang Long9697a552015-03-11 08:36:54 +00001528 unittest(1, "overlay test %d passed\n", 6);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001529}
1530
1531/* test overlay application in sequence */
Wang Long9697a552015-03-11 08:36:54 +00001532static void of_unittest_overlay_8(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001533{
1534 struct device_node *np;
1535 int ret, i, ov_id[2];
Wang Long9697a552015-03-11 08:36:54 +00001536 int overlay_nr = 8, unittest_nr = 8;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001537
1538 /* we don't care about device state in this test */
1539
1540 /* apply the overlays */
1541 for (i = 0; i < 2; i++) {
1542
1543 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1544 if (np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001545 unittest(0, "could not find overlay node @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001546 overlay_path(overlay_nr + i));
1547 return;
1548 }
1549
1550 ret = of_overlay_create(np);
1551 if (ret < 0) {
Wang Long9697a552015-03-11 08:36:54 +00001552 unittest(0, "could not create overlay from \"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001553 overlay_path(overlay_nr + i));
1554 return;
1555 }
1556 ov_id[i] = ret;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001557 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001558 }
1559
1560 /* now try to remove first overlay (it should fail) */
1561 ret = of_overlay_destroy(ov_id[0]);
1562 if (ret == 0) {
Wang Long9697a552015-03-11 08:36:54 +00001563 unittest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001564 overlay_path(overlay_nr + 0),
Wang Long9697a552015-03-11 08:36:54 +00001565 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001566 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001567 return;
1568 }
1569
1570 /* removing them in order should work */
1571 for (i = 1; i >= 0; i--) {
1572 ret = of_overlay_destroy(ov_id[i]);
1573 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001574 unittest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001575 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001576 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001577 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001578 return;
1579 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001580 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001581 }
1582
Wang Long9697a552015-03-11 08:36:54 +00001583 unittest(1, "overlay test %d passed\n", 8);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001584}
1585
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001586/* test insertion of a bus with parent devices */
Wang Long9697a552015-03-11 08:36:54 +00001587static void of_unittest_overlay_10(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001588{
1589 int ret;
1590 char *child_path;
1591
1592 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001593 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1594 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001595 "overlay test %d failed; overlay application\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001596 return;
1597
Wang Long9697a552015-03-11 08:36:54 +00001598 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1599 unittest_path(10, PDEV_OVERLAY));
1600 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001601 return;
1602
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001603 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001604 kfree(child_path);
Wang Long9697a552015-03-11 08:36:54 +00001605 if (unittest(ret, "overlay test %d failed; no child device\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001606 return;
1607}
1608
1609/* test insertion of a bus with parent devices (and revert) */
Wang Long9697a552015-03-11 08:36:54 +00001610static void of_unittest_overlay_11(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001611{
1612 int ret;
1613
1614 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001615 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001616 PDEV_OVERLAY);
Wang Long9697a552015-03-11 08:36:54 +00001617 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001618 "overlay test %d failed; overlay application\n", 11))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001619 return;
1620}
1621
Arnd Bergmann4252de32015-03-04 20:49:47 +01001622#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001623
Wang Long9697a552015-03-11 08:36:54 +00001624struct unittest_i2c_bus_data {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001625 struct platform_device *pdev;
1626 struct i2c_adapter adap;
1627};
1628
Wang Long9697a552015-03-11 08:36:54 +00001629static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001630 struct i2c_msg *msgs, int num)
1631{
Wang Long9697a552015-03-11 08:36:54 +00001632 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001633
1634 (void)std;
1635
1636 return num;
1637}
1638
Wang Long9697a552015-03-11 08:36:54 +00001639static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001640{
1641 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1642}
1643
Wang Long9697a552015-03-11 08:36:54 +00001644static const struct i2c_algorithm unittest_i2c_algo = {
1645 .master_xfer = unittest_i2c_master_xfer,
1646 .functionality = unittest_i2c_functionality,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001647};
1648
Wang Long9697a552015-03-11 08:36:54 +00001649static int unittest_i2c_bus_probe(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001650{
1651 struct device *dev = &pdev->dev;
1652 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001653 struct unittest_i2c_bus_data *std;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001654 struct i2c_adapter *adap;
1655 int ret;
1656
1657 if (np == NULL) {
1658 dev_err(dev, "No OF data for device\n");
1659 return -EINVAL;
1660
1661 }
1662
Rob Herring0d638a02017-06-01 15:50:55 -05001663 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001664
1665 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1666 if (!std) {
Wang Long9697a552015-03-11 08:36:54 +00001667 dev_err(dev, "Failed to allocate unittest i2c data\n");
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001668 return -ENOMEM;
1669 }
1670
1671 /* link them together */
1672 std->pdev = pdev;
1673 platform_set_drvdata(pdev, std);
1674
1675 adap = &std->adap;
1676 i2c_set_adapdata(adap, std);
1677 adap->nr = -1;
1678 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1679 adap->class = I2C_CLASS_DEPRECATED;
Wang Long9697a552015-03-11 08:36:54 +00001680 adap->algo = &unittest_i2c_algo;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001681 adap->dev.parent = dev;
1682 adap->dev.of_node = dev->of_node;
1683 adap->timeout = 5 * HZ;
1684 adap->retries = 3;
1685
1686 ret = i2c_add_numbered_adapter(adap);
1687 if (ret != 0) {
1688 dev_err(dev, "Failed to add I2C adapter\n");
1689 return ret;
1690 }
1691
1692 return 0;
1693}
1694
Wang Long9697a552015-03-11 08:36:54 +00001695static int unittest_i2c_bus_remove(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001696{
1697 struct device *dev = &pdev->dev;
1698 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001699 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001700
Rob Herring0d638a02017-06-01 15:50:55 -05001701 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001702 i2c_del_adapter(&std->adap);
1703
1704 return 0;
1705}
1706
Grant Likelya2166ca2015-03-29 08:59:58 +01001707static const struct of_device_id unittest_i2c_bus_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001708 { .compatible = "unittest-i2c-bus", },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001709 {},
1710};
1711
Wang Long9697a552015-03-11 08:36:54 +00001712static struct platform_driver unittest_i2c_bus_driver = {
1713 .probe = unittest_i2c_bus_probe,
1714 .remove = unittest_i2c_bus_remove,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001715 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001716 .name = "unittest-i2c-bus",
1717 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001718 },
1719};
1720
Wang Long9697a552015-03-11 08:36:54 +00001721static int unittest_i2c_dev_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001722 const struct i2c_device_id *id)
1723{
1724 struct device *dev = &client->dev;
1725 struct device_node *np = client->dev.of_node;
1726
1727 if (!np) {
1728 dev_err(dev, "No OF node\n");
1729 return -EINVAL;
1730 }
1731
Rob Herring0d638a02017-06-01 15:50:55 -05001732 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001733
1734 return 0;
1735};
1736
Wang Long9697a552015-03-11 08:36:54 +00001737static int unittest_i2c_dev_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001738{
1739 struct device *dev = &client->dev;
1740 struct device_node *np = client->dev.of_node;
1741
Rob Herring0d638a02017-06-01 15:50:55 -05001742 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001743 return 0;
1744}
1745
Wang Long9697a552015-03-11 08:36:54 +00001746static const struct i2c_device_id unittest_i2c_dev_id[] = {
1747 { .name = "unittest-i2c-dev" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001748 { }
1749};
1750
Wang Long9697a552015-03-11 08:36:54 +00001751static struct i2c_driver unittest_i2c_dev_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001752 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001753 .name = "unittest-i2c-dev",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001754 },
Wang Long9697a552015-03-11 08:36:54 +00001755 .probe = unittest_i2c_dev_probe,
1756 .remove = unittest_i2c_dev_remove,
1757 .id_table = unittest_i2c_dev_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001758};
1759
Arnd Bergmann4252de32015-03-04 20:49:47 +01001760#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001761
Peter Rosinb6e3b712016-04-20 08:42:47 +02001762static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001763{
1764 return 0;
1765}
1766
Wang Long9697a552015-03-11 08:36:54 +00001767static int unittest_i2c_mux_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001768 const struct i2c_device_id *id)
1769{
Peter Rosinb6e3b712016-04-20 08:42:47 +02001770 int ret, i, nchans;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001771 struct device *dev = &client->dev;
1772 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1773 struct device_node *np = client->dev.of_node, *child;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001774 struct i2c_mux_core *muxc;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001775 u32 reg, max_reg;
1776
Rob Herring0d638a02017-06-01 15:50:55 -05001777 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001778
1779 if (!np) {
1780 dev_err(dev, "No OF node\n");
1781 return -EINVAL;
1782 }
1783
1784 max_reg = (u32)-1;
1785 for_each_child_of_node(np, child) {
1786 ret = of_property_read_u32(child, "reg", &reg);
1787 if (ret)
1788 continue;
1789 if (max_reg == (u32)-1 || reg > max_reg)
1790 max_reg = reg;
1791 }
1792 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1793 if (nchans == 0) {
1794 dev_err(dev, "No channels\n");
1795 return -EINVAL;
1796 }
1797
Peter Rosinb6e3b712016-04-20 08:42:47 +02001798 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
1799 unittest_i2c_mux_select_chan, NULL);
1800 if (!muxc)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001801 return -ENOMEM;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001802 for (i = 0; i < nchans; i++) {
Peter Rosinb6e3b712016-04-20 08:42:47 +02001803 ret = i2c_mux_add_adapter(muxc, 0, i, 0);
1804 if (ret) {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001805 dev_err(dev, "Failed to register mux #%d\n", i);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001806 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001807 return -ENODEV;
1808 }
1809 }
1810
Peter Rosinb6e3b712016-04-20 08:42:47 +02001811 i2c_set_clientdata(client, muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001812
1813 return 0;
1814};
1815
Wang Long9697a552015-03-11 08:36:54 +00001816static int unittest_i2c_mux_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001817{
1818 struct device *dev = &client->dev;
1819 struct device_node *np = client->dev.of_node;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001820 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001821
Rob Herring0d638a02017-06-01 15:50:55 -05001822 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001823 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001824 return 0;
1825}
1826
Wang Long9697a552015-03-11 08:36:54 +00001827static const struct i2c_device_id unittest_i2c_mux_id[] = {
1828 { .name = "unittest-i2c-mux" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001829 { }
1830};
1831
Wang Long9697a552015-03-11 08:36:54 +00001832static struct i2c_driver unittest_i2c_mux_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001833 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001834 .name = "unittest-i2c-mux",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001835 },
Wang Long9697a552015-03-11 08:36:54 +00001836 .probe = unittest_i2c_mux_probe,
1837 .remove = unittest_i2c_mux_remove,
1838 .id_table = unittest_i2c_mux_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001839};
1840
1841#endif
1842
Wang Long9697a552015-03-11 08:36:54 +00001843static int of_unittest_overlay_i2c_init(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001844{
1845 int ret;
1846
Wang Long9697a552015-03-11 08:36:54 +00001847 ret = i2c_add_driver(&unittest_i2c_dev_driver);
1848 if (unittest(ret == 0,
1849 "could not register unittest i2c device driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001850 return ret;
1851
Wang Long9697a552015-03-11 08:36:54 +00001852 ret = platform_driver_register(&unittest_i2c_bus_driver);
1853 if (unittest(ret == 0,
1854 "could not register unittest i2c bus driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001855 return ret;
1856
Arnd Bergmann4252de32015-03-04 20:49:47 +01001857#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001858 ret = i2c_add_driver(&unittest_i2c_mux_driver);
1859 if (unittest(ret == 0,
1860 "could not register unittest i2c mux driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001861 return ret;
1862#endif
1863
1864 return 0;
1865}
1866
Wang Long9697a552015-03-11 08:36:54 +00001867static void of_unittest_overlay_i2c_cleanup(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001868{
Arnd Bergmann4252de32015-03-04 20:49:47 +01001869#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001870 i2c_del_driver(&unittest_i2c_mux_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001871#endif
Wang Long9697a552015-03-11 08:36:54 +00001872 platform_driver_unregister(&unittest_i2c_bus_driver);
1873 i2c_del_driver(&unittest_i2c_dev_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001874}
1875
Wang Long9697a552015-03-11 08:36:54 +00001876static void of_unittest_overlay_i2c_12(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001877{
1878 int ret;
1879
1880 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001881 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001882 if (ret != 0)
1883 return;
1884
Wang Long9697a552015-03-11 08:36:54 +00001885 unittest(1, "overlay test %d passed\n", 12);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001886}
1887
1888/* test deactivation of device */
Wang Long9697a552015-03-11 08:36:54 +00001889static void of_unittest_overlay_i2c_13(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001890{
1891 int ret;
1892
1893 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001894 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001895 if (ret != 0)
1896 return;
1897
Wang Long9697a552015-03-11 08:36:54 +00001898 unittest(1, "overlay test %d passed\n", 13);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001899}
1900
1901/* just check for i2c mux existence */
Wang Long9697a552015-03-11 08:36:54 +00001902static void of_unittest_overlay_i2c_14(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001903{
1904}
1905
Wang Long9697a552015-03-11 08:36:54 +00001906static void of_unittest_overlay_i2c_15(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001907{
1908 int ret;
1909
1910 /* device should enable */
Alexander Sverdlinca7b8962017-01-19 11:06:16 +01001911 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001912 if (ret != 0)
1913 return;
1914
Wang Long9697a552015-03-11 08:36:54 +00001915 unittest(1, "overlay test %d passed\n", 15);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001916}
1917
1918#else
1919
Wang Long9697a552015-03-11 08:36:54 +00001920static inline void of_unittest_overlay_i2c_14(void) { }
1921static inline void of_unittest_overlay_i2c_15(void) { }
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001922
1923#endif
1924
Wang Long9697a552015-03-11 08:36:54 +00001925static void __init of_unittest_overlay(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001926{
1927 struct device_node *bus_np = NULL;
1928 int ret;
1929
Wang Long9697a552015-03-11 08:36:54 +00001930 ret = platform_driver_register(&unittest_driver);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001931 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001932 unittest(0, "could not register unittest driver\n");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001933 goto out;
1934 }
1935
1936 bus_np = of_find_node_by_path(bus_path);
1937 if (bus_np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001938 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001939 goto out;
1940 }
1941
Kefeng Wang146dedb2016-06-01 14:53:10 +08001942 ret = of_platform_default_populate(bus_np, NULL, NULL);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001943 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001944 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001945 goto out;
1946 }
1947
Wang Long9697a552015-03-11 08:36:54 +00001948 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
1949 unittest(0, "could not find unittest0 @ \"%s\"\n",
1950 unittest_path(100, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001951 goto out;
1952 }
1953
Wang Long9697a552015-03-11 08:36:54 +00001954 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
1955 unittest(0, "unittest1 @ \"%s\" should not exist\n",
1956 unittest_path(101, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001957 goto out;
1958 }
1959
Wang Long9697a552015-03-11 08:36:54 +00001960 unittest(1, "basic infrastructure of overlays passed");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001961
1962 /* tests in sequence */
Wang Long9697a552015-03-11 08:36:54 +00001963 of_unittest_overlay_0();
1964 of_unittest_overlay_1();
1965 of_unittest_overlay_2();
1966 of_unittest_overlay_3();
1967 of_unittest_overlay_4();
1968 of_unittest_overlay_5();
1969 of_unittest_overlay_6();
1970 of_unittest_overlay_8();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001971
Wang Long9697a552015-03-11 08:36:54 +00001972 of_unittest_overlay_10();
1973 of_unittest_overlay_11();
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001974
Arnd Bergmann4252de32015-03-04 20:49:47 +01001975#if IS_BUILTIN(CONFIG_I2C)
Wang Long9697a552015-03-11 08:36:54 +00001976 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001977 goto out;
1978
Wang Long9697a552015-03-11 08:36:54 +00001979 of_unittest_overlay_i2c_12();
1980 of_unittest_overlay_i2c_13();
1981 of_unittest_overlay_i2c_14();
1982 of_unittest_overlay_i2c_15();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001983
Wang Long9697a552015-03-11 08:36:54 +00001984 of_unittest_overlay_i2c_cleanup();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001985#endif
1986
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001987 of_unittest_destroy_tracked_overlays();
1988
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001989out:
1990 of_node_put(bus_np);
1991}
1992
1993#else
Wang Long9697a552015-03-11 08:36:54 +00001994static inline void __init of_unittest_overlay(void) { }
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001995#endif
1996
Frank Rowand60a00042017-07-19 09:25:20 -07001997#ifdef CONFIG_OF_OVERLAY
1998
Frank Rowand81d0848f2017-04-25 17:09:54 -07001999/*
2000 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2001 * in scripts/Makefile.lib
2002 */
2003
2004#define OVERLAY_INFO_EXTERN(name) \
2005 extern uint8_t __dtb_##name##_begin[]; \
2006 extern uint8_t __dtb_##name##_end[]
2007
2008#define OVERLAY_INFO(name, expected) \
2009{ .dtb_begin = __dtb_##name##_begin, \
2010 .dtb_end = __dtb_##name##_end, \
2011 .expected_result = expected, \
2012}
2013
2014struct overlay_info {
2015 uint8_t *dtb_begin;
2016 uint8_t *dtb_end;
2017 void *data;
2018 struct device_node *np_overlay;
2019 int expected_result;
2020 int overlay_id;
2021};
2022
2023OVERLAY_INFO_EXTERN(overlay_base);
2024OVERLAY_INFO_EXTERN(overlay);
2025OVERLAY_INFO_EXTERN(overlay_bad_phandle);
Frank Rowand60a00042017-07-19 09:25:20 -07002026OVERLAY_INFO_EXTERN(overlay_bad_symbol);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002027
2028/* order of entries is hard-coded into users of overlays[] */
2029static struct overlay_info overlays[] = {
2030 OVERLAY_INFO(overlay_base, -9999),
2031 OVERLAY_INFO(overlay, 0),
2032 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
Frank Rowand60a00042017-07-19 09:25:20 -07002033 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002034 {}
2035};
2036
2037static struct device_node *overlay_base_root;
2038
2039/*
2040 * Create base device tree for the overlay unittest.
2041 *
2042 * This is called from very early boot code.
2043 *
2044 * Do as much as possible the same way as done in __unflatten_device_tree
2045 * and other early boot steps for the normal FDT so that the overlay base
2046 * unflattened tree will have the same characteristics as the real tree
2047 * (such as having memory allocated by the early allocator). The goal
2048 * is to test "the real thing" as much as possible, and test "test setup
2049 * code" as little as possible.
2050 *
2051 * Have to stop before resolving phandles, because that uses kmalloc.
2052 */
2053void __init unittest_unflatten_overlay_base(void)
2054{
2055 struct overlay_info *info;
2056 u32 data_size;
2057 u32 size;
2058
2059 info = &overlays[0];
2060
2061 if (info->expected_result != -9999) {
2062 pr_err("No dtb 'overlay_base' to attach\n");
2063 return;
2064 }
2065
2066 data_size = info->dtb_end - info->dtb_begin;
2067 if (!data_size) {
2068 pr_err("No dtb 'overlay_base' to attach\n");
2069 return;
2070 }
2071
2072 size = fdt_totalsize(info->dtb_begin);
2073 if (size != data_size) {
2074 pr_err("dtb 'overlay_base' header totalsize != actual size");
2075 return;
2076 }
2077
2078 info->data = early_init_dt_alloc_memory_arch(size,
2079 roundup_pow_of_two(FDT_V17_SIZE));
2080 if (!info->data) {
2081 pr_err("alloc for dtb 'overlay_base' failed");
2082 return;
2083 }
2084
2085 memcpy(info->data, info->dtb_begin, size);
2086
2087 __unflatten_device_tree(info->data, NULL, &info->np_overlay,
2088 early_init_dt_alloc_memory_arch, true);
2089 overlay_base_root = info->np_overlay;
2090}
2091
2092/*
2093 * The purpose of of_unittest_overlay_data_add is to add an
2094 * overlay in the normal fashion. This is a test of the whole
2095 * picture, instead of testing individual elements.
2096 *
2097 * A secondary purpose is to be able to verify that the contents of
2098 * /proc/device-tree/ contains the updated structure and values from
2099 * the overlay. That must be verified separately in user space.
2100 *
2101 * Return 0 on unexpected error.
2102 */
2103static int __init overlay_data_add(int onum)
2104{
2105 struct overlay_info *info;
2106 int k;
2107 int ret;
2108 u32 size;
2109 u32 size_from_header;
2110
2111 for (k = 0, info = overlays; info; info++, k++) {
2112 if (k == onum)
2113 break;
2114 }
2115 if (onum > k)
2116 return 0;
2117
2118 size = info->dtb_end - info->dtb_begin;
2119 if (!size) {
2120 pr_err("no overlay to attach, %d\n", onum);
2121 ret = 0;
2122 }
2123
2124 size_from_header = fdt_totalsize(info->dtb_begin);
2125 if (size_from_header != size) {
2126 pr_err("overlay header totalsize != actual size, %d", onum);
2127 return 0;
2128 }
2129
2130 /*
2131 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
2132 * will create pointers to the passed in FDT in the EDT.
2133 */
2134 info->data = kmemdup(info->dtb_begin, size, GFP_KERNEL);
2135 if (!info->data) {
2136 pr_err("unable to allocate memory for data, %d\n", onum);
2137 return 0;
2138 }
2139
2140 of_fdt_unflatten_tree(info->data, NULL, &info->np_overlay);
2141 if (!info->np_overlay) {
2142 pr_err("unable to unflatten overlay, %d\n", onum);
2143 ret = 0;
2144 goto out_free_data;
2145 }
2146 of_node_set_flag(info->np_overlay, OF_DETACHED);
2147
2148 ret = of_resolve_phandles(info->np_overlay);
2149 if (ret) {
2150 pr_err("resolve ot phandles (ret=%d), %d\n", ret, onum);
2151 goto out_free_np_overlay;
2152 }
2153
2154 ret = of_overlay_create(info->np_overlay);
2155 if (ret < 0) {
2156 pr_err("of_overlay_create() (ret=%d), %d\n", ret, onum);
2157 goto out_free_np_overlay;
2158 } else {
2159 info->overlay_id = ret;
2160 ret = 0;
2161 }
2162
2163 pr_debug("__dtb_overlay_begin applied, overlay id %d\n", ret);
2164
2165 goto out;
2166
2167out_free_np_overlay:
2168 /*
2169 * info->np_overlay is the unflattened device tree
2170 * It has not been spliced into the live tree.
2171 */
2172
2173 /* todo: function to free unflattened device tree */
2174
2175out_free_data:
2176 kfree(info->data);
2177
2178out:
2179 return (ret == info->expected_result);
2180}
2181
2182/*
2183 * The purpose of of_unittest_overlay_high_level is to add an overlay
2184 * in the normal fashion. This is a test of the whole picture,
2185 * instead of individual elements.
2186 *
2187 * The first part of the function is _not_ normal overlay usage; it is
2188 * finishing splicing the base overlay device tree into the live tree.
2189 */
2190static __init void of_unittest_overlay_high_level(void)
2191{
2192 struct device_node *last_sibling;
2193 struct device_node *np;
2194 struct device_node *of_symbols;
2195 struct device_node *overlay_base_symbols;
2196 struct device_node **pprev;
2197 struct property *prop;
2198 int ret;
2199
2200 if (!overlay_base_root) {
2201 unittest(0, "overlay_base_root not initialized\n");
2202 return;
2203 }
2204
2205 /*
2206 * Could not fixup phandles in unittest_unflatten_overlay_base()
2207 * because kmalloc() was not yet available.
2208 */
2209 of_resolve_phandles(overlay_base_root);
2210
2211 /*
2212 * do not allow overlay_base to duplicate any node already in
2213 * tree, this greatly simplifies the code
2214 */
2215
2216 /*
2217 * remove overlay_base_root node "__local_fixups", after
2218 * being used by of_resolve_phandles()
2219 */
2220 pprev = &overlay_base_root->child;
2221 for (np = overlay_base_root->child; np; np = np->sibling) {
2222 if (!of_node_cmp(np->name, "__local_fixups__")) {
2223 *pprev = np->sibling;
2224 break;
2225 }
2226 pprev = &np->sibling;
2227 }
2228
2229 /* remove overlay_base_root node "__symbols__" if in live tree */
2230 of_symbols = of_get_child_by_name(of_root, "__symbols__");
2231 if (of_symbols) {
2232 /* will have to graft properties from node into live tree */
2233 pprev = &overlay_base_root->child;
2234 for (np = overlay_base_root->child; np; np = np->sibling) {
2235 if (!of_node_cmp(np->name, "__symbols__")) {
2236 overlay_base_symbols = np;
2237 *pprev = np->sibling;
2238 break;
2239 }
2240 pprev = &np->sibling;
2241 }
2242 }
2243
2244 for (np = overlay_base_root->child; np; np = np->sibling) {
2245 if (of_get_child_by_name(of_root, np->name)) {
2246 unittest(0, "illegal node name in overlay_base %s",
2247 np->name);
2248 return;
2249 }
2250 }
2251
2252 /*
2253 * overlay 'overlay_base' is not allowed to have root
2254 * properties, so only need to splice nodes into main device tree.
2255 *
2256 * root node of *overlay_base_root will not be freed, it is lost
2257 * memory.
2258 */
2259
2260 for (np = overlay_base_root->child; np; np = np->sibling)
2261 np->parent = of_root;
2262
2263 mutex_lock(&of_mutex);
2264
Arnd Bergmannee320b32017-04-28 11:44:11 +02002265 for (last_sibling = np = of_root->child; np; np = np->sibling)
Frank Rowand81d0848f2017-04-25 17:09:54 -07002266 last_sibling = np;
2267
2268 if (last_sibling)
2269 last_sibling->sibling = overlay_base_root->child;
2270 else
2271 of_root->child = overlay_base_root->child;
2272
2273 for_each_of_allnodes_from(overlay_base_root, np)
2274 __of_attach_node_sysfs(np);
2275
2276 if (of_symbols) {
2277 for_each_property_of_node(overlay_base_symbols, prop) {
2278 ret = __of_add_property(of_symbols, prop);
2279 if (ret) {
2280 unittest(0,
2281 "duplicate property '%s' in overlay_base node __symbols__",
2282 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002283 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002284 }
2285 ret = __of_add_property_sysfs(of_symbols, prop);
2286 if (ret) {
2287 unittest(0,
2288 "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
2289 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002290 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002291 }
2292 }
2293 }
2294
2295 mutex_unlock(&of_mutex);
2296
2297
2298 /* now do the normal overlay usage test */
2299
2300 unittest(overlay_data_add(1),
2301 "Adding overlay 'overlay' failed\n");
2302
2303 unittest(overlay_data_add(2),
2304 "Adding overlay 'overlay_bad_phandle' failed\n");
Frank Rowand60a00042017-07-19 09:25:20 -07002305
2306 unittest(overlay_data_add(3),
2307 "Adding overlay 'overlay_bad_symbol' failed\n");
2308
Dan Carpenter8756cd12017-05-03 22:49:50 +03002309 return;
2310
2311err_unlock:
2312 mutex_unlock(&of_mutex);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002313}
2314
2315#else
2316
2317static inline __init void of_unittest_overlay_high_level(void) {}
2318
2319#endif
2320
Wang Long9697a552015-03-11 08:36:54 +00002321static int __init of_unittest(void)
Grant Likely53a42092011-12-12 09:25:57 -07002322{
2323 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002324 int res;
2325
Wang Long9697a552015-03-11 08:36:54 +00002326 /* adding data for unittest */
2327 res = unittest_data_add();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002328 if (res)
2329 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00002330 if (!of_aliases)
2331 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07002332
2333 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2334 if (!np) {
2335 pr_info("No testcase data in device tree; not running tests\n");
2336 return 0;
2337 }
2338 of_node_put(np);
2339
Wang Long9697a552015-03-11 08:36:54 +00002340 pr_info("start of unittest - you will see error messages\n");
2341 of_unittest_check_tree_linkage();
2342 of_unittest_check_phandles();
2343 of_unittest_find_node_by_name();
2344 of_unittest_dynamic();
2345 of_unittest_parse_phandle_with_args();
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002346 of_unittest_printf();
Wang Long9697a552015-03-11 08:36:54 +00002347 of_unittest_property_string();
2348 of_unittest_property_copy();
2349 of_unittest_changeset();
2350 of_unittest_parse_interrupts();
2351 of_unittest_parse_interrupts_extended();
2352 of_unittest_match_node();
2353 of_unittest_platform_populate();
2354 of_unittest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002355
Grant Likelyf2051d62014-10-01 17:40:22 +01002356 /* Double check linkage after removing testcase data */
Wang Long9697a552015-03-11 08:36:54 +00002357 of_unittest_check_tree_linkage();
Grant Likelyf2051d62014-10-01 17:40:22 +01002358
Frank Rowand81d0848f2017-04-25 17:09:54 -07002359 of_unittest_overlay_high_level();
2360
Wang Long9697a552015-03-11 08:36:54 +00002361 pr_info("end of unittest - %i passed, %i failed\n",
2362 unittest_results.passed, unittest_results.failed);
Grant Likelyf2051d62014-10-01 17:40:22 +01002363
Grant Likely53a42092011-12-12 09:25:57 -07002364 return 0;
2365}
Wang Long9697a552015-03-11 08:36:54 +00002366late_initcall(of_unittest);