blob: 7a9abaae874d5446cf31b96f7f819f83acfad1ef [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Grant Likely53a42092011-12-12 09:25:57 -07002/*
3 * Self tests for device tree subsystem
4 */
5
Grant Likelycabb7d52013-02-12 21:19:37 +00006#define pr_fmt(fmt) "### dt-test ### " fmt
Grant Likely53a42092011-12-12 09:25:57 -07007
Rob Herring0fa1c572018-01-05 15:32:33 -06008#include <linux/bootmem.h>
Grant Likely53a42092011-12-12 09:25:57 -07009#include <linux/clk.h>
10#include <linux/err.h>
11#include <linux/errno.h>
Grant Likely841ec212014-10-02 13:09:15 +010012#include <linux/hashtable.h>
Frank Rowand81d0848f2017-04-25 17:09:54 -070013#include <linux/libfdt.h>
Grant Likely53a42092011-12-12 09:25:57 -070014#include <linux/of.h>
Gaurav Minochaae9304c2014-07-16 23:09:39 -070015#include <linux/of_fdt.h>
Grant Likelya9f10ca2013-10-11 22:04:23 +010016#include <linux/of_irq.h>
Rob Herring82c0f582014-04-23 17:57:40 -050017#include <linux/of_platform.h>
Grant Likely53a42092011-12-12 09:25:57 -070018#include <linux/list.h>
19#include <linux/mutex.h>
20#include <linux/slab.h>
21#include <linux/device.h>
Pantelis Antoniou177d2712014-10-28 22:35:59 +020022#include <linux/platform_device.h>
Grant Likely53a42092011-12-12 09:25:57 -070023
Pantelis Antonioud5e75502015-01-12 19:02:49 +020024#include <linux/i2c.h>
25#include <linux/i2c-mux.h>
26
Pantelis Antoniou492a22a2015-04-07 22:23:49 +030027#include <linux/bitops.h>
28
Pantelis Antoniou69843392014-07-04 19:58:47 +030029#include "of_private.h"
30
Wang Long9697a552015-03-11 08:36:54 +000031static struct unittest_results {
Grant Likelya9f10ca2013-10-11 22:04:23 +010032 int passed;
33 int failed;
Wang Long9697a552015-03-11 08:36:54 +000034} unittest_results;
Grant Likelya9f10ca2013-10-11 22:04:23 +010035
Wang Long9697a552015-03-11 08:36:54 +000036#define unittest(result, fmt, ...) ({ \
Grant Likely851da972014-11-04 13:14:13 +000037 bool failed = !(result); \
38 if (failed) { \
Wang Long9697a552015-03-11 08:36:54 +000039 unittest_results.failed++; \
Grant Likelya9f10ca2013-10-11 22:04:23 +010040 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000041 } else { \
Wang Long9697a552015-03-11 08:36:54 +000042 unittest_results.passed++; \
Grant Likelya9f10ca2013-10-11 22:04:23 +010043 pr_debug("pass %s():%i\n", __func__, __LINE__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000044 } \
Grant Likely851da972014-11-04 13:14:13 +000045 failed; \
46})
Grant Likely53a42092011-12-12 09:25:57 -070047
Wang Long9697a552015-03-11 08:36:54 +000048static void __init of_unittest_find_node_by_name(void)
Grant Likelyae91ff72014-03-14 13:53:10 +000049{
50 struct device_node *np;
Rob Herring0d638a02017-06-01 15:50:55 -050051 const char *options, *name;
Grant Likelyae91ff72014-03-14 13:53:10 +000052
53 np = of_find_node_by_path("/testcase-data");
Rob Herring0d638a02017-06-01 15:50:55 -050054 name = kasprintf(GFP_KERNEL, "%pOF", np);
55 unittest(np && !strcmp("/testcase-data", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000056 "find /testcase-data failed\n");
57 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050058 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000059
60 /* Test if trailing '/' works */
61 np = of_find_node_by_path("/testcase-data/");
Wang Long9697a552015-03-11 08:36:54 +000062 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
Grant Likelyae91ff72014-03-14 13:53:10 +000063
64 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
Rob Herring0d638a02017-06-01 15:50:55 -050065 name = kasprintf(GFP_KERNEL, "%pOF", np);
66 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000067 "find /testcase-data/phandle-tests/consumer-a failed\n");
68 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050069 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000070
71 np = of_find_node_by_path("testcase-alias");
Rob Herring0d638a02017-06-01 15:50:55 -050072 name = kasprintf(GFP_KERNEL, "%pOF", np);
73 unittest(np && !strcmp("/testcase-data", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000074 "find testcase-alias failed\n");
75 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050076 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000077
78 /* Test if trailing '/' works on aliases */
79 np = of_find_node_by_path("testcase-alias/");
Wang Long9697a552015-03-11 08:36:54 +000080 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
Grant Likelyae91ff72014-03-14 13:53:10 +000081
82 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
Rob Herring0d638a02017-06-01 15:50:55 -050083 name = kasprintf(GFP_KERNEL, "%pOF", np);
84 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000085 "find testcase-alias/phandle-tests/consumer-a failed\n");
86 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050087 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000088
89 np = of_find_node_by_path("/testcase-data/missing-path");
Rob Herring0d638a02017-06-01 15:50:55 -050090 unittest(!np, "non-existent path returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +000091 of_node_put(np);
92
93 np = of_find_node_by_path("missing-alias");
Rob Herring0d638a02017-06-01 15:50:55 -050094 unittest(!np, "non-existent alias returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +000095 of_node_put(np);
96
97 np = of_find_node_by_path("testcase-alias/missing-path");
Rob Herring0d638a02017-06-01 15:50:55 -050098 unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +000099 of_node_put(np);
Leif Lindholm75c28c02014-11-28 11:34:28 +0000100
101 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
Wang Long9697a552015-03-11 08:36:54 +0000102 unittest(np && !strcmp("testoption", options),
Leif Lindholm75c28c02014-11-28 11:34:28 +0000103 "option path test failed\n");
104 of_node_put(np);
105
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500106 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
Wang Long9697a552015-03-11 08:36:54 +0000107 unittest(np && !strcmp("test/option", options),
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500108 "option path test, subcase #1 failed\n");
109 of_node_put(np);
110
Brian Norris5ca1b0d2015-03-17 12:30:32 -0700111 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
Wang Long9697a552015-03-11 08:36:54 +0000112 unittest(np && !strcmp("test/option", options),
Brian Norris5ca1b0d2015-03-17 12:30:32 -0700113 "option path test, subcase #2 failed\n");
114 of_node_put(np);
115
Leif Lindholm75c28c02014-11-28 11:34:28 +0000116 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000117 unittest(np, "NULL option path test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000118 of_node_put(np);
119
120 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
121 &options);
Wang Long9697a552015-03-11 08:36:54 +0000122 unittest(np && !strcmp("testaliasoption", options),
Leif Lindholm75c28c02014-11-28 11:34:28 +0000123 "option alias path test failed\n");
124 of_node_put(np);
125
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500126 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
127 &options);
Wang Long9697a552015-03-11 08:36:54 +0000128 unittest(np && !strcmp("test/alias/option", options),
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500129 "option alias path test, subcase #1 failed\n");
130 of_node_put(np);
131
Leif Lindholm75c28c02014-11-28 11:34:28 +0000132 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000133 unittest(np, "NULL option alias path test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000134 of_node_put(np);
135
136 options = "testoption";
137 np = of_find_node_opts_by_path("testcase-alias", &options);
Wang Long9697a552015-03-11 08:36:54 +0000138 unittest(np && !options, "option clearing test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000139 of_node_put(np);
140
141 options = "testoption";
142 np = of_find_node_opts_by_path("/", &options);
Wang Long9697a552015-03-11 08:36:54 +0000143 unittest(np && !options, "option clearing root node test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000144 of_node_put(np);
Grant Likelyae91ff72014-03-14 13:53:10 +0000145}
146
Wang Long9697a552015-03-11 08:36:54 +0000147static void __init of_unittest_dynamic(void)
Grant Likely7e66c5c2013-11-15 17:19:09 +0000148{
149 struct device_node *np;
150 struct property *prop;
151
152 np = of_find_node_by_path("/testcase-data");
153 if (!np) {
154 pr_err("missing testcase data\n");
155 return;
156 }
157
158 /* Array of 4 properties for the purpose of testing */
159 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
160 if (!prop) {
Wang Long9697a552015-03-11 08:36:54 +0000161 unittest(0, "kzalloc() failed\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000162 return;
163 }
164
165 /* Add a new property - should pass*/
166 prop->name = "new-property";
167 prop->value = "new-property-data";
168 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000169 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000170
171 /* Try to add an existing property - should fail */
172 prop++;
173 prop->name = "new-property";
174 prop->value = "new-property-data-should-fail";
175 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000176 unittest(of_add_property(np, prop) != 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000177 "Adding an existing property should have failed\n");
178
179 /* Try to modify an existing property - should pass */
180 prop->value = "modify-property-data-should-pass";
181 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000182 unittest(of_update_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000183 "Updating an existing property should have passed\n");
184
185 /* Try to modify non-existent property - should pass*/
186 prop++;
187 prop->name = "modify-property";
188 prop->value = "modify-missing-property-data-should-pass";
189 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000190 unittest(of_update_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000191 "Updating a missing property should have passed\n");
192
193 /* Remove property - should pass */
Wang Long9697a552015-03-11 08:36:54 +0000194 unittest(of_remove_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000195 "Removing a property should have passed\n");
196
197 /* Adding very large property - should pass */
198 prop++;
199 prop->name = "large-property-PAGE_SIZEx8";
200 prop->length = PAGE_SIZE * 8;
201 prop->value = kzalloc(prop->length, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000202 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000203 if (prop->value)
Wang Long9697a552015-03-11 08:36:54 +0000204 unittest(of_add_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000205 "Adding a large property should have passed\n");
206}
207
Wang Long9697a552015-03-11 08:36:54 +0000208static int __init of_unittest_check_node_linkage(struct device_node *np)
Grant Likelyf2051d62014-10-01 17:40:22 +0100209{
Grant Likely5063e252014-10-03 16:28:27 +0100210 struct device_node *child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100211 int count = 0, rc;
212
213 for_each_child_of_node(np, child) {
214 if (child->parent != np) {
215 pr_err("Child node %s links to wrong parent %s\n",
216 child->name, np->name);
Julia Lawall855ff282015-10-22 11:02:50 +0200217 rc = -EINVAL;
218 goto put_child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100219 }
220
Wang Long9697a552015-03-11 08:36:54 +0000221 rc = of_unittest_check_node_linkage(child);
Grant Likelyf2051d62014-10-01 17:40:22 +0100222 if (rc < 0)
Julia Lawall855ff282015-10-22 11:02:50 +0200223 goto put_child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100224 count += rc;
225 }
226
227 return count + 1;
Julia Lawall855ff282015-10-22 11:02:50 +0200228put_child:
229 of_node_put(child);
230 return rc;
Grant Likelyf2051d62014-10-01 17:40:22 +0100231}
232
Wang Long9697a552015-03-11 08:36:54 +0000233static void __init of_unittest_check_tree_linkage(void)
Grant Likelyf2051d62014-10-01 17:40:22 +0100234{
235 struct device_node *np;
236 int allnode_count = 0, child_count;
237
Grant Likely5063e252014-10-03 16:28:27 +0100238 if (!of_root)
Grant Likelyf2051d62014-10-01 17:40:22 +0100239 return;
240
241 for_each_of_allnodes(np)
242 allnode_count++;
Wang Long9697a552015-03-11 08:36:54 +0000243 child_count = of_unittest_check_node_linkage(of_root);
Grant Likelyf2051d62014-10-01 17:40:22 +0100244
Wang Long9697a552015-03-11 08:36:54 +0000245 unittest(child_count > 0, "Device node data structure is corrupted\n");
Grant Likelya2166ca2015-03-29 08:59:58 +0100246 unittest(child_count == allnode_count,
Frank Rowanda6bb1212015-03-13 23:59:01 -0700247 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
248 allnode_count, child_count);
Grant Likelyf2051d62014-10-01 17:40:22 +0100249 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
250}
251
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200252static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
253 const char *expected)
254{
255 unsigned char buf[strlen(expected)+10];
256 int size, i;
257
258 /* Baseline; check conversion with a large size limit */
259 memset(buf, 0xff, sizeof(buf));
260 size = snprintf(buf, sizeof(buf) - 2, fmt, np);
261
262 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
263 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
264 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
265 fmt, expected, buf);
266
267 /* Make sure length limits work */
268 size++;
269 for (i = 0; i < 2; i++, size--) {
270 /* Clear the buffer, and make sure it works correctly still */
271 memset(buf, 0xff, sizeof(buf));
272 snprintf(buf, size+1, fmt, np);
273 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
274 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
275 size, fmt, expected, buf);
276 }
277}
278
279static void __init of_unittest_printf(void)
280{
281 struct device_node *np;
282 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
283 char phandle_str[16] = "";
284
285 np = of_find_node_by_path(full_name);
286 if (!np) {
287 unittest(np, "testcase data missing\n");
288 return;
289 }
290
291 num_to_str(phandle_str, sizeof(phandle_str), np->phandle);
292
293 of_unittest_printf_one(np, "%pOF", full_name);
294 of_unittest_printf_one(np, "%pOFf", full_name);
295 of_unittest_printf_one(np, "%pOFp", phandle_str);
296 of_unittest_printf_one(np, "%pOFP", "dev@100");
297 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
298 of_unittest_printf_one(np, "%10pOFP", " dev@100");
299 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
300 of_unittest_printf_one(of_root, "%pOFP", "/");
301 of_unittest_printf_one(np, "%pOFF", "----");
302 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
303 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
304 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
305 of_unittest_printf_one(np, "%pOFC",
306 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
307}
308
Grant Likely841ec212014-10-02 13:09:15 +0100309struct node_hash {
310 struct hlist_node node;
311 struct device_node *np;
312};
313
Grant Likely2118f4b2014-10-07 11:30:31 +0100314static DEFINE_HASHTABLE(phandle_ht, 8);
Wang Long9697a552015-03-11 08:36:54 +0000315static void __init of_unittest_check_phandles(void)
Grant Likely841ec212014-10-02 13:09:15 +0100316{
317 struct device_node *np;
318 struct node_hash *nh;
319 struct hlist_node *tmp;
320 int i, dup_count = 0, phandle_count = 0;
Grant Likely841ec212014-10-02 13:09:15 +0100321
Grant Likely841ec212014-10-02 13:09:15 +0100322 for_each_of_allnodes(np) {
323 if (!np->phandle)
324 continue;
325
Grant Likely2118f4b2014-10-07 11:30:31 +0100326 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100327 if (nh->np->phandle == np->phandle) {
Rob Herring0d638a02017-06-01 15:50:55 -0500328 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
329 np->phandle, nh->np, np);
Grant Likely841ec212014-10-02 13:09:15 +0100330 dup_count++;
331 break;
332 }
333 }
334
335 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
336 if (WARN_ON(!nh))
337 return;
338
339 nh->np = np;
Grant Likely2118f4b2014-10-07 11:30:31 +0100340 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100341 phandle_count++;
342 }
Wang Long9697a552015-03-11 08:36:54 +0000343 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
Grant Likely841ec212014-10-02 13:09:15 +0100344 dup_count, phandle_count);
345
346 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100347 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100348 hash_del(&nh->node);
349 kfree(nh);
350 }
351}
352
Wang Long9697a552015-03-11 08:36:54 +0000353static void __init of_unittest_parse_phandle_with_args(void)
Grant Likely53a42092011-12-12 09:25:57 -0700354{
355 struct device_node *np;
356 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000357 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700358
Grant Likely53a42092011-12-12 09:25:57 -0700359 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
360 if (!np) {
361 pr_err("missing testcase data\n");
362 return;
363 }
364
Grant Likelybd69f732013-02-10 22:57:21 +0000365 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000366 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000367
Grant Likelyf7f951c2013-02-12 17:41:22 +0000368 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700369 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700370
Grant Likely53a42092011-12-12 09:25:57 -0700371 rc = of_parse_phandle_with_args(np, "phandle-list",
372 "#phandle-cells", i, &args);
373
374 /* Test the values from tests-phandle.dtsi */
375 switch (i) {
376 case 0:
377 passed &= !rc;
378 passed &= (args.args_count == 1);
379 passed &= (args.args[0] == (i + 1));
380 break;
381 case 1:
382 passed &= !rc;
383 passed &= (args.args_count == 2);
384 passed &= (args.args[0] == (i + 1));
385 passed &= (args.args[1] == 0);
386 break;
387 case 2:
388 passed &= (rc == -ENOENT);
389 break;
390 case 3:
391 passed &= !rc;
392 passed &= (args.args_count == 3);
393 passed &= (args.args[0] == (i + 1));
394 passed &= (args.args[1] == 4);
395 passed &= (args.args[2] == 3);
396 break;
397 case 4:
398 passed &= !rc;
399 passed &= (args.args_count == 2);
400 passed &= (args.args[0] == (i + 1));
401 passed &= (args.args[1] == 100);
402 break;
403 case 5:
404 passed &= !rc;
405 passed &= (args.args_count == 0);
406 break;
407 case 6:
408 passed &= !rc;
409 passed &= (args.args_count == 1);
410 passed &= (args.args[0] == (i + 1));
411 break;
412 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000413 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700414 break;
415 default:
416 passed = false;
417 }
418
Rob Herring0d638a02017-06-01 15:50:55 -0500419 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
420 i, args.np, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700421 }
422
423 /* Check for missing list property */
424 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
425 "#phandle-cells", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000426 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000427 rc = of_count_phandle_with_args(np, "phandle-list-missing",
428 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000429 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700430
431 /* Check for missing cells property */
432 rc = of_parse_phandle_with_args(np, "phandle-list",
433 "#phandle-cells-missing", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000434 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000435 rc = of_count_phandle_with_args(np, "phandle-list",
436 "#phandle-cells-missing");
Wang Long9697a552015-03-11 08:36:54 +0000437 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700438
439 /* Check for bad phandle in list */
440 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
441 "#phandle-cells", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000442 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000443 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
444 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000445 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700446
447 /* Check for incorrectly formed argument list */
448 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
449 "#phandle-cells", 1, &args);
Wang Long9697a552015-03-11 08:36:54 +0000450 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000451 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
452 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000453 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700454}
455
Wang Long9697a552015-03-11 08:36:54 +0000456static void __init of_unittest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700457{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000458 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700459 struct device_node *np;
460 int rc;
461
Grant Likely7aff0fe2011-12-12 09:25:58 -0700462 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
463 if (!np) {
464 pr_err("No testcase data in device tree\n");
465 return;
466 }
467
468 rc = of_property_match_string(np, "phandle-list-names", "first");
Wang Long9697a552015-03-11 08:36:54 +0000469 unittest(rc == 0, "first expected:0 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700470 rc = of_property_match_string(np, "phandle-list-names", "second");
Wang Long9697a552015-03-11 08:36:54 +0000471 unittest(rc == 1, "second expected:1 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700472 rc = of_property_match_string(np, "phandle-list-names", "third");
Wang Long9697a552015-03-11 08:36:54 +0000473 unittest(rc == 2, "third expected:2 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700474 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Wang Long9697a552015-03-11 08:36:54 +0000475 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700476 rc = of_property_match_string(np, "missing-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000477 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700478 rc = of_property_match_string(np, "empty-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000479 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700480 rc = of_property_match_string(np, "unterminated-string", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000481 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000482
483 /* of_property_count_strings() tests */
484 rc = of_property_count_strings(np, "string-property");
Wang Long9697a552015-03-11 08:36:54 +0000485 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000486 rc = of_property_count_strings(np, "phandle-list-names");
Wang Long9697a552015-03-11 08:36:54 +0000487 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000488 rc = of_property_count_strings(np, "unterminated-string");
Wang Long9697a552015-03-11 08:36:54 +0000489 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000490 rc = of_property_count_strings(np, "unterminated-string-list");
Wang Long9697a552015-03-11 08:36:54 +0000491 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000492
493 /* of_property_read_string_index() tests */
494 rc = of_property_read_string_index(np, "string-property", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000495 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000496 strings[0] = NULL;
497 rc = of_property_read_string_index(np, "string-property", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000498 unittest(rc == -ENODATA && strings[0] == NULL, "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", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000500 unittest(rc == 0 && !strcmp(strings[0], "first"), "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", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000502 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000503 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
Wang Long9697a552015-03-11 08:36:54 +0000504 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000505 strings[0] = NULL;
506 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
Wang Long9697a552015-03-11 08:36:54 +0000507 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000508 strings[0] = NULL;
509 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000510 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000511 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000512 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000513 strings[0] = NULL;
514 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
Wang Long9697a552015-03-11 08:36:54 +0000515 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000516 strings[1] = NULL;
517
518 /* of_property_read_string_array() tests */
519 rc = of_property_read_string_array(np, "string-property", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000520 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000521 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000522 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000523 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000524 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000525 /* -- An incorrectly formed string should cause a failure */
526 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000527 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000528 /* -- parsing the correctly formed strings should still work: */
529 strings[2] = NULL;
530 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
Wang Long9697a552015-03-11 08:36:54 +0000531 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000532 strings[1] = NULL;
533 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
Wang Long9697a552015-03-11 08:36:54 +0000534 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 -0700535}
536
Pantelis Antoniou69843392014-07-04 19:58:47 +0300537#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
538 (p1)->value && (p2)->value && \
539 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
540 !strcmp((p1)->name, (p2)->name))
Wang Long9697a552015-03-11 08:36:54 +0000541static void __init of_unittest_property_copy(void)
Pantelis Antoniou69843392014-07-04 19:58:47 +0300542{
543#ifdef CONFIG_OF_DYNAMIC
544 struct property p1 = { .name = "p1", .length = 0, .value = "" };
545 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
546 struct property *new;
547
548 new = __of_prop_dup(&p1, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000549 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300550 kfree(new->value);
551 kfree(new->name);
552 kfree(new);
553
554 new = __of_prop_dup(&p2, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000555 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300556 kfree(new->value);
557 kfree(new->name);
558 kfree(new);
559#endif
560}
561
Wang Long9697a552015-03-11 08:36:54 +0000562static void __init of_unittest_changeset(void)
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300563{
564#ifdef CONFIG_OF_DYNAMIC
565 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
566 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
567 struct property *ppremove;
Grant Likelye5179582014-11-17 22:31:32 +0000568 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300569 struct of_changeset chgset;
570
Grant Likelye5179582014-11-17 22:31:32 +0000571 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
Wang Long9697a552015-03-11 08:36:54 +0000572 unittest(n1, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000573 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
Wang Long9697a552015-03-11 08:36:54 +0000574 unittest(n2, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000575 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
Wang Long9697a552015-03-11 08:36:54 +0000576 unittest(n21, "testcase setup failure %p\n", n21);
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300577 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
Wang Long9697a552015-03-11 08:36:54 +0000578 unittest(nremove, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300579 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000580 unittest(ppadd, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300581 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000582 unittest(ppupdate, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300583 parent = nremove->parent;
584 n1->parent = parent;
585 n2->parent = parent;
586 n21->parent = n2;
587 n2->child = n21;
588 ppremove = of_find_property(parent, "prop-remove", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000589 unittest(ppremove, "failed to find removal prop");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300590
591 of_changeset_init(&chgset);
Wang Long9697a552015-03-11 08:36:54 +0000592 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
593 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
594 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
595 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
596 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
597 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
598 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
Wang Long9697a552015-03-11 08:36:54 +0000599 unittest(!of_changeset_apply(&chgset), "apply failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300600
Grant Likelye5179582014-11-17 22:31:32 +0000601 /* Make sure node names are constructed correctly */
Wang Long9697a552015-03-11 08:36:54 +0000602 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
Rob Herring0d638a02017-06-01 15:50:55 -0500603 "'%pOF' not added\n", n21);
Markus Elfringc46ca3c2014-12-02 13:54:00 +0100604 of_node_put(np);
Grant Likelye5179582014-11-17 22:31:32 +0000605
Wang Long9697a552015-03-11 08:36:54 +0000606 unittest(!of_changeset_revert(&chgset), "revert failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300607
608 of_changeset_destroy(&chgset);
609#endif
610}
611
Wang Long9697a552015-03-11 08:36:54 +0000612static void __init of_unittest_parse_interrupts(void)
Grant Likelya9f10ca2013-10-11 22:04:23 +0100613{
614 struct device_node *np;
615 struct of_phandle_args args;
616 int i, rc;
617
618 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
619 if (!np) {
620 pr_err("missing testcase data\n");
621 return;
622 }
623
624 for (i = 0; i < 4; i++) {
625 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700626
Grant Likelya9f10ca2013-10-11 22:04:23 +0100627 args.args_count = 0;
628 rc = of_irq_parse_one(np, i, &args);
629
630 passed &= !rc;
631 passed &= (args.args_count == 1);
632 passed &= (args.args[0] == (i + 1));
633
Rob Herring0d638a02017-06-01 15:50:55 -0500634 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
635 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100636 }
637 of_node_put(np);
638
639 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
640 if (!np) {
641 pr_err("missing testcase data\n");
642 return;
643 }
644
645 for (i = 0; i < 4; i++) {
646 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700647
Grant Likelya9f10ca2013-10-11 22:04:23 +0100648 args.args_count = 0;
649 rc = of_irq_parse_one(np, i, &args);
650
651 /* Test the values from tests-phandle.dtsi */
652 switch (i) {
653 case 0:
654 passed &= !rc;
655 passed &= (args.args_count == 1);
656 passed &= (args.args[0] == 9);
657 break;
658 case 1:
659 passed &= !rc;
660 passed &= (args.args_count == 3);
661 passed &= (args.args[0] == 10);
662 passed &= (args.args[1] == 11);
663 passed &= (args.args[2] == 12);
664 break;
665 case 2:
666 passed &= !rc;
667 passed &= (args.args_count == 2);
668 passed &= (args.args[0] == 13);
669 passed &= (args.args[1] == 14);
670 break;
671 case 3:
672 passed &= !rc;
673 passed &= (args.args_count == 2);
674 passed &= (args.args[0] == 15);
675 passed &= (args.args[1] == 16);
676 break;
677 default:
678 passed = false;
679 }
Rob Herring0d638a02017-06-01 15:50:55 -0500680 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
681 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100682 }
683 of_node_put(np);
684}
685
Wang Long9697a552015-03-11 08:36:54 +0000686static void __init of_unittest_parse_interrupts_extended(void)
Grant Likely79d97012013-09-19 16:47:37 -0500687{
688 struct device_node *np;
689 struct of_phandle_args args;
690 int i, rc;
691
692 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
693 if (!np) {
694 pr_err("missing testcase data\n");
695 return;
696 }
697
698 for (i = 0; i < 7; i++) {
699 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700700
Grant Likely79d97012013-09-19 16:47:37 -0500701 rc = of_irq_parse_one(np, i, &args);
702
703 /* Test the values from tests-phandle.dtsi */
704 switch (i) {
705 case 0:
706 passed &= !rc;
707 passed &= (args.args_count == 1);
708 passed &= (args.args[0] == 1);
709 break;
710 case 1:
711 passed &= !rc;
712 passed &= (args.args_count == 3);
713 passed &= (args.args[0] == 2);
714 passed &= (args.args[1] == 3);
715 passed &= (args.args[2] == 4);
716 break;
717 case 2:
718 passed &= !rc;
719 passed &= (args.args_count == 2);
720 passed &= (args.args[0] == 5);
721 passed &= (args.args[1] == 6);
722 break;
723 case 3:
724 passed &= !rc;
725 passed &= (args.args_count == 1);
726 passed &= (args.args[0] == 9);
727 break;
728 case 4:
729 passed &= !rc;
730 passed &= (args.args_count == 3);
731 passed &= (args.args[0] == 10);
732 passed &= (args.args[1] == 11);
733 passed &= (args.args[2] == 12);
734 break;
735 case 5:
736 passed &= !rc;
737 passed &= (args.args_count == 2);
738 passed &= (args.args[0] == 13);
739 passed &= (args.args[1] == 14);
740 break;
741 case 6:
742 passed &= !rc;
743 passed &= (args.args_count == 1);
744 passed &= (args.args[0] == 15);
745 break;
746 default:
747 passed = false;
748 }
749
Rob Herring0d638a02017-06-01 15:50:55 -0500750 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
751 i, args.np, rc);
Grant Likely79d97012013-09-19 16:47:37 -0500752 }
753 of_node_put(np);
754}
755
Frank Rowandafaed7a2015-03-14 00:00:36 -0700756static const struct of_device_id match_node_table[] = {
Grant Likely1f42e5d2014-02-18 21:38:55 +0000757 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
758 { .data = "B", .type = "type1", }, /* followed by type alone */
759
760 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
761 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
762 { .data = "Cc", .name = "name2", .type = "type2", },
763
764 { .data = "E", .compatible = "compat3" },
765 { .data = "G", .compatible = "compat2", },
766 { .data = "H", .compatible = "compat2", .name = "name5", },
767 { .data = "I", .compatible = "compat2", .type = "type1", },
768 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
769 { .data = "K", .compatible = "compat2", .name = "name9", },
770 {}
771};
772
773static struct {
774 const char *path;
775 const char *data;
776} match_node_tests[] = {
777 { .path = "/testcase-data/match-node/name0", .data = "A", },
778 { .path = "/testcase-data/match-node/name1", .data = "B", },
779 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
780 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
781 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
782 { .path = "/testcase-data/match-node/name3", .data = "E", },
783 { .path = "/testcase-data/match-node/name4", .data = "G", },
784 { .path = "/testcase-data/match-node/name5", .data = "H", },
785 { .path = "/testcase-data/match-node/name6", .data = "G", },
786 { .path = "/testcase-data/match-node/name7", .data = "I", },
787 { .path = "/testcase-data/match-node/name8", .data = "J", },
788 { .path = "/testcase-data/match-node/name9", .data = "K", },
789};
790
Wang Long9697a552015-03-11 08:36:54 +0000791static void __init of_unittest_match_node(void)
Grant Likely1f42e5d2014-02-18 21:38:55 +0000792{
793 struct device_node *np;
794 const struct of_device_id *match;
795 int i;
796
797 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
798 np = of_find_node_by_path(match_node_tests[i].path);
799 if (!np) {
Wang Long9697a552015-03-11 08:36:54 +0000800 unittest(0, "missing testcase node %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000801 match_node_tests[i].path);
802 continue;
803 }
804
805 match = of_match_node(match_node_table, np);
806 if (!match) {
Wang Long9697a552015-03-11 08:36:54 +0000807 unittest(0, "%s didn't match anything\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000808 match_node_tests[i].path);
809 continue;
810 }
811
812 if (strcmp(match->data, match_node_tests[i].data) != 0) {
Wang Long9697a552015-03-11 08:36:54 +0000813 unittest(0, "%s got wrong match. expected %s, got %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000814 match_node_tests[i].path, match_node_tests[i].data,
815 (const char *)match->data);
816 continue;
817 }
Wang Long9697a552015-03-11 08:36:54 +0000818 unittest(1, "passed");
Grant Likely1f42e5d2014-02-18 21:38:55 +0000819 }
820}
821
Grant Likelyd2329fb2016-01-04 13:13:21 +0100822static struct resource test_bus_res = {
823 .start = 0xfffffff8,
824 .end = 0xfffffff9,
825 .flags = IORESOURCE_MEM,
826};
Grant Likely37791b62015-03-27 20:30:04 -0700827static const struct platform_device_info test_bus_info = {
828 .name = "unittest-bus",
Grant Likely851da972014-11-04 13:14:13 +0000829};
Wang Long9697a552015-03-11 08:36:54 +0000830static void __init of_unittest_platform_populate(void)
Rob Herring82c0f582014-04-23 17:57:40 -0500831{
Grant Likely851da972014-11-04 13:14:13 +0000832 int irq, rc;
833 struct device_node *np, *child, *grandchild;
Grant Likely37791b62015-03-27 20:30:04 -0700834 struct platform_device *pdev, *test_bus;
Frank Rowandafaed7a2015-03-14 00:00:36 -0700835 const struct of_device_id match[] = {
Rob Herringfb2caa52014-05-13 10:07:54 -0500836 { .compatible = "test-device", },
837 {}
838 };
Rob Herring82c0f582014-04-23 17:57:40 -0500839
840 np = of_find_node_by_path("/testcase-data");
Kefeng Wang146dedb2016-06-01 14:53:10 +0800841 of_platform_default_populate(np, NULL, NULL);
Rob Herring82c0f582014-04-23 17:57:40 -0500842
843 /* Test that a missing irq domain returns -EPROBE_DEFER */
844 np = of_find_node_by_path("/testcase-data/testcase-device1");
845 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +0000846 unittest(pdev, "device 1 creation failed\n");
Rob Herring7d1cdc82014-05-13 10:07:29 -0500847
Rob Herring82c0f582014-04-23 17:57:40 -0500848 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +0000849 unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500850
851 /* Test that a parsing failure does not return -EPROBE_DEFER */
852 np = of_find_node_by_path("/testcase-data/testcase-device2");
853 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +0000854 unittest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500855 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +0000856 unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500857
Frank Rowand716e1d42015-03-13 23:57:40 -0700858 np = of_find_node_by_path("/testcase-data/platform-tests");
Grant Likelya2166ca2015-03-29 08:59:58 +0100859 unittest(np, "No testcase data in device tree\n");
Frank Rowand716e1d42015-03-13 23:57:40 -0700860 if (!np)
Rob Herringfb2caa52014-05-13 10:07:54 -0500861 return;
Grant Likely851da972014-11-04 13:14:13 +0000862
Grant Likely37791b62015-03-27 20:30:04 -0700863 test_bus = platform_device_register_full(&test_bus_info);
864 rc = PTR_ERR_OR_ZERO(test_bus);
Grant Likelya2166ca2015-03-29 08:59:58 +0100865 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
Frank Rowand716e1d42015-03-13 23:57:40 -0700866 if (rc)
Grant Likely851da972014-11-04 13:14:13 +0000867 return;
Grant Likely37791b62015-03-27 20:30:04 -0700868 test_bus->dev.of_node = np;
Rob Herringfb2caa52014-05-13 10:07:54 -0500869
Grant Likelyd2329fb2016-01-04 13:13:21 +0100870 /*
871 * Add a dummy resource to the test bus node after it is
872 * registered to catch problems with un-inserted resources. The
873 * DT code doesn't insert the resources, and it has caused the
874 * kernel to oops in the past. This makes sure the same bug
875 * doesn't crop up again.
876 */
877 platform_device_add_resources(test_bus, &test_bus_res, 1);
878
Grant Likely37791b62015-03-27 20:30:04 -0700879 of_platform_populate(np, match, NULL, &test_bus->dev);
Rob Herringfb2caa52014-05-13 10:07:54 -0500880 for_each_child_of_node(np, child) {
Rob Herringfb2caa52014-05-13 10:07:54 -0500881 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +0000882 unittest(of_find_device_by_node(grandchild),
Rob Herringfb2caa52014-05-13 10:07:54 -0500883 "Could not create device for node '%s'\n",
884 grandchild->name);
885 }
Grant Likely851da972014-11-04 13:14:13 +0000886
Grant Likely37791b62015-03-27 20:30:04 -0700887 of_platform_depopulate(&test_bus->dev);
Grant Likely851da972014-11-04 13:14:13 +0000888 for_each_child_of_node(np, child) {
889 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +0000890 unittest(!of_find_device_by_node(grandchild),
Grant Likely851da972014-11-04 13:14:13 +0000891 "device didn't get destroyed '%s'\n",
892 grandchild->name);
893 }
894
Grant Likely37791b62015-03-27 20:30:04 -0700895 platform_device_unregister(test_bus);
Grant Likely851da972014-11-04 13:14:13 +0000896 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -0500897}
898
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700899/**
900 * update_node_properties - adds the properties
901 * of np into dup node (present in live tree) and
902 * updates parent of children of np to dup.
903 *
904 * @np: node already present in live tree
905 * @dup: node present in live tree to be updated
906 */
907static void update_node_properties(struct device_node *np,
908 struct device_node *dup)
909{
910 struct property *prop;
911 struct device_node *child;
912
913 for_each_property_of_node(np, prop)
914 of_add_property(dup, prop);
915
916 for_each_child_of_node(np, child)
917 child->parent = dup;
918}
919
920/**
921 * attach_node_and_children - attaches nodes
922 * and its children to live tree
923 *
924 * @np: Node to attach to live tree
925 */
926static int attach_node_and_children(struct device_node *np)
927{
Grant Likely5063e252014-10-03 16:28:27 +0100928 struct device_node *next, *dup, *child;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800929 unsigned long flags;
Rob Herring0d638a02017-06-01 15:50:55 -0500930 const char *full_name;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700931
Rob Herring0d638a02017-06-01 15:50:55 -0500932 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
933 dup = of_find_node_by_path(full_name);
934 kfree(full_name);
Grant Likely5063e252014-10-03 16:28:27 +0100935 if (dup) {
936 update_node_properties(np, dup);
937 return 0;
938 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700939
Grant Likely5063e252014-10-03 16:28:27 +0100940 child = np->child;
941 np->child = NULL;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800942
943 mutex_lock(&of_mutex);
944 raw_spin_lock_irqsave(&devtree_lock, flags);
945 np->sibling = np->parent->child;
946 np->parent->child = np;
947 of_node_clear_flag(np, OF_DETACHED);
948 raw_spin_unlock_irqrestore(&devtree_lock, flags);
949
950 __of_attach_node_sysfs(np);
951 mutex_unlock(&of_mutex);
952
Grant Likely5063e252014-10-03 16:28:27 +0100953 while (child) {
954 next = child->sibling;
955 attach_node_and_children(child);
956 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700957 }
958
959 return 0;
960}
961
962/**
Wang Long9697a552015-03-11 08:36:54 +0000963 * unittest_data_add - Reads, copies data from
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700964 * linked tree and attaches it to the live tree
965 */
Wang Long9697a552015-03-11 08:36:54 +0000966static int __init unittest_data_add(void)
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700967{
Wang Long9697a552015-03-11 08:36:54 +0000968 void *unittest_data;
969 struct device_node *unittest_data_node, *np;
Frank Rowandc8547112015-03-14 00:04:24 -0700970 /*
971 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
972 * created by cmd_dt_S_dtb in scripts/Makefile.lib
973 */
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700974 extern uint8_t __dtb_testcases_begin[];
975 extern uint8_t __dtb_testcases_end[];
976 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +0100977 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700978
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700979 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700980 pr_warn("%s: No testcase data to attach; not running tests\n",
981 __func__);
982 return -ENODATA;
983 }
984
985 /* creating copy */
Wang Long9697a552015-03-11 08:36:54 +0000986 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700987
Wang Long9697a552015-03-11 08:36:54 +0000988 if (!unittest_data) {
989 pr_warn("%s: Failed to allocate memory for unittest_data; "
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700990 "not running tests\n", __func__);
991 return -ENOMEM;
992 }
Gavin Shanc4263232016-05-03 23:22:50 +1000993 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
Wang Long9697a552015-03-11 08:36:54 +0000994 if (!unittest_data_node) {
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700995 pr_warn("%s: No tree to attach; not running tests\n", __func__);
996 return -ENODATA;
997 }
Frank Rowandf948d6d2017-10-17 16:36:29 -0700998
999 /*
1000 * This lock normally encloses of_overlay_apply() as well as
1001 * of_resolve_phandles().
1002 */
1003 of_overlay_mutex_lock();
1004
Wang Long9697a552015-03-11 08:36:54 +00001005 rc = of_resolve_phandles(unittest_data_node);
Grant Likely2eb46da2014-10-02 14:36:46 +01001006 if (rc) {
1007 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
Frank Rowandf948d6d2017-10-17 16:36:29 -07001008 of_overlay_mutex_unlock();
Grant Likely2eb46da2014-10-02 14:36:46 +01001009 return -EINVAL;
1010 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001011
Grant Likely5063e252014-10-03 16:28:27 +01001012 if (!of_root) {
Wang Long9697a552015-03-11 08:36:54 +00001013 of_root = unittest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001014 for_each_of_allnodes(np)
1015 __of_attach_node_sysfs(np);
1016 of_aliases = of_find_node_by_path("/aliases");
1017 of_chosen = of_find_node_by_path("/chosen");
Frank Rowandf948d6d2017-10-17 16:36:29 -07001018 of_overlay_mutex_unlock();
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001019 return 0;
1020 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001021
1022 /* attach the sub-tree to live tree */
Wang Long9697a552015-03-11 08:36:54 +00001023 np = unittest_data_node->child;
Grant Likely5063e252014-10-03 16:28:27 +01001024 while (np) {
1025 struct device_node *next = np->sibling;
Frank Rowand3db316d2015-03-14 00:02:31 -07001026
Grant Likely5063e252014-10-03 16:28:27 +01001027 np->parent = of_root;
1028 attach_node_and_children(np);
1029 np = next;
1030 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001031
1032 of_overlay_mutex_unlock();
1033
Grant Likely5063e252014-10-03 16:28:27 +01001034 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001035}
1036
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001037#ifdef CONFIG_OF_OVERLAY
1038
Wang Long9697a552015-03-11 08:36:54 +00001039static int unittest_probe(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001040{
1041 struct device *dev = &pdev->dev;
1042 struct device_node *np = dev->of_node;
1043
1044 if (np == NULL) {
1045 dev_err(dev, "No OF data for device\n");
1046 return -EINVAL;
1047
1048 }
1049
Rob Herring0d638a02017-06-01 15:50:55 -05001050 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001051
1052 of_platform_populate(np, NULL, NULL, &pdev->dev);
1053
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001054 return 0;
1055}
1056
Wang Long9697a552015-03-11 08:36:54 +00001057static int unittest_remove(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001058{
1059 struct device *dev = &pdev->dev;
1060 struct device_node *np = dev->of_node;
1061
Rob Herring0d638a02017-06-01 15:50:55 -05001062 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001063 return 0;
1064}
1065
Grant Likelya2166ca2015-03-29 08:59:58 +01001066static const struct of_device_id unittest_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001067 { .compatible = "unittest", },
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001068 {},
1069};
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001070
Wang Long9697a552015-03-11 08:36:54 +00001071static struct platform_driver unittest_driver = {
1072 .probe = unittest_probe,
1073 .remove = unittest_remove,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001074 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001075 .name = "unittest",
Wang Long9697a552015-03-11 08:36:54 +00001076 .of_match_table = of_match_ptr(unittest_match),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001077 },
1078};
1079
1080/* get the platform device instantiated at the path */
1081static struct platform_device *of_path_to_platform_device(const char *path)
1082{
1083 struct device_node *np;
1084 struct platform_device *pdev;
1085
1086 np = of_find_node_by_path(path);
1087 if (np == NULL)
1088 return NULL;
1089
1090 pdev = of_find_device_by_node(np);
1091 of_node_put(np);
1092
1093 return pdev;
1094}
1095
1096/* find out if a platform device exists at that path */
1097static int of_path_platform_device_exists(const char *path)
1098{
1099 struct platform_device *pdev;
1100
1101 pdev = of_path_to_platform_device(path);
1102 platform_device_put(pdev);
1103 return pdev != NULL;
1104}
1105
Arnd Bergmann4252de32015-03-04 20:49:47 +01001106#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001107
1108/* get the i2c client device instantiated at the path */
1109static struct i2c_client *of_path_to_i2c_client(const char *path)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001110{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001111 struct device_node *np;
1112 struct i2c_client *client;
1113
1114 np = of_find_node_by_path(path);
1115 if (np == NULL)
1116 return NULL;
1117
1118 client = of_find_i2c_device_by_node(np);
1119 of_node_put(np);
1120
1121 return client;
1122}
1123
1124/* find out if a i2c client device exists at that path */
1125static int of_path_i2c_client_exists(const char *path)
1126{
1127 struct i2c_client *client;
1128
1129 client = of_path_to_i2c_client(path);
1130 if (client)
1131 put_device(&client->dev);
1132 return client != NULL;
1133}
1134#else
1135static int of_path_i2c_client_exists(const char *path)
1136{
1137 return 0;
1138}
1139#endif
1140
1141enum overlay_type {
1142 PDEV_OVERLAY,
1143 I2C_OVERLAY
1144};
1145
1146static int of_path_device_type_exists(const char *path,
1147 enum overlay_type ovtype)
1148{
1149 switch (ovtype) {
1150 case PDEV_OVERLAY:
1151 return of_path_platform_device_exists(path);
1152 case I2C_OVERLAY:
1153 return of_path_i2c_client_exists(path);
1154 }
1155 return 0;
1156}
1157
Wang Long9697a552015-03-11 08:36:54 +00001158static const char *unittest_path(int nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001159{
1160 const char *base;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001161 static char buf[256];
1162
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001163 switch (ovtype) {
1164 case PDEV_OVERLAY:
1165 base = "/testcase-data/overlay-node/test-bus";
1166 break;
1167 case I2C_OVERLAY:
1168 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1169 break;
1170 default:
1171 buf[0] = '\0';
1172 return buf;
1173 }
Wang Long9697a552015-03-11 08:36:54 +00001174 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001175 buf[sizeof(buf) - 1] = '\0';
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001176 return buf;
1177}
1178
Wang Long9697a552015-03-11 08:36:54 +00001179static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001180{
1181 const char *path;
1182
Wang Long9697a552015-03-11 08:36:54 +00001183 path = unittest_path(unittest_nr, ovtype);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001184
1185 switch (ovtype) {
1186 case PDEV_OVERLAY:
1187 return of_path_platform_device_exists(path);
1188 case I2C_OVERLAY:
1189 return of_path_i2c_client_exists(path);
1190 }
1191 return 0;
1192}
1193
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001194static const char *overlay_path(int nr)
1195{
1196 static char buf[256];
1197
1198 snprintf(buf, sizeof(buf) - 1,
1199 "/testcase-data/overlay%d", nr);
1200 buf[sizeof(buf) - 1] = '\0';
1201
1202 return buf;
1203}
1204
1205static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1206
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001207/* it is guaranteed that overlay ids are assigned in sequence */
1208#define MAX_UNITTEST_OVERLAYS 256
1209static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1210static int overlay_first_id = -1;
1211
1212static void of_unittest_track_overlay(int id)
1213{
1214 if (overlay_first_id < 0)
1215 overlay_first_id = id;
1216 id -= overlay_first_id;
1217
1218 /* we shouldn't need that many */
1219 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1220 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1221}
1222
1223static void of_unittest_untrack_overlay(int id)
1224{
1225 if (overlay_first_id < 0)
1226 return;
1227 id -= overlay_first_id;
1228 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1229 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1230}
1231
1232static void of_unittest_destroy_tracked_overlays(void)
1233{
Frank Rowand24789c52017-10-17 16:36:26 -07001234 int id, ret, defers, ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001235
1236 if (overlay_first_id < 0)
1237 return;
1238
1239 /* try until no defers */
1240 do {
1241 defers = 0;
1242 /* remove in reverse order */
1243 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1244 if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1245 continue;
1246
Frank Rowand24789c52017-10-17 16:36:26 -07001247 ovcs_id = id + overlay_first_id;
1248 ret = of_overlay_remove(&ovcs_id);
Sergey Senozhatsky815d74b2016-03-02 20:24:49 +09001249 if (ret == -ENODEV) {
1250 pr_warn("%s: no overlay to destroy for #%d\n",
1251 __func__, id + overlay_first_id);
1252 continue;
1253 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001254 if (ret != 0) {
1255 defers++;
1256 pr_warn("%s: overlay destroy failed for #%d\n",
1257 __func__, id + overlay_first_id);
1258 continue;
1259 }
1260
1261 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1262 }
1263 } while (defers > 0);
1264}
1265
Alexander Sverdlinca7b8962017-01-19 11:06:16 +01001266static int of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001267 int *overlay_id)
1268{
1269 struct device_node *np = NULL;
Frank Rowand24789c52017-10-17 16:36:26 -07001270 int ret;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001271
1272 np = of_find_node_by_path(overlay_path(overlay_nr));
1273 if (np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001274 unittest(0, "could not find overlay node @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001275 overlay_path(overlay_nr));
1276 ret = -EINVAL;
1277 goto out;
1278 }
1279
Frank Rowand24789c52017-10-17 16:36:26 -07001280 *overlay_id = 0;
1281 ret = of_overlay_apply(np, overlay_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001282 if (ret < 0) {
Wang Long9697a552015-03-11 08:36:54 +00001283 unittest(0, "could not create overlay from \"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001284 overlay_path(overlay_nr));
1285 goto out;
1286 }
Frank Rowand24789c52017-10-17 16:36:26 -07001287 of_unittest_track_overlay(*overlay_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001288
1289 ret = 0;
1290
1291out:
1292 of_node_put(np);
1293
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001294 return ret;
1295}
1296
1297/* apply an overlay while checking before and after states */
Wang Long9697a552015-03-11 08:36:54 +00001298static int of_unittest_apply_overlay_check(int overlay_nr, int unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001299 int before, int after, enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001300{
Frank Rowand24789c52017-10-17 16:36:26 -07001301 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001302
Wang Long9697a552015-03-11 08:36:54 +00001303 /* unittest device must not be in before state */
1304 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1305 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001306 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001307 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001308 !before ? "enabled" : "disabled");
1309 return -EINVAL;
1310 }
1311
Frank Rowand24789c52017-10-17 16:36:26 -07001312 ovcs_id = 0;
1313 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001314 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001315 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001316 return ret;
1317 }
1318
Wang Long9697a552015-03-11 08:36:54 +00001319 /* unittest device must be to set to after state */
1320 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1321 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001322 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001323 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001324 !after ? "enabled" : "disabled");
1325 return -EINVAL;
1326 }
1327
1328 return 0;
1329}
1330
1331/* apply an overlay and then revert it while checking before, after states */
Wang Long9697a552015-03-11 08:36:54 +00001332static int of_unittest_apply_revert_overlay_check(int overlay_nr,
1333 int unittest_nr, int before, int after,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001334 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001335{
Frank Rowand24789c52017-10-17 16:36:26 -07001336 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001337
Wang Long9697a552015-03-11 08:36:54 +00001338 /* unittest device must be in before state */
1339 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1340 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001341 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001342 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001343 !before ? "enabled" : "disabled");
1344 return -EINVAL;
1345 }
1346
1347 /* apply the overlay */
Frank Rowand24789c52017-10-17 16:36:26 -07001348 ovcs_id = 0;
1349 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001350 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001351 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001352 return ret;
1353 }
1354
Wang Long9697a552015-03-11 08:36:54 +00001355 /* unittest device must be in after state */
1356 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1357 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001358 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001359 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001360 !after ? "enabled" : "disabled");
1361 return -EINVAL;
1362 }
1363
Frank Rowand24789c52017-10-17 16:36:26 -07001364 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001365 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001366 unittest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001367 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001368 unittest_path(unittest_nr, ovtype));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001369 return ret;
1370 }
1371
Wang Long9697a552015-03-11 08:36:54 +00001372 /* unittest device must be again in before state */
1373 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
1374 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001375 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001376 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001377 !before ? "enabled" : "disabled");
1378 return -EINVAL;
1379 }
1380
1381 return 0;
1382}
1383
1384/* test activation of device */
Wang Long9697a552015-03-11 08:36:54 +00001385static void of_unittest_overlay_0(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001386{
1387 int ret;
1388
1389 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001390 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, 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", 0);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001395}
1396
1397/* test deactivation of device */
Wang Long9697a552015-03-11 08:36:54 +00001398static void of_unittest_overlay_1(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001399{
1400 int ret;
1401
1402 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001403 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, 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", 1);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001408}
1409
1410/* test activation of device */
Wang Long9697a552015-03-11 08:36:54 +00001411static void of_unittest_overlay_2(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001412{
1413 int ret;
1414
1415 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001416 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, 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", 2);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001421}
1422
1423/* test deactivation of device */
Wang Long9697a552015-03-11 08:36:54 +00001424static void of_unittest_overlay_3(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(3, 3, 1, 0, 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", 3);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001434}
1435
1436/* test activation of a full device node */
Wang Long9697a552015-03-11 08:36:54 +00001437static void of_unittest_overlay_4(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_overlay_check(4, 4, 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", 4);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001447}
1448
1449/* test overlay apply/revert sequence */
Wang Long9697a552015-03-11 08:36:54 +00001450static void of_unittest_overlay_5(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001451{
1452 int ret;
1453
1454 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001455 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001456 if (ret != 0)
1457 return;
1458
Wang Long9697a552015-03-11 08:36:54 +00001459 unittest(1, "overlay test %d passed\n", 5);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001460}
1461
1462/* test overlay application in sequence */
Wang Long9697a552015-03-11 08:36:54 +00001463static void of_unittest_overlay_6(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001464{
1465 struct device_node *np;
Frank Rowand24789c52017-10-17 16:36:26 -07001466 int ret, i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00001467 int overlay_nr = 6, unittest_nr = 6;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001468 int before = 0, after = 1;
1469
Wang Long9697a552015-03-11 08:36:54 +00001470 /* unittest device must be in before state */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001471 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001472 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001473 != before) {
Wang Long9697a552015-03-11 08:36:54 +00001474 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001475 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001476 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001477 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001478 !before ? "enabled" : "disabled");
1479 return;
1480 }
1481 }
1482
1483 /* apply the overlays */
1484 for (i = 0; i < 2; i++) {
1485
1486 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1487 if (np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001488 unittest(0, "could not find overlay node @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001489 overlay_path(overlay_nr + i));
1490 return;
1491 }
1492
Frank Rowand24789c52017-10-17 16:36:26 -07001493 ovcs_id = 0;
1494 ret = of_overlay_apply(np, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001495 if (ret < 0) {
Wang Long9697a552015-03-11 08:36:54 +00001496 unittest(0, "could not create overlay from \"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001497 overlay_path(overlay_nr + i));
1498 return;
1499 }
Frank Rowand24789c52017-10-17 16:36:26 -07001500 ov_id[i] = ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001501 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001502 }
1503
1504 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001505 /* unittest device must be in after state */
1506 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001507 != after) {
Wang Long9697a552015-03-11 08:36:54 +00001508 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001509 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001510 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001511 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001512 !after ? "enabled" : "disabled");
1513 return;
1514 }
1515 }
1516
1517 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07001518 ovcs_id = ov_id[i];
1519 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001520 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001521 unittest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001522 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001523 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001524 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001525 return;
1526 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001527 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001528 }
1529
1530 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001531 /* unittest device must be again in before state */
1532 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001533 != before) {
Wang Long9697a552015-03-11 08:36:54 +00001534 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001535 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001536 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001537 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001538 !before ? "enabled" : "disabled");
1539 return;
1540 }
1541 }
1542
Wang Long9697a552015-03-11 08:36:54 +00001543 unittest(1, "overlay test %d passed\n", 6);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001544}
1545
1546/* test overlay application in sequence */
Wang Long9697a552015-03-11 08:36:54 +00001547static void of_unittest_overlay_8(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001548{
1549 struct device_node *np;
Frank Rowand24789c52017-10-17 16:36:26 -07001550 int ret, i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00001551 int overlay_nr = 8, unittest_nr = 8;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001552
1553 /* we don't care about device state in this test */
1554
1555 /* apply the overlays */
1556 for (i = 0; i < 2; i++) {
1557
1558 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1559 if (np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001560 unittest(0, "could not find overlay node @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001561 overlay_path(overlay_nr + i));
1562 return;
1563 }
1564
Frank Rowand24789c52017-10-17 16:36:26 -07001565 ovcs_id = 0;
1566 ret = of_overlay_apply(np, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001567 if (ret < 0) {
Wang Long9697a552015-03-11 08:36:54 +00001568 unittest(0, "could not create overlay from \"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001569 overlay_path(overlay_nr + i));
1570 return;
1571 }
Frank Rowand24789c52017-10-17 16:36:26 -07001572 ov_id[i] = ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001573 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001574 }
1575
1576 /* now try to remove first overlay (it should fail) */
Frank Rowand24789c52017-10-17 16:36:26 -07001577 ovcs_id = ov_id[0];
1578 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001579 if (ret == 0) {
Wang Long9697a552015-03-11 08:36:54 +00001580 unittest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001581 overlay_path(overlay_nr + 0),
Wang Long9697a552015-03-11 08:36:54 +00001582 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001583 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001584 return;
1585 }
1586
1587 /* removing them in order should work */
1588 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07001589 ovcs_id = ov_id[i];
1590 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001591 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001592 unittest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001593 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001594 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001595 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001596 return;
1597 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001598 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001599 }
1600
Wang Long9697a552015-03-11 08:36:54 +00001601 unittest(1, "overlay test %d passed\n", 8);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001602}
1603
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001604/* test insertion of a bus with parent devices */
Wang Long9697a552015-03-11 08:36:54 +00001605static void of_unittest_overlay_10(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001606{
1607 int ret;
1608 char *child_path;
1609
1610 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001611 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1612 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001613 "overlay test %d failed; overlay application\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001614 return;
1615
Wang Long9697a552015-03-11 08:36:54 +00001616 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1617 unittest_path(10, PDEV_OVERLAY));
1618 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001619 return;
1620
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001621 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001622 kfree(child_path);
Wang Long9697a552015-03-11 08:36:54 +00001623 if (unittest(ret, "overlay test %d failed; no child device\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001624 return;
1625}
1626
1627/* test insertion of a bus with parent devices (and revert) */
Wang Long9697a552015-03-11 08:36:54 +00001628static void of_unittest_overlay_11(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001629{
1630 int ret;
1631
1632 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001633 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001634 PDEV_OVERLAY);
Wang Long9697a552015-03-11 08:36:54 +00001635 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001636 "overlay test %d failed; overlay application\n", 11))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001637 return;
1638}
1639
Arnd Bergmann4252de32015-03-04 20:49:47 +01001640#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001641
Wang Long9697a552015-03-11 08:36:54 +00001642struct unittest_i2c_bus_data {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001643 struct platform_device *pdev;
1644 struct i2c_adapter adap;
1645};
1646
Wang Long9697a552015-03-11 08:36:54 +00001647static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001648 struct i2c_msg *msgs, int num)
1649{
Wang Long9697a552015-03-11 08:36:54 +00001650 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001651
1652 (void)std;
1653
1654 return num;
1655}
1656
Wang Long9697a552015-03-11 08:36:54 +00001657static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001658{
1659 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1660}
1661
Wang Long9697a552015-03-11 08:36:54 +00001662static const struct i2c_algorithm unittest_i2c_algo = {
1663 .master_xfer = unittest_i2c_master_xfer,
1664 .functionality = unittest_i2c_functionality,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001665};
1666
Wang Long9697a552015-03-11 08:36:54 +00001667static int unittest_i2c_bus_probe(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001668{
1669 struct device *dev = &pdev->dev;
1670 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001671 struct unittest_i2c_bus_data *std;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001672 struct i2c_adapter *adap;
1673 int ret;
1674
1675 if (np == NULL) {
1676 dev_err(dev, "No OF data for device\n");
1677 return -EINVAL;
1678
1679 }
1680
Rob Herring0d638a02017-06-01 15:50:55 -05001681 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001682
1683 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1684 if (!std) {
Wang Long9697a552015-03-11 08:36:54 +00001685 dev_err(dev, "Failed to allocate unittest i2c data\n");
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001686 return -ENOMEM;
1687 }
1688
1689 /* link them together */
1690 std->pdev = pdev;
1691 platform_set_drvdata(pdev, std);
1692
1693 adap = &std->adap;
1694 i2c_set_adapdata(adap, std);
1695 adap->nr = -1;
1696 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1697 adap->class = I2C_CLASS_DEPRECATED;
Wang Long9697a552015-03-11 08:36:54 +00001698 adap->algo = &unittest_i2c_algo;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001699 adap->dev.parent = dev;
1700 adap->dev.of_node = dev->of_node;
1701 adap->timeout = 5 * HZ;
1702 adap->retries = 3;
1703
1704 ret = i2c_add_numbered_adapter(adap);
1705 if (ret != 0) {
1706 dev_err(dev, "Failed to add I2C adapter\n");
1707 return ret;
1708 }
1709
1710 return 0;
1711}
1712
Wang Long9697a552015-03-11 08:36:54 +00001713static int unittest_i2c_bus_remove(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001714{
1715 struct device *dev = &pdev->dev;
1716 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001717 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001718
Rob Herring0d638a02017-06-01 15:50:55 -05001719 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001720 i2c_del_adapter(&std->adap);
1721
1722 return 0;
1723}
1724
Grant Likelya2166ca2015-03-29 08:59:58 +01001725static const struct of_device_id unittest_i2c_bus_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001726 { .compatible = "unittest-i2c-bus", },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001727 {},
1728};
1729
Wang Long9697a552015-03-11 08:36:54 +00001730static struct platform_driver unittest_i2c_bus_driver = {
1731 .probe = unittest_i2c_bus_probe,
1732 .remove = unittest_i2c_bus_remove,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001733 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001734 .name = "unittest-i2c-bus",
1735 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001736 },
1737};
1738
Wang Long9697a552015-03-11 08:36:54 +00001739static int unittest_i2c_dev_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001740 const struct i2c_device_id *id)
1741{
1742 struct device *dev = &client->dev;
1743 struct device_node *np = client->dev.of_node;
1744
1745 if (!np) {
1746 dev_err(dev, "No OF node\n");
1747 return -EINVAL;
1748 }
1749
Rob Herring0d638a02017-06-01 15:50:55 -05001750 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001751
1752 return 0;
1753};
1754
Wang Long9697a552015-03-11 08:36:54 +00001755static int unittest_i2c_dev_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001756{
1757 struct device *dev = &client->dev;
1758 struct device_node *np = client->dev.of_node;
1759
Rob Herring0d638a02017-06-01 15:50:55 -05001760 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001761 return 0;
1762}
1763
Wang Long9697a552015-03-11 08:36:54 +00001764static const struct i2c_device_id unittest_i2c_dev_id[] = {
1765 { .name = "unittest-i2c-dev" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001766 { }
1767};
1768
Wang Long9697a552015-03-11 08:36:54 +00001769static struct i2c_driver unittest_i2c_dev_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001770 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001771 .name = "unittest-i2c-dev",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001772 },
Wang Long9697a552015-03-11 08:36:54 +00001773 .probe = unittest_i2c_dev_probe,
1774 .remove = unittest_i2c_dev_remove,
1775 .id_table = unittest_i2c_dev_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001776};
1777
Arnd Bergmann4252de32015-03-04 20:49:47 +01001778#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001779
Peter Rosinb6e3b712016-04-20 08:42:47 +02001780static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001781{
1782 return 0;
1783}
1784
Wang Long9697a552015-03-11 08:36:54 +00001785static int unittest_i2c_mux_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001786 const struct i2c_device_id *id)
1787{
Peter Rosinb6e3b712016-04-20 08:42:47 +02001788 int ret, i, nchans;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001789 struct device *dev = &client->dev;
1790 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1791 struct device_node *np = client->dev.of_node, *child;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001792 struct i2c_mux_core *muxc;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001793 u32 reg, max_reg;
1794
Rob Herring0d638a02017-06-01 15:50:55 -05001795 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001796
1797 if (!np) {
1798 dev_err(dev, "No OF node\n");
1799 return -EINVAL;
1800 }
1801
1802 max_reg = (u32)-1;
1803 for_each_child_of_node(np, child) {
1804 ret = of_property_read_u32(child, "reg", &reg);
1805 if (ret)
1806 continue;
1807 if (max_reg == (u32)-1 || reg > max_reg)
1808 max_reg = reg;
1809 }
1810 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1811 if (nchans == 0) {
1812 dev_err(dev, "No channels\n");
1813 return -EINVAL;
1814 }
1815
Peter Rosinb6e3b712016-04-20 08:42:47 +02001816 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
1817 unittest_i2c_mux_select_chan, NULL);
1818 if (!muxc)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001819 return -ENOMEM;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001820 for (i = 0; i < nchans; i++) {
Peter Rosinb6e3b712016-04-20 08:42:47 +02001821 ret = i2c_mux_add_adapter(muxc, 0, i, 0);
1822 if (ret) {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001823 dev_err(dev, "Failed to register mux #%d\n", i);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001824 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001825 return -ENODEV;
1826 }
1827 }
1828
Peter Rosinb6e3b712016-04-20 08:42:47 +02001829 i2c_set_clientdata(client, muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001830
1831 return 0;
1832};
1833
Wang Long9697a552015-03-11 08:36:54 +00001834static int unittest_i2c_mux_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001835{
1836 struct device *dev = &client->dev;
1837 struct device_node *np = client->dev.of_node;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001838 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001839
Rob Herring0d638a02017-06-01 15:50:55 -05001840 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001841 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001842 return 0;
1843}
1844
Wang Long9697a552015-03-11 08:36:54 +00001845static const struct i2c_device_id unittest_i2c_mux_id[] = {
1846 { .name = "unittest-i2c-mux" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001847 { }
1848};
1849
Wang Long9697a552015-03-11 08:36:54 +00001850static struct i2c_driver unittest_i2c_mux_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001851 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001852 .name = "unittest-i2c-mux",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001853 },
Wang Long9697a552015-03-11 08:36:54 +00001854 .probe = unittest_i2c_mux_probe,
1855 .remove = unittest_i2c_mux_remove,
1856 .id_table = unittest_i2c_mux_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001857};
1858
1859#endif
1860
Wang Long9697a552015-03-11 08:36:54 +00001861static int of_unittest_overlay_i2c_init(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001862{
1863 int ret;
1864
Wang Long9697a552015-03-11 08:36:54 +00001865 ret = i2c_add_driver(&unittest_i2c_dev_driver);
1866 if (unittest(ret == 0,
1867 "could not register unittest i2c device driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001868 return ret;
1869
Wang Long9697a552015-03-11 08:36:54 +00001870 ret = platform_driver_register(&unittest_i2c_bus_driver);
1871 if (unittest(ret == 0,
1872 "could not register unittest i2c bus driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001873 return ret;
1874
Arnd Bergmann4252de32015-03-04 20:49:47 +01001875#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001876 ret = i2c_add_driver(&unittest_i2c_mux_driver);
1877 if (unittest(ret == 0,
1878 "could not register unittest i2c mux driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001879 return ret;
1880#endif
1881
1882 return 0;
1883}
1884
Wang Long9697a552015-03-11 08:36:54 +00001885static void of_unittest_overlay_i2c_cleanup(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001886{
Arnd Bergmann4252de32015-03-04 20:49:47 +01001887#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001888 i2c_del_driver(&unittest_i2c_mux_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001889#endif
Wang Long9697a552015-03-11 08:36:54 +00001890 platform_driver_unregister(&unittest_i2c_bus_driver);
1891 i2c_del_driver(&unittest_i2c_dev_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001892}
1893
Wang Long9697a552015-03-11 08:36:54 +00001894static void of_unittest_overlay_i2c_12(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001895{
1896 int ret;
1897
1898 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001899 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001900 if (ret != 0)
1901 return;
1902
Wang Long9697a552015-03-11 08:36:54 +00001903 unittest(1, "overlay test %d passed\n", 12);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001904}
1905
1906/* test deactivation of device */
Wang Long9697a552015-03-11 08:36:54 +00001907static void of_unittest_overlay_i2c_13(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001908{
1909 int ret;
1910
1911 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001912 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001913 if (ret != 0)
1914 return;
1915
Wang Long9697a552015-03-11 08:36:54 +00001916 unittest(1, "overlay test %d passed\n", 13);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001917}
1918
1919/* just check for i2c mux existence */
Wang Long9697a552015-03-11 08:36:54 +00001920static void of_unittest_overlay_i2c_14(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001921{
1922}
1923
Wang Long9697a552015-03-11 08:36:54 +00001924static void of_unittest_overlay_i2c_15(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001925{
1926 int ret;
1927
1928 /* device should enable */
Alexander Sverdlinca7b8962017-01-19 11:06:16 +01001929 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001930 if (ret != 0)
1931 return;
1932
Wang Long9697a552015-03-11 08:36:54 +00001933 unittest(1, "overlay test %d passed\n", 15);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001934}
1935
1936#else
1937
Wang Long9697a552015-03-11 08:36:54 +00001938static inline void of_unittest_overlay_i2c_14(void) { }
1939static inline void of_unittest_overlay_i2c_15(void) { }
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001940
1941#endif
1942
Wang Long9697a552015-03-11 08:36:54 +00001943static void __init of_unittest_overlay(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001944{
1945 struct device_node *bus_np = NULL;
1946 int ret;
1947
Wang Long9697a552015-03-11 08:36:54 +00001948 ret = platform_driver_register(&unittest_driver);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001949 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001950 unittest(0, "could not register unittest driver\n");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001951 goto out;
1952 }
1953
1954 bus_np = of_find_node_by_path(bus_path);
1955 if (bus_np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001956 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001957 goto out;
1958 }
1959
Kefeng Wang146dedb2016-06-01 14:53:10 +08001960 ret = of_platform_default_populate(bus_np, NULL, NULL);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001961 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001962 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001963 goto out;
1964 }
1965
Wang Long9697a552015-03-11 08:36:54 +00001966 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
1967 unittest(0, "could not find unittest0 @ \"%s\"\n",
1968 unittest_path(100, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001969 goto out;
1970 }
1971
Wang Long9697a552015-03-11 08:36:54 +00001972 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
1973 unittest(0, "unittest1 @ \"%s\" should not exist\n",
1974 unittest_path(101, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001975 goto out;
1976 }
1977
Wang Long9697a552015-03-11 08:36:54 +00001978 unittest(1, "basic infrastructure of overlays passed");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001979
1980 /* tests in sequence */
Wang Long9697a552015-03-11 08:36:54 +00001981 of_unittest_overlay_0();
1982 of_unittest_overlay_1();
1983 of_unittest_overlay_2();
1984 of_unittest_overlay_3();
1985 of_unittest_overlay_4();
1986 of_unittest_overlay_5();
1987 of_unittest_overlay_6();
1988 of_unittest_overlay_8();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001989
Wang Long9697a552015-03-11 08:36:54 +00001990 of_unittest_overlay_10();
1991 of_unittest_overlay_11();
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001992
Arnd Bergmann4252de32015-03-04 20:49:47 +01001993#if IS_BUILTIN(CONFIG_I2C)
Wang Long9697a552015-03-11 08:36:54 +00001994 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001995 goto out;
1996
Wang Long9697a552015-03-11 08:36:54 +00001997 of_unittest_overlay_i2c_12();
1998 of_unittest_overlay_i2c_13();
1999 of_unittest_overlay_i2c_14();
2000 of_unittest_overlay_i2c_15();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002001
Wang Long9697a552015-03-11 08:36:54 +00002002 of_unittest_overlay_i2c_cleanup();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002003#endif
2004
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03002005 of_unittest_destroy_tracked_overlays();
2006
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002007out:
2008 of_node_put(bus_np);
2009}
2010
2011#else
Wang Long9697a552015-03-11 08:36:54 +00002012static inline void __init of_unittest_overlay(void) { }
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002013#endif
2014
Frank Rowand60a00042017-07-19 09:25:20 -07002015#ifdef CONFIG_OF_OVERLAY
2016
Frank Rowand81d0848f2017-04-25 17:09:54 -07002017/*
2018 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2019 * in scripts/Makefile.lib
2020 */
2021
2022#define OVERLAY_INFO_EXTERN(name) \
2023 extern uint8_t __dtb_##name##_begin[]; \
2024 extern uint8_t __dtb_##name##_end[]
2025
2026#define OVERLAY_INFO(name, expected) \
2027{ .dtb_begin = __dtb_##name##_begin, \
2028 .dtb_end = __dtb_##name##_end, \
2029 .expected_result = expected, \
2030}
2031
2032struct overlay_info {
2033 uint8_t *dtb_begin;
2034 uint8_t *dtb_end;
2035 void *data;
2036 struct device_node *np_overlay;
2037 int expected_result;
2038 int overlay_id;
2039};
2040
2041OVERLAY_INFO_EXTERN(overlay_base);
2042OVERLAY_INFO_EXTERN(overlay);
2043OVERLAY_INFO_EXTERN(overlay_bad_phandle);
Frank Rowand60a00042017-07-19 09:25:20 -07002044OVERLAY_INFO_EXTERN(overlay_bad_symbol);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002045
2046/* order of entries is hard-coded into users of overlays[] */
2047static struct overlay_info overlays[] = {
2048 OVERLAY_INFO(overlay_base, -9999),
2049 OVERLAY_INFO(overlay, 0),
2050 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
Frank Rowand60a00042017-07-19 09:25:20 -07002051 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002052 {}
2053};
2054
2055static struct device_node *overlay_base_root;
2056
Rob Herring0fa1c572018-01-05 15:32:33 -06002057static void * __init dt_alloc_memory(u64 size, u64 align)
2058{
2059 return memblock_virt_alloc(size, align);
2060}
2061
Frank Rowand81d0848f2017-04-25 17:09:54 -07002062/*
2063 * Create base device tree for the overlay unittest.
2064 *
2065 * This is called from very early boot code.
2066 *
2067 * Do as much as possible the same way as done in __unflatten_device_tree
2068 * and other early boot steps for the normal FDT so that the overlay base
2069 * unflattened tree will have the same characteristics as the real tree
2070 * (such as having memory allocated by the early allocator). The goal
2071 * is to test "the real thing" as much as possible, and test "test setup
2072 * code" as little as possible.
2073 *
2074 * Have to stop before resolving phandles, because that uses kmalloc.
2075 */
2076void __init unittest_unflatten_overlay_base(void)
2077{
2078 struct overlay_info *info;
2079 u32 data_size;
2080 u32 size;
2081
2082 info = &overlays[0];
2083
2084 if (info->expected_result != -9999) {
2085 pr_err("No dtb 'overlay_base' to attach\n");
2086 return;
2087 }
2088
2089 data_size = info->dtb_end - info->dtb_begin;
2090 if (!data_size) {
2091 pr_err("No dtb 'overlay_base' to attach\n");
2092 return;
2093 }
2094
2095 size = fdt_totalsize(info->dtb_begin);
2096 if (size != data_size) {
2097 pr_err("dtb 'overlay_base' header totalsize != actual size");
2098 return;
2099 }
2100
Rob Herring0fa1c572018-01-05 15:32:33 -06002101 info->data = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
Frank Rowand81d0848f2017-04-25 17:09:54 -07002102 if (!info->data) {
2103 pr_err("alloc for dtb 'overlay_base' failed");
2104 return;
2105 }
2106
2107 memcpy(info->data, info->dtb_begin, size);
2108
2109 __unflatten_device_tree(info->data, NULL, &info->np_overlay,
Rob Herring0fa1c572018-01-05 15:32:33 -06002110 dt_alloc_memory, true);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002111 overlay_base_root = info->np_overlay;
2112}
2113
2114/*
2115 * The purpose of of_unittest_overlay_data_add is to add an
2116 * overlay in the normal fashion. This is a test of the whole
2117 * picture, instead of testing individual elements.
2118 *
2119 * A secondary purpose is to be able to verify that the contents of
2120 * /proc/device-tree/ contains the updated structure and values from
2121 * the overlay. That must be verified separately in user space.
2122 *
2123 * Return 0 on unexpected error.
2124 */
2125static int __init overlay_data_add(int onum)
2126{
2127 struct overlay_info *info;
2128 int k;
2129 int ret;
2130 u32 size;
2131 u32 size_from_header;
2132
2133 for (k = 0, info = overlays; info; info++, k++) {
2134 if (k == onum)
2135 break;
2136 }
2137 if (onum > k)
2138 return 0;
2139
2140 size = info->dtb_end - info->dtb_begin;
2141 if (!size) {
2142 pr_err("no overlay to attach, %d\n", onum);
2143 ret = 0;
2144 }
2145
2146 size_from_header = fdt_totalsize(info->dtb_begin);
2147 if (size_from_header != size) {
2148 pr_err("overlay header totalsize != actual size, %d", onum);
2149 return 0;
2150 }
2151
2152 /*
2153 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
2154 * will create pointers to the passed in FDT in the EDT.
2155 */
2156 info->data = kmemdup(info->dtb_begin, size, GFP_KERNEL);
2157 if (!info->data) {
2158 pr_err("unable to allocate memory for data, %d\n", onum);
2159 return 0;
2160 }
2161
2162 of_fdt_unflatten_tree(info->data, NULL, &info->np_overlay);
2163 if (!info->np_overlay) {
2164 pr_err("unable to unflatten overlay, %d\n", onum);
2165 ret = 0;
2166 goto out_free_data;
2167 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07002168
Frank Rowand24789c52017-10-17 16:36:26 -07002169 info->overlay_id = 0;
2170 ret = of_overlay_apply(info->np_overlay, &info->overlay_id);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002171 if (ret < 0) {
Frank Rowand0290c4c2017-10-17 16:36:23 -07002172 pr_err("of_overlay_apply() (ret=%d), %d\n", ret, onum);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002173 goto out_free_np_overlay;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002174 }
2175
2176 pr_debug("__dtb_overlay_begin applied, overlay id %d\n", ret);
2177
2178 goto out;
2179
2180out_free_np_overlay:
2181 /*
2182 * info->np_overlay is the unflattened device tree
2183 * It has not been spliced into the live tree.
2184 */
2185
2186 /* todo: function to free unflattened device tree */
2187
2188out_free_data:
2189 kfree(info->data);
2190
2191out:
2192 return (ret == info->expected_result);
2193}
2194
2195/*
2196 * The purpose of of_unittest_overlay_high_level is to add an overlay
2197 * in the normal fashion. This is a test of the whole picture,
2198 * instead of individual elements.
2199 *
2200 * The first part of the function is _not_ normal overlay usage; it is
2201 * finishing splicing the base overlay device tree into the live tree.
2202 */
2203static __init void of_unittest_overlay_high_level(void)
2204{
2205 struct device_node *last_sibling;
2206 struct device_node *np;
2207 struct device_node *of_symbols;
2208 struct device_node *overlay_base_symbols;
2209 struct device_node **pprev;
2210 struct property *prop;
2211 int ret;
2212
2213 if (!overlay_base_root) {
2214 unittest(0, "overlay_base_root not initialized\n");
2215 return;
2216 }
2217
2218 /*
2219 * Could not fixup phandles in unittest_unflatten_overlay_base()
2220 * because kmalloc() was not yet available.
2221 */
Frank Rowandf948d6d2017-10-17 16:36:29 -07002222 of_overlay_mutex_lock();
Frank Rowand81d0848f2017-04-25 17:09:54 -07002223 of_resolve_phandles(overlay_base_root);
Frank Rowandf948d6d2017-10-17 16:36:29 -07002224 of_overlay_mutex_unlock();
2225
Frank Rowand81d0848f2017-04-25 17:09:54 -07002226
2227 /*
2228 * do not allow overlay_base to duplicate any node already in
2229 * tree, this greatly simplifies the code
2230 */
2231
2232 /*
2233 * remove overlay_base_root node "__local_fixups", after
2234 * being used by of_resolve_phandles()
2235 */
2236 pprev = &overlay_base_root->child;
2237 for (np = overlay_base_root->child; np; np = np->sibling) {
2238 if (!of_node_cmp(np->name, "__local_fixups__")) {
2239 *pprev = np->sibling;
2240 break;
2241 }
2242 pprev = &np->sibling;
2243 }
2244
2245 /* remove overlay_base_root node "__symbols__" if in live tree */
2246 of_symbols = of_get_child_by_name(of_root, "__symbols__");
2247 if (of_symbols) {
2248 /* will have to graft properties from node into live tree */
2249 pprev = &overlay_base_root->child;
2250 for (np = overlay_base_root->child; np; np = np->sibling) {
2251 if (!of_node_cmp(np->name, "__symbols__")) {
2252 overlay_base_symbols = np;
2253 *pprev = np->sibling;
2254 break;
2255 }
2256 pprev = &np->sibling;
2257 }
2258 }
2259
2260 for (np = overlay_base_root->child; np; np = np->sibling) {
2261 if (of_get_child_by_name(of_root, np->name)) {
2262 unittest(0, "illegal node name in overlay_base %s",
2263 np->name);
2264 return;
2265 }
2266 }
2267
2268 /*
2269 * overlay 'overlay_base' is not allowed to have root
2270 * properties, so only need to splice nodes into main device tree.
2271 *
2272 * root node of *overlay_base_root will not be freed, it is lost
2273 * memory.
2274 */
2275
2276 for (np = overlay_base_root->child; np; np = np->sibling)
2277 np->parent = of_root;
2278
2279 mutex_lock(&of_mutex);
2280
Arnd Bergmannee320b32017-04-28 11:44:11 +02002281 for (last_sibling = np = of_root->child; np; np = np->sibling)
Frank Rowand81d0848f2017-04-25 17:09:54 -07002282 last_sibling = np;
2283
2284 if (last_sibling)
2285 last_sibling->sibling = overlay_base_root->child;
2286 else
2287 of_root->child = overlay_base_root->child;
2288
2289 for_each_of_allnodes_from(overlay_base_root, np)
2290 __of_attach_node_sysfs(np);
2291
2292 if (of_symbols) {
2293 for_each_property_of_node(overlay_base_symbols, prop) {
2294 ret = __of_add_property(of_symbols, prop);
2295 if (ret) {
2296 unittest(0,
2297 "duplicate property '%s' in overlay_base node __symbols__",
2298 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002299 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002300 }
2301 ret = __of_add_property_sysfs(of_symbols, prop);
2302 if (ret) {
2303 unittest(0,
2304 "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
2305 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002306 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002307 }
2308 }
2309 }
2310
2311 mutex_unlock(&of_mutex);
2312
2313
2314 /* now do the normal overlay usage test */
2315
2316 unittest(overlay_data_add(1),
2317 "Adding overlay 'overlay' failed\n");
2318
2319 unittest(overlay_data_add(2),
2320 "Adding overlay 'overlay_bad_phandle' failed\n");
Frank Rowand60a00042017-07-19 09:25:20 -07002321
2322 unittest(overlay_data_add(3),
2323 "Adding overlay 'overlay_bad_symbol' failed\n");
2324
Dan Carpenter8756cd12017-05-03 22:49:50 +03002325 return;
2326
2327err_unlock:
2328 mutex_unlock(&of_mutex);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002329}
2330
2331#else
2332
2333static inline __init void of_unittest_overlay_high_level(void) {}
2334
2335#endif
2336
Wang Long9697a552015-03-11 08:36:54 +00002337static int __init of_unittest(void)
Grant Likely53a42092011-12-12 09:25:57 -07002338{
2339 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002340 int res;
2341
Wang Long9697a552015-03-11 08:36:54 +00002342 /* adding data for unittest */
2343 res = unittest_data_add();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002344 if (res)
2345 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00002346 if (!of_aliases)
2347 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07002348
2349 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2350 if (!np) {
2351 pr_info("No testcase data in device tree; not running tests\n");
2352 return 0;
2353 }
2354 of_node_put(np);
2355
Wang Long9697a552015-03-11 08:36:54 +00002356 pr_info("start of unittest - you will see error messages\n");
2357 of_unittest_check_tree_linkage();
2358 of_unittest_check_phandles();
2359 of_unittest_find_node_by_name();
2360 of_unittest_dynamic();
2361 of_unittest_parse_phandle_with_args();
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002362 of_unittest_printf();
Wang Long9697a552015-03-11 08:36:54 +00002363 of_unittest_property_string();
2364 of_unittest_property_copy();
2365 of_unittest_changeset();
2366 of_unittest_parse_interrupts();
2367 of_unittest_parse_interrupts_extended();
2368 of_unittest_match_node();
2369 of_unittest_platform_populate();
2370 of_unittest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002371
Grant Likelyf2051d62014-10-01 17:40:22 +01002372 /* Double check linkage after removing testcase data */
Wang Long9697a552015-03-11 08:36:54 +00002373 of_unittest_check_tree_linkage();
Grant Likelyf2051d62014-10-01 17:40:22 +01002374
Frank Rowand81d0848f2017-04-25 17:09:54 -07002375 of_unittest_overlay_high_level();
2376
Wang Long9697a552015-03-11 08:36:54 +00002377 pr_info("end of unittest - %i passed, %i failed\n",
2378 unittest_results.passed, unittest_results.failed);
Grant Likelyf2051d62014-10-01 17:40:22 +01002379
Grant Likely53a42092011-12-12 09:25:57 -07002380 return 0;
2381}
Wang Long9697a552015-03-11 08:36:54 +00002382late_initcall(of_unittest);