blob: 8e23b51eead1a1991ecab7c105fdea65432f6b3e [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
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800456static void __init of_unittest_parse_phandle_with_args_map(void)
457{
458 struct device_node *np, *p0, *p1, *p2, *p3;
459 struct of_phandle_args args;
460 int i, rc;
461
462 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
463 if (!np) {
464 pr_err("missing testcase data\n");
465 return;
466 }
467
468 p0 = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
469 if (!p0) {
470 pr_err("missing testcase data\n");
471 return;
472 }
473
474 p1 = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
475 if (!p1) {
476 pr_err("missing testcase data\n");
477 return;
478 }
479
480 p2 = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
481 if (!p2) {
482 pr_err("missing testcase data\n");
483 return;
484 }
485
486 p3 = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
487 if (!p3) {
488 pr_err("missing testcase data\n");
489 return;
490 }
491
492 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
493 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
494
495 for (i = 0; i < 8; i++) {
496 bool passed = true;
497
498 rc = of_parse_phandle_with_args_map(np, "phandle-list",
499 "phandle", i, &args);
500
501 /* Test the values from tests-phandle.dtsi */
502 switch (i) {
503 case 0:
504 passed &= !rc;
505 passed &= (args.np == p1);
506 passed &= (args.args_count == 1);
507 passed &= (args.args[0] == 1);
508 break;
509 case 1:
510 passed &= !rc;
511 passed &= (args.np == p3);
512 passed &= (args.args_count == 3);
513 passed &= (args.args[0] == 2);
514 passed &= (args.args[1] == 5);
515 passed &= (args.args[2] == 3);
516 break;
517 case 2:
518 passed &= (rc == -ENOENT);
519 break;
520 case 3:
521 passed &= !rc;
522 passed &= (args.np == p0);
523 passed &= (args.args_count == 0);
524 break;
525 case 4:
526 passed &= !rc;
527 passed &= (args.np == p1);
528 passed &= (args.args_count == 1);
529 passed &= (args.args[0] == 3);
530 break;
531 case 5:
532 passed &= !rc;
533 passed &= (args.np == p0);
534 passed &= (args.args_count == 0);
535 break;
536 case 6:
537 passed &= !rc;
538 passed &= (args.np == p2);
539 passed &= (args.args_count == 2);
540 passed &= (args.args[0] == 15);
541 passed &= (args.args[1] == 0x20);
542 break;
543 case 7:
544 passed &= (rc == -ENOENT);
545 break;
546 default:
547 passed = false;
548 }
549
550 unittest(passed, "index %i - data error on node %s rc=%i\n",
551 i, args.np->full_name, rc);
552 }
553
554 /* Check for missing list property */
555 rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
556 "phandle", 0, &args);
557 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
558
559 /* Check for missing cells,map,mask property */
560 rc = of_parse_phandle_with_args_map(np, "phandle-list",
561 "phandle-missing", 0, &args);
562 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
563
564 /* Check for bad phandle in list */
565 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
566 "phandle", 0, &args);
567 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
568
569 /* Check for incorrectly formed argument list */
570 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
571 "phandle", 1, &args);
572 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
573}
574
Wang Long9697a552015-03-11 08:36:54 +0000575static void __init of_unittest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700576{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000577 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700578 struct device_node *np;
579 int rc;
580
Grant Likely7aff0fe2011-12-12 09:25:58 -0700581 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
582 if (!np) {
583 pr_err("No testcase data in device tree\n");
584 return;
585 }
586
587 rc = of_property_match_string(np, "phandle-list-names", "first");
Wang Long9697a552015-03-11 08:36:54 +0000588 unittest(rc == 0, "first expected:0 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700589 rc = of_property_match_string(np, "phandle-list-names", "second");
Wang Long9697a552015-03-11 08:36:54 +0000590 unittest(rc == 1, "second expected:1 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700591 rc = of_property_match_string(np, "phandle-list-names", "third");
Wang Long9697a552015-03-11 08:36:54 +0000592 unittest(rc == 2, "third expected:2 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700593 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Wang Long9697a552015-03-11 08:36:54 +0000594 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700595 rc = of_property_match_string(np, "missing-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000596 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700597 rc = of_property_match_string(np, "empty-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000598 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700599 rc = of_property_match_string(np, "unterminated-string", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000600 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000601
602 /* of_property_count_strings() tests */
603 rc = of_property_count_strings(np, "string-property");
Wang Long9697a552015-03-11 08:36:54 +0000604 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000605 rc = of_property_count_strings(np, "phandle-list-names");
Wang Long9697a552015-03-11 08:36:54 +0000606 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000607 rc = of_property_count_strings(np, "unterminated-string");
Wang Long9697a552015-03-11 08:36:54 +0000608 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000609 rc = of_property_count_strings(np, "unterminated-string-list");
Wang Long9697a552015-03-11 08:36:54 +0000610 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000611
612 /* of_property_read_string_index() tests */
613 rc = of_property_read_string_index(np, "string-property", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000614 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000615 strings[0] = NULL;
616 rc = of_property_read_string_index(np, "string-property", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000617 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000618 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000619 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000620 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000621 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000622 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
Wang Long9697a552015-03-11 08:36:54 +0000623 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000624 strings[0] = NULL;
625 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
Wang Long9697a552015-03-11 08:36:54 +0000626 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000627 strings[0] = NULL;
628 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000629 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000630 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000631 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000632 strings[0] = NULL;
633 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
Wang Long9697a552015-03-11 08:36:54 +0000634 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000635 strings[1] = NULL;
636
637 /* of_property_read_string_array() tests */
638 rc = of_property_read_string_array(np, "string-property", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000639 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000640 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000641 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000642 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000643 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000644 /* -- An incorrectly formed string should cause a failure */
645 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000646 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000647 /* -- parsing the correctly formed strings should still work: */
648 strings[2] = NULL;
649 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
Wang Long9697a552015-03-11 08:36:54 +0000650 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000651 strings[1] = NULL;
652 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
Wang Long9697a552015-03-11 08:36:54 +0000653 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 -0700654}
655
Pantelis Antoniou69843392014-07-04 19:58:47 +0300656#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
657 (p1)->value && (p2)->value && \
658 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
659 !strcmp((p1)->name, (p2)->name))
Wang Long9697a552015-03-11 08:36:54 +0000660static void __init of_unittest_property_copy(void)
Pantelis Antoniou69843392014-07-04 19:58:47 +0300661{
662#ifdef CONFIG_OF_DYNAMIC
663 struct property p1 = { .name = "p1", .length = 0, .value = "" };
664 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
665 struct property *new;
666
667 new = __of_prop_dup(&p1, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000668 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300669 kfree(new->value);
670 kfree(new->name);
671 kfree(new);
672
673 new = __of_prop_dup(&p2, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000674 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300675 kfree(new->value);
676 kfree(new->name);
677 kfree(new);
678#endif
679}
680
Wang Long9697a552015-03-11 08:36:54 +0000681static void __init of_unittest_changeset(void)
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300682{
683#ifdef CONFIG_OF_DYNAMIC
684 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
685 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
686 struct property *ppremove;
Grant Likelye5179582014-11-17 22:31:32 +0000687 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300688 struct of_changeset chgset;
689
Grant Likelye5179582014-11-17 22:31:32 +0000690 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
Wang Long9697a552015-03-11 08:36:54 +0000691 unittest(n1, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000692 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
Wang Long9697a552015-03-11 08:36:54 +0000693 unittest(n2, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000694 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
Wang Long9697a552015-03-11 08:36:54 +0000695 unittest(n21, "testcase setup failure %p\n", n21);
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300696 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
Wang Long9697a552015-03-11 08:36:54 +0000697 unittest(nremove, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300698 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000699 unittest(ppadd, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300700 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000701 unittest(ppupdate, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300702 parent = nremove->parent;
703 n1->parent = parent;
704 n2->parent = parent;
705 n21->parent = n2;
706 n2->child = n21;
707 ppremove = of_find_property(parent, "prop-remove", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000708 unittest(ppremove, "failed to find removal prop");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300709
710 of_changeset_init(&chgset);
Wang Long9697a552015-03-11 08:36:54 +0000711 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
712 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
713 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
714 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
715 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
716 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
717 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
Wang Long9697a552015-03-11 08:36:54 +0000718 unittest(!of_changeset_apply(&chgset), "apply failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300719
Grant Likelye5179582014-11-17 22:31:32 +0000720 /* Make sure node names are constructed correctly */
Wang Long9697a552015-03-11 08:36:54 +0000721 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
Rob Herring0d638a02017-06-01 15:50:55 -0500722 "'%pOF' not added\n", n21);
Markus Elfringc46ca3c2014-12-02 13:54:00 +0100723 of_node_put(np);
Grant Likelye5179582014-11-17 22:31:32 +0000724
Wang Long9697a552015-03-11 08:36:54 +0000725 unittest(!of_changeset_revert(&chgset), "revert failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300726
727 of_changeset_destroy(&chgset);
728#endif
729}
730
Wang Long9697a552015-03-11 08:36:54 +0000731static void __init of_unittest_parse_interrupts(void)
Grant Likelya9f10ca2013-10-11 22:04:23 +0100732{
733 struct device_node *np;
734 struct of_phandle_args args;
735 int i, rc;
736
737 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
738 if (!np) {
739 pr_err("missing testcase data\n");
740 return;
741 }
742
743 for (i = 0; i < 4; i++) {
744 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700745
Grant Likelya9f10ca2013-10-11 22:04:23 +0100746 args.args_count = 0;
747 rc = of_irq_parse_one(np, i, &args);
748
749 passed &= !rc;
750 passed &= (args.args_count == 1);
751 passed &= (args.args[0] == (i + 1));
752
Rob Herring0d638a02017-06-01 15:50:55 -0500753 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
754 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100755 }
756 of_node_put(np);
757
758 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
759 if (!np) {
760 pr_err("missing testcase data\n");
761 return;
762 }
763
764 for (i = 0; i < 4; i++) {
765 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700766
Grant Likelya9f10ca2013-10-11 22:04:23 +0100767 args.args_count = 0;
768 rc = of_irq_parse_one(np, i, &args);
769
770 /* Test the values from tests-phandle.dtsi */
771 switch (i) {
772 case 0:
773 passed &= !rc;
774 passed &= (args.args_count == 1);
775 passed &= (args.args[0] == 9);
776 break;
777 case 1:
778 passed &= !rc;
779 passed &= (args.args_count == 3);
780 passed &= (args.args[0] == 10);
781 passed &= (args.args[1] == 11);
782 passed &= (args.args[2] == 12);
783 break;
784 case 2:
785 passed &= !rc;
786 passed &= (args.args_count == 2);
787 passed &= (args.args[0] == 13);
788 passed &= (args.args[1] == 14);
789 break;
790 case 3:
791 passed &= !rc;
792 passed &= (args.args_count == 2);
793 passed &= (args.args[0] == 15);
794 passed &= (args.args[1] == 16);
795 break;
796 default:
797 passed = false;
798 }
Rob Herring0d638a02017-06-01 15:50:55 -0500799 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
800 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100801 }
802 of_node_put(np);
803}
804
Wang Long9697a552015-03-11 08:36:54 +0000805static void __init of_unittest_parse_interrupts_extended(void)
Grant Likely79d97012013-09-19 16:47:37 -0500806{
807 struct device_node *np;
808 struct of_phandle_args args;
809 int i, rc;
810
811 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
812 if (!np) {
813 pr_err("missing testcase data\n");
814 return;
815 }
816
817 for (i = 0; i < 7; i++) {
818 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700819
Grant Likely79d97012013-09-19 16:47:37 -0500820 rc = of_irq_parse_one(np, i, &args);
821
822 /* Test the values from tests-phandle.dtsi */
823 switch (i) {
824 case 0:
825 passed &= !rc;
826 passed &= (args.args_count == 1);
827 passed &= (args.args[0] == 1);
828 break;
829 case 1:
830 passed &= !rc;
831 passed &= (args.args_count == 3);
832 passed &= (args.args[0] == 2);
833 passed &= (args.args[1] == 3);
834 passed &= (args.args[2] == 4);
835 break;
836 case 2:
837 passed &= !rc;
838 passed &= (args.args_count == 2);
839 passed &= (args.args[0] == 5);
840 passed &= (args.args[1] == 6);
841 break;
842 case 3:
843 passed &= !rc;
844 passed &= (args.args_count == 1);
845 passed &= (args.args[0] == 9);
846 break;
847 case 4:
848 passed &= !rc;
849 passed &= (args.args_count == 3);
850 passed &= (args.args[0] == 10);
851 passed &= (args.args[1] == 11);
852 passed &= (args.args[2] == 12);
853 break;
854 case 5:
855 passed &= !rc;
856 passed &= (args.args_count == 2);
857 passed &= (args.args[0] == 13);
858 passed &= (args.args[1] == 14);
859 break;
860 case 6:
861 passed &= !rc;
862 passed &= (args.args_count == 1);
863 passed &= (args.args[0] == 15);
864 break;
865 default:
866 passed = false;
867 }
868
Rob Herring0d638a02017-06-01 15:50:55 -0500869 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
870 i, args.np, rc);
Grant Likely79d97012013-09-19 16:47:37 -0500871 }
872 of_node_put(np);
873}
874
Frank Rowandafaed7a2015-03-14 00:00:36 -0700875static const struct of_device_id match_node_table[] = {
Grant Likely1f42e5d2014-02-18 21:38:55 +0000876 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
877 { .data = "B", .type = "type1", }, /* followed by type alone */
878
879 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
880 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
881 { .data = "Cc", .name = "name2", .type = "type2", },
882
883 { .data = "E", .compatible = "compat3" },
884 { .data = "G", .compatible = "compat2", },
885 { .data = "H", .compatible = "compat2", .name = "name5", },
886 { .data = "I", .compatible = "compat2", .type = "type1", },
887 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
888 { .data = "K", .compatible = "compat2", .name = "name9", },
889 {}
890};
891
892static struct {
893 const char *path;
894 const char *data;
895} match_node_tests[] = {
896 { .path = "/testcase-data/match-node/name0", .data = "A", },
897 { .path = "/testcase-data/match-node/name1", .data = "B", },
898 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
899 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
900 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
901 { .path = "/testcase-data/match-node/name3", .data = "E", },
902 { .path = "/testcase-data/match-node/name4", .data = "G", },
903 { .path = "/testcase-data/match-node/name5", .data = "H", },
904 { .path = "/testcase-data/match-node/name6", .data = "G", },
905 { .path = "/testcase-data/match-node/name7", .data = "I", },
906 { .path = "/testcase-data/match-node/name8", .data = "J", },
907 { .path = "/testcase-data/match-node/name9", .data = "K", },
908};
909
Wang Long9697a552015-03-11 08:36:54 +0000910static void __init of_unittest_match_node(void)
Grant Likely1f42e5d2014-02-18 21:38:55 +0000911{
912 struct device_node *np;
913 const struct of_device_id *match;
914 int i;
915
916 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
917 np = of_find_node_by_path(match_node_tests[i].path);
918 if (!np) {
Wang Long9697a552015-03-11 08:36:54 +0000919 unittest(0, "missing testcase node %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000920 match_node_tests[i].path);
921 continue;
922 }
923
924 match = of_match_node(match_node_table, np);
925 if (!match) {
Wang Long9697a552015-03-11 08:36:54 +0000926 unittest(0, "%s didn't match anything\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000927 match_node_tests[i].path);
928 continue;
929 }
930
931 if (strcmp(match->data, match_node_tests[i].data) != 0) {
Wang Long9697a552015-03-11 08:36:54 +0000932 unittest(0, "%s got wrong match. expected %s, got %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000933 match_node_tests[i].path, match_node_tests[i].data,
934 (const char *)match->data);
935 continue;
936 }
Wang Long9697a552015-03-11 08:36:54 +0000937 unittest(1, "passed");
Grant Likely1f42e5d2014-02-18 21:38:55 +0000938 }
939}
940
Grant Likelyd2329fb2016-01-04 13:13:21 +0100941static struct resource test_bus_res = {
942 .start = 0xfffffff8,
943 .end = 0xfffffff9,
944 .flags = IORESOURCE_MEM,
945};
Grant Likely37791b62015-03-27 20:30:04 -0700946static const struct platform_device_info test_bus_info = {
947 .name = "unittest-bus",
Grant Likely851da972014-11-04 13:14:13 +0000948};
Wang Long9697a552015-03-11 08:36:54 +0000949static void __init of_unittest_platform_populate(void)
Rob Herring82c0f582014-04-23 17:57:40 -0500950{
Grant Likely851da972014-11-04 13:14:13 +0000951 int irq, rc;
952 struct device_node *np, *child, *grandchild;
Grant Likely37791b62015-03-27 20:30:04 -0700953 struct platform_device *pdev, *test_bus;
Frank Rowandafaed7a2015-03-14 00:00:36 -0700954 const struct of_device_id match[] = {
Rob Herringfb2caa52014-05-13 10:07:54 -0500955 { .compatible = "test-device", },
956 {}
957 };
Rob Herring82c0f582014-04-23 17:57:40 -0500958
959 np = of_find_node_by_path("/testcase-data");
Kefeng Wang146dedb2016-06-01 14:53:10 +0800960 of_platform_default_populate(np, NULL, NULL);
Rob Herring82c0f582014-04-23 17:57:40 -0500961
962 /* Test that a missing irq domain returns -EPROBE_DEFER */
963 np = of_find_node_by_path("/testcase-data/testcase-device1");
964 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +0000965 unittest(pdev, "device 1 creation failed\n");
Rob Herring7d1cdc82014-05-13 10:07:29 -0500966
Rob Herring82c0f582014-04-23 17:57:40 -0500967 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +0000968 unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500969
970 /* Test that a parsing failure does not return -EPROBE_DEFER */
971 np = of_find_node_by_path("/testcase-data/testcase-device2");
972 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +0000973 unittest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500974 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +0000975 unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500976
Frank Rowand716e1d42015-03-13 23:57:40 -0700977 np = of_find_node_by_path("/testcase-data/platform-tests");
Grant Likelya2166ca2015-03-29 08:59:58 +0100978 unittest(np, "No testcase data in device tree\n");
Frank Rowand716e1d42015-03-13 23:57:40 -0700979 if (!np)
Rob Herringfb2caa52014-05-13 10:07:54 -0500980 return;
Grant Likely851da972014-11-04 13:14:13 +0000981
Grant Likely37791b62015-03-27 20:30:04 -0700982 test_bus = platform_device_register_full(&test_bus_info);
983 rc = PTR_ERR_OR_ZERO(test_bus);
Grant Likelya2166ca2015-03-29 08:59:58 +0100984 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
Frank Rowand716e1d42015-03-13 23:57:40 -0700985 if (rc)
Grant Likely851da972014-11-04 13:14:13 +0000986 return;
Grant Likely37791b62015-03-27 20:30:04 -0700987 test_bus->dev.of_node = np;
Rob Herringfb2caa52014-05-13 10:07:54 -0500988
Grant Likelyd2329fb2016-01-04 13:13:21 +0100989 /*
990 * Add a dummy resource to the test bus node after it is
991 * registered to catch problems with un-inserted resources. The
992 * DT code doesn't insert the resources, and it has caused the
993 * kernel to oops in the past. This makes sure the same bug
994 * doesn't crop up again.
995 */
996 platform_device_add_resources(test_bus, &test_bus_res, 1);
997
Grant Likely37791b62015-03-27 20:30:04 -0700998 of_platform_populate(np, match, NULL, &test_bus->dev);
Rob Herringfb2caa52014-05-13 10:07:54 -0500999 for_each_child_of_node(np, child) {
Rob Herringfb2caa52014-05-13 10:07:54 -05001000 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +00001001 unittest(of_find_device_by_node(grandchild),
Rob Herringfb2caa52014-05-13 10:07:54 -05001002 "Could not create device for node '%s'\n",
1003 grandchild->name);
1004 }
Grant Likely851da972014-11-04 13:14:13 +00001005
Grant Likely37791b62015-03-27 20:30:04 -07001006 of_platform_depopulate(&test_bus->dev);
Grant Likely851da972014-11-04 13:14:13 +00001007 for_each_child_of_node(np, child) {
1008 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +00001009 unittest(!of_find_device_by_node(grandchild),
Grant Likely851da972014-11-04 13:14:13 +00001010 "device didn't get destroyed '%s'\n",
1011 grandchild->name);
1012 }
1013
Grant Likely37791b62015-03-27 20:30:04 -07001014 platform_device_unregister(test_bus);
Grant Likely851da972014-11-04 13:14:13 +00001015 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -05001016}
1017
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001018/**
1019 * update_node_properties - adds the properties
1020 * of np into dup node (present in live tree) and
1021 * updates parent of children of np to dup.
1022 *
1023 * @np: node already present in live tree
1024 * @dup: node present in live tree to be updated
1025 */
1026static void update_node_properties(struct device_node *np,
1027 struct device_node *dup)
1028{
1029 struct property *prop;
1030 struct device_node *child;
1031
1032 for_each_property_of_node(np, prop)
1033 of_add_property(dup, prop);
1034
1035 for_each_child_of_node(np, child)
1036 child->parent = dup;
1037}
1038
1039/**
1040 * attach_node_and_children - attaches nodes
1041 * and its children to live tree
1042 *
1043 * @np: Node to attach to live tree
1044 */
1045static int attach_node_and_children(struct device_node *np)
1046{
Grant Likely5063e252014-10-03 16:28:27 +01001047 struct device_node *next, *dup, *child;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -08001048 unsigned long flags;
Rob Herring0d638a02017-06-01 15:50:55 -05001049 const char *full_name;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001050
Rob Herring0d638a02017-06-01 15:50:55 -05001051 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1052 dup = of_find_node_by_path(full_name);
1053 kfree(full_name);
Grant Likely5063e252014-10-03 16:28:27 +01001054 if (dup) {
1055 update_node_properties(np, dup);
1056 return 0;
1057 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001058
Grant Likely5063e252014-10-03 16:28:27 +01001059 child = np->child;
1060 np->child = NULL;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -08001061
1062 mutex_lock(&of_mutex);
1063 raw_spin_lock_irqsave(&devtree_lock, flags);
1064 np->sibling = np->parent->child;
1065 np->parent->child = np;
1066 of_node_clear_flag(np, OF_DETACHED);
1067 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1068
1069 __of_attach_node_sysfs(np);
1070 mutex_unlock(&of_mutex);
1071
Grant Likely5063e252014-10-03 16:28:27 +01001072 while (child) {
1073 next = child->sibling;
1074 attach_node_and_children(child);
1075 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001076 }
1077
1078 return 0;
1079}
1080
1081/**
Wang Long9697a552015-03-11 08:36:54 +00001082 * unittest_data_add - Reads, copies data from
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001083 * linked tree and attaches it to the live tree
1084 */
Wang Long9697a552015-03-11 08:36:54 +00001085static int __init unittest_data_add(void)
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001086{
Wang Long9697a552015-03-11 08:36:54 +00001087 void *unittest_data;
1088 struct device_node *unittest_data_node, *np;
Frank Rowandc8547112015-03-14 00:04:24 -07001089 /*
1090 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
1091 * created by cmd_dt_S_dtb in scripts/Makefile.lib
1092 */
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001093 extern uint8_t __dtb_testcases_begin[];
1094 extern uint8_t __dtb_testcases_end[];
1095 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +01001096 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001097
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001098 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001099 pr_warn("%s: No testcase data to attach; not running tests\n",
1100 __func__);
1101 return -ENODATA;
1102 }
1103
1104 /* creating copy */
Wang Long9697a552015-03-11 08:36:54 +00001105 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001106
Wang Long9697a552015-03-11 08:36:54 +00001107 if (!unittest_data) {
1108 pr_warn("%s: Failed to allocate memory for unittest_data; "
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001109 "not running tests\n", __func__);
1110 return -ENOMEM;
1111 }
Gavin Shanc4263232016-05-03 23:22:50 +10001112 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
Wang Long9697a552015-03-11 08:36:54 +00001113 if (!unittest_data_node) {
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001114 pr_warn("%s: No tree to attach; not running tests\n", __func__);
1115 return -ENODATA;
1116 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001117
1118 /*
1119 * This lock normally encloses of_overlay_apply() as well as
1120 * of_resolve_phandles().
1121 */
1122 of_overlay_mutex_lock();
1123
Wang Long9697a552015-03-11 08:36:54 +00001124 rc = of_resolve_phandles(unittest_data_node);
Grant Likely2eb46da2014-10-02 14:36:46 +01001125 if (rc) {
1126 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
Frank Rowandf948d6d2017-10-17 16:36:29 -07001127 of_overlay_mutex_unlock();
Grant Likely2eb46da2014-10-02 14:36:46 +01001128 return -EINVAL;
1129 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001130
Grant Likely5063e252014-10-03 16:28:27 +01001131 if (!of_root) {
Wang Long9697a552015-03-11 08:36:54 +00001132 of_root = unittest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001133 for_each_of_allnodes(np)
1134 __of_attach_node_sysfs(np);
1135 of_aliases = of_find_node_by_path("/aliases");
1136 of_chosen = of_find_node_by_path("/chosen");
Frank Rowandf948d6d2017-10-17 16:36:29 -07001137 of_overlay_mutex_unlock();
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001138 return 0;
1139 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001140
1141 /* attach the sub-tree to live tree */
Wang Long9697a552015-03-11 08:36:54 +00001142 np = unittest_data_node->child;
Grant Likely5063e252014-10-03 16:28:27 +01001143 while (np) {
1144 struct device_node *next = np->sibling;
Frank Rowand3db316d2015-03-14 00:02:31 -07001145
Grant Likely5063e252014-10-03 16:28:27 +01001146 np->parent = of_root;
1147 attach_node_and_children(np);
1148 np = next;
1149 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001150
1151 of_overlay_mutex_unlock();
1152
Grant Likely5063e252014-10-03 16:28:27 +01001153 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001154}
1155
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001156#ifdef CONFIG_OF_OVERLAY
1157
Wang Long9697a552015-03-11 08:36:54 +00001158static int unittest_probe(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001159{
1160 struct device *dev = &pdev->dev;
1161 struct device_node *np = dev->of_node;
1162
1163 if (np == NULL) {
1164 dev_err(dev, "No OF data for device\n");
1165 return -EINVAL;
1166
1167 }
1168
Rob Herring0d638a02017-06-01 15:50:55 -05001169 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001170
1171 of_platform_populate(np, NULL, NULL, &pdev->dev);
1172
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001173 return 0;
1174}
1175
Wang Long9697a552015-03-11 08:36:54 +00001176static int unittest_remove(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001177{
1178 struct device *dev = &pdev->dev;
1179 struct device_node *np = dev->of_node;
1180
Rob Herring0d638a02017-06-01 15:50:55 -05001181 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001182 return 0;
1183}
1184
Grant Likelya2166ca2015-03-29 08:59:58 +01001185static const struct of_device_id unittest_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001186 { .compatible = "unittest", },
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001187 {},
1188};
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001189
Wang Long9697a552015-03-11 08:36:54 +00001190static struct platform_driver unittest_driver = {
1191 .probe = unittest_probe,
1192 .remove = unittest_remove,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001193 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001194 .name = "unittest",
Wang Long9697a552015-03-11 08:36:54 +00001195 .of_match_table = of_match_ptr(unittest_match),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001196 },
1197};
1198
1199/* get the platform device instantiated at the path */
1200static struct platform_device *of_path_to_platform_device(const char *path)
1201{
1202 struct device_node *np;
1203 struct platform_device *pdev;
1204
1205 np = of_find_node_by_path(path);
1206 if (np == NULL)
1207 return NULL;
1208
1209 pdev = of_find_device_by_node(np);
1210 of_node_put(np);
1211
1212 return pdev;
1213}
1214
1215/* find out if a platform device exists at that path */
1216static int of_path_platform_device_exists(const char *path)
1217{
1218 struct platform_device *pdev;
1219
1220 pdev = of_path_to_platform_device(path);
1221 platform_device_put(pdev);
1222 return pdev != NULL;
1223}
1224
Arnd Bergmann4252de32015-03-04 20:49:47 +01001225#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001226
1227/* get the i2c client device instantiated at the path */
1228static struct i2c_client *of_path_to_i2c_client(const char *path)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001229{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001230 struct device_node *np;
1231 struct i2c_client *client;
1232
1233 np = of_find_node_by_path(path);
1234 if (np == NULL)
1235 return NULL;
1236
1237 client = of_find_i2c_device_by_node(np);
1238 of_node_put(np);
1239
1240 return client;
1241}
1242
1243/* find out if a i2c client device exists at that path */
1244static int of_path_i2c_client_exists(const char *path)
1245{
1246 struct i2c_client *client;
1247
1248 client = of_path_to_i2c_client(path);
1249 if (client)
1250 put_device(&client->dev);
1251 return client != NULL;
1252}
1253#else
1254static int of_path_i2c_client_exists(const char *path)
1255{
1256 return 0;
1257}
1258#endif
1259
1260enum overlay_type {
1261 PDEV_OVERLAY,
1262 I2C_OVERLAY
1263};
1264
1265static int of_path_device_type_exists(const char *path,
1266 enum overlay_type ovtype)
1267{
1268 switch (ovtype) {
1269 case PDEV_OVERLAY:
1270 return of_path_platform_device_exists(path);
1271 case I2C_OVERLAY:
1272 return of_path_i2c_client_exists(path);
1273 }
1274 return 0;
1275}
1276
Wang Long9697a552015-03-11 08:36:54 +00001277static const char *unittest_path(int nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001278{
1279 const char *base;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001280 static char buf[256];
1281
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001282 switch (ovtype) {
1283 case PDEV_OVERLAY:
1284 base = "/testcase-data/overlay-node/test-bus";
1285 break;
1286 case I2C_OVERLAY:
1287 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1288 break;
1289 default:
1290 buf[0] = '\0';
1291 return buf;
1292 }
Wang Long9697a552015-03-11 08:36:54 +00001293 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001294 buf[sizeof(buf) - 1] = '\0';
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001295 return buf;
1296}
1297
Wang Long9697a552015-03-11 08:36:54 +00001298static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001299{
1300 const char *path;
1301
Wang Long9697a552015-03-11 08:36:54 +00001302 path = unittest_path(unittest_nr, ovtype);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001303
1304 switch (ovtype) {
1305 case PDEV_OVERLAY:
1306 return of_path_platform_device_exists(path);
1307 case I2C_OVERLAY:
1308 return of_path_i2c_client_exists(path);
1309 }
1310 return 0;
1311}
1312
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001313static const char *overlay_path(int nr)
1314{
1315 static char buf[256];
1316
1317 snprintf(buf, sizeof(buf) - 1,
1318 "/testcase-data/overlay%d", nr);
1319 buf[sizeof(buf) - 1] = '\0';
1320
1321 return buf;
1322}
1323
1324static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1325
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001326/* it is guaranteed that overlay ids are assigned in sequence */
1327#define MAX_UNITTEST_OVERLAYS 256
1328static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1329static int overlay_first_id = -1;
1330
1331static void of_unittest_track_overlay(int id)
1332{
1333 if (overlay_first_id < 0)
1334 overlay_first_id = id;
1335 id -= overlay_first_id;
1336
1337 /* we shouldn't need that many */
1338 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1339 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1340}
1341
1342static void of_unittest_untrack_overlay(int id)
1343{
1344 if (overlay_first_id < 0)
1345 return;
1346 id -= overlay_first_id;
1347 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1348 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1349}
1350
1351static void of_unittest_destroy_tracked_overlays(void)
1352{
Frank Rowand24789c52017-10-17 16:36:26 -07001353 int id, ret, defers, ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001354
1355 if (overlay_first_id < 0)
1356 return;
1357
1358 /* try until no defers */
1359 do {
1360 defers = 0;
1361 /* remove in reverse order */
1362 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1363 if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1364 continue;
1365
Frank Rowand24789c52017-10-17 16:36:26 -07001366 ovcs_id = id + overlay_first_id;
1367 ret = of_overlay_remove(&ovcs_id);
Sergey Senozhatsky815d74b2016-03-02 20:24:49 +09001368 if (ret == -ENODEV) {
1369 pr_warn("%s: no overlay to destroy for #%d\n",
1370 __func__, id + overlay_first_id);
1371 continue;
1372 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001373 if (ret != 0) {
1374 defers++;
1375 pr_warn("%s: overlay destroy failed for #%d\n",
1376 __func__, id + overlay_first_id);
1377 continue;
1378 }
1379
1380 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1381 }
1382 } while (defers > 0);
1383}
1384
Alexander Sverdlinca7b8962017-01-19 11:06:16 +01001385static int of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001386 int *overlay_id)
1387{
1388 struct device_node *np = NULL;
Frank Rowand24789c52017-10-17 16:36:26 -07001389 int ret;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001390
1391 np = of_find_node_by_path(overlay_path(overlay_nr));
1392 if (np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001393 unittest(0, "could not find overlay node @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001394 overlay_path(overlay_nr));
1395 ret = -EINVAL;
1396 goto out;
1397 }
1398
Frank Rowand24789c52017-10-17 16:36:26 -07001399 *overlay_id = 0;
1400 ret = of_overlay_apply(np, overlay_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001401 if (ret < 0) {
Wang Long9697a552015-03-11 08:36:54 +00001402 unittest(0, "could not create overlay from \"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001403 overlay_path(overlay_nr));
1404 goto out;
1405 }
Frank Rowand24789c52017-10-17 16:36:26 -07001406 of_unittest_track_overlay(*overlay_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001407
1408 ret = 0;
1409
1410out:
1411 of_node_put(np);
1412
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001413 return ret;
1414}
1415
1416/* apply an overlay while checking before and after states */
Wang Long9697a552015-03-11 08:36:54 +00001417static int of_unittest_apply_overlay_check(int overlay_nr, int unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001418 int before, int after, enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001419{
Frank Rowand24789c52017-10-17 16:36:26 -07001420 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001421
Wang Long9697a552015-03-11 08:36:54 +00001422 /* unittest device must not be in before state */
1423 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1424 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001425 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001426 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001427 !before ? "enabled" : "disabled");
1428 return -EINVAL;
1429 }
1430
Frank Rowand24789c52017-10-17 16:36:26 -07001431 ovcs_id = 0;
1432 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001433 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001434 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001435 return ret;
1436 }
1437
Wang Long9697a552015-03-11 08:36:54 +00001438 /* unittest device must be to set to after state */
1439 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1440 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001441 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001442 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001443 !after ? "enabled" : "disabled");
1444 return -EINVAL;
1445 }
1446
1447 return 0;
1448}
1449
1450/* apply an overlay and then revert it while checking before, after states */
Wang Long9697a552015-03-11 08:36:54 +00001451static int of_unittest_apply_revert_overlay_check(int overlay_nr,
1452 int unittest_nr, int before, int after,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001453 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001454{
Frank Rowand24789c52017-10-17 16:36:26 -07001455 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001456
Wang Long9697a552015-03-11 08:36:54 +00001457 /* unittest device must be in before state */
1458 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
1459 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001460 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001461 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001462 !before ? "enabled" : "disabled");
1463 return -EINVAL;
1464 }
1465
1466 /* apply the overlay */
Frank Rowand24789c52017-10-17 16:36:26 -07001467 ovcs_id = 0;
1468 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001469 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001470 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001471 return ret;
1472 }
1473
Wang Long9697a552015-03-11 08:36:54 +00001474 /* unittest device must be in after state */
1475 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
1476 unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001477 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001478 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001479 !after ? "enabled" : "disabled");
1480 return -EINVAL;
1481 }
1482
Frank Rowand24789c52017-10-17 16:36:26 -07001483 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001484 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001485 unittest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001486 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001487 unittest_path(unittest_nr, ovtype));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001488 return ret;
1489 }
1490
Wang Long9697a552015-03-11 08:36:54 +00001491 /* unittest device must be again in before state */
1492 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
1493 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001494 overlay_path(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001495 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001496 !before ? "enabled" : "disabled");
1497 return -EINVAL;
1498 }
1499
1500 return 0;
1501}
1502
1503/* test activation of device */
Wang Long9697a552015-03-11 08:36:54 +00001504static void of_unittest_overlay_0(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001505{
1506 int ret;
1507
1508 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001509 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001510 if (ret != 0)
1511 return;
1512
Wang Long9697a552015-03-11 08:36:54 +00001513 unittest(1, "overlay test %d passed\n", 0);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001514}
1515
1516/* test deactivation of device */
Wang Long9697a552015-03-11 08:36:54 +00001517static void of_unittest_overlay_1(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001518{
1519 int ret;
1520
1521 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001522 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001523 if (ret != 0)
1524 return;
1525
Wang Long9697a552015-03-11 08:36:54 +00001526 unittest(1, "overlay test %d passed\n", 1);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001527}
1528
1529/* test activation of device */
Wang Long9697a552015-03-11 08:36:54 +00001530static void of_unittest_overlay_2(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001531{
1532 int ret;
1533
1534 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001535 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001536 if (ret != 0)
1537 return;
1538
Wang Long9697a552015-03-11 08:36:54 +00001539 unittest(1, "overlay test %d passed\n", 2);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001540}
1541
1542/* test deactivation of device */
Wang Long9697a552015-03-11 08:36:54 +00001543static void of_unittest_overlay_3(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001544{
1545 int ret;
1546
1547 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001548 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001549 if (ret != 0)
1550 return;
1551
Wang Long9697a552015-03-11 08:36:54 +00001552 unittest(1, "overlay test %d passed\n", 3);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001553}
1554
1555/* test activation of a full device node */
Wang Long9697a552015-03-11 08:36:54 +00001556static void of_unittest_overlay_4(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001557{
1558 int ret;
1559
1560 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001561 ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001562 if (ret != 0)
1563 return;
1564
Wang Long9697a552015-03-11 08:36:54 +00001565 unittest(1, "overlay test %d passed\n", 4);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001566}
1567
1568/* test overlay apply/revert sequence */
Wang Long9697a552015-03-11 08:36:54 +00001569static void of_unittest_overlay_5(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001570{
1571 int ret;
1572
1573 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001574 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001575 if (ret != 0)
1576 return;
1577
Wang Long9697a552015-03-11 08:36:54 +00001578 unittest(1, "overlay test %d passed\n", 5);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001579}
1580
1581/* test overlay application in sequence */
Wang Long9697a552015-03-11 08:36:54 +00001582static void of_unittest_overlay_6(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001583{
1584 struct device_node *np;
Frank Rowand24789c52017-10-17 16:36:26 -07001585 int ret, i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00001586 int overlay_nr = 6, unittest_nr = 6;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001587 int before = 0, after = 1;
1588
Wang Long9697a552015-03-11 08:36:54 +00001589 /* unittest device must be in before state */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001590 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001591 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001592 != before) {
Wang Long9697a552015-03-11 08:36:54 +00001593 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001594 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001595 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001596 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001597 !before ? "enabled" : "disabled");
1598 return;
1599 }
1600 }
1601
1602 /* apply the overlays */
1603 for (i = 0; i < 2; i++) {
1604
1605 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1606 if (np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001607 unittest(0, "could not find overlay node @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001608 overlay_path(overlay_nr + i));
1609 return;
1610 }
1611
Frank Rowand24789c52017-10-17 16:36:26 -07001612 ovcs_id = 0;
1613 ret = of_overlay_apply(np, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001614 if (ret < 0) {
Wang Long9697a552015-03-11 08:36:54 +00001615 unittest(0, "could not create overlay from \"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001616 overlay_path(overlay_nr + i));
1617 return;
1618 }
Frank Rowand24789c52017-10-17 16:36:26 -07001619 ov_id[i] = ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001620 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001621 }
1622
1623 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001624 /* unittest device must be in after state */
1625 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001626 != after) {
Wang Long9697a552015-03-11 08:36:54 +00001627 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001628 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001629 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001630 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001631 !after ? "enabled" : "disabled");
1632 return;
1633 }
1634 }
1635
1636 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07001637 ovcs_id = ov_id[i];
1638 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001639 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001640 unittest(0, "overlay @\"%s\" failed destroy @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001641 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001642 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001643 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001644 return;
1645 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001646 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001647 }
1648
1649 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001650 /* unittest device must be again in before state */
1651 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001652 != before) {
Wang Long9697a552015-03-11 08:36:54 +00001653 unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001654 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001655 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001656 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001657 !before ? "enabled" : "disabled");
1658 return;
1659 }
1660 }
1661
Wang Long9697a552015-03-11 08:36:54 +00001662 unittest(1, "overlay test %d passed\n", 6);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001663}
1664
1665/* test overlay application in sequence */
Wang Long9697a552015-03-11 08:36:54 +00001666static void of_unittest_overlay_8(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001667{
1668 struct device_node *np;
Frank Rowand24789c52017-10-17 16:36:26 -07001669 int ret, i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00001670 int overlay_nr = 8, unittest_nr = 8;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001671
1672 /* we don't care about device state in this test */
1673
1674 /* apply the overlays */
1675 for (i = 0; i < 2; i++) {
1676
1677 np = of_find_node_by_path(overlay_path(overlay_nr + i));
1678 if (np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001679 unittest(0, "could not find overlay node @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001680 overlay_path(overlay_nr + i));
1681 return;
1682 }
1683
Frank Rowand24789c52017-10-17 16:36:26 -07001684 ovcs_id = 0;
1685 ret = of_overlay_apply(np, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001686 if (ret < 0) {
Wang Long9697a552015-03-11 08:36:54 +00001687 unittest(0, "could not create overlay from \"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001688 overlay_path(overlay_nr + i));
1689 return;
1690 }
Frank Rowand24789c52017-10-17 16:36:26 -07001691 ov_id[i] = ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001692 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001693 }
1694
1695 /* now try to remove first overlay (it should fail) */
Frank Rowand24789c52017-10-17 16:36:26 -07001696 ovcs_id = ov_id[0];
1697 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001698 if (ret == 0) {
Wang Long9697a552015-03-11 08:36:54 +00001699 unittest(0, "overlay @\"%s\" was destroyed @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001700 overlay_path(overlay_nr + 0),
Wang Long9697a552015-03-11 08:36:54 +00001701 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001702 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001703 return;
1704 }
1705
1706 /* removing them in order should work */
1707 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07001708 ovcs_id = ov_id[i];
1709 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001710 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001711 unittest(0, "overlay @\"%s\" not destroyed @\"%s\"\n",
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001712 overlay_path(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001713 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001714 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001715 return;
1716 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001717 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001718 }
1719
Wang Long9697a552015-03-11 08:36:54 +00001720 unittest(1, "overlay test %d passed\n", 8);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001721}
1722
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001723/* test insertion of a bus with parent devices */
Wang Long9697a552015-03-11 08:36:54 +00001724static void of_unittest_overlay_10(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001725{
1726 int ret;
1727 char *child_path;
1728
1729 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001730 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1731 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001732 "overlay test %d failed; overlay application\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001733 return;
1734
Wang Long9697a552015-03-11 08:36:54 +00001735 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1736 unittest_path(10, PDEV_OVERLAY));
1737 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001738 return;
1739
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001740 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001741 kfree(child_path);
Wang Long9697a552015-03-11 08:36:54 +00001742 if (unittest(ret, "overlay test %d failed; no child device\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001743 return;
1744}
1745
1746/* test insertion of a bus with parent devices (and revert) */
Wang Long9697a552015-03-11 08:36:54 +00001747static void of_unittest_overlay_11(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001748{
1749 int ret;
1750
1751 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001752 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001753 PDEV_OVERLAY);
Wang Long9697a552015-03-11 08:36:54 +00001754 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001755 "overlay test %d failed; overlay application\n", 11))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001756 return;
1757}
1758
Arnd Bergmann4252de32015-03-04 20:49:47 +01001759#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001760
Wang Long9697a552015-03-11 08:36:54 +00001761struct unittest_i2c_bus_data {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001762 struct platform_device *pdev;
1763 struct i2c_adapter adap;
1764};
1765
Wang Long9697a552015-03-11 08:36:54 +00001766static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001767 struct i2c_msg *msgs, int num)
1768{
Wang Long9697a552015-03-11 08:36:54 +00001769 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001770
1771 (void)std;
1772
1773 return num;
1774}
1775
Wang Long9697a552015-03-11 08:36:54 +00001776static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001777{
1778 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1779}
1780
Wang Long9697a552015-03-11 08:36:54 +00001781static const struct i2c_algorithm unittest_i2c_algo = {
1782 .master_xfer = unittest_i2c_master_xfer,
1783 .functionality = unittest_i2c_functionality,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001784};
1785
Wang Long9697a552015-03-11 08:36:54 +00001786static int unittest_i2c_bus_probe(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001787{
1788 struct device *dev = &pdev->dev;
1789 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001790 struct unittest_i2c_bus_data *std;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001791 struct i2c_adapter *adap;
1792 int ret;
1793
1794 if (np == NULL) {
1795 dev_err(dev, "No OF data for device\n");
1796 return -EINVAL;
1797
1798 }
1799
Rob Herring0d638a02017-06-01 15:50:55 -05001800 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001801
1802 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1803 if (!std) {
Wang Long9697a552015-03-11 08:36:54 +00001804 dev_err(dev, "Failed to allocate unittest i2c data\n");
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001805 return -ENOMEM;
1806 }
1807
1808 /* link them together */
1809 std->pdev = pdev;
1810 platform_set_drvdata(pdev, std);
1811
1812 adap = &std->adap;
1813 i2c_set_adapdata(adap, std);
1814 adap->nr = -1;
1815 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1816 adap->class = I2C_CLASS_DEPRECATED;
Wang Long9697a552015-03-11 08:36:54 +00001817 adap->algo = &unittest_i2c_algo;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001818 adap->dev.parent = dev;
1819 adap->dev.of_node = dev->of_node;
1820 adap->timeout = 5 * HZ;
1821 adap->retries = 3;
1822
1823 ret = i2c_add_numbered_adapter(adap);
1824 if (ret != 0) {
1825 dev_err(dev, "Failed to add I2C adapter\n");
1826 return ret;
1827 }
1828
1829 return 0;
1830}
1831
Wang Long9697a552015-03-11 08:36:54 +00001832static int unittest_i2c_bus_remove(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001833{
1834 struct device *dev = &pdev->dev;
1835 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001836 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001837
Rob Herring0d638a02017-06-01 15:50:55 -05001838 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001839 i2c_del_adapter(&std->adap);
1840
1841 return 0;
1842}
1843
Grant Likelya2166ca2015-03-29 08:59:58 +01001844static const struct of_device_id unittest_i2c_bus_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001845 { .compatible = "unittest-i2c-bus", },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001846 {},
1847};
1848
Wang Long9697a552015-03-11 08:36:54 +00001849static struct platform_driver unittest_i2c_bus_driver = {
1850 .probe = unittest_i2c_bus_probe,
1851 .remove = unittest_i2c_bus_remove,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001852 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001853 .name = "unittest-i2c-bus",
1854 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001855 },
1856};
1857
Wang Long9697a552015-03-11 08:36:54 +00001858static int unittest_i2c_dev_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001859 const struct i2c_device_id *id)
1860{
1861 struct device *dev = &client->dev;
1862 struct device_node *np = client->dev.of_node;
1863
1864 if (!np) {
1865 dev_err(dev, "No OF node\n");
1866 return -EINVAL;
1867 }
1868
Rob Herring0d638a02017-06-01 15:50:55 -05001869 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001870
1871 return 0;
1872};
1873
Wang Long9697a552015-03-11 08:36:54 +00001874static int unittest_i2c_dev_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001875{
1876 struct device *dev = &client->dev;
1877 struct device_node *np = client->dev.of_node;
1878
Rob Herring0d638a02017-06-01 15:50:55 -05001879 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001880 return 0;
1881}
1882
Wang Long9697a552015-03-11 08:36:54 +00001883static const struct i2c_device_id unittest_i2c_dev_id[] = {
1884 { .name = "unittest-i2c-dev" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001885 { }
1886};
1887
Wang Long9697a552015-03-11 08:36:54 +00001888static struct i2c_driver unittest_i2c_dev_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001889 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001890 .name = "unittest-i2c-dev",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001891 },
Wang Long9697a552015-03-11 08:36:54 +00001892 .probe = unittest_i2c_dev_probe,
1893 .remove = unittest_i2c_dev_remove,
1894 .id_table = unittest_i2c_dev_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001895};
1896
Arnd Bergmann4252de32015-03-04 20:49:47 +01001897#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001898
Peter Rosinb6e3b712016-04-20 08:42:47 +02001899static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001900{
1901 return 0;
1902}
1903
Wang Long9697a552015-03-11 08:36:54 +00001904static int unittest_i2c_mux_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001905 const struct i2c_device_id *id)
1906{
Peter Rosinb6e3b712016-04-20 08:42:47 +02001907 int ret, i, nchans;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001908 struct device *dev = &client->dev;
1909 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1910 struct device_node *np = client->dev.of_node, *child;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001911 struct i2c_mux_core *muxc;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001912 u32 reg, max_reg;
1913
Rob Herring0d638a02017-06-01 15:50:55 -05001914 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001915
1916 if (!np) {
1917 dev_err(dev, "No OF node\n");
1918 return -EINVAL;
1919 }
1920
1921 max_reg = (u32)-1;
1922 for_each_child_of_node(np, child) {
1923 ret = of_property_read_u32(child, "reg", &reg);
1924 if (ret)
1925 continue;
1926 if (max_reg == (u32)-1 || reg > max_reg)
1927 max_reg = reg;
1928 }
1929 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1930 if (nchans == 0) {
1931 dev_err(dev, "No channels\n");
1932 return -EINVAL;
1933 }
1934
Peter Rosinb6e3b712016-04-20 08:42:47 +02001935 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
1936 unittest_i2c_mux_select_chan, NULL);
1937 if (!muxc)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001938 return -ENOMEM;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001939 for (i = 0; i < nchans; i++) {
Peter Rosinb6e3b712016-04-20 08:42:47 +02001940 ret = i2c_mux_add_adapter(muxc, 0, i, 0);
1941 if (ret) {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001942 dev_err(dev, "Failed to register mux #%d\n", i);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001943 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001944 return -ENODEV;
1945 }
1946 }
1947
Peter Rosinb6e3b712016-04-20 08:42:47 +02001948 i2c_set_clientdata(client, muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001949
1950 return 0;
1951};
1952
Wang Long9697a552015-03-11 08:36:54 +00001953static int unittest_i2c_mux_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001954{
1955 struct device *dev = &client->dev;
1956 struct device_node *np = client->dev.of_node;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001957 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001958
Rob Herring0d638a02017-06-01 15:50:55 -05001959 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001960 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001961 return 0;
1962}
1963
Wang Long9697a552015-03-11 08:36:54 +00001964static const struct i2c_device_id unittest_i2c_mux_id[] = {
1965 { .name = "unittest-i2c-mux" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001966 { }
1967};
1968
Wang Long9697a552015-03-11 08:36:54 +00001969static struct i2c_driver unittest_i2c_mux_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001970 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001971 .name = "unittest-i2c-mux",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001972 },
Wang Long9697a552015-03-11 08:36:54 +00001973 .probe = unittest_i2c_mux_probe,
1974 .remove = unittest_i2c_mux_remove,
1975 .id_table = unittest_i2c_mux_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001976};
1977
1978#endif
1979
Wang Long9697a552015-03-11 08:36:54 +00001980static int of_unittest_overlay_i2c_init(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001981{
1982 int ret;
1983
Wang Long9697a552015-03-11 08:36:54 +00001984 ret = i2c_add_driver(&unittest_i2c_dev_driver);
1985 if (unittest(ret == 0,
1986 "could not register unittest i2c device driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001987 return ret;
1988
Wang Long9697a552015-03-11 08:36:54 +00001989 ret = platform_driver_register(&unittest_i2c_bus_driver);
1990 if (unittest(ret == 0,
1991 "could not register unittest i2c bus driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001992 return ret;
1993
Arnd Bergmann4252de32015-03-04 20:49:47 +01001994#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001995 ret = i2c_add_driver(&unittest_i2c_mux_driver);
1996 if (unittest(ret == 0,
1997 "could not register unittest i2c mux driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001998 return ret;
1999#endif
2000
2001 return 0;
2002}
2003
Wang Long9697a552015-03-11 08:36:54 +00002004static void of_unittest_overlay_i2c_cleanup(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002005{
Arnd Bergmann4252de32015-03-04 20:49:47 +01002006#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00002007 i2c_del_driver(&unittest_i2c_mux_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002008#endif
Wang Long9697a552015-03-11 08:36:54 +00002009 platform_driver_unregister(&unittest_i2c_bus_driver);
2010 i2c_del_driver(&unittest_i2c_dev_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002011}
2012
Wang Long9697a552015-03-11 08:36:54 +00002013static void of_unittest_overlay_i2c_12(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002014{
2015 int ret;
2016
2017 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00002018 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002019 if (ret != 0)
2020 return;
2021
Wang Long9697a552015-03-11 08:36:54 +00002022 unittest(1, "overlay test %d passed\n", 12);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002023}
2024
2025/* test deactivation of device */
Wang Long9697a552015-03-11 08:36:54 +00002026static void of_unittest_overlay_i2c_13(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002027{
2028 int ret;
2029
2030 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00002031 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002032 if (ret != 0)
2033 return;
2034
Wang Long9697a552015-03-11 08:36:54 +00002035 unittest(1, "overlay test %d passed\n", 13);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002036}
2037
2038/* just check for i2c mux existence */
Wang Long9697a552015-03-11 08:36:54 +00002039static void of_unittest_overlay_i2c_14(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002040{
2041}
2042
Wang Long9697a552015-03-11 08:36:54 +00002043static void of_unittest_overlay_i2c_15(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002044{
2045 int ret;
2046
2047 /* device should enable */
Alexander Sverdlinca7b8962017-01-19 11:06:16 +01002048 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002049 if (ret != 0)
2050 return;
2051
Wang Long9697a552015-03-11 08:36:54 +00002052 unittest(1, "overlay test %d passed\n", 15);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002053}
2054
2055#else
2056
Wang Long9697a552015-03-11 08:36:54 +00002057static inline void of_unittest_overlay_i2c_14(void) { }
2058static inline void of_unittest_overlay_i2c_15(void) { }
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002059
2060#endif
2061
Wang Long9697a552015-03-11 08:36:54 +00002062static void __init of_unittest_overlay(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002063{
2064 struct device_node *bus_np = NULL;
2065 int ret;
2066
Wang Long9697a552015-03-11 08:36:54 +00002067 ret = platform_driver_register(&unittest_driver);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002068 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00002069 unittest(0, "could not register unittest driver\n");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002070 goto out;
2071 }
2072
2073 bus_np = of_find_node_by_path(bus_path);
2074 if (bus_np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00002075 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002076 goto out;
2077 }
2078
Kefeng Wang146dedb2016-06-01 14:53:10 +08002079 ret = of_platform_default_populate(bus_np, NULL, NULL);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002080 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00002081 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002082 goto out;
2083 }
2084
Wang Long9697a552015-03-11 08:36:54 +00002085 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
2086 unittest(0, "could not find unittest0 @ \"%s\"\n",
2087 unittest_path(100, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002088 goto out;
2089 }
2090
Wang Long9697a552015-03-11 08:36:54 +00002091 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
2092 unittest(0, "unittest1 @ \"%s\" should not exist\n",
2093 unittest_path(101, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002094 goto out;
2095 }
2096
Wang Long9697a552015-03-11 08:36:54 +00002097 unittest(1, "basic infrastructure of overlays passed");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002098
2099 /* tests in sequence */
Wang Long9697a552015-03-11 08:36:54 +00002100 of_unittest_overlay_0();
2101 of_unittest_overlay_1();
2102 of_unittest_overlay_2();
2103 of_unittest_overlay_3();
2104 of_unittest_overlay_4();
2105 of_unittest_overlay_5();
2106 of_unittest_overlay_6();
2107 of_unittest_overlay_8();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002108
Wang Long9697a552015-03-11 08:36:54 +00002109 of_unittest_overlay_10();
2110 of_unittest_overlay_11();
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002111
Arnd Bergmann4252de32015-03-04 20:49:47 +01002112#if IS_BUILTIN(CONFIG_I2C)
Wang Long9697a552015-03-11 08:36:54 +00002113 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002114 goto out;
2115
Wang Long9697a552015-03-11 08:36:54 +00002116 of_unittest_overlay_i2c_12();
2117 of_unittest_overlay_i2c_13();
2118 of_unittest_overlay_i2c_14();
2119 of_unittest_overlay_i2c_15();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002120
Wang Long9697a552015-03-11 08:36:54 +00002121 of_unittest_overlay_i2c_cleanup();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002122#endif
2123
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03002124 of_unittest_destroy_tracked_overlays();
2125
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002126out:
2127 of_node_put(bus_np);
2128}
2129
2130#else
Wang Long9697a552015-03-11 08:36:54 +00002131static inline void __init of_unittest_overlay(void) { }
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002132#endif
2133
Frank Rowand60a00042017-07-19 09:25:20 -07002134#ifdef CONFIG_OF_OVERLAY
2135
Frank Rowand81d0848f2017-04-25 17:09:54 -07002136/*
2137 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2138 * in scripts/Makefile.lib
2139 */
2140
2141#define OVERLAY_INFO_EXTERN(name) \
2142 extern uint8_t __dtb_##name##_begin[]; \
2143 extern uint8_t __dtb_##name##_end[]
2144
2145#define OVERLAY_INFO(name, expected) \
2146{ .dtb_begin = __dtb_##name##_begin, \
2147 .dtb_end = __dtb_##name##_end, \
2148 .expected_result = expected, \
2149}
2150
2151struct overlay_info {
2152 uint8_t *dtb_begin;
2153 uint8_t *dtb_end;
2154 void *data;
2155 struct device_node *np_overlay;
2156 int expected_result;
2157 int overlay_id;
2158};
2159
2160OVERLAY_INFO_EXTERN(overlay_base);
2161OVERLAY_INFO_EXTERN(overlay);
2162OVERLAY_INFO_EXTERN(overlay_bad_phandle);
Frank Rowand60a00042017-07-19 09:25:20 -07002163OVERLAY_INFO_EXTERN(overlay_bad_symbol);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002164
2165/* order of entries is hard-coded into users of overlays[] */
2166static struct overlay_info overlays[] = {
2167 OVERLAY_INFO(overlay_base, -9999),
2168 OVERLAY_INFO(overlay, 0),
2169 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
Frank Rowand60a00042017-07-19 09:25:20 -07002170 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002171 {}
2172};
2173
2174static struct device_node *overlay_base_root;
2175
Rob Herring0fa1c572018-01-05 15:32:33 -06002176static void * __init dt_alloc_memory(u64 size, u64 align)
2177{
2178 return memblock_virt_alloc(size, align);
2179}
2180
Frank Rowand81d0848f2017-04-25 17:09:54 -07002181/*
2182 * Create base device tree for the overlay unittest.
2183 *
2184 * This is called from very early boot code.
2185 *
2186 * Do as much as possible the same way as done in __unflatten_device_tree
2187 * and other early boot steps for the normal FDT so that the overlay base
2188 * unflattened tree will have the same characteristics as the real tree
2189 * (such as having memory allocated by the early allocator). The goal
2190 * is to test "the real thing" as much as possible, and test "test setup
2191 * code" as little as possible.
2192 *
2193 * Have to stop before resolving phandles, because that uses kmalloc.
2194 */
2195void __init unittest_unflatten_overlay_base(void)
2196{
2197 struct overlay_info *info;
2198 u32 data_size;
2199 u32 size;
2200
2201 info = &overlays[0];
2202
2203 if (info->expected_result != -9999) {
2204 pr_err("No dtb 'overlay_base' to attach\n");
2205 return;
2206 }
2207
2208 data_size = info->dtb_end - info->dtb_begin;
2209 if (!data_size) {
2210 pr_err("No dtb 'overlay_base' to attach\n");
2211 return;
2212 }
2213
2214 size = fdt_totalsize(info->dtb_begin);
2215 if (size != data_size) {
2216 pr_err("dtb 'overlay_base' header totalsize != actual size");
2217 return;
2218 }
2219
Rob Herring0fa1c572018-01-05 15:32:33 -06002220 info->data = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
Frank Rowand81d0848f2017-04-25 17:09:54 -07002221 if (!info->data) {
2222 pr_err("alloc for dtb 'overlay_base' failed");
2223 return;
2224 }
2225
2226 memcpy(info->data, info->dtb_begin, size);
2227
2228 __unflatten_device_tree(info->data, NULL, &info->np_overlay,
Rob Herring0fa1c572018-01-05 15:32:33 -06002229 dt_alloc_memory, true);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002230 overlay_base_root = info->np_overlay;
2231}
2232
2233/*
2234 * The purpose of of_unittest_overlay_data_add is to add an
2235 * overlay in the normal fashion. This is a test of the whole
2236 * picture, instead of testing individual elements.
2237 *
2238 * A secondary purpose is to be able to verify that the contents of
2239 * /proc/device-tree/ contains the updated structure and values from
2240 * the overlay. That must be verified separately in user space.
2241 *
2242 * Return 0 on unexpected error.
2243 */
2244static int __init overlay_data_add(int onum)
2245{
2246 struct overlay_info *info;
2247 int k;
2248 int ret;
2249 u32 size;
2250 u32 size_from_header;
2251
2252 for (k = 0, info = overlays; info; info++, k++) {
2253 if (k == onum)
2254 break;
2255 }
2256 if (onum > k)
2257 return 0;
2258
2259 size = info->dtb_end - info->dtb_begin;
2260 if (!size) {
2261 pr_err("no overlay to attach, %d\n", onum);
2262 ret = 0;
2263 }
2264
2265 size_from_header = fdt_totalsize(info->dtb_begin);
2266 if (size_from_header != size) {
2267 pr_err("overlay header totalsize != actual size, %d", onum);
2268 return 0;
2269 }
2270
2271 /*
2272 * Must create permanent copy of FDT because of_fdt_unflatten_tree()
2273 * will create pointers to the passed in FDT in the EDT.
2274 */
2275 info->data = kmemdup(info->dtb_begin, size, GFP_KERNEL);
2276 if (!info->data) {
2277 pr_err("unable to allocate memory for data, %d\n", onum);
2278 return 0;
2279 }
2280
2281 of_fdt_unflatten_tree(info->data, NULL, &info->np_overlay);
2282 if (!info->np_overlay) {
2283 pr_err("unable to unflatten overlay, %d\n", onum);
2284 ret = 0;
2285 goto out_free_data;
2286 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07002287
Frank Rowand24789c52017-10-17 16:36:26 -07002288 info->overlay_id = 0;
2289 ret = of_overlay_apply(info->np_overlay, &info->overlay_id);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002290 if (ret < 0) {
Frank Rowand0290c4c2017-10-17 16:36:23 -07002291 pr_err("of_overlay_apply() (ret=%d), %d\n", ret, onum);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002292 goto out_free_np_overlay;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002293 }
2294
2295 pr_debug("__dtb_overlay_begin applied, overlay id %d\n", ret);
2296
2297 goto out;
2298
2299out_free_np_overlay:
2300 /*
2301 * info->np_overlay is the unflattened device tree
2302 * It has not been spliced into the live tree.
2303 */
2304
2305 /* todo: function to free unflattened device tree */
2306
2307out_free_data:
2308 kfree(info->data);
2309
2310out:
2311 return (ret == info->expected_result);
2312}
2313
2314/*
2315 * The purpose of of_unittest_overlay_high_level is to add an overlay
2316 * in the normal fashion. This is a test of the whole picture,
2317 * instead of individual elements.
2318 *
2319 * The first part of the function is _not_ normal overlay usage; it is
2320 * finishing splicing the base overlay device tree into the live tree.
2321 */
2322static __init void of_unittest_overlay_high_level(void)
2323{
2324 struct device_node *last_sibling;
2325 struct device_node *np;
2326 struct device_node *of_symbols;
2327 struct device_node *overlay_base_symbols;
2328 struct device_node **pprev;
2329 struct property *prop;
2330 int ret;
2331
2332 if (!overlay_base_root) {
2333 unittest(0, "overlay_base_root not initialized\n");
2334 return;
2335 }
2336
2337 /*
2338 * Could not fixup phandles in unittest_unflatten_overlay_base()
2339 * because kmalloc() was not yet available.
2340 */
Frank Rowandf948d6d2017-10-17 16:36:29 -07002341 of_overlay_mutex_lock();
Frank Rowand81d0848f2017-04-25 17:09:54 -07002342 of_resolve_phandles(overlay_base_root);
Frank Rowandf948d6d2017-10-17 16:36:29 -07002343 of_overlay_mutex_unlock();
2344
Frank Rowand81d0848f2017-04-25 17:09:54 -07002345
2346 /*
2347 * do not allow overlay_base to duplicate any node already in
2348 * tree, this greatly simplifies the code
2349 */
2350
2351 /*
2352 * remove overlay_base_root node "__local_fixups", after
2353 * being used by of_resolve_phandles()
2354 */
2355 pprev = &overlay_base_root->child;
2356 for (np = overlay_base_root->child; np; np = np->sibling) {
2357 if (!of_node_cmp(np->name, "__local_fixups__")) {
2358 *pprev = np->sibling;
2359 break;
2360 }
2361 pprev = &np->sibling;
2362 }
2363
2364 /* remove overlay_base_root node "__symbols__" if in live tree */
2365 of_symbols = of_get_child_by_name(of_root, "__symbols__");
2366 if (of_symbols) {
2367 /* will have to graft properties from node into live tree */
2368 pprev = &overlay_base_root->child;
2369 for (np = overlay_base_root->child; np; np = np->sibling) {
2370 if (!of_node_cmp(np->name, "__symbols__")) {
2371 overlay_base_symbols = np;
2372 *pprev = np->sibling;
2373 break;
2374 }
2375 pprev = &np->sibling;
2376 }
2377 }
2378
2379 for (np = overlay_base_root->child; np; np = np->sibling) {
2380 if (of_get_child_by_name(of_root, np->name)) {
2381 unittest(0, "illegal node name in overlay_base %s",
2382 np->name);
2383 return;
2384 }
2385 }
2386
2387 /*
2388 * overlay 'overlay_base' is not allowed to have root
2389 * properties, so only need to splice nodes into main device tree.
2390 *
2391 * root node of *overlay_base_root will not be freed, it is lost
2392 * memory.
2393 */
2394
2395 for (np = overlay_base_root->child; np; np = np->sibling)
2396 np->parent = of_root;
2397
2398 mutex_lock(&of_mutex);
2399
Arnd Bergmannee320b32017-04-28 11:44:11 +02002400 for (last_sibling = np = of_root->child; np; np = np->sibling)
Frank Rowand81d0848f2017-04-25 17:09:54 -07002401 last_sibling = np;
2402
2403 if (last_sibling)
2404 last_sibling->sibling = overlay_base_root->child;
2405 else
2406 of_root->child = overlay_base_root->child;
2407
2408 for_each_of_allnodes_from(overlay_base_root, np)
2409 __of_attach_node_sysfs(np);
2410
2411 if (of_symbols) {
2412 for_each_property_of_node(overlay_base_symbols, prop) {
2413 ret = __of_add_property(of_symbols, prop);
2414 if (ret) {
2415 unittest(0,
2416 "duplicate property '%s' in overlay_base node __symbols__",
2417 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002418 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002419 }
2420 ret = __of_add_property_sysfs(of_symbols, prop);
2421 if (ret) {
2422 unittest(0,
2423 "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
2424 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002425 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002426 }
2427 }
2428 }
2429
2430 mutex_unlock(&of_mutex);
2431
2432
2433 /* now do the normal overlay usage test */
2434
2435 unittest(overlay_data_add(1),
2436 "Adding overlay 'overlay' failed\n");
2437
2438 unittest(overlay_data_add(2),
2439 "Adding overlay 'overlay_bad_phandle' failed\n");
Frank Rowand60a00042017-07-19 09:25:20 -07002440
2441 unittest(overlay_data_add(3),
2442 "Adding overlay 'overlay_bad_symbol' failed\n");
2443
Dan Carpenter8756cd12017-05-03 22:49:50 +03002444 return;
2445
2446err_unlock:
2447 mutex_unlock(&of_mutex);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002448}
2449
2450#else
2451
2452static inline __init void of_unittest_overlay_high_level(void) {}
2453
2454#endif
2455
Wang Long9697a552015-03-11 08:36:54 +00002456static int __init of_unittest(void)
Grant Likely53a42092011-12-12 09:25:57 -07002457{
2458 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002459 int res;
2460
Wang Long9697a552015-03-11 08:36:54 +00002461 /* adding data for unittest */
2462 res = unittest_data_add();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002463 if (res)
2464 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00002465 if (!of_aliases)
2466 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07002467
2468 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2469 if (!np) {
2470 pr_info("No testcase data in device tree; not running tests\n");
2471 return 0;
2472 }
2473 of_node_put(np);
2474
Wang Long9697a552015-03-11 08:36:54 +00002475 pr_info("start of unittest - you will see error messages\n");
2476 of_unittest_check_tree_linkage();
2477 of_unittest_check_phandles();
2478 of_unittest_find_node_by_name();
2479 of_unittest_dynamic();
2480 of_unittest_parse_phandle_with_args();
Stephen Boyd357aa4b2018-01-30 18:36:17 -08002481 of_unittest_parse_phandle_with_args_map();
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002482 of_unittest_printf();
Wang Long9697a552015-03-11 08:36:54 +00002483 of_unittest_property_string();
2484 of_unittest_property_copy();
2485 of_unittest_changeset();
2486 of_unittest_parse_interrupts();
2487 of_unittest_parse_interrupts_extended();
2488 of_unittest_match_node();
2489 of_unittest_platform_populate();
2490 of_unittest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002491
Grant Likelyf2051d62014-10-01 17:40:22 +01002492 /* Double check linkage after removing testcase data */
Wang Long9697a552015-03-11 08:36:54 +00002493 of_unittest_check_tree_linkage();
Grant Likelyf2051d62014-10-01 17:40:22 +01002494
Frank Rowand81d0848f2017-04-25 17:09:54 -07002495 of_unittest_overlay_high_level();
2496
Wang Long9697a552015-03-11 08:36:54 +00002497 pr_info("end of unittest - %i passed, %i failed\n",
2498 unittest_results.passed, unittest_results.failed);
Grant Likelyf2051d62014-10-01 17:40:22 +01002499
Grant Likely53a42092011-12-12 09:25:57 -07002500 return 0;
2501}
Wang Long9697a552015-03-11 08:36:54 +00002502late_initcall(of_unittest);