blob: df93149d6146e5d9c800e253dddfce70fa0bda65 [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
Frank Rowand39a751a2018-02-12 00:19:42 -080048static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
49
Wang Long9697a552015-03-11 08:36:54 +000050static void __init of_unittest_find_node_by_name(void)
Grant Likelyae91ff72014-03-14 13:53:10 +000051{
52 struct device_node *np;
Rob Herring0d638a02017-06-01 15:50:55 -050053 const char *options, *name;
Grant Likelyae91ff72014-03-14 13:53:10 +000054
55 np = of_find_node_by_path("/testcase-data");
Rob Herring0d638a02017-06-01 15:50:55 -050056 name = kasprintf(GFP_KERNEL, "%pOF", np);
57 unittest(np && !strcmp("/testcase-data", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000058 "find /testcase-data failed\n");
59 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050060 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000061
62 /* Test if trailing '/' works */
63 np = of_find_node_by_path("/testcase-data/");
Wang Long9697a552015-03-11 08:36:54 +000064 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
Grant Likelyae91ff72014-03-14 13:53:10 +000065
66 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
Rob Herring0d638a02017-06-01 15:50:55 -050067 name = kasprintf(GFP_KERNEL, "%pOF", np);
68 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000069 "find /testcase-data/phandle-tests/consumer-a failed\n");
70 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050071 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000072
73 np = of_find_node_by_path("testcase-alias");
Rob Herring0d638a02017-06-01 15:50:55 -050074 name = kasprintf(GFP_KERNEL, "%pOF", np);
75 unittest(np && !strcmp("/testcase-data", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000076 "find testcase-alias failed\n");
77 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050078 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000079
80 /* Test if trailing '/' works on aliases */
81 np = of_find_node_by_path("testcase-alias/");
Wang Long9697a552015-03-11 08:36:54 +000082 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
Grant Likelyae91ff72014-03-14 13:53:10 +000083
84 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
Rob Herring0d638a02017-06-01 15:50:55 -050085 name = kasprintf(GFP_KERNEL, "%pOF", np);
86 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000087 "find testcase-alias/phandle-tests/consumer-a failed\n");
88 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050089 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000090
91 np = of_find_node_by_path("/testcase-data/missing-path");
Rob Herring0d638a02017-06-01 15:50:55 -050092 unittest(!np, "non-existent path returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +000093 of_node_put(np);
94
95 np = of_find_node_by_path("missing-alias");
Rob Herring0d638a02017-06-01 15:50:55 -050096 unittest(!np, "non-existent alias returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +000097 of_node_put(np);
98
99 np = of_find_node_by_path("testcase-alias/missing-path");
Rob Herring0d638a02017-06-01 15:50:55 -0500100 unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +0000101 of_node_put(np);
Leif Lindholm75c28c02014-11-28 11:34:28 +0000102
103 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
Wang Long9697a552015-03-11 08:36:54 +0000104 unittest(np && !strcmp("testoption", options),
Leif Lindholm75c28c02014-11-28 11:34:28 +0000105 "option path test failed\n");
106 of_node_put(np);
107
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500108 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
Wang Long9697a552015-03-11 08:36:54 +0000109 unittest(np && !strcmp("test/option", options),
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500110 "option path test, subcase #1 failed\n");
111 of_node_put(np);
112
Brian Norris5ca1b0d2015-03-17 12:30:32 -0700113 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
Wang Long9697a552015-03-11 08:36:54 +0000114 unittest(np && !strcmp("test/option", options),
Brian Norris5ca1b0d2015-03-17 12:30:32 -0700115 "option path test, subcase #2 failed\n");
116 of_node_put(np);
117
Leif Lindholm75c28c02014-11-28 11:34:28 +0000118 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000119 unittest(np, "NULL option path test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000120 of_node_put(np);
121
122 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
123 &options);
Wang Long9697a552015-03-11 08:36:54 +0000124 unittest(np && !strcmp("testaliasoption", options),
Leif Lindholm75c28c02014-11-28 11:34:28 +0000125 "option alias path test failed\n");
126 of_node_put(np);
127
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500128 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
129 &options);
Wang Long9697a552015-03-11 08:36:54 +0000130 unittest(np && !strcmp("test/alias/option", options),
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500131 "option alias path test, subcase #1 failed\n");
132 of_node_put(np);
133
Leif Lindholm75c28c02014-11-28 11:34:28 +0000134 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000135 unittest(np, "NULL option alias path test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000136 of_node_put(np);
137
138 options = "testoption";
139 np = of_find_node_opts_by_path("testcase-alias", &options);
Wang Long9697a552015-03-11 08:36:54 +0000140 unittest(np && !options, "option clearing test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000141 of_node_put(np);
142
143 options = "testoption";
144 np = of_find_node_opts_by_path("/", &options);
Wang Long9697a552015-03-11 08:36:54 +0000145 unittest(np && !options, "option clearing root node test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000146 of_node_put(np);
Grant Likelyae91ff72014-03-14 13:53:10 +0000147}
148
Wang Long9697a552015-03-11 08:36:54 +0000149static void __init of_unittest_dynamic(void)
Grant Likely7e66c5c2013-11-15 17:19:09 +0000150{
151 struct device_node *np;
152 struct property *prop;
153
154 np = of_find_node_by_path("/testcase-data");
155 if (!np) {
156 pr_err("missing testcase data\n");
157 return;
158 }
159
160 /* Array of 4 properties for the purpose of testing */
161 prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL);
162 if (!prop) {
Wang Long9697a552015-03-11 08:36:54 +0000163 unittest(0, "kzalloc() failed\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000164 return;
165 }
166
167 /* Add a new property - should pass*/
168 prop->name = "new-property";
169 prop->value = "new-property-data";
170 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000171 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000172
173 /* Try to add an existing property - should fail */
174 prop++;
175 prop->name = "new-property";
176 prop->value = "new-property-data-should-fail";
177 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000178 unittest(of_add_property(np, prop) != 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000179 "Adding an existing property should have failed\n");
180
181 /* Try to modify an existing property - should pass */
182 prop->value = "modify-property-data-should-pass";
183 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000184 unittest(of_update_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000185 "Updating an existing property should have passed\n");
186
187 /* Try to modify non-existent property - should pass*/
188 prop++;
189 prop->name = "modify-property";
190 prop->value = "modify-missing-property-data-should-pass";
191 prop->length = strlen(prop->value);
Wang Long9697a552015-03-11 08:36:54 +0000192 unittest(of_update_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000193 "Updating a missing property should have passed\n");
194
195 /* Remove property - should pass */
Wang Long9697a552015-03-11 08:36:54 +0000196 unittest(of_remove_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000197 "Removing a property should have passed\n");
198
199 /* Adding very large property - should pass */
200 prop++;
201 prop->name = "large-property-PAGE_SIZEx8";
202 prop->length = PAGE_SIZE * 8;
203 prop->value = kzalloc(prop->length, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000204 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000205 if (prop->value)
Wang Long9697a552015-03-11 08:36:54 +0000206 unittest(of_add_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000207 "Adding a large property should have passed\n");
208}
209
Wang Long9697a552015-03-11 08:36:54 +0000210static int __init of_unittest_check_node_linkage(struct device_node *np)
Grant Likelyf2051d62014-10-01 17:40:22 +0100211{
Grant Likely5063e252014-10-03 16:28:27 +0100212 struct device_node *child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100213 int count = 0, rc;
214
215 for_each_child_of_node(np, child) {
216 if (child->parent != np) {
217 pr_err("Child node %s links to wrong parent %s\n",
218 child->name, np->name);
Julia Lawall855ff282015-10-22 11:02:50 +0200219 rc = -EINVAL;
220 goto put_child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100221 }
222
Wang Long9697a552015-03-11 08:36:54 +0000223 rc = of_unittest_check_node_linkage(child);
Grant Likelyf2051d62014-10-01 17:40:22 +0100224 if (rc < 0)
Julia Lawall855ff282015-10-22 11:02:50 +0200225 goto put_child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100226 count += rc;
227 }
228
229 return count + 1;
Julia Lawall855ff282015-10-22 11:02:50 +0200230put_child:
231 of_node_put(child);
232 return rc;
Grant Likelyf2051d62014-10-01 17:40:22 +0100233}
234
Wang Long9697a552015-03-11 08:36:54 +0000235static void __init of_unittest_check_tree_linkage(void)
Grant Likelyf2051d62014-10-01 17:40:22 +0100236{
237 struct device_node *np;
238 int allnode_count = 0, child_count;
239
Grant Likely5063e252014-10-03 16:28:27 +0100240 if (!of_root)
Grant Likelyf2051d62014-10-01 17:40:22 +0100241 return;
242
243 for_each_of_allnodes(np)
244 allnode_count++;
Wang Long9697a552015-03-11 08:36:54 +0000245 child_count = of_unittest_check_node_linkage(of_root);
Grant Likelyf2051d62014-10-01 17:40:22 +0100246
Wang Long9697a552015-03-11 08:36:54 +0000247 unittest(child_count > 0, "Device node data structure is corrupted\n");
Grant Likelya2166ca2015-03-29 08:59:58 +0100248 unittest(child_count == allnode_count,
Frank Rowanda6bb1212015-03-13 23:59:01 -0700249 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
250 allnode_count, child_count);
Grant Likelyf2051d62014-10-01 17:40:22 +0100251 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
252}
253
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200254static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
255 const char *expected)
256{
Tobin C. Hardingd4b9e422018-03-12 15:27:23 +1100257 unsigned char *buf;
258 int buf_size;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200259 int size, i;
260
Tobin C. Hardingd4b9e422018-03-12 15:27:23 +1100261 buf_size = strlen(expected) + 10;
262 buf = kmalloc(buf_size, GFP_KERNEL);
263 if (!buf)
264 return;
265
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200266 /* Baseline; check conversion with a large size limit */
Tobin C. Hardingd4b9e422018-03-12 15:27:23 +1100267 memset(buf, 0xff, buf_size);
268 size = snprintf(buf, buf_size - 2, fmt, np);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200269
270 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
271 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
272 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
273 fmt, expected, buf);
274
275 /* Make sure length limits work */
276 size++;
277 for (i = 0; i < 2; i++, size--) {
278 /* Clear the buffer, and make sure it works correctly still */
Tobin C. Hardingd4b9e422018-03-12 15:27:23 +1100279 memset(buf, 0xff, buf_size);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200280 snprintf(buf, size+1, fmt, np);
281 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
282 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
283 size, fmt, expected, buf);
284 }
Tobin C. Hardingd4b9e422018-03-12 15:27:23 +1100285 kfree(buf);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200286}
287
288static void __init of_unittest_printf(void)
289{
290 struct device_node *np;
291 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
292 char phandle_str[16] = "";
293
294 np = of_find_node_by_path(full_name);
295 if (!np) {
296 unittest(np, "testcase data missing\n");
297 return;
298 }
299
300 num_to_str(phandle_str, sizeof(phandle_str), np->phandle);
301
302 of_unittest_printf_one(np, "%pOF", full_name);
303 of_unittest_printf_one(np, "%pOFf", full_name);
304 of_unittest_printf_one(np, "%pOFp", phandle_str);
305 of_unittest_printf_one(np, "%pOFP", "dev@100");
306 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
307 of_unittest_printf_one(np, "%10pOFP", " dev@100");
308 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
309 of_unittest_printf_one(of_root, "%pOFP", "/");
310 of_unittest_printf_one(np, "%pOFF", "----");
311 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
312 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
313 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
314 of_unittest_printf_one(np, "%pOFC",
315 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
316}
317
Grant Likely841ec212014-10-02 13:09:15 +0100318struct node_hash {
319 struct hlist_node node;
320 struct device_node *np;
321};
322
Grant Likely2118f4b2014-10-07 11:30:31 +0100323static DEFINE_HASHTABLE(phandle_ht, 8);
Wang Long9697a552015-03-11 08:36:54 +0000324static void __init of_unittest_check_phandles(void)
Grant Likely841ec212014-10-02 13:09:15 +0100325{
326 struct device_node *np;
327 struct node_hash *nh;
328 struct hlist_node *tmp;
329 int i, dup_count = 0, phandle_count = 0;
Grant Likely841ec212014-10-02 13:09:15 +0100330
Grant Likely841ec212014-10-02 13:09:15 +0100331 for_each_of_allnodes(np) {
332 if (!np->phandle)
333 continue;
334
Grant Likely2118f4b2014-10-07 11:30:31 +0100335 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100336 if (nh->np->phandle == np->phandle) {
Rob Herring0d638a02017-06-01 15:50:55 -0500337 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
338 np->phandle, nh->np, np);
Grant Likely841ec212014-10-02 13:09:15 +0100339 dup_count++;
340 break;
341 }
342 }
343
344 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
345 if (WARN_ON(!nh))
346 return;
347
348 nh->np = np;
Grant Likely2118f4b2014-10-07 11:30:31 +0100349 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100350 phandle_count++;
351 }
Wang Long9697a552015-03-11 08:36:54 +0000352 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
Grant Likely841ec212014-10-02 13:09:15 +0100353 dup_count, phandle_count);
354
355 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100356 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100357 hash_del(&nh->node);
358 kfree(nh);
359 }
360}
361
Wang Long9697a552015-03-11 08:36:54 +0000362static void __init of_unittest_parse_phandle_with_args(void)
Grant Likely53a42092011-12-12 09:25:57 -0700363{
364 struct device_node *np;
365 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000366 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700367
Grant Likely53a42092011-12-12 09:25:57 -0700368 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
369 if (!np) {
370 pr_err("missing testcase data\n");
371 return;
372 }
373
Grant Likelybd69f732013-02-10 22:57:21 +0000374 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000375 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000376
Grant Likelyf7f951c2013-02-12 17:41:22 +0000377 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700378 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700379
Grant Likely53a42092011-12-12 09:25:57 -0700380 rc = of_parse_phandle_with_args(np, "phandle-list",
381 "#phandle-cells", i, &args);
382
383 /* Test the values from tests-phandle.dtsi */
384 switch (i) {
385 case 0:
386 passed &= !rc;
387 passed &= (args.args_count == 1);
388 passed &= (args.args[0] == (i + 1));
389 break;
390 case 1:
391 passed &= !rc;
392 passed &= (args.args_count == 2);
393 passed &= (args.args[0] == (i + 1));
394 passed &= (args.args[1] == 0);
395 break;
396 case 2:
397 passed &= (rc == -ENOENT);
398 break;
399 case 3:
400 passed &= !rc;
401 passed &= (args.args_count == 3);
402 passed &= (args.args[0] == (i + 1));
403 passed &= (args.args[1] == 4);
404 passed &= (args.args[2] == 3);
405 break;
406 case 4:
407 passed &= !rc;
408 passed &= (args.args_count == 2);
409 passed &= (args.args[0] == (i + 1));
410 passed &= (args.args[1] == 100);
411 break;
412 case 5:
413 passed &= !rc;
414 passed &= (args.args_count == 0);
415 break;
416 case 6:
417 passed &= !rc;
418 passed &= (args.args_count == 1);
419 passed &= (args.args[0] == (i + 1));
420 break;
421 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000422 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700423 break;
424 default:
425 passed = false;
426 }
427
Rob Herring0d638a02017-06-01 15:50:55 -0500428 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
429 i, args.np, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700430 }
431
432 /* Check for missing list property */
433 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
434 "#phandle-cells", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000435 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000436 rc = of_count_phandle_with_args(np, "phandle-list-missing",
437 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000438 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700439
440 /* Check for missing cells property */
441 rc = of_parse_phandle_with_args(np, "phandle-list",
442 "#phandle-cells-missing", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000443 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000444 rc = of_count_phandle_with_args(np, "phandle-list",
445 "#phandle-cells-missing");
Wang Long9697a552015-03-11 08:36:54 +0000446 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700447
448 /* Check for bad phandle in list */
449 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
450 "#phandle-cells", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000451 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000452 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
453 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000454 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700455
456 /* Check for incorrectly formed argument list */
457 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
458 "#phandle-cells", 1, &args);
Wang Long9697a552015-03-11 08:36:54 +0000459 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000460 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
461 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000462 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700463}
464
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800465static void __init of_unittest_parse_phandle_with_args_map(void)
466{
467 struct device_node *np, *p0, *p1, *p2, *p3;
468 struct of_phandle_args args;
469 int i, rc;
470
471 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
472 if (!np) {
473 pr_err("missing testcase data\n");
474 return;
475 }
476
477 p0 = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
478 if (!p0) {
479 pr_err("missing testcase data\n");
480 return;
481 }
482
483 p1 = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
484 if (!p1) {
485 pr_err("missing testcase data\n");
486 return;
487 }
488
489 p2 = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
490 if (!p2) {
491 pr_err("missing testcase data\n");
492 return;
493 }
494
495 p3 = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
496 if (!p3) {
497 pr_err("missing testcase data\n");
498 return;
499 }
500
501 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
502 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
503
504 for (i = 0; i < 8; i++) {
505 bool passed = true;
506
507 rc = of_parse_phandle_with_args_map(np, "phandle-list",
508 "phandle", i, &args);
509
510 /* Test the values from tests-phandle.dtsi */
511 switch (i) {
512 case 0:
513 passed &= !rc;
514 passed &= (args.np == p1);
515 passed &= (args.args_count == 1);
516 passed &= (args.args[0] == 1);
517 break;
518 case 1:
519 passed &= !rc;
520 passed &= (args.np == p3);
521 passed &= (args.args_count == 3);
522 passed &= (args.args[0] == 2);
523 passed &= (args.args[1] == 5);
524 passed &= (args.args[2] == 3);
525 break;
526 case 2:
527 passed &= (rc == -ENOENT);
528 break;
529 case 3:
530 passed &= !rc;
531 passed &= (args.np == p0);
532 passed &= (args.args_count == 0);
533 break;
534 case 4:
535 passed &= !rc;
536 passed &= (args.np == p1);
537 passed &= (args.args_count == 1);
538 passed &= (args.args[0] == 3);
539 break;
540 case 5:
541 passed &= !rc;
542 passed &= (args.np == p0);
543 passed &= (args.args_count == 0);
544 break;
545 case 6:
546 passed &= !rc;
547 passed &= (args.np == p2);
548 passed &= (args.args_count == 2);
549 passed &= (args.args[0] == 15);
550 passed &= (args.args[1] == 0x20);
551 break;
552 case 7:
553 passed &= (rc == -ENOENT);
554 break;
555 default:
556 passed = false;
557 }
558
559 unittest(passed, "index %i - data error on node %s rc=%i\n",
560 i, args.np->full_name, rc);
561 }
562
563 /* Check for missing list property */
564 rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
565 "phandle", 0, &args);
566 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
567
568 /* Check for missing cells,map,mask property */
569 rc = of_parse_phandle_with_args_map(np, "phandle-list",
570 "phandle-missing", 0, &args);
571 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
572
573 /* Check for bad phandle in list */
574 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
575 "phandle", 0, &args);
576 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
577
578 /* Check for incorrectly formed argument list */
579 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
580 "phandle", 1, &args);
581 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
582}
583
Wang Long9697a552015-03-11 08:36:54 +0000584static void __init of_unittest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700585{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000586 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700587 struct device_node *np;
588 int rc;
589
Grant Likely7aff0fe2011-12-12 09:25:58 -0700590 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
591 if (!np) {
592 pr_err("No testcase data in device tree\n");
593 return;
594 }
595
596 rc = of_property_match_string(np, "phandle-list-names", "first");
Wang Long9697a552015-03-11 08:36:54 +0000597 unittest(rc == 0, "first expected:0 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700598 rc = of_property_match_string(np, "phandle-list-names", "second");
Wang Long9697a552015-03-11 08:36:54 +0000599 unittest(rc == 1, "second expected:1 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700600 rc = of_property_match_string(np, "phandle-list-names", "third");
Wang Long9697a552015-03-11 08:36:54 +0000601 unittest(rc == 2, "third expected:2 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700602 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Wang Long9697a552015-03-11 08:36:54 +0000603 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700604 rc = of_property_match_string(np, "missing-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000605 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700606 rc = of_property_match_string(np, "empty-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000607 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700608 rc = of_property_match_string(np, "unterminated-string", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000609 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000610
611 /* of_property_count_strings() tests */
612 rc = of_property_count_strings(np, "string-property");
Wang Long9697a552015-03-11 08:36:54 +0000613 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000614 rc = of_property_count_strings(np, "phandle-list-names");
Wang Long9697a552015-03-11 08:36:54 +0000615 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000616 rc = of_property_count_strings(np, "unterminated-string");
Wang Long9697a552015-03-11 08:36:54 +0000617 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000618 rc = of_property_count_strings(np, "unterminated-string-list");
Wang Long9697a552015-03-11 08:36:54 +0000619 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000620
621 /* of_property_read_string_index() tests */
622 rc = of_property_read_string_index(np, "string-property", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000623 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "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, "string-property", 1, 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 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000628 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000629 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000630 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000631 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
Wang Long9697a552015-03-11 08:36:54 +0000632 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000633 strings[0] = NULL;
634 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
Wang Long9697a552015-03-11 08:36:54 +0000635 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000636 strings[0] = NULL;
637 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000638 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000639 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000640 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000641 strings[0] = NULL;
642 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
Wang Long9697a552015-03-11 08:36:54 +0000643 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000644 strings[1] = NULL;
645
646 /* of_property_read_string_array() tests */
647 rc = of_property_read_string_array(np, "string-property", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000648 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000649 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000650 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000651 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000652 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000653 /* -- An incorrectly formed string should cause a failure */
654 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000655 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000656 /* -- parsing the correctly formed strings should still work: */
657 strings[2] = NULL;
658 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
Wang Long9697a552015-03-11 08:36:54 +0000659 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000660 strings[1] = NULL;
661 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
Wang Long9697a552015-03-11 08:36:54 +0000662 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 -0700663}
664
Pantelis Antoniou69843392014-07-04 19:58:47 +0300665#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
666 (p1)->value && (p2)->value && \
667 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
668 !strcmp((p1)->name, (p2)->name))
Wang Long9697a552015-03-11 08:36:54 +0000669static void __init of_unittest_property_copy(void)
Pantelis Antoniou69843392014-07-04 19:58:47 +0300670{
671#ifdef CONFIG_OF_DYNAMIC
672 struct property p1 = { .name = "p1", .length = 0, .value = "" };
673 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
674 struct property *new;
675
676 new = __of_prop_dup(&p1, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000677 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300678 kfree(new->value);
679 kfree(new->name);
680 kfree(new);
681
682 new = __of_prop_dup(&p2, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000683 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300684 kfree(new->value);
685 kfree(new->name);
686 kfree(new);
687#endif
688}
689
Wang Long9697a552015-03-11 08:36:54 +0000690static void __init of_unittest_changeset(void)
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300691{
692#ifdef CONFIG_OF_DYNAMIC
Frank Rowanda4f91f02018-02-26 14:01:22 -0800693 struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
694 struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
695 struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
696 struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300697 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
698 struct property *ppremove;
Frank Rowanda4f91f02018-02-26 14:01:22 -0800699 struct device_node *n1, *n2, *n21, *nchangeset, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300700 struct of_changeset chgset;
701
Frank Rowandb89dae12018-02-26 14:01:23 -0800702 n1 = __of_node_dup(NULL, "n1");
Wang Long9697a552015-03-11 08:36:54 +0000703 unittest(n1, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800704
Frank Rowandb89dae12018-02-26 14:01:23 -0800705 n2 = __of_node_dup(NULL, "n2");
Wang Long9697a552015-03-11 08:36:54 +0000706 unittest(n2, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800707
Frank Rowandb89dae12018-02-26 14:01:23 -0800708 n21 = __of_node_dup(NULL, "n21");
Wang Long9697a552015-03-11 08:36:54 +0000709 unittest(n21, "testcase setup failure %p\n", n21);
Frank Rowanda4f91f02018-02-26 14:01:22 -0800710
711 nchangeset = of_find_node_by_path("/testcase-data/changeset");
712 nremove = of_get_child_by_name(nchangeset, "node-remove");
Wang Long9697a552015-03-11 08:36:54 +0000713 unittest(nremove, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800714
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300715 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000716 unittest(ppadd, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800717
718 ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
719 unittest(ppname_n1, "testcase setup failure\n");
720
721 ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
722 unittest(ppname_n2, "testcase setup failure\n");
723
724 ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
725 unittest(ppname_n21, "testcase setup failure\n");
726
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300727 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000728 unittest(ppupdate, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800729
730 parent = nchangeset;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300731 n1->parent = parent;
732 n2->parent = parent;
733 n21->parent = n2;
Frank Rowanda4f91f02018-02-26 14:01:22 -0800734
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300735 ppremove = of_find_property(parent, "prop-remove", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000736 unittest(ppremove, "failed to find removal prop");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300737
738 of_changeset_init(&chgset);
Frank Rowanda4f91f02018-02-26 14:01:22 -0800739
Wang Long9697a552015-03-11 08:36:54 +0000740 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800741 unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
742
Wang Long9697a552015-03-11 08:36:54 +0000743 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800744 unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
745
Wang Long9697a552015-03-11 08:36:54 +0000746 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800747 unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
748
Wang Long9697a552015-03-11 08:36:54 +0000749 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800750
751 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
Wang Long9697a552015-03-11 08:36:54 +0000752 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
753 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800754
Wang Long9697a552015-03-11 08:36:54 +0000755 unittest(!of_changeset_apply(&chgset), "apply failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300756
Frank Rowanda4f91f02018-02-26 14:01:22 -0800757 of_node_put(nchangeset);
758
Grant Likelye5179582014-11-17 22:31:32 +0000759 /* Make sure node names are constructed correctly */
Wang Long9697a552015-03-11 08:36:54 +0000760 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
Rob Herring0d638a02017-06-01 15:50:55 -0500761 "'%pOF' not added\n", n21);
Markus Elfringc46ca3c2014-12-02 13:54:00 +0100762 of_node_put(np);
Grant Likelye5179582014-11-17 22:31:32 +0000763
Wang Long9697a552015-03-11 08:36:54 +0000764 unittest(!of_changeset_revert(&chgset), "revert failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300765
766 of_changeset_destroy(&chgset);
767#endif
768}
769
Wang Long9697a552015-03-11 08:36:54 +0000770static void __init of_unittest_parse_interrupts(void)
Grant Likelya9f10ca2013-10-11 22:04:23 +0100771{
772 struct device_node *np;
773 struct of_phandle_args args;
774 int i, rc;
775
776 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
777 if (!np) {
778 pr_err("missing testcase data\n");
779 return;
780 }
781
782 for (i = 0; i < 4; i++) {
783 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700784
Grant Likelya9f10ca2013-10-11 22:04:23 +0100785 args.args_count = 0;
786 rc = of_irq_parse_one(np, i, &args);
787
788 passed &= !rc;
789 passed &= (args.args_count == 1);
790 passed &= (args.args[0] == (i + 1));
791
Rob Herring0d638a02017-06-01 15:50:55 -0500792 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
793 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100794 }
795 of_node_put(np);
796
797 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
798 if (!np) {
799 pr_err("missing testcase data\n");
800 return;
801 }
802
803 for (i = 0; i < 4; i++) {
804 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700805
Grant Likelya9f10ca2013-10-11 22:04:23 +0100806 args.args_count = 0;
807 rc = of_irq_parse_one(np, i, &args);
808
809 /* Test the values from tests-phandle.dtsi */
810 switch (i) {
811 case 0:
812 passed &= !rc;
813 passed &= (args.args_count == 1);
814 passed &= (args.args[0] == 9);
815 break;
816 case 1:
817 passed &= !rc;
818 passed &= (args.args_count == 3);
819 passed &= (args.args[0] == 10);
820 passed &= (args.args[1] == 11);
821 passed &= (args.args[2] == 12);
822 break;
823 case 2:
824 passed &= !rc;
825 passed &= (args.args_count == 2);
826 passed &= (args.args[0] == 13);
827 passed &= (args.args[1] == 14);
828 break;
829 case 3:
830 passed &= !rc;
831 passed &= (args.args_count == 2);
832 passed &= (args.args[0] == 15);
833 passed &= (args.args[1] == 16);
834 break;
835 default:
836 passed = false;
837 }
Rob Herring0d638a02017-06-01 15:50:55 -0500838 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
839 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100840 }
841 of_node_put(np);
842}
843
Wang Long9697a552015-03-11 08:36:54 +0000844static void __init of_unittest_parse_interrupts_extended(void)
Grant Likely79d97012013-09-19 16:47:37 -0500845{
846 struct device_node *np;
847 struct of_phandle_args args;
848 int i, rc;
849
850 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
851 if (!np) {
852 pr_err("missing testcase data\n");
853 return;
854 }
855
856 for (i = 0; i < 7; i++) {
857 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700858
Grant Likely79d97012013-09-19 16:47:37 -0500859 rc = of_irq_parse_one(np, i, &args);
860
861 /* Test the values from tests-phandle.dtsi */
862 switch (i) {
863 case 0:
864 passed &= !rc;
865 passed &= (args.args_count == 1);
866 passed &= (args.args[0] == 1);
867 break;
868 case 1:
869 passed &= !rc;
870 passed &= (args.args_count == 3);
871 passed &= (args.args[0] == 2);
872 passed &= (args.args[1] == 3);
873 passed &= (args.args[2] == 4);
874 break;
875 case 2:
876 passed &= !rc;
877 passed &= (args.args_count == 2);
878 passed &= (args.args[0] == 5);
879 passed &= (args.args[1] == 6);
880 break;
881 case 3:
882 passed &= !rc;
883 passed &= (args.args_count == 1);
884 passed &= (args.args[0] == 9);
885 break;
886 case 4:
887 passed &= !rc;
888 passed &= (args.args_count == 3);
889 passed &= (args.args[0] == 10);
890 passed &= (args.args[1] == 11);
891 passed &= (args.args[2] == 12);
892 break;
893 case 5:
894 passed &= !rc;
895 passed &= (args.args_count == 2);
896 passed &= (args.args[0] == 13);
897 passed &= (args.args[1] == 14);
898 break;
899 case 6:
900 passed &= !rc;
901 passed &= (args.args_count == 1);
902 passed &= (args.args[0] == 15);
903 break;
904 default:
905 passed = false;
906 }
907
Rob Herring0d638a02017-06-01 15:50:55 -0500908 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
909 i, args.np, rc);
Grant Likely79d97012013-09-19 16:47:37 -0500910 }
911 of_node_put(np);
912}
913
Frank Rowandafaed7a2015-03-14 00:00:36 -0700914static const struct of_device_id match_node_table[] = {
Grant Likely1f42e5d2014-02-18 21:38:55 +0000915 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
916 { .data = "B", .type = "type1", }, /* followed by type alone */
917
918 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
919 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
920 { .data = "Cc", .name = "name2", .type = "type2", },
921
922 { .data = "E", .compatible = "compat3" },
923 { .data = "G", .compatible = "compat2", },
924 { .data = "H", .compatible = "compat2", .name = "name5", },
925 { .data = "I", .compatible = "compat2", .type = "type1", },
926 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
927 { .data = "K", .compatible = "compat2", .name = "name9", },
928 {}
929};
930
931static struct {
932 const char *path;
933 const char *data;
934} match_node_tests[] = {
935 { .path = "/testcase-data/match-node/name0", .data = "A", },
936 { .path = "/testcase-data/match-node/name1", .data = "B", },
937 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
938 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
939 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
940 { .path = "/testcase-data/match-node/name3", .data = "E", },
941 { .path = "/testcase-data/match-node/name4", .data = "G", },
942 { .path = "/testcase-data/match-node/name5", .data = "H", },
943 { .path = "/testcase-data/match-node/name6", .data = "G", },
944 { .path = "/testcase-data/match-node/name7", .data = "I", },
945 { .path = "/testcase-data/match-node/name8", .data = "J", },
946 { .path = "/testcase-data/match-node/name9", .data = "K", },
947};
948
Wang Long9697a552015-03-11 08:36:54 +0000949static void __init of_unittest_match_node(void)
Grant Likely1f42e5d2014-02-18 21:38:55 +0000950{
951 struct device_node *np;
952 const struct of_device_id *match;
953 int i;
954
955 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
956 np = of_find_node_by_path(match_node_tests[i].path);
957 if (!np) {
Wang Long9697a552015-03-11 08:36:54 +0000958 unittest(0, "missing testcase node %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000959 match_node_tests[i].path);
960 continue;
961 }
962
963 match = of_match_node(match_node_table, np);
964 if (!match) {
Wang Long9697a552015-03-11 08:36:54 +0000965 unittest(0, "%s didn't match anything\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000966 match_node_tests[i].path);
967 continue;
968 }
969
970 if (strcmp(match->data, match_node_tests[i].data) != 0) {
Wang Long9697a552015-03-11 08:36:54 +0000971 unittest(0, "%s got wrong match. expected %s, got %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000972 match_node_tests[i].path, match_node_tests[i].data,
973 (const char *)match->data);
974 continue;
975 }
Wang Long9697a552015-03-11 08:36:54 +0000976 unittest(1, "passed");
Grant Likely1f42e5d2014-02-18 21:38:55 +0000977 }
978}
979
Grant Likelyd2329fb2016-01-04 13:13:21 +0100980static struct resource test_bus_res = {
981 .start = 0xfffffff8,
982 .end = 0xfffffff9,
983 .flags = IORESOURCE_MEM,
984};
Grant Likely37791b62015-03-27 20:30:04 -0700985static const struct platform_device_info test_bus_info = {
986 .name = "unittest-bus",
Grant Likely851da972014-11-04 13:14:13 +0000987};
Wang Long9697a552015-03-11 08:36:54 +0000988static void __init of_unittest_platform_populate(void)
Rob Herring82c0f582014-04-23 17:57:40 -0500989{
Grant Likely851da972014-11-04 13:14:13 +0000990 int irq, rc;
991 struct device_node *np, *child, *grandchild;
Grant Likely37791b62015-03-27 20:30:04 -0700992 struct platform_device *pdev, *test_bus;
Frank Rowandafaed7a2015-03-14 00:00:36 -0700993 const struct of_device_id match[] = {
Rob Herringfb2caa52014-05-13 10:07:54 -0500994 { .compatible = "test-device", },
995 {}
996 };
Rob Herring82c0f582014-04-23 17:57:40 -0500997
998 np = of_find_node_by_path("/testcase-data");
Kefeng Wang146dedb2016-06-01 14:53:10 +0800999 of_platform_default_populate(np, NULL, NULL);
Rob Herring82c0f582014-04-23 17:57:40 -05001000
1001 /* Test that a missing irq domain returns -EPROBE_DEFER */
1002 np = of_find_node_by_path("/testcase-data/testcase-device1");
1003 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +00001004 unittest(pdev, "device 1 creation failed\n");
Rob Herring7d1cdc82014-05-13 10:07:29 -05001005
Rob Herring82c0f582014-04-23 17:57:40 -05001006 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +00001007 unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -05001008
1009 /* Test that a parsing failure does not return -EPROBE_DEFER */
1010 np = of_find_node_by_path("/testcase-data/testcase-device2");
1011 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +00001012 unittest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -05001013 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +00001014 unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -05001015
Frank Rowand716e1d42015-03-13 23:57:40 -07001016 np = of_find_node_by_path("/testcase-data/platform-tests");
Grant Likelya2166ca2015-03-29 08:59:58 +01001017 unittest(np, "No testcase data in device tree\n");
Frank Rowand716e1d42015-03-13 23:57:40 -07001018 if (!np)
Rob Herringfb2caa52014-05-13 10:07:54 -05001019 return;
Grant Likely851da972014-11-04 13:14:13 +00001020
Grant Likely37791b62015-03-27 20:30:04 -07001021 test_bus = platform_device_register_full(&test_bus_info);
1022 rc = PTR_ERR_OR_ZERO(test_bus);
Grant Likelya2166ca2015-03-29 08:59:58 +01001023 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
Frank Rowand716e1d42015-03-13 23:57:40 -07001024 if (rc)
Grant Likely851da972014-11-04 13:14:13 +00001025 return;
Grant Likely37791b62015-03-27 20:30:04 -07001026 test_bus->dev.of_node = np;
Rob Herringfb2caa52014-05-13 10:07:54 -05001027
Grant Likelyd2329fb2016-01-04 13:13:21 +01001028 /*
1029 * Add a dummy resource to the test bus node after it is
1030 * registered to catch problems with un-inserted resources. The
1031 * DT code doesn't insert the resources, and it has caused the
1032 * kernel to oops in the past. This makes sure the same bug
1033 * doesn't crop up again.
1034 */
1035 platform_device_add_resources(test_bus, &test_bus_res, 1);
1036
Grant Likely37791b62015-03-27 20:30:04 -07001037 of_platform_populate(np, match, NULL, &test_bus->dev);
Rob Herringfb2caa52014-05-13 10:07:54 -05001038 for_each_child_of_node(np, child) {
Rob Herringfb2caa52014-05-13 10:07:54 -05001039 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +00001040 unittest(of_find_device_by_node(grandchild),
Rob Herringfb2caa52014-05-13 10:07:54 -05001041 "Could not create device for node '%s'\n",
1042 grandchild->name);
1043 }
Grant Likely851da972014-11-04 13:14:13 +00001044
Grant Likely37791b62015-03-27 20:30:04 -07001045 of_platform_depopulate(&test_bus->dev);
Grant Likely851da972014-11-04 13:14:13 +00001046 for_each_child_of_node(np, child) {
1047 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +00001048 unittest(!of_find_device_by_node(grandchild),
Grant Likely851da972014-11-04 13:14:13 +00001049 "device didn't get destroyed '%s'\n",
1050 grandchild->name);
1051 }
1052
Grant Likely37791b62015-03-27 20:30:04 -07001053 platform_device_unregister(test_bus);
Grant Likely851da972014-11-04 13:14:13 +00001054 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -05001055}
1056
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001057/**
1058 * update_node_properties - adds the properties
1059 * of np into dup node (present in live tree) and
1060 * updates parent of children of np to dup.
1061 *
1062 * @np: node already present in live tree
1063 * @dup: node present in live tree to be updated
1064 */
1065static void update_node_properties(struct device_node *np,
1066 struct device_node *dup)
1067{
1068 struct property *prop;
1069 struct device_node *child;
1070
1071 for_each_property_of_node(np, prop)
1072 of_add_property(dup, prop);
1073
1074 for_each_child_of_node(np, child)
1075 child->parent = dup;
1076}
1077
1078/**
1079 * attach_node_and_children - attaches nodes
1080 * and its children to live tree
1081 *
1082 * @np: Node to attach to live tree
1083 */
1084static int attach_node_and_children(struct device_node *np)
1085{
Grant Likely5063e252014-10-03 16:28:27 +01001086 struct device_node *next, *dup, *child;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -08001087 unsigned long flags;
Rob Herring0d638a02017-06-01 15:50:55 -05001088 const char *full_name;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001089
Rob Herring0d638a02017-06-01 15:50:55 -05001090 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1091 dup = of_find_node_by_path(full_name);
1092 kfree(full_name);
Grant Likely5063e252014-10-03 16:28:27 +01001093 if (dup) {
1094 update_node_properties(np, dup);
1095 return 0;
1096 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001097
Grant Likely5063e252014-10-03 16:28:27 +01001098 child = np->child;
1099 np->child = NULL;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -08001100
1101 mutex_lock(&of_mutex);
1102 raw_spin_lock_irqsave(&devtree_lock, flags);
1103 np->sibling = np->parent->child;
1104 np->parent->child = np;
1105 of_node_clear_flag(np, OF_DETACHED);
1106 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1107
1108 __of_attach_node_sysfs(np);
1109 mutex_unlock(&of_mutex);
1110
Grant Likely5063e252014-10-03 16:28:27 +01001111 while (child) {
1112 next = child->sibling;
1113 attach_node_and_children(child);
1114 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001115 }
1116
1117 return 0;
1118}
1119
1120/**
Wang Long9697a552015-03-11 08:36:54 +00001121 * unittest_data_add - Reads, copies data from
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001122 * linked tree and attaches it to the live tree
1123 */
Wang Long9697a552015-03-11 08:36:54 +00001124static int __init unittest_data_add(void)
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001125{
Wang Long9697a552015-03-11 08:36:54 +00001126 void *unittest_data;
1127 struct device_node *unittest_data_node, *np;
Frank Rowandc8547112015-03-14 00:04:24 -07001128 /*
1129 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
1130 * created by cmd_dt_S_dtb in scripts/Makefile.lib
1131 */
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001132 extern uint8_t __dtb_testcases_begin[];
1133 extern uint8_t __dtb_testcases_end[];
1134 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +01001135 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001136
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001137 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001138 pr_warn("%s: No testcase data to attach; not running tests\n",
1139 __func__);
1140 return -ENODATA;
1141 }
1142
1143 /* creating copy */
Wang Long9697a552015-03-11 08:36:54 +00001144 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001145
Wang Long9697a552015-03-11 08:36:54 +00001146 if (!unittest_data) {
1147 pr_warn("%s: Failed to allocate memory for unittest_data; "
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001148 "not running tests\n", __func__);
1149 return -ENOMEM;
1150 }
Gavin Shanc4263232016-05-03 23:22:50 +10001151 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
Wang Long9697a552015-03-11 08:36:54 +00001152 if (!unittest_data_node) {
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001153 pr_warn("%s: No tree to attach; not running tests\n", __func__);
1154 return -ENODATA;
1155 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001156
1157 /*
Frank Rowand39a751a2018-02-12 00:19:42 -08001158 * This lock normally encloses of_resolve_phandles()
Frank Rowandf948d6d2017-10-17 16:36:29 -07001159 */
1160 of_overlay_mutex_lock();
1161
Wang Long9697a552015-03-11 08:36:54 +00001162 rc = of_resolve_phandles(unittest_data_node);
Grant Likely2eb46da2014-10-02 14:36:46 +01001163 if (rc) {
1164 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
Frank Rowandf948d6d2017-10-17 16:36:29 -07001165 of_overlay_mutex_unlock();
Grant Likely2eb46da2014-10-02 14:36:46 +01001166 return -EINVAL;
1167 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001168
Grant Likely5063e252014-10-03 16:28:27 +01001169 if (!of_root) {
Wang Long9697a552015-03-11 08:36:54 +00001170 of_root = unittest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001171 for_each_of_allnodes(np)
1172 __of_attach_node_sysfs(np);
1173 of_aliases = of_find_node_by_path("/aliases");
1174 of_chosen = of_find_node_by_path("/chosen");
Frank Rowandf948d6d2017-10-17 16:36:29 -07001175 of_overlay_mutex_unlock();
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001176 return 0;
1177 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001178
1179 /* attach the sub-tree to live tree */
Wang Long9697a552015-03-11 08:36:54 +00001180 np = unittest_data_node->child;
Grant Likely5063e252014-10-03 16:28:27 +01001181 while (np) {
1182 struct device_node *next = np->sibling;
Frank Rowand3db316d2015-03-14 00:02:31 -07001183
Grant Likely5063e252014-10-03 16:28:27 +01001184 np->parent = of_root;
1185 attach_node_and_children(np);
1186 np = next;
1187 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001188
1189 of_overlay_mutex_unlock();
1190
Grant Likely5063e252014-10-03 16:28:27 +01001191 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001192}
1193
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001194#ifdef CONFIG_OF_OVERLAY
1195
Wang Long9697a552015-03-11 08:36:54 +00001196static int unittest_probe(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001197{
1198 struct device *dev = &pdev->dev;
1199 struct device_node *np = dev->of_node;
1200
1201 if (np == NULL) {
1202 dev_err(dev, "No OF data for device\n");
1203 return -EINVAL;
1204
1205 }
1206
Rob Herring0d638a02017-06-01 15:50:55 -05001207 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001208
1209 of_platform_populate(np, NULL, NULL, &pdev->dev);
1210
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001211 return 0;
1212}
1213
Wang Long9697a552015-03-11 08:36:54 +00001214static int unittest_remove(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001215{
1216 struct device *dev = &pdev->dev;
1217 struct device_node *np = dev->of_node;
1218
Rob Herring0d638a02017-06-01 15:50:55 -05001219 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001220 return 0;
1221}
1222
Grant Likelya2166ca2015-03-29 08:59:58 +01001223static const struct of_device_id unittest_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001224 { .compatible = "unittest", },
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001225 {},
1226};
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001227
Wang Long9697a552015-03-11 08:36:54 +00001228static struct platform_driver unittest_driver = {
1229 .probe = unittest_probe,
1230 .remove = unittest_remove,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001231 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001232 .name = "unittest",
Wang Long9697a552015-03-11 08:36:54 +00001233 .of_match_table = of_match_ptr(unittest_match),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001234 },
1235};
1236
1237/* get the platform device instantiated at the path */
1238static struct platform_device *of_path_to_platform_device(const char *path)
1239{
1240 struct device_node *np;
1241 struct platform_device *pdev;
1242
1243 np = of_find_node_by_path(path);
1244 if (np == NULL)
1245 return NULL;
1246
1247 pdev = of_find_device_by_node(np);
1248 of_node_put(np);
1249
1250 return pdev;
1251}
1252
1253/* find out if a platform device exists at that path */
1254static int of_path_platform_device_exists(const char *path)
1255{
1256 struct platform_device *pdev;
1257
1258 pdev = of_path_to_platform_device(path);
1259 platform_device_put(pdev);
1260 return pdev != NULL;
1261}
1262
Arnd Bergmann4252de32015-03-04 20:49:47 +01001263#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001264
1265/* get the i2c client device instantiated at the path */
1266static struct i2c_client *of_path_to_i2c_client(const char *path)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001267{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001268 struct device_node *np;
1269 struct i2c_client *client;
1270
1271 np = of_find_node_by_path(path);
1272 if (np == NULL)
1273 return NULL;
1274
1275 client = of_find_i2c_device_by_node(np);
1276 of_node_put(np);
1277
1278 return client;
1279}
1280
1281/* find out if a i2c client device exists at that path */
1282static int of_path_i2c_client_exists(const char *path)
1283{
1284 struct i2c_client *client;
1285
1286 client = of_path_to_i2c_client(path);
1287 if (client)
1288 put_device(&client->dev);
1289 return client != NULL;
1290}
1291#else
1292static int of_path_i2c_client_exists(const char *path)
1293{
1294 return 0;
1295}
1296#endif
1297
1298enum overlay_type {
1299 PDEV_OVERLAY,
1300 I2C_OVERLAY
1301};
1302
1303static int of_path_device_type_exists(const char *path,
1304 enum overlay_type ovtype)
1305{
1306 switch (ovtype) {
1307 case PDEV_OVERLAY:
1308 return of_path_platform_device_exists(path);
1309 case I2C_OVERLAY:
1310 return of_path_i2c_client_exists(path);
1311 }
1312 return 0;
1313}
1314
Wang Long9697a552015-03-11 08:36:54 +00001315static const char *unittest_path(int nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001316{
1317 const char *base;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001318 static char buf[256];
1319
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001320 switch (ovtype) {
1321 case PDEV_OVERLAY:
1322 base = "/testcase-data/overlay-node/test-bus";
1323 break;
1324 case I2C_OVERLAY:
1325 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1326 break;
1327 default:
1328 buf[0] = '\0';
1329 return buf;
1330 }
Wang Long9697a552015-03-11 08:36:54 +00001331 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001332 buf[sizeof(buf) - 1] = '\0';
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001333 return buf;
1334}
1335
Wang Long9697a552015-03-11 08:36:54 +00001336static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001337{
1338 const char *path;
1339
Wang Long9697a552015-03-11 08:36:54 +00001340 path = unittest_path(unittest_nr, ovtype);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001341
1342 switch (ovtype) {
1343 case PDEV_OVERLAY:
1344 return of_path_platform_device_exists(path);
1345 case I2C_OVERLAY:
1346 return of_path_i2c_client_exists(path);
1347 }
1348 return 0;
1349}
1350
Frank Rowand39a751a2018-02-12 00:19:42 -08001351static const char *overlay_name_from_nr(int nr)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001352{
1353 static char buf[256];
1354
1355 snprintf(buf, sizeof(buf) - 1,
Frank Rowand39a751a2018-02-12 00:19:42 -08001356 "overlay_%d", nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001357 buf[sizeof(buf) - 1] = '\0';
1358
1359 return buf;
1360}
1361
1362static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1363
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001364/* it is guaranteed that overlay ids are assigned in sequence */
1365#define MAX_UNITTEST_OVERLAYS 256
1366static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1367static int overlay_first_id = -1;
1368
1369static void of_unittest_track_overlay(int id)
1370{
1371 if (overlay_first_id < 0)
1372 overlay_first_id = id;
1373 id -= overlay_first_id;
1374
1375 /* we shouldn't need that many */
1376 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1377 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1378}
1379
1380static void of_unittest_untrack_overlay(int id)
1381{
1382 if (overlay_first_id < 0)
1383 return;
1384 id -= overlay_first_id;
1385 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1386 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1387}
1388
1389static void of_unittest_destroy_tracked_overlays(void)
1390{
Frank Rowand24789c52017-10-17 16:36:26 -07001391 int id, ret, defers, ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001392
1393 if (overlay_first_id < 0)
1394 return;
1395
1396 /* try until no defers */
1397 do {
1398 defers = 0;
1399 /* remove in reverse order */
1400 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1401 if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1402 continue;
1403
Frank Rowand24789c52017-10-17 16:36:26 -07001404 ovcs_id = id + overlay_first_id;
1405 ret = of_overlay_remove(&ovcs_id);
Sergey Senozhatsky815d74b2016-03-02 20:24:49 +09001406 if (ret == -ENODEV) {
1407 pr_warn("%s: no overlay to destroy for #%d\n",
1408 __func__, id + overlay_first_id);
1409 continue;
1410 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001411 if (ret != 0) {
1412 defers++;
1413 pr_warn("%s: overlay destroy failed for #%d\n",
1414 __func__, id + overlay_first_id);
1415 continue;
1416 }
1417
1418 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1419 }
1420 } while (defers > 0);
1421}
1422
Frank Rowand39a751a2018-02-12 00:19:42 -08001423static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001424 int *overlay_id)
1425{
Frank Rowand39a751a2018-02-12 00:19:42 -08001426 const char *overlay_name;
Frank Rowand24789c52017-10-17 16:36:26 -07001427 int ret;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001428
Frank Rowand39a751a2018-02-12 00:19:42 -08001429 overlay_name = overlay_name_from_nr(overlay_nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001430
Frank Rowand39a751a2018-02-12 00:19:42 -08001431 ret = overlay_data_apply(overlay_name, overlay_id);
1432 if (!ret) {
1433 unittest(0, "could not apply overlay \"%s\"\n",
1434 overlay_name);
Frank Rowand54587be2018-03-08 14:39:05 -08001435 return ret;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001436 }
Frank Rowand24789c52017-10-17 16:36:26 -07001437 of_unittest_track_overlay(*overlay_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001438
Frank Rowand54587be2018-03-08 14:39:05 -08001439 return 0;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001440}
1441
1442/* apply an overlay while checking before and after states */
Frank Rowand39a751a2018-02-12 00:19:42 -08001443static int __init of_unittest_apply_overlay_check(int overlay_nr,
1444 int unittest_nr, int before, int after,
1445 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001446{
Frank Rowand24789c52017-10-17 16:36:26 -07001447 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001448
Wang Long9697a552015-03-11 08:36:54 +00001449 /* unittest device must not be in before state */
1450 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001451 unittest(0, "%s with device @\"%s\" %s\n",
1452 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001453 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001454 !before ? "enabled" : "disabled");
1455 return -EINVAL;
1456 }
1457
Frank Rowand24789c52017-10-17 16:36:26 -07001458 ovcs_id = 0;
1459 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001460 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001461 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001462 return ret;
1463 }
1464
Wang Long9697a552015-03-11 08:36:54 +00001465 /* unittest device must be to set to after state */
1466 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001467 unittest(0, "%s failed to create @\"%s\" %s\n",
1468 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001469 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001470 !after ? "enabled" : "disabled");
1471 return -EINVAL;
1472 }
1473
1474 return 0;
1475}
1476
1477/* apply an overlay and then revert it while checking before, after states */
Frank Rowand39a751a2018-02-12 00:19:42 -08001478static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
Wang Long9697a552015-03-11 08:36:54 +00001479 int unittest_nr, int before, int after,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001480 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001481{
Frank Rowand24789c52017-10-17 16:36:26 -07001482 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001483
Wang Long9697a552015-03-11 08:36:54 +00001484 /* unittest device must be in before state */
1485 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001486 unittest(0, "%s with device @\"%s\" %s\n",
1487 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001488 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001489 !before ? "enabled" : "disabled");
1490 return -EINVAL;
1491 }
1492
1493 /* apply the overlay */
Frank Rowand24789c52017-10-17 16:36:26 -07001494 ovcs_id = 0;
1495 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001496 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001497 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001498 return ret;
1499 }
1500
Wang Long9697a552015-03-11 08:36:54 +00001501 /* unittest device must be in after state */
1502 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001503 unittest(0, "%s failed to create @\"%s\" %s\n",
1504 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001505 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001506 !after ? "enabled" : "disabled");
1507 return -EINVAL;
1508 }
1509
Frank Rowand24789c52017-10-17 16:36:26 -07001510 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001511 if (ret != 0) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001512 unittest(0, "%s failed to be destroyed @\"%s\"\n",
1513 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001514 unittest_path(unittest_nr, ovtype));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001515 return ret;
1516 }
1517
Wang Long9697a552015-03-11 08:36:54 +00001518 /* unittest device must be again in before state */
1519 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001520 unittest(0, "%s with device @\"%s\" %s\n",
1521 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001522 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001523 !before ? "enabled" : "disabled");
1524 return -EINVAL;
1525 }
1526
1527 return 0;
1528}
1529
1530/* test activation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001531static void __init of_unittest_overlay_0(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001532{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001533 /* device should enable */
Frank Rowand06c46972018-03-08 14:39:04 -08001534 if (of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001535 return;
1536
Wang Long9697a552015-03-11 08:36:54 +00001537 unittest(1, "overlay test %d passed\n", 0);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001538}
1539
1540/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001541static void __init of_unittest_overlay_1(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001542{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001543 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08001544 if (of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001545 return;
1546
Wang Long9697a552015-03-11 08:36:54 +00001547 unittest(1, "overlay test %d passed\n", 1);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001548}
1549
1550/* test activation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001551static void __init of_unittest_overlay_2(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001552{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001553 /* device should enable */
Frank Rowand06c46972018-03-08 14:39:04 -08001554 if (of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001555 return;
1556
Wang Long9697a552015-03-11 08:36:54 +00001557 unittest(1, "overlay test %d passed\n", 2);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001558}
1559
1560/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001561static void __init of_unittest_overlay_3(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001562{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001563 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08001564 if (of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001565 return;
1566
Wang Long9697a552015-03-11 08:36:54 +00001567 unittest(1, "overlay test %d passed\n", 3);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001568}
1569
1570/* test activation of a full device node */
Frank Rowand39a751a2018-02-12 00:19:42 -08001571static void __init of_unittest_overlay_4(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001572{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001573 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08001574 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001575 return;
1576
Wang Long9697a552015-03-11 08:36:54 +00001577 unittest(1, "overlay test %d passed\n", 4);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001578}
1579
1580/* test overlay apply/revert sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08001581static void __init of_unittest_overlay_5(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001582{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001583 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08001584 if (of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001585 return;
1586
Wang Long9697a552015-03-11 08:36:54 +00001587 unittest(1, "overlay test %d passed\n", 5);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001588}
1589
1590/* test overlay application in sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08001591static void __init of_unittest_overlay_6(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001592{
Frank Rowand06c46972018-03-08 14:39:04 -08001593 int i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00001594 int overlay_nr = 6, unittest_nr = 6;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001595 int before = 0, after = 1;
Frank Rowand39a751a2018-02-12 00:19:42 -08001596 const char *overlay_name;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001597
Wang Long9697a552015-03-11 08:36:54 +00001598 /* unittest device must be in before state */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001599 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001600 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001601 != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001602 unittest(0, "%s with device @\"%s\" %s\n",
1603 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001604 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001605 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001606 !before ? "enabled" : "disabled");
1607 return;
1608 }
1609 }
1610
1611 /* apply the overlays */
1612 for (i = 0; i < 2; i++) {
1613
Frank Rowand39a751a2018-02-12 00:19:42 -08001614 overlay_name = overlay_name_from_nr(overlay_nr + i);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001615
Frank Rowand06c46972018-03-08 14:39:04 -08001616 if (!overlay_data_apply(overlay_name, &ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001617 unittest(0, "could not apply overlay \"%s\"\n",
1618 overlay_name);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001619 return;
1620 }
Frank Rowand24789c52017-10-17 16:36:26 -07001621 ov_id[i] = ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001622 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001623 }
1624
1625 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001626 /* unittest device must be in after state */
1627 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001628 != after) {
Wang Long9697a552015-03-11 08:36:54 +00001629 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
Frank Rowand39a751a2018-02-12 00:19:42 -08001630 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001631 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001632 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001633 !after ? "enabled" : "disabled");
1634 return;
1635 }
1636 }
1637
1638 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07001639 ovcs_id = ov_id[i];
Frank Rowand06c46972018-03-08 14:39:04 -08001640 if (of_overlay_remove(&ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001641 unittest(0, "%s failed destroy @\"%s\"\n",
1642 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001643 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001644 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001645 return;
1646 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001647 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001648 }
1649
1650 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001651 /* unittest device must be again in before state */
1652 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001653 != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001654 unittest(0, "%s with device @\"%s\" %s\n",
1655 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001656 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001657 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001658 !before ? "enabled" : "disabled");
1659 return;
1660 }
1661 }
1662
Wang Long9697a552015-03-11 08:36:54 +00001663 unittest(1, "overlay test %d passed\n", 6);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001664}
1665
1666/* test overlay application in sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08001667static void __init of_unittest_overlay_8(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001668{
Frank Rowand06c46972018-03-08 14:39:04 -08001669 int i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00001670 int overlay_nr = 8, unittest_nr = 8;
Frank Rowand39a751a2018-02-12 00:19:42 -08001671 const char *overlay_name;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001672
1673 /* we don't care about device state in this test */
1674
1675 /* apply the overlays */
1676 for (i = 0; i < 2; i++) {
1677
Frank Rowand39a751a2018-02-12 00:19:42 -08001678 overlay_name = overlay_name_from_nr(overlay_nr + i);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001679
Dan Carpenterbdb70132018-03-07 09:18:08 +03001680 if (!overlay_data_apply(overlay_name, &ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001681 unittest(0, "could not apply overlay \"%s\"\n",
1682 overlay_name);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001683 return;
1684 }
Frank Rowand24789c52017-10-17 16:36:26 -07001685 ov_id[i] = ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001686 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001687 }
1688
1689 /* now try to remove first overlay (it should fail) */
Frank Rowand24789c52017-10-17 16:36:26 -07001690 ovcs_id = ov_id[0];
Frank Rowand06c46972018-03-08 14:39:04 -08001691 if (!of_overlay_remove(&ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001692 unittest(0, "%s was destroyed @\"%s\"\n",
1693 overlay_name_from_nr(overlay_nr + 0),
Wang Long9697a552015-03-11 08:36:54 +00001694 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001695 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001696 return;
1697 }
1698
1699 /* removing them in order should work */
1700 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07001701 ovcs_id = ov_id[i];
Frank Rowand06c46972018-03-08 14:39:04 -08001702 if (of_overlay_remove(&ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001703 unittest(0, "%s not destroyed @\"%s\"\n",
1704 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001705 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001706 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001707 return;
1708 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001709 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001710 }
1711
Wang Long9697a552015-03-11 08:36:54 +00001712 unittest(1, "overlay test %d passed\n", 8);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001713}
1714
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001715/* test insertion of a bus with parent devices */
Frank Rowand39a751a2018-02-12 00:19:42 -08001716static void __init of_unittest_overlay_10(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001717{
1718 int ret;
1719 char *child_path;
1720
1721 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001722 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1723 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001724 "overlay test %d failed; overlay application\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001725 return;
1726
Wang Long9697a552015-03-11 08:36:54 +00001727 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1728 unittest_path(10, PDEV_OVERLAY));
1729 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001730 return;
1731
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001732 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001733 kfree(child_path);
Frank Rowand54587be2018-03-08 14:39:05 -08001734
1735 unittest(ret, "overlay test %d failed; no child device\n", 10);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001736}
1737
1738/* test insertion of a bus with parent devices (and revert) */
Frank Rowand39a751a2018-02-12 00:19:42 -08001739static void __init of_unittest_overlay_11(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001740{
1741 int ret;
1742
1743 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001744 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001745 PDEV_OVERLAY);
Frank Rowand54587be2018-03-08 14:39:05 -08001746 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001747}
1748
Arnd Bergmann4252de32015-03-04 20:49:47 +01001749#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001750
Wang Long9697a552015-03-11 08:36:54 +00001751struct unittest_i2c_bus_data {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001752 struct platform_device *pdev;
1753 struct i2c_adapter adap;
1754};
1755
Wang Long9697a552015-03-11 08:36:54 +00001756static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001757 struct i2c_msg *msgs, int num)
1758{
Wang Long9697a552015-03-11 08:36:54 +00001759 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001760
1761 (void)std;
1762
1763 return num;
1764}
1765
Wang Long9697a552015-03-11 08:36:54 +00001766static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001767{
1768 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1769}
1770
Wang Long9697a552015-03-11 08:36:54 +00001771static const struct i2c_algorithm unittest_i2c_algo = {
1772 .master_xfer = unittest_i2c_master_xfer,
1773 .functionality = unittest_i2c_functionality,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001774};
1775
Wang Long9697a552015-03-11 08:36:54 +00001776static int unittest_i2c_bus_probe(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001777{
1778 struct device *dev = &pdev->dev;
1779 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001780 struct unittest_i2c_bus_data *std;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001781 struct i2c_adapter *adap;
1782 int ret;
1783
1784 if (np == NULL) {
1785 dev_err(dev, "No OF data for device\n");
1786 return -EINVAL;
1787
1788 }
1789
Rob Herring0d638a02017-06-01 15:50:55 -05001790 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001791
1792 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1793 if (!std) {
Wang Long9697a552015-03-11 08:36:54 +00001794 dev_err(dev, "Failed to allocate unittest i2c data\n");
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001795 return -ENOMEM;
1796 }
1797
1798 /* link them together */
1799 std->pdev = pdev;
1800 platform_set_drvdata(pdev, std);
1801
1802 adap = &std->adap;
1803 i2c_set_adapdata(adap, std);
1804 adap->nr = -1;
1805 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1806 adap->class = I2C_CLASS_DEPRECATED;
Wang Long9697a552015-03-11 08:36:54 +00001807 adap->algo = &unittest_i2c_algo;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001808 adap->dev.parent = dev;
1809 adap->dev.of_node = dev->of_node;
1810 adap->timeout = 5 * HZ;
1811 adap->retries = 3;
1812
1813 ret = i2c_add_numbered_adapter(adap);
1814 if (ret != 0) {
1815 dev_err(dev, "Failed to add I2C adapter\n");
1816 return ret;
1817 }
1818
1819 return 0;
1820}
1821
Wang Long9697a552015-03-11 08:36:54 +00001822static int unittest_i2c_bus_remove(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001823{
1824 struct device *dev = &pdev->dev;
1825 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001826 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001827
Rob Herring0d638a02017-06-01 15:50:55 -05001828 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001829 i2c_del_adapter(&std->adap);
1830
1831 return 0;
1832}
1833
Grant Likelya2166ca2015-03-29 08:59:58 +01001834static const struct of_device_id unittest_i2c_bus_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001835 { .compatible = "unittest-i2c-bus", },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001836 {},
1837};
1838
Wang Long9697a552015-03-11 08:36:54 +00001839static struct platform_driver unittest_i2c_bus_driver = {
1840 .probe = unittest_i2c_bus_probe,
1841 .remove = unittest_i2c_bus_remove,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001842 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001843 .name = "unittest-i2c-bus",
1844 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001845 },
1846};
1847
Wang Long9697a552015-03-11 08:36:54 +00001848static int unittest_i2c_dev_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001849 const struct i2c_device_id *id)
1850{
1851 struct device *dev = &client->dev;
1852 struct device_node *np = client->dev.of_node;
1853
1854 if (!np) {
1855 dev_err(dev, "No OF node\n");
1856 return -EINVAL;
1857 }
1858
Rob Herring0d638a02017-06-01 15:50:55 -05001859 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001860
1861 return 0;
1862};
1863
Wang Long9697a552015-03-11 08:36:54 +00001864static int unittest_i2c_dev_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001865{
1866 struct device *dev = &client->dev;
1867 struct device_node *np = client->dev.of_node;
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 return 0;
1871}
1872
Wang Long9697a552015-03-11 08:36:54 +00001873static const struct i2c_device_id unittest_i2c_dev_id[] = {
1874 { .name = "unittest-i2c-dev" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001875 { }
1876};
1877
Wang Long9697a552015-03-11 08:36:54 +00001878static struct i2c_driver unittest_i2c_dev_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001879 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001880 .name = "unittest-i2c-dev",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001881 },
Wang Long9697a552015-03-11 08:36:54 +00001882 .probe = unittest_i2c_dev_probe,
1883 .remove = unittest_i2c_dev_remove,
1884 .id_table = unittest_i2c_dev_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001885};
1886
Arnd Bergmann4252de32015-03-04 20:49:47 +01001887#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001888
Peter Rosinb6e3b712016-04-20 08:42:47 +02001889static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001890{
1891 return 0;
1892}
1893
Wang Long9697a552015-03-11 08:36:54 +00001894static int unittest_i2c_mux_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001895 const struct i2c_device_id *id)
1896{
Frank Rowand06c46972018-03-08 14:39:04 -08001897 int i, nchans;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001898 struct device *dev = &client->dev;
1899 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1900 struct device_node *np = client->dev.of_node, *child;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001901 struct i2c_mux_core *muxc;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001902 u32 reg, max_reg;
1903
Rob Herring0d638a02017-06-01 15:50:55 -05001904 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001905
1906 if (!np) {
1907 dev_err(dev, "No OF node\n");
1908 return -EINVAL;
1909 }
1910
1911 max_reg = (u32)-1;
1912 for_each_child_of_node(np, child) {
Frank Rowand06c46972018-03-08 14:39:04 -08001913 if (of_property_read_u32(child, "reg", &reg))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001914 continue;
1915 if (max_reg == (u32)-1 || reg > max_reg)
1916 max_reg = reg;
1917 }
1918 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1919 if (nchans == 0) {
1920 dev_err(dev, "No channels\n");
1921 return -EINVAL;
1922 }
1923
Peter Rosinb6e3b712016-04-20 08:42:47 +02001924 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
1925 unittest_i2c_mux_select_chan, NULL);
1926 if (!muxc)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001927 return -ENOMEM;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001928 for (i = 0; i < nchans; i++) {
Frank Rowand06c46972018-03-08 14:39:04 -08001929 if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001930 dev_err(dev, "Failed to register mux #%d\n", i);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001931 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001932 return -ENODEV;
1933 }
1934 }
1935
Peter Rosinb6e3b712016-04-20 08:42:47 +02001936 i2c_set_clientdata(client, muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001937
1938 return 0;
1939};
1940
Wang Long9697a552015-03-11 08:36:54 +00001941static int unittest_i2c_mux_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001942{
1943 struct device *dev = &client->dev;
1944 struct device_node *np = client->dev.of_node;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001945 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001946
Rob Herring0d638a02017-06-01 15:50:55 -05001947 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001948 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001949 return 0;
1950}
1951
Wang Long9697a552015-03-11 08:36:54 +00001952static const struct i2c_device_id unittest_i2c_mux_id[] = {
1953 { .name = "unittest-i2c-mux" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001954 { }
1955};
1956
Wang Long9697a552015-03-11 08:36:54 +00001957static struct i2c_driver unittest_i2c_mux_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001958 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001959 .name = "unittest-i2c-mux",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001960 },
Wang Long9697a552015-03-11 08:36:54 +00001961 .probe = unittest_i2c_mux_probe,
1962 .remove = unittest_i2c_mux_remove,
1963 .id_table = unittest_i2c_mux_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001964};
1965
1966#endif
1967
Wang Long9697a552015-03-11 08:36:54 +00001968static int of_unittest_overlay_i2c_init(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001969{
1970 int ret;
1971
Wang Long9697a552015-03-11 08:36:54 +00001972 ret = i2c_add_driver(&unittest_i2c_dev_driver);
1973 if (unittest(ret == 0,
1974 "could not register unittest i2c device driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001975 return ret;
1976
Wang Long9697a552015-03-11 08:36:54 +00001977 ret = platform_driver_register(&unittest_i2c_bus_driver);
1978 if (unittest(ret == 0,
1979 "could not register unittest i2c bus driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001980 return ret;
1981
Arnd Bergmann4252de32015-03-04 20:49:47 +01001982#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001983 ret = i2c_add_driver(&unittest_i2c_mux_driver);
1984 if (unittest(ret == 0,
1985 "could not register unittest i2c mux driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001986 return ret;
1987#endif
1988
1989 return 0;
1990}
1991
Wang Long9697a552015-03-11 08:36:54 +00001992static void of_unittest_overlay_i2c_cleanup(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001993{
Arnd Bergmann4252de32015-03-04 20:49:47 +01001994#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001995 i2c_del_driver(&unittest_i2c_mux_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001996#endif
Wang Long9697a552015-03-11 08:36:54 +00001997 platform_driver_unregister(&unittest_i2c_bus_driver);
1998 i2c_del_driver(&unittest_i2c_dev_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001999}
2000
Frank Rowand39a751a2018-02-12 00:19:42 -08002001static void __init of_unittest_overlay_i2c_12(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002002{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002003 /* device should enable */
Frank Rowand06c46972018-03-08 14:39:04 -08002004 if (of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002005 return;
2006
Wang Long9697a552015-03-11 08:36:54 +00002007 unittest(1, "overlay test %d passed\n", 12);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002008}
2009
2010/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08002011static void __init of_unittest_overlay_i2c_13(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002012{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002013 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08002014 if (of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002015 return;
2016
Wang Long9697a552015-03-11 08:36:54 +00002017 unittest(1, "overlay test %d passed\n", 13);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002018}
2019
2020/* just check for i2c mux existence */
Wang Long9697a552015-03-11 08:36:54 +00002021static void of_unittest_overlay_i2c_14(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002022{
2023}
2024
Frank Rowand39a751a2018-02-12 00:19:42 -08002025static void __init of_unittest_overlay_i2c_15(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002026{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002027 /* device should enable */
Frank Rowand06c46972018-03-08 14:39:04 -08002028 if (of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002029 return;
2030
Wang Long9697a552015-03-11 08:36:54 +00002031 unittest(1, "overlay test %d passed\n", 15);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002032}
2033
2034#else
2035
Wang Long9697a552015-03-11 08:36:54 +00002036static inline void of_unittest_overlay_i2c_14(void) { }
2037static inline void of_unittest_overlay_i2c_15(void) { }
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002038
2039#endif
2040
Wang Long9697a552015-03-11 08:36:54 +00002041static void __init of_unittest_overlay(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002042{
2043 struct device_node *bus_np = NULL;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002044
Frank Rowand06c46972018-03-08 14:39:04 -08002045 if (platform_driver_register(&unittest_driver)) {
Wang Long9697a552015-03-11 08:36:54 +00002046 unittest(0, "could not register unittest driver\n");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002047 goto out;
2048 }
2049
2050 bus_np = of_find_node_by_path(bus_path);
2051 if (bus_np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00002052 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002053 goto out;
2054 }
2055
Frank Rowand06c46972018-03-08 14:39:04 -08002056 if (of_platform_default_populate(bus_np, NULL, NULL)) {
Wang Long9697a552015-03-11 08:36:54 +00002057 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002058 goto out;
2059 }
2060
Wang Long9697a552015-03-11 08:36:54 +00002061 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
2062 unittest(0, "could not find unittest0 @ \"%s\"\n",
2063 unittest_path(100, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002064 goto out;
2065 }
2066
Wang Long9697a552015-03-11 08:36:54 +00002067 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
2068 unittest(0, "unittest1 @ \"%s\" should not exist\n",
2069 unittest_path(101, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002070 goto out;
2071 }
2072
Wang Long9697a552015-03-11 08:36:54 +00002073 unittest(1, "basic infrastructure of overlays passed");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002074
2075 /* tests in sequence */
Wang Long9697a552015-03-11 08:36:54 +00002076 of_unittest_overlay_0();
2077 of_unittest_overlay_1();
2078 of_unittest_overlay_2();
2079 of_unittest_overlay_3();
2080 of_unittest_overlay_4();
2081 of_unittest_overlay_5();
2082 of_unittest_overlay_6();
2083 of_unittest_overlay_8();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002084
Wang Long9697a552015-03-11 08:36:54 +00002085 of_unittest_overlay_10();
2086 of_unittest_overlay_11();
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002087
Arnd Bergmann4252de32015-03-04 20:49:47 +01002088#if IS_BUILTIN(CONFIG_I2C)
Wang Long9697a552015-03-11 08:36:54 +00002089 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002090 goto out;
2091
Wang Long9697a552015-03-11 08:36:54 +00002092 of_unittest_overlay_i2c_12();
2093 of_unittest_overlay_i2c_13();
2094 of_unittest_overlay_i2c_14();
2095 of_unittest_overlay_i2c_15();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002096
Wang Long9697a552015-03-11 08:36:54 +00002097 of_unittest_overlay_i2c_cleanup();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002098#endif
2099
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03002100 of_unittest_destroy_tracked_overlays();
2101
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002102out:
2103 of_node_put(bus_np);
2104}
2105
2106#else
Wang Long9697a552015-03-11 08:36:54 +00002107static inline void __init of_unittest_overlay(void) { }
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002108#endif
2109
Frank Rowand60a00042017-07-19 09:25:20 -07002110#ifdef CONFIG_OF_OVERLAY
2111
Frank Rowand81d0848f2017-04-25 17:09:54 -07002112/*
2113 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2114 * in scripts/Makefile.lib
2115 */
2116
2117#define OVERLAY_INFO_EXTERN(name) \
2118 extern uint8_t __dtb_##name##_begin[]; \
2119 extern uint8_t __dtb_##name##_end[]
2120
Frank Rowand39a751a2018-02-12 00:19:42 -08002121#define OVERLAY_INFO(overlay_name, expected) \
2122{ .dtb_begin = __dtb_##overlay_name##_begin, \
2123 .dtb_end = __dtb_##overlay_name##_end, \
2124 .expected_result = expected, \
2125 .name = #overlay_name, \
Frank Rowand81d0848f2017-04-25 17:09:54 -07002126}
2127
2128struct overlay_info {
Frank Rowand39a751a2018-02-12 00:19:42 -08002129 uint8_t *dtb_begin;
2130 uint8_t *dtb_end;
2131 int expected_result;
2132 int overlay_id;
2133 char *name;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002134};
2135
2136OVERLAY_INFO_EXTERN(overlay_base);
2137OVERLAY_INFO_EXTERN(overlay);
Frank Rowand39a751a2018-02-12 00:19:42 -08002138OVERLAY_INFO_EXTERN(overlay_0);
2139OVERLAY_INFO_EXTERN(overlay_1);
2140OVERLAY_INFO_EXTERN(overlay_2);
2141OVERLAY_INFO_EXTERN(overlay_3);
2142OVERLAY_INFO_EXTERN(overlay_4);
2143OVERLAY_INFO_EXTERN(overlay_5);
2144OVERLAY_INFO_EXTERN(overlay_6);
2145OVERLAY_INFO_EXTERN(overlay_7);
2146OVERLAY_INFO_EXTERN(overlay_8);
2147OVERLAY_INFO_EXTERN(overlay_9);
2148OVERLAY_INFO_EXTERN(overlay_10);
2149OVERLAY_INFO_EXTERN(overlay_11);
2150OVERLAY_INFO_EXTERN(overlay_12);
2151OVERLAY_INFO_EXTERN(overlay_13);
2152OVERLAY_INFO_EXTERN(overlay_15);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002153OVERLAY_INFO_EXTERN(overlay_bad_phandle);
Frank Rowand60a00042017-07-19 09:25:20 -07002154OVERLAY_INFO_EXTERN(overlay_bad_symbol);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002155
2156/* order of entries is hard-coded into users of overlays[] */
2157static struct overlay_info overlays[] = {
2158 OVERLAY_INFO(overlay_base, -9999),
2159 OVERLAY_INFO(overlay, 0),
Frank Rowand39a751a2018-02-12 00:19:42 -08002160 OVERLAY_INFO(overlay_0, 0),
2161 OVERLAY_INFO(overlay_1, 0),
2162 OVERLAY_INFO(overlay_2, 0),
2163 OVERLAY_INFO(overlay_3, 0),
2164 OVERLAY_INFO(overlay_4, 0),
2165 OVERLAY_INFO(overlay_5, 0),
2166 OVERLAY_INFO(overlay_6, 0),
2167 OVERLAY_INFO(overlay_7, 0),
2168 OVERLAY_INFO(overlay_8, 0),
2169 OVERLAY_INFO(overlay_9, 0),
2170 OVERLAY_INFO(overlay_10, 0),
2171 OVERLAY_INFO(overlay_11, 0),
2172 OVERLAY_INFO(overlay_12, 0),
2173 OVERLAY_INFO(overlay_13, 0),
2174 OVERLAY_INFO(overlay_15, 0),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002175 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
Frank Rowand60a00042017-07-19 09:25:20 -07002176 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002177 {}
2178};
2179
2180static struct device_node *overlay_base_root;
2181
Rob Herring0fa1c572018-01-05 15:32:33 -06002182static void * __init dt_alloc_memory(u64 size, u64 align)
2183{
2184 return memblock_virt_alloc(size, align);
2185}
2186
Frank Rowand81d0848f2017-04-25 17:09:54 -07002187/*
2188 * Create base device tree for the overlay unittest.
2189 *
2190 * This is called from very early boot code.
2191 *
2192 * Do as much as possible the same way as done in __unflatten_device_tree
2193 * and other early boot steps for the normal FDT so that the overlay base
2194 * unflattened tree will have the same characteristics as the real tree
2195 * (such as having memory allocated by the early allocator). The goal
2196 * is to test "the real thing" as much as possible, and test "test setup
2197 * code" as little as possible.
2198 *
2199 * Have to stop before resolving phandles, because that uses kmalloc.
2200 */
2201void __init unittest_unflatten_overlay_base(void)
2202{
2203 struct overlay_info *info;
2204 u32 data_size;
Frank Rowand39a751a2018-02-12 00:19:42 -08002205 void *new_fdt;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002206 u32 size;
2207
2208 info = &overlays[0];
2209
2210 if (info->expected_result != -9999) {
2211 pr_err("No dtb 'overlay_base' to attach\n");
2212 return;
2213 }
2214
2215 data_size = info->dtb_end - info->dtb_begin;
2216 if (!data_size) {
2217 pr_err("No dtb 'overlay_base' to attach\n");
2218 return;
2219 }
2220
2221 size = fdt_totalsize(info->dtb_begin);
2222 if (size != data_size) {
2223 pr_err("dtb 'overlay_base' header totalsize != actual size");
2224 return;
2225 }
2226
Frank Rowand39a751a2018-02-12 00:19:42 -08002227 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
2228 if (!new_fdt) {
Frank Rowand81d0848f2017-04-25 17:09:54 -07002229 pr_err("alloc for dtb 'overlay_base' failed");
2230 return;
2231 }
2232
Frank Rowand39a751a2018-02-12 00:19:42 -08002233 memcpy(new_fdt, info->dtb_begin, size);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002234
Frank Rowand39a751a2018-02-12 00:19:42 -08002235 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
Rob Herring0fa1c572018-01-05 15:32:33 -06002236 dt_alloc_memory, true);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002237}
2238
2239/*
2240 * The purpose of of_unittest_overlay_data_add is to add an
2241 * overlay in the normal fashion. This is a test of the whole
2242 * picture, instead of testing individual elements.
2243 *
2244 * A secondary purpose is to be able to verify that the contents of
2245 * /proc/device-tree/ contains the updated structure and values from
2246 * the overlay. That must be verified separately in user space.
2247 *
2248 * Return 0 on unexpected error.
2249 */
Frank Rowand39a751a2018-02-12 00:19:42 -08002250static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
Frank Rowand81d0848f2017-04-25 17:09:54 -07002251{
2252 struct overlay_info *info;
Frank Rowand39a751a2018-02-12 00:19:42 -08002253 int found = 0;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002254 int k;
2255 int ret;
2256 u32 size;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002257
Frank Rowand39a751a2018-02-12 00:19:42 -08002258 for (k = 0, info = overlays; info && info->name; info++, k++) {
2259 if (!strcmp(overlay_name, info->name)) {
2260 found = 1;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002261 break;
Frank Rowand39a751a2018-02-12 00:19:42 -08002262 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07002263 }
Frank Rowand39a751a2018-02-12 00:19:42 -08002264 if (!found) {
2265 pr_err("no overlay data for %s\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002266 return 0;
Frank Rowand39a751a2018-02-12 00:19:42 -08002267 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07002268
2269 size = info->dtb_end - info->dtb_begin;
Frank Rowand54587be2018-03-08 14:39:05 -08002270 if (!size)
Frank Rowand39a751a2018-02-12 00:19:42 -08002271 pr_err("no overlay data for %s\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002272
Frank Rowand39a751a2018-02-12 00:19:42 -08002273 ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
2274 if (overlay_id)
2275 *overlay_id = info->overlay_id;
2276 if (ret < 0)
2277 goto out;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002278
Frank Rowand39a751a2018-02-12 00:19:42 -08002279 pr_debug("%s applied\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002280
2281out:
Frank Rowand39a751a2018-02-12 00:19:42 -08002282 if (ret != info->expected_result)
2283 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
2284 info->expected_result, ret, overlay_name);
2285
Frank Rowand81d0848f2017-04-25 17:09:54 -07002286 return (ret == info->expected_result);
2287}
2288
2289/*
2290 * The purpose of of_unittest_overlay_high_level is to add an overlay
2291 * in the normal fashion. This is a test of the whole picture,
2292 * instead of individual elements.
2293 *
2294 * The first part of the function is _not_ normal overlay usage; it is
2295 * finishing splicing the base overlay device tree into the live tree.
2296 */
2297static __init void of_unittest_overlay_high_level(void)
2298{
2299 struct device_node *last_sibling;
2300 struct device_node *np;
2301 struct device_node *of_symbols;
2302 struct device_node *overlay_base_symbols;
2303 struct device_node **pprev;
2304 struct property *prop;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002305
2306 if (!overlay_base_root) {
2307 unittest(0, "overlay_base_root not initialized\n");
2308 return;
2309 }
2310
2311 /*
2312 * Could not fixup phandles in unittest_unflatten_overlay_base()
2313 * because kmalloc() was not yet available.
2314 */
Frank Rowandf948d6d2017-10-17 16:36:29 -07002315 of_overlay_mutex_lock();
Frank Rowand81d0848f2017-04-25 17:09:54 -07002316 of_resolve_phandles(overlay_base_root);
Frank Rowandf948d6d2017-10-17 16:36:29 -07002317 of_overlay_mutex_unlock();
2318
Frank Rowand81d0848f2017-04-25 17:09:54 -07002319
2320 /*
2321 * do not allow overlay_base to duplicate any node already in
2322 * tree, this greatly simplifies the code
2323 */
2324
2325 /*
2326 * remove overlay_base_root node "__local_fixups", after
2327 * being used by of_resolve_phandles()
2328 */
2329 pprev = &overlay_base_root->child;
2330 for (np = overlay_base_root->child; np; np = np->sibling) {
2331 if (!of_node_cmp(np->name, "__local_fixups__")) {
2332 *pprev = np->sibling;
2333 break;
2334 }
2335 pprev = &np->sibling;
2336 }
2337
2338 /* remove overlay_base_root node "__symbols__" if in live tree */
2339 of_symbols = of_get_child_by_name(of_root, "__symbols__");
2340 if (of_symbols) {
2341 /* will have to graft properties from node into live tree */
2342 pprev = &overlay_base_root->child;
2343 for (np = overlay_base_root->child; np; np = np->sibling) {
2344 if (!of_node_cmp(np->name, "__symbols__")) {
2345 overlay_base_symbols = np;
2346 *pprev = np->sibling;
2347 break;
2348 }
2349 pprev = &np->sibling;
2350 }
2351 }
2352
2353 for (np = overlay_base_root->child; np; np = np->sibling) {
2354 if (of_get_child_by_name(of_root, np->name)) {
2355 unittest(0, "illegal node name in overlay_base %s",
2356 np->name);
2357 return;
2358 }
2359 }
2360
2361 /*
2362 * overlay 'overlay_base' is not allowed to have root
2363 * properties, so only need to splice nodes into main device tree.
2364 *
2365 * root node of *overlay_base_root will not be freed, it is lost
2366 * memory.
2367 */
2368
2369 for (np = overlay_base_root->child; np; np = np->sibling)
2370 np->parent = of_root;
2371
2372 mutex_lock(&of_mutex);
2373
Arnd Bergmannee320b32017-04-28 11:44:11 +02002374 for (last_sibling = np = of_root->child; np; np = np->sibling)
Frank Rowand81d0848f2017-04-25 17:09:54 -07002375 last_sibling = np;
2376
2377 if (last_sibling)
2378 last_sibling->sibling = overlay_base_root->child;
2379 else
2380 of_root->child = overlay_base_root->child;
2381
2382 for_each_of_allnodes_from(overlay_base_root, np)
2383 __of_attach_node_sysfs(np);
2384
2385 if (of_symbols) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002386 struct property *new_prop;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002387 for_each_property_of_node(overlay_base_symbols, prop) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002388
2389 new_prop = __of_prop_dup(prop, GFP_KERNEL);
2390 if (!new_prop) {
2391 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
Frank Rowand81d0848f2017-04-25 17:09:54 -07002392 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002393 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002394 }
Frank Rowand06c46972018-03-08 14:39:04 -08002395 if (__of_add_property(of_symbols, new_prop)) {
2396 /* "name" auto-generated by unflatten */
2397 if (!strcmp(new_prop->name, "name"))
Frank Rowand39a751a2018-02-12 00:19:42 -08002398 continue;
Frank Rowand39a751a2018-02-12 00:19:42 -08002399 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
2400 prop->name);
2401 goto err_unlock;
2402 }
Frank Rowand06c46972018-03-08 14:39:04 -08002403 if (__of_add_property_sysfs(of_symbols, new_prop)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002404 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
Frank Rowand81d0848f2017-04-25 17:09:54 -07002405 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002406 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002407 }
2408 }
2409 }
2410
2411 mutex_unlock(&of_mutex);
2412
2413
2414 /* now do the normal overlay usage test */
2415
Frank Rowand39a751a2018-02-12 00:19:42 -08002416 unittest(overlay_data_apply("overlay", NULL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002417 "Adding overlay 'overlay' failed\n");
2418
Frank Rowand39a751a2018-02-12 00:19:42 -08002419 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002420 "Adding overlay 'overlay_bad_phandle' failed\n");
Frank Rowand60a00042017-07-19 09:25:20 -07002421
Frank Rowand39a751a2018-02-12 00:19:42 -08002422 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
Frank Rowand60a00042017-07-19 09:25:20 -07002423 "Adding overlay 'overlay_bad_symbol' failed\n");
2424
Dan Carpenter8756cd12017-05-03 22:49:50 +03002425 return;
2426
2427err_unlock:
2428 mutex_unlock(&of_mutex);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002429}
2430
2431#else
2432
2433static inline __init void of_unittest_overlay_high_level(void) {}
2434
2435#endif
2436
Wang Long9697a552015-03-11 08:36:54 +00002437static int __init of_unittest(void)
Grant Likely53a42092011-12-12 09:25:57 -07002438{
2439 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002440 int res;
2441
Wang Long9697a552015-03-11 08:36:54 +00002442 /* adding data for unittest */
2443 res = unittest_data_add();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002444 if (res)
2445 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00002446 if (!of_aliases)
2447 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07002448
2449 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2450 if (!np) {
2451 pr_info("No testcase data in device tree; not running tests\n");
2452 return 0;
2453 }
2454 of_node_put(np);
2455
Wang Long9697a552015-03-11 08:36:54 +00002456 pr_info("start of unittest - you will see error messages\n");
2457 of_unittest_check_tree_linkage();
2458 of_unittest_check_phandles();
2459 of_unittest_find_node_by_name();
2460 of_unittest_dynamic();
2461 of_unittest_parse_phandle_with_args();
Stephen Boyd357aa4b2018-01-30 18:36:17 -08002462 of_unittest_parse_phandle_with_args_map();
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002463 of_unittest_printf();
Wang Long9697a552015-03-11 08:36:54 +00002464 of_unittest_property_string();
2465 of_unittest_property_copy();
2466 of_unittest_changeset();
2467 of_unittest_parse_interrupts();
2468 of_unittest_parse_interrupts_extended();
2469 of_unittest_match_node();
2470 of_unittest_platform_populate();
2471 of_unittest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002472
Grant Likelyf2051d62014-10-01 17:40:22 +01002473 /* Double check linkage after removing testcase data */
Wang Long9697a552015-03-11 08:36:54 +00002474 of_unittest_check_tree_linkage();
Grant Likelyf2051d62014-10-01 17:40:22 +01002475
Frank Rowand81d0848f2017-04-25 17:09:54 -07002476 of_unittest_overlay_high_level();
2477
Wang Long9697a552015-03-11 08:36:54 +00002478 pr_info("end of unittest - %i passed, %i failed\n",
2479 unittest_results.passed, unittest_results.failed);
Grant Likelyf2051d62014-10-01 17:40:22 +01002480
Grant Likely53a42092011-12-12 09:25:57 -07002481 return 0;
2482}
Wang Long9697a552015-03-11 08:36:54 +00002483late_initcall(of_unittest);