blob: 070caefb3b398f2caf480250ccf472d4ab482616 [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{
257 unsigned char buf[strlen(expected)+10];
258 int size, i;
259
260 /* Baseline; check conversion with a large size limit */
261 memset(buf, 0xff, sizeof(buf));
262 size = snprintf(buf, sizeof(buf) - 2, fmt, np);
263
264 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
265 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
266 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
267 fmt, expected, buf);
268
269 /* Make sure length limits work */
270 size++;
271 for (i = 0; i < 2; i++, size--) {
272 /* Clear the buffer, and make sure it works correctly still */
273 memset(buf, 0xff, sizeof(buf));
274 snprintf(buf, size+1, fmt, np);
275 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
276 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
277 size, fmt, expected, buf);
278 }
279}
280
281static void __init of_unittest_printf(void)
282{
283 struct device_node *np;
284 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
285 char phandle_str[16] = "";
286
287 np = of_find_node_by_path(full_name);
288 if (!np) {
289 unittest(np, "testcase data missing\n");
290 return;
291 }
292
293 num_to_str(phandle_str, sizeof(phandle_str), np->phandle);
294
295 of_unittest_printf_one(np, "%pOF", full_name);
296 of_unittest_printf_one(np, "%pOFf", full_name);
297 of_unittest_printf_one(np, "%pOFp", phandle_str);
298 of_unittest_printf_one(np, "%pOFP", "dev@100");
299 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
300 of_unittest_printf_one(np, "%10pOFP", " dev@100");
301 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
302 of_unittest_printf_one(of_root, "%pOFP", "/");
303 of_unittest_printf_one(np, "%pOFF", "----");
304 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
305 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
306 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
307 of_unittest_printf_one(np, "%pOFC",
308 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
309}
310
Grant Likely841ec212014-10-02 13:09:15 +0100311struct node_hash {
312 struct hlist_node node;
313 struct device_node *np;
314};
315
Grant Likely2118f4b2014-10-07 11:30:31 +0100316static DEFINE_HASHTABLE(phandle_ht, 8);
Wang Long9697a552015-03-11 08:36:54 +0000317static void __init of_unittest_check_phandles(void)
Grant Likely841ec212014-10-02 13:09:15 +0100318{
319 struct device_node *np;
320 struct node_hash *nh;
321 struct hlist_node *tmp;
322 int i, dup_count = 0, phandle_count = 0;
Grant Likely841ec212014-10-02 13:09:15 +0100323
Grant Likely841ec212014-10-02 13:09:15 +0100324 for_each_of_allnodes(np) {
325 if (!np->phandle)
326 continue;
327
Grant Likely2118f4b2014-10-07 11:30:31 +0100328 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100329 if (nh->np->phandle == np->phandle) {
Rob Herring0d638a02017-06-01 15:50:55 -0500330 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
331 np->phandle, nh->np, np);
Grant Likely841ec212014-10-02 13:09:15 +0100332 dup_count++;
333 break;
334 }
335 }
336
337 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
338 if (WARN_ON(!nh))
339 return;
340
341 nh->np = np;
Grant Likely2118f4b2014-10-07 11:30:31 +0100342 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100343 phandle_count++;
344 }
Wang Long9697a552015-03-11 08:36:54 +0000345 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
Grant Likely841ec212014-10-02 13:09:15 +0100346 dup_count, phandle_count);
347
348 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100349 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100350 hash_del(&nh->node);
351 kfree(nh);
352 }
353}
354
Wang Long9697a552015-03-11 08:36:54 +0000355static void __init of_unittest_parse_phandle_with_args(void)
Grant Likely53a42092011-12-12 09:25:57 -0700356{
357 struct device_node *np;
358 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000359 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700360
Grant Likely53a42092011-12-12 09:25:57 -0700361 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
362 if (!np) {
363 pr_err("missing testcase data\n");
364 return;
365 }
366
Grant Likelybd69f732013-02-10 22:57:21 +0000367 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000368 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000369
Grant Likelyf7f951c2013-02-12 17:41:22 +0000370 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700371 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700372
Grant Likely53a42092011-12-12 09:25:57 -0700373 rc = of_parse_phandle_with_args(np, "phandle-list",
374 "#phandle-cells", i, &args);
375
376 /* Test the values from tests-phandle.dtsi */
377 switch (i) {
378 case 0:
379 passed &= !rc;
380 passed &= (args.args_count == 1);
381 passed &= (args.args[0] == (i + 1));
382 break;
383 case 1:
384 passed &= !rc;
385 passed &= (args.args_count == 2);
386 passed &= (args.args[0] == (i + 1));
387 passed &= (args.args[1] == 0);
388 break;
389 case 2:
390 passed &= (rc == -ENOENT);
391 break;
392 case 3:
393 passed &= !rc;
394 passed &= (args.args_count == 3);
395 passed &= (args.args[0] == (i + 1));
396 passed &= (args.args[1] == 4);
397 passed &= (args.args[2] == 3);
398 break;
399 case 4:
400 passed &= !rc;
401 passed &= (args.args_count == 2);
402 passed &= (args.args[0] == (i + 1));
403 passed &= (args.args[1] == 100);
404 break;
405 case 5:
406 passed &= !rc;
407 passed &= (args.args_count == 0);
408 break;
409 case 6:
410 passed &= !rc;
411 passed &= (args.args_count == 1);
412 passed &= (args.args[0] == (i + 1));
413 break;
414 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000415 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700416 break;
417 default:
418 passed = false;
419 }
420
Rob Herring0d638a02017-06-01 15:50:55 -0500421 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
422 i, args.np, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700423 }
424
425 /* Check for missing list property */
426 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
427 "#phandle-cells", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000428 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000429 rc = of_count_phandle_with_args(np, "phandle-list-missing",
430 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000431 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700432
433 /* Check for missing cells property */
434 rc = of_parse_phandle_with_args(np, "phandle-list",
435 "#phandle-cells-missing", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000436 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000437 rc = of_count_phandle_with_args(np, "phandle-list",
438 "#phandle-cells-missing");
Wang Long9697a552015-03-11 08:36:54 +0000439 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700440
441 /* Check for bad phandle in list */
442 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
443 "#phandle-cells", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000444 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000445 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
446 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000447 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700448
449 /* Check for incorrectly formed argument list */
450 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
451 "#phandle-cells", 1, &args);
Wang Long9697a552015-03-11 08:36:54 +0000452 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000453 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
454 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000455 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700456}
457
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800458static void __init of_unittest_parse_phandle_with_args_map(void)
459{
460 struct device_node *np, *p0, *p1, *p2, *p3;
461 struct of_phandle_args args;
462 int i, rc;
463
464 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
465 if (!np) {
466 pr_err("missing testcase data\n");
467 return;
468 }
469
470 p0 = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
471 if (!p0) {
472 pr_err("missing testcase data\n");
473 return;
474 }
475
476 p1 = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
477 if (!p1) {
478 pr_err("missing testcase data\n");
479 return;
480 }
481
482 p2 = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
483 if (!p2) {
484 pr_err("missing testcase data\n");
485 return;
486 }
487
488 p3 = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
489 if (!p3) {
490 pr_err("missing testcase data\n");
491 return;
492 }
493
494 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
495 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
496
497 for (i = 0; i < 8; i++) {
498 bool passed = true;
499
500 rc = of_parse_phandle_with_args_map(np, "phandle-list",
501 "phandle", i, &args);
502
503 /* Test the values from tests-phandle.dtsi */
504 switch (i) {
505 case 0:
506 passed &= !rc;
507 passed &= (args.np == p1);
508 passed &= (args.args_count == 1);
509 passed &= (args.args[0] == 1);
510 break;
511 case 1:
512 passed &= !rc;
513 passed &= (args.np == p3);
514 passed &= (args.args_count == 3);
515 passed &= (args.args[0] == 2);
516 passed &= (args.args[1] == 5);
517 passed &= (args.args[2] == 3);
518 break;
519 case 2:
520 passed &= (rc == -ENOENT);
521 break;
522 case 3:
523 passed &= !rc;
524 passed &= (args.np == p0);
525 passed &= (args.args_count == 0);
526 break;
527 case 4:
528 passed &= !rc;
529 passed &= (args.np == p1);
530 passed &= (args.args_count == 1);
531 passed &= (args.args[0] == 3);
532 break;
533 case 5:
534 passed &= !rc;
535 passed &= (args.np == p0);
536 passed &= (args.args_count == 0);
537 break;
538 case 6:
539 passed &= !rc;
540 passed &= (args.np == p2);
541 passed &= (args.args_count == 2);
542 passed &= (args.args[0] == 15);
543 passed &= (args.args[1] == 0x20);
544 break;
545 case 7:
546 passed &= (rc == -ENOENT);
547 break;
548 default:
549 passed = false;
550 }
551
552 unittest(passed, "index %i - data error on node %s rc=%i\n",
553 i, args.np->full_name, rc);
554 }
555
556 /* Check for missing list property */
557 rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
558 "phandle", 0, &args);
559 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
560
561 /* Check for missing cells,map,mask property */
562 rc = of_parse_phandle_with_args_map(np, "phandle-list",
563 "phandle-missing", 0, &args);
564 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
565
566 /* Check for bad phandle in list */
567 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
568 "phandle", 0, &args);
569 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
570
571 /* Check for incorrectly formed argument list */
572 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
573 "phandle", 1, &args);
574 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
575}
576
Wang Long9697a552015-03-11 08:36:54 +0000577static void __init of_unittest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700578{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000579 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700580 struct device_node *np;
581 int rc;
582
Grant Likely7aff0fe2011-12-12 09:25:58 -0700583 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
584 if (!np) {
585 pr_err("No testcase data in device tree\n");
586 return;
587 }
588
589 rc = of_property_match_string(np, "phandle-list-names", "first");
Wang Long9697a552015-03-11 08:36:54 +0000590 unittest(rc == 0, "first expected:0 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700591 rc = of_property_match_string(np, "phandle-list-names", "second");
Wang Long9697a552015-03-11 08:36:54 +0000592 unittest(rc == 1, "second expected:1 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700593 rc = of_property_match_string(np, "phandle-list-names", "third");
Wang Long9697a552015-03-11 08:36:54 +0000594 unittest(rc == 2, "third expected:2 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700595 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Wang Long9697a552015-03-11 08:36:54 +0000596 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700597 rc = of_property_match_string(np, "missing-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000598 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700599 rc = of_property_match_string(np, "empty-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000600 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700601 rc = of_property_match_string(np, "unterminated-string", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000602 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000603
604 /* of_property_count_strings() tests */
605 rc = of_property_count_strings(np, "string-property");
Wang Long9697a552015-03-11 08:36:54 +0000606 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000607 rc = of_property_count_strings(np, "phandle-list-names");
Wang Long9697a552015-03-11 08:36:54 +0000608 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000609 rc = of_property_count_strings(np, "unterminated-string");
Wang Long9697a552015-03-11 08:36:54 +0000610 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000611 rc = of_property_count_strings(np, "unterminated-string-list");
Wang Long9697a552015-03-11 08:36:54 +0000612 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000613
614 /* of_property_read_string_index() tests */
615 rc = of_property_read_string_index(np, "string-property", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000616 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000617 strings[0] = NULL;
618 rc = of_property_read_string_index(np, "string-property", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000619 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000620 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000621 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000622 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000623 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000624 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
Wang Long9697a552015-03-11 08:36:54 +0000625 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000626 strings[0] = NULL;
627 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
Wang Long9697a552015-03-11 08:36:54 +0000628 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000629 strings[0] = NULL;
630 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000631 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000632 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000633 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000634 strings[0] = NULL;
635 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
Wang Long9697a552015-03-11 08:36:54 +0000636 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000637 strings[1] = NULL;
638
639 /* of_property_read_string_array() tests */
640 rc = of_property_read_string_array(np, "string-property", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000641 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000642 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000643 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000644 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000645 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000646 /* -- An incorrectly formed string should cause a failure */
647 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000648 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000649 /* -- parsing the correctly formed strings should still work: */
650 strings[2] = NULL;
651 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
Wang Long9697a552015-03-11 08:36:54 +0000652 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000653 strings[1] = NULL;
654 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
Wang Long9697a552015-03-11 08:36:54 +0000655 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 -0700656}
657
Pantelis Antoniou69843392014-07-04 19:58:47 +0300658#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
659 (p1)->value && (p2)->value && \
660 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
661 !strcmp((p1)->name, (p2)->name))
Wang Long9697a552015-03-11 08:36:54 +0000662static void __init of_unittest_property_copy(void)
Pantelis Antoniou69843392014-07-04 19:58:47 +0300663{
664#ifdef CONFIG_OF_DYNAMIC
665 struct property p1 = { .name = "p1", .length = 0, .value = "" };
666 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
667 struct property *new;
668
669 new = __of_prop_dup(&p1, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000670 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300671 kfree(new->value);
672 kfree(new->name);
673 kfree(new);
674
675 new = __of_prop_dup(&p2, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000676 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300677 kfree(new->value);
678 kfree(new->name);
679 kfree(new);
680#endif
681}
682
Wang Long9697a552015-03-11 08:36:54 +0000683static void __init of_unittest_changeset(void)
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300684{
685#ifdef CONFIG_OF_DYNAMIC
Frank Rowanda4f91f02018-02-26 14:01:22 -0800686 struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
687 struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
688 struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
689 struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300690 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
691 struct property *ppremove;
Frank Rowanda4f91f02018-02-26 14:01:22 -0800692 struct device_node *n1, *n2, *n21, *nchangeset, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300693 struct of_changeset chgset;
694
Frank Rowandb89dae12018-02-26 14:01:23 -0800695 n1 = __of_node_dup(NULL, "n1");
Wang Long9697a552015-03-11 08:36:54 +0000696 unittest(n1, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800697
Frank Rowandb89dae12018-02-26 14:01:23 -0800698 n2 = __of_node_dup(NULL, "n2");
Wang Long9697a552015-03-11 08:36:54 +0000699 unittest(n2, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800700
Frank Rowandb89dae12018-02-26 14:01:23 -0800701 n21 = __of_node_dup(NULL, "n21");
Wang Long9697a552015-03-11 08:36:54 +0000702 unittest(n21, "testcase setup failure %p\n", n21);
Frank Rowanda4f91f02018-02-26 14:01:22 -0800703
704 nchangeset = of_find_node_by_path("/testcase-data/changeset");
705 nremove = of_get_child_by_name(nchangeset, "node-remove");
Wang Long9697a552015-03-11 08:36:54 +0000706 unittest(nremove, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800707
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300708 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000709 unittest(ppadd, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800710
711 ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
712 unittest(ppname_n1, "testcase setup failure\n");
713
714 ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
715 unittest(ppname_n2, "testcase setup failure\n");
716
717 ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
718 unittest(ppname_n21, "testcase setup failure\n");
719
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300720 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000721 unittest(ppupdate, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800722
723 parent = nchangeset;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300724 n1->parent = parent;
725 n2->parent = parent;
726 n21->parent = n2;
Frank Rowanda4f91f02018-02-26 14:01:22 -0800727
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300728 ppremove = of_find_property(parent, "prop-remove", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000729 unittest(ppremove, "failed to find removal prop");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300730
731 of_changeset_init(&chgset);
Frank Rowanda4f91f02018-02-26 14:01:22 -0800732
Wang Long9697a552015-03-11 08:36:54 +0000733 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800734 unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
735
Wang Long9697a552015-03-11 08:36:54 +0000736 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800737 unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
738
Wang Long9697a552015-03-11 08:36:54 +0000739 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800740 unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
741
Wang Long9697a552015-03-11 08:36:54 +0000742 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800743
744 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
Wang Long9697a552015-03-11 08:36:54 +0000745 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
746 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800747
Wang Long9697a552015-03-11 08:36:54 +0000748 unittest(!of_changeset_apply(&chgset), "apply failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300749
Frank Rowanda4f91f02018-02-26 14:01:22 -0800750 of_node_put(nchangeset);
751
Grant Likelye5179582014-11-17 22:31:32 +0000752 /* Make sure node names are constructed correctly */
Wang Long9697a552015-03-11 08:36:54 +0000753 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
Rob Herring0d638a02017-06-01 15:50:55 -0500754 "'%pOF' not added\n", n21);
Markus Elfringc46ca3c2014-12-02 13:54:00 +0100755 of_node_put(np);
Grant Likelye5179582014-11-17 22:31:32 +0000756
Wang Long9697a552015-03-11 08:36:54 +0000757 unittest(!of_changeset_revert(&chgset), "revert failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300758
759 of_changeset_destroy(&chgset);
760#endif
761}
762
Wang Long9697a552015-03-11 08:36:54 +0000763static void __init of_unittest_parse_interrupts(void)
Grant Likelya9f10ca2013-10-11 22:04:23 +0100764{
765 struct device_node *np;
766 struct of_phandle_args args;
767 int i, rc;
768
769 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
770 if (!np) {
771 pr_err("missing testcase data\n");
772 return;
773 }
774
775 for (i = 0; i < 4; i++) {
776 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700777
Grant Likelya9f10ca2013-10-11 22:04:23 +0100778 args.args_count = 0;
779 rc = of_irq_parse_one(np, i, &args);
780
781 passed &= !rc;
782 passed &= (args.args_count == 1);
783 passed &= (args.args[0] == (i + 1));
784
Rob Herring0d638a02017-06-01 15:50:55 -0500785 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
786 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100787 }
788 of_node_put(np);
789
790 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
791 if (!np) {
792 pr_err("missing testcase data\n");
793 return;
794 }
795
796 for (i = 0; i < 4; i++) {
797 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700798
Grant Likelya9f10ca2013-10-11 22:04:23 +0100799 args.args_count = 0;
800 rc = of_irq_parse_one(np, i, &args);
801
802 /* Test the values from tests-phandle.dtsi */
803 switch (i) {
804 case 0:
805 passed &= !rc;
806 passed &= (args.args_count == 1);
807 passed &= (args.args[0] == 9);
808 break;
809 case 1:
810 passed &= !rc;
811 passed &= (args.args_count == 3);
812 passed &= (args.args[0] == 10);
813 passed &= (args.args[1] == 11);
814 passed &= (args.args[2] == 12);
815 break;
816 case 2:
817 passed &= !rc;
818 passed &= (args.args_count == 2);
819 passed &= (args.args[0] == 13);
820 passed &= (args.args[1] == 14);
821 break;
822 case 3:
823 passed &= !rc;
824 passed &= (args.args_count == 2);
825 passed &= (args.args[0] == 15);
826 passed &= (args.args[1] == 16);
827 break;
828 default:
829 passed = false;
830 }
Rob Herring0d638a02017-06-01 15:50:55 -0500831 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
832 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100833 }
834 of_node_put(np);
835}
836
Wang Long9697a552015-03-11 08:36:54 +0000837static void __init of_unittest_parse_interrupts_extended(void)
Grant Likely79d97012013-09-19 16:47:37 -0500838{
839 struct device_node *np;
840 struct of_phandle_args args;
841 int i, rc;
842
843 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
844 if (!np) {
845 pr_err("missing testcase data\n");
846 return;
847 }
848
849 for (i = 0; i < 7; i++) {
850 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700851
Grant Likely79d97012013-09-19 16:47:37 -0500852 rc = of_irq_parse_one(np, i, &args);
853
854 /* Test the values from tests-phandle.dtsi */
855 switch (i) {
856 case 0:
857 passed &= !rc;
858 passed &= (args.args_count == 1);
859 passed &= (args.args[0] == 1);
860 break;
861 case 1:
862 passed &= !rc;
863 passed &= (args.args_count == 3);
864 passed &= (args.args[0] == 2);
865 passed &= (args.args[1] == 3);
866 passed &= (args.args[2] == 4);
867 break;
868 case 2:
869 passed &= !rc;
870 passed &= (args.args_count == 2);
871 passed &= (args.args[0] == 5);
872 passed &= (args.args[1] == 6);
873 break;
874 case 3:
875 passed &= !rc;
876 passed &= (args.args_count == 1);
877 passed &= (args.args[0] == 9);
878 break;
879 case 4:
880 passed &= !rc;
881 passed &= (args.args_count == 3);
882 passed &= (args.args[0] == 10);
883 passed &= (args.args[1] == 11);
884 passed &= (args.args[2] == 12);
885 break;
886 case 5:
887 passed &= !rc;
888 passed &= (args.args_count == 2);
889 passed &= (args.args[0] == 13);
890 passed &= (args.args[1] == 14);
891 break;
892 case 6:
893 passed &= !rc;
894 passed &= (args.args_count == 1);
895 passed &= (args.args[0] == 15);
896 break;
897 default:
898 passed = false;
899 }
900
Rob Herring0d638a02017-06-01 15:50:55 -0500901 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
902 i, args.np, rc);
Grant Likely79d97012013-09-19 16:47:37 -0500903 }
904 of_node_put(np);
905}
906
Frank Rowandafaed7a2015-03-14 00:00:36 -0700907static const struct of_device_id match_node_table[] = {
Grant Likely1f42e5d2014-02-18 21:38:55 +0000908 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
909 { .data = "B", .type = "type1", }, /* followed by type alone */
910
911 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
912 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
913 { .data = "Cc", .name = "name2", .type = "type2", },
914
915 { .data = "E", .compatible = "compat3" },
916 { .data = "G", .compatible = "compat2", },
917 { .data = "H", .compatible = "compat2", .name = "name5", },
918 { .data = "I", .compatible = "compat2", .type = "type1", },
919 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
920 { .data = "K", .compatible = "compat2", .name = "name9", },
921 {}
922};
923
924static struct {
925 const char *path;
926 const char *data;
927} match_node_tests[] = {
928 { .path = "/testcase-data/match-node/name0", .data = "A", },
929 { .path = "/testcase-data/match-node/name1", .data = "B", },
930 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
931 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
932 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
933 { .path = "/testcase-data/match-node/name3", .data = "E", },
934 { .path = "/testcase-data/match-node/name4", .data = "G", },
935 { .path = "/testcase-data/match-node/name5", .data = "H", },
936 { .path = "/testcase-data/match-node/name6", .data = "G", },
937 { .path = "/testcase-data/match-node/name7", .data = "I", },
938 { .path = "/testcase-data/match-node/name8", .data = "J", },
939 { .path = "/testcase-data/match-node/name9", .data = "K", },
940};
941
Wang Long9697a552015-03-11 08:36:54 +0000942static void __init of_unittest_match_node(void)
Grant Likely1f42e5d2014-02-18 21:38:55 +0000943{
944 struct device_node *np;
945 const struct of_device_id *match;
946 int i;
947
948 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
949 np = of_find_node_by_path(match_node_tests[i].path);
950 if (!np) {
Wang Long9697a552015-03-11 08:36:54 +0000951 unittest(0, "missing testcase node %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000952 match_node_tests[i].path);
953 continue;
954 }
955
956 match = of_match_node(match_node_table, np);
957 if (!match) {
Wang Long9697a552015-03-11 08:36:54 +0000958 unittest(0, "%s didn't match anything\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000959 match_node_tests[i].path);
960 continue;
961 }
962
963 if (strcmp(match->data, match_node_tests[i].data) != 0) {
Wang Long9697a552015-03-11 08:36:54 +0000964 unittest(0, "%s got wrong match. expected %s, got %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000965 match_node_tests[i].path, match_node_tests[i].data,
966 (const char *)match->data);
967 continue;
968 }
Wang Long9697a552015-03-11 08:36:54 +0000969 unittest(1, "passed");
Grant Likely1f42e5d2014-02-18 21:38:55 +0000970 }
971}
972
Grant Likelyd2329fb2016-01-04 13:13:21 +0100973static struct resource test_bus_res = {
974 .start = 0xfffffff8,
975 .end = 0xfffffff9,
976 .flags = IORESOURCE_MEM,
977};
Grant Likely37791b62015-03-27 20:30:04 -0700978static const struct platform_device_info test_bus_info = {
979 .name = "unittest-bus",
Grant Likely851da972014-11-04 13:14:13 +0000980};
Wang Long9697a552015-03-11 08:36:54 +0000981static void __init of_unittest_platform_populate(void)
Rob Herring82c0f582014-04-23 17:57:40 -0500982{
Grant Likely851da972014-11-04 13:14:13 +0000983 int irq, rc;
984 struct device_node *np, *child, *grandchild;
Grant Likely37791b62015-03-27 20:30:04 -0700985 struct platform_device *pdev, *test_bus;
Frank Rowandafaed7a2015-03-14 00:00:36 -0700986 const struct of_device_id match[] = {
Rob Herringfb2caa52014-05-13 10:07:54 -0500987 { .compatible = "test-device", },
988 {}
989 };
Rob Herring82c0f582014-04-23 17:57:40 -0500990
991 np = of_find_node_by_path("/testcase-data");
Kefeng Wang146dedb2016-06-01 14:53:10 +0800992 of_platform_default_populate(np, NULL, NULL);
Rob Herring82c0f582014-04-23 17:57:40 -0500993
994 /* Test that a missing irq domain returns -EPROBE_DEFER */
995 np = of_find_node_by_path("/testcase-data/testcase-device1");
996 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +0000997 unittest(pdev, "device 1 creation failed\n");
Rob Herring7d1cdc82014-05-13 10:07:29 -0500998
Rob Herring82c0f582014-04-23 17:57:40 -0500999 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +00001000 unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -05001001
1002 /* Test that a parsing failure does not return -EPROBE_DEFER */
1003 np = of_find_node_by_path("/testcase-data/testcase-device2");
1004 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +00001005 unittest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -05001006 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +00001007 unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -05001008
Frank Rowand716e1d42015-03-13 23:57:40 -07001009 np = of_find_node_by_path("/testcase-data/platform-tests");
Grant Likelya2166ca2015-03-29 08:59:58 +01001010 unittest(np, "No testcase data in device tree\n");
Frank Rowand716e1d42015-03-13 23:57:40 -07001011 if (!np)
Rob Herringfb2caa52014-05-13 10:07:54 -05001012 return;
Grant Likely851da972014-11-04 13:14:13 +00001013
Grant Likely37791b62015-03-27 20:30:04 -07001014 test_bus = platform_device_register_full(&test_bus_info);
1015 rc = PTR_ERR_OR_ZERO(test_bus);
Grant Likelya2166ca2015-03-29 08:59:58 +01001016 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
Frank Rowand716e1d42015-03-13 23:57:40 -07001017 if (rc)
Grant Likely851da972014-11-04 13:14:13 +00001018 return;
Grant Likely37791b62015-03-27 20:30:04 -07001019 test_bus->dev.of_node = np;
Rob Herringfb2caa52014-05-13 10:07:54 -05001020
Grant Likelyd2329fb2016-01-04 13:13:21 +01001021 /*
1022 * Add a dummy resource to the test bus node after it is
1023 * registered to catch problems with un-inserted resources. The
1024 * DT code doesn't insert the resources, and it has caused the
1025 * kernel to oops in the past. This makes sure the same bug
1026 * doesn't crop up again.
1027 */
1028 platform_device_add_resources(test_bus, &test_bus_res, 1);
1029
Grant Likely37791b62015-03-27 20:30:04 -07001030 of_platform_populate(np, match, NULL, &test_bus->dev);
Rob Herringfb2caa52014-05-13 10:07:54 -05001031 for_each_child_of_node(np, child) {
Rob Herringfb2caa52014-05-13 10:07:54 -05001032 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +00001033 unittest(of_find_device_by_node(grandchild),
Rob Herringfb2caa52014-05-13 10:07:54 -05001034 "Could not create device for node '%s'\n",
1035 grandchild->name);
1036 }
Grant Likely851da972014-11-04 13:14:13 +00001037
Grant Likely37791b62015-03-27 20:30:04 -07001038 of_platform_depopulate(&test_bus->dev);
Grant Likely851da972014-11-04 13:14:13 +00001039 for_each_child_of_node(np, child) {
1040 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +00001041 unittest(!of_find_device_by_node(grandchild),
Grant Likely851da972014-11-04 13:14:13 +00001042 "device didn't get destroyed '%s'\n",
1043 grandchild->name);
1044 }
1045
Grant Likely37791b62015-03-27 20:30:04 -07001046 platform_device_unregister(test_bus);
Grant Likely851da972014-11-04 13:14:13 +00001047 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -05001048}
1049
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001050/**
1051 * update_node_properties - adds the properties
1052 * of np into dup node (present in live tree) and
1053 * updates parent of children of np to dup.
1054 *
1055 * @np: node already present in live tree
1056 * @dup: node present in live tree to be updated
1057 */
1058static void update_node_properties(struct device_node *np,
1059 struct device_node *dup)
1060{
1061 struct property *prop;
1062 struct device_node *child;
1063
1064 for_each_property_of_node(np, prop)
1065 of_add_property(dup, prop);
1066
1067 for_each_child_of_node(np, child)
1068 child->parent = dup;
1069}
1070
1071/**
1072 * attach_node_and_children - attaches nodes
1073 * and its children to live tree
1074 *
1075 * @np: Node to attach to live tree
1076 */
1077static int attach_node_and_children(struct device_node *np)
1078{
Grant Likely5063e252014-10-03 16:28:27 +01001079 struct device_node *next, *dup, *child;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -08001080 unsigned long flags;
Rob Herring0d638a02017-06-01 15:50:55 -05001081 const char *full_name;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001082
Rob Herring0d638a02017-06-01 15:50:55 -05001083 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
1084 dup = of_find_node_by_path(full_name);
1085 kfree(full_name);
Grant Likely5063e252014-10-03 16:28:27 +01001086 if (dup) {
1087 update_node_properties(np, dup);
1088 return 0;
1089 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001090
Grant Likely5063e252014-10-03 16:28:27 +01001091 child = np->child;
1092 np->child = NULL;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -08001093
1094 mutex_lock(&of_mutex);
1095 raw_spin_lock_irqsave(&devtree_lock, flags);
1096 np->sibling = np->parent->child;
1097 np->parent->child = np;
1098 of_node_clear_flag(np, OF_DETACHED);
1099 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1100
1101 __of_attach_node_sysfs(np);
1102 mutex_unlock(&of_mutex);
1103
Grant Likely5063e252014-10-03 16:28:27 +01001104 while (child) {
1105 next = child->sibling;
1106 attach_node_and_children(child);
1107 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001108 }
1109
1110 return 0;
1111}
1112
1113/**
Wang Long9697a552015-03-11 08:36:54 +00001114 * unittest_data_add - Reads, copies data from
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001115 * linked tree and attaches it to the live tree
1116 */
Wang Long9697a552015-03-11 08:36:54 +00001117static int __init unittest_data_add(void)
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001118{
Wang Long9697a552015-03-11 08:36:54 +00001119 void *unittest_data;
1120 struct device_node *unittest_data_node, *np;
Frank Rowandc8547112015-03-14 00:04:24 -07001121 /*
1122 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
1123 * created by cmd_dt_S_dtb in scripts/Makefile.lib
1124 */
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001125 extern uint8_t __dtb_testcases_begin[];
1126 extern uint8_t __dtb_testcases_end[];
1127 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +01001128 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001129
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001130 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001131 pr_warn("%s: No testcase data to attach; not running tests\n",
1132 __func__);
1133 return -ENODATA;
1134 }
1135
1136 /* creating copy */
Wang Long9697a552015-03-11 08:36:54 +00001137 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001138
Wang Long9697a552015-03-11 08:36:54 +00001139 if (!unittest_data) {
1140 pr_warn("%s: Failed to allocate memory for unittest_data; "
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001141 "not running tests\n", __func__);
1142 return -ENOMEM;
1143 }
Gavin Shanc4263232016-05-03 23:22:50 +10001144 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
Wang Long9697a552015-03-11 08:36:54 +00001145 if (!unittest_data_node) {
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001146 pr_warn("%s: No tree to attach; not running tests\n", __func__);
1147 return -ENODATA;
1148 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001149
1150 /*
Frank Rowand39a751a2018-02-12 00:19:42 -08001151 * This lock normally encloses of_resolve_phandles()
Frank Rowandf948d6d2017-10-17 16:36:29 -07001152 */
1153 of_overlay_mutex_lock();
1154
Wang Long9697a552015-03-11 08:36:54 +00001155 rc = of_resolve_phandles(unittest_data_node);
Grant Likely2eb46da2014-10-02 14:36:46 +01001156 if (rc) {
1157 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
Frank Rowandf948d6d2017-10-17 16:36:29 -07001158 of_overlay_mutex_unlock();
Grant Likely2eb46da2014-10-02 14:36:46 +01001159 return -EINVAL;
1160 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001161
Grant Likely5063e252014-10-03 16:28:27 +01001162 if (!of_root) {
Wang Long9697a552015-03-11 08:36:54 +00001163 of_root = unittest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001164 for_each_of_allnodes(np)
1165 __of_attach_node_sysfs(np);
1166 of_aliases = of_find_node_by_path("/aliases");
1167 of_chosen = of_find_node_by_path("/chosen");
Frank Rowandf948d6d2017-10-17 16:36:29 -07001168 of_overlay_mutex_unlock();
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001169 return 0;
1170 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001171
1172 /* attach the sub-tree to live tree */
Wang Long9697a552015-03-11 08:36:54 +00001173 np = unittest_data_node->child;
Grant Likely5063e252014-10-03 16:28:27 +01001174 while (np) {
1175 struct device_node *next = np->sibling;
Frank Rowand3db316d2015-03-14 00:02:31 -07001176
Grant Likely5063e252014-10-03 16:28:27 +01001177 np->parent = of_root;
1178 attach_node_and_children(np);
1179 np = next;
1180 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001181
1182 of_overlay_mutex_unlock();
1183
Grant Likely5063e252014-10-03 16:28:27 +01001184 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001185}
1186
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001187#ifdef CONFIG_OF_OVERLAY
1188
Wang Long9697a552015-03-11 08:36:54 +00001189static int unittest_probe(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001190{
1191 struct device *dev = &pdev->dev;
1192 struct device_node *np = dev->of_node;
1193
1194 if (np == NULL) {
1195 dev_err(dev, "No OF data for device\n");
1196 return -EINVAL;
1197
1198 }
1199
Rob Herring0d638a02017-06-01 15:50:55 -05001200 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001201
1202 of_platform_populate(np, NULL, NULL, &pdev->dev);
1203
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001204 return 0;
1205}
1206
Wang Long9697a552015-03-11 08:36:54 +00001207static int unittest_remove(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001208{
1209 struct device *dev = &pdev->dev;
1210 struct device_node *np = dev->of_node;
1211
Rob Herring0d638a02017-06-01 15:50:55 -05001212 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001213 return 0;
1214}
1215
Grant Likelya2166ca2015-03-29 08:59:58 +01001216static const struct of_device_id unittest_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001217 { .compatible = "unittest", },
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001218 {},
1219};
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001220
Wang Long9697a552015-03-11 08:36:54 +00001221static struct platform_driver unittest_driver = {
1222 .probe = unittest_probe,
1223 .remove = unittest_remove,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001224 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001225 .name = "unittest",
Wang Long9697a552015-03-11 08:36:54 +00001226 .of_match_table = of_match_ptr(unittest_match),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001227 },
1228};
1229
1230/* get the platform device instantiated at the path */
1231static struct platform_device *of_path_to_platform_device(const char *path)
1232{
1233 struct device_node *np;
1234 struct platform_device *pdev;
1235
1236 np = of_find_node_by_path(path);
1237 if (np == NULL)
1238 return NULL;
1239
1240 pdev = of_find_device_by_node(np);
1241 of_node_put(np);
1242
1243 return pdev;
1244}
1245
1246/* find out if a platform device exists at that path */
1247static int of_path_platform_device_exists(const char *path)
1248{
1249 struct platform_device *pdev;
1250
1251 pdev = of_path_to_platform_device(path);
1252 platform_device_put(pdev);
1253 return pdev != NULL;
1254}
1255
Arnd Bergmann4252de32015-03-04 20:49:47 +01001256#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001257
1258/* get the i2c client device instantiated at the path */
1259static struct i2c_client *of_path_to_i2c_client(const char *path)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001260{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001261 struct device_node *np;
1262 struct i2c_client *client;
1263
1264 np = of_find_node_by_path(path);
1265 if (np == NULL)
1266 return NULL;
1267
1268 client = of_find_i2c_device_by_node(np);
1269 of_node_put(np);
1270
1271 return client;
1272}
1273
1274/* find out if a i2c client device exists at that path */
1275static int of_path_i2c_client_exists(const char *path)
1276{
1277 struct i2c_client *client;
1278
1279 client = of_path_to_i2c_client(path);
1280 if (client)
1281 put_device(&client->dev);
1282 return client != NULL;
1283}
1284#else
1285static int of_path_i2c_client_exists(const char *path)
1286{
1287 return 0;
1288}
1289#endif
1290
1291enum overlay_type {
1292 PDEV_OVERLAY,
1293 I2C_OVERLAY
1294};
1295
1296static int of_path_device_type_exists(const char *path,
1297 enum overlay_type ovtype)
1298{
1299 switch (ovtype) {
1300 case PDEV_OVERLAY:
1301 return of_path_platform_device_exists(path);
1302 case I2C_OVERLAY:
1303 return of_path_i2c_client_exists(path);
1304 }
1305 return 0;
1306}
1307
Wang Long9697a552015-03-11 08:36:54 +00001308static const char *unittest_path(int nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001309{
1310 const char *base;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001311 static char buf[256];
1312
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001313 switch (ovtype) {
1314 case PDEV_OVERLAY:
1315 base = "/testcase-data/overlay-node/test-bus";
1316 break;
1317 case I2C_OVERLAY:
1318 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1319 break;
1320 default:
1321 buf[0] = '\0';
1322 return buf;
1323 }
Wang Long9697a552015-03-11 08:36:54 +00001324 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001325 buf[sizeof(buf) - 1] = '\0';
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001326 return buf;
1327}
1328
Wang Long9697a552015-03-11 08:36:54 +00001329static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001330{
1331 const char *path;
1332
Wang Long9697a552015-03-11 08:36:54 +00001333 path = unittest_path(unittest_nr, ovtype);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001334
1335 switch (ovtype) {
1336 case PDEV_OVERLAY:
1337 return of_path_platform_device_exists(path);
1338 case I2C_OVERLAY:
1339 return of_path_i2c_client_exists(path);
1340 }
1341 return 0;
1342}
1343
Frank Rowand39a751a2018-02-12 00:19:42 -08001344static const char *overlay_name_from_nr(int nr)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001345{
1346 static char buf[256];
1347
1348 snprintf(buf, sizeof(buf) - 1,
Frank Rowand39a751a2018-02-12 00:19:42 -08001349 "overlay_%d", nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001350 buf[sizeof(buf) - 1] = '\0';
1351
1352 return buf;
1353}
1354
1355static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1356
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001357/* it is guaranteed that overlay ids are assigned in sequence */
1358#define MAX_UNITTEST_OVERLAYS 256
1359static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1360static int overlay_first_id = -1;
1361
1362static void of_unittest_track_overlay(int id)
1363{
1364 if (overlay_first_id < 0)
1365 overlay_first_id = id;
1366 id -= overlay_first_id;
1367
1368 /* we shouldn't need that many */
1369 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1370 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1371}
1372
1373static void of_unittest_untrack_overlay(int id)
1374{
1375 if (overlay_first_id < 0)
1376 return;
1377 id -= overlay_first_id;
1378 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1379 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1380}
1381
1382static void of_unittest_destroy_tracked_overlays(void)
1383{
Frank Rowand24789c52017-10-17 16:36:26 -07001384 int id, ret, defers, ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001385
1386 if (overlay_first_id < 0)
1387 return;
1388
1389 /* try until no defers */
1390 do {
1391 defers = 0;
1392 /* remove in reverse order */
1393 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1394 if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1395 continue;
1396
Frank Rowand24789c52017-10-17 16:36:26 -07001397 ovcs_id = id + overlay_first_id;
1398 ret = of_overlay_remove(&ovcs_id);
Sergey Senozhatsky815d74b2016-03-02 20:24:49 +09001399 if (ret == -ENODEV) {
1400 pr_warn("%s: no overlay to destroy for #%d\n",
1401 __func__, id + overlay_first_id);
1402 continue;
1403 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001404 if (ret != 0) {
1405 defers++;
1406 pr_warn("%s: overlay destroy failed for #%d\n",
1407 __func__, id + overlay_first_id);
1408 continue;
1409 }
1410
1411 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1412 }
1413 } while (defers > 0);
1414}
1415
Frank Rowand39a751a2018-02-12 00:19:42 -08001416static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001417 int *overlay_id)
1418{
Frank Rowand39a751a2018-02-12 00:19:42 -08001419 const char *overlay_name;
Frank Rowand24789c52017-10-17 16:36:26 -07001420 int ret;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001421
Frank Rowand39a751a2018-02-12 00:19:42 -08001422 overlay_name = overlay_name_from_nr(overlay_nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001423
Frank Rowand39a751a2018-02-12 00:19:42 -08001424 ret = overlay_data_apply(overlay_name, overlay_id);
1425 if (!ret) {
1426 unittest(0, "could not apply overlay \"%s\"\n",
1427 overlay_name);
Frank Rowand54587be2018-03-08 14:39:05 -08001428 return ret;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001429 }
Frank Rowand24789c52017-10-17 16:36:26 -07001430 of_unittest_track_overlay(*overlay_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001431
Frank Rowand54587be2018-03-08 14:39:05 -08001432 return 0;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001433}
1434
1435/* apply an overlay while checking before and after states */
Frank Rowand39a751a2018-02-12 00:19:42 -08001436static int __init of_unittest_apply_overlay_check(int overlay_nr,
1437 int unittest_nr, int before, int after,
1438 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001439{
Frank Rowand24789c52017-10-17 16:36:26 -07001440 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001441
Wang Long9697a552015-03-11 08:36:54 +00001442 /* unittest device must not be in before state */
1443 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001444 unittest(0, "%s with device @\"%s\" %s\n",
1445 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001446 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001447 !before ? "enabled" : "disabled");
1448 return -EINVAL;
1449 }
1450
Frank Rowand24789c52017-10-17 16:36:26 -07001451 ovcs_id = 0;
1452 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001453 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001454 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001455 return ret;
1456 }
1457
Wang Long9697a552015-03-11 08:36:54 +00001458 /* unittest device must be to set to after state */
1459 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001460 unittest(0, "%s failed to create @\"%s\" %s\n",
1461 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001462 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001463 !after ? "enabled" : "disabled");
1464 return -EINVAL;
1465 }
1466
1467 return 0;
1468}
1469
1470/* apply an overlay and then revert it while checking before, after states */
Frank Rowand39a751a2018-02-12 00:19:42 -08001471static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
Wang Long9697a552015-03-11 08:36:54 +00001472 int unittest_nr, int before, int after,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001473 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001474{
Frank Rowand24789c52017-10-17 16:36:26 -07001475 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001476
Wang Long9697a552015-03-11 08:36:54 +00001477 /* unittest device must be in before state */
1478 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001479 unittest(0, "%s with device @\"%s\" %s\n",
1480 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001481 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001482 !before ? "enabled" : "disabled");
1483 return -EINVAL;
1484 }
1485
1486 /* apply the overlay */
Frank Rowand24789c52017-10-17 16:36:26 -07001487 ovcs_id = 0;
1488 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001489 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001490 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001491 return ret;
1492 }
1493
Wang Long9697a552015-03-11 08:36:54 +00001494 /* unittest device must be in after state */
1495 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001496 unittest(0, "%s failed to create @\"%s\" %s\n",
1497 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001498 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001499 !after ? "enabled" : "disabled");
1500 return -EINVAL;
1501 }
1502
Frank Rowand24789c52017-10-17 16:36:26 -07001503 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001504 if (ret != 0) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001505 unittest(0, "%s failed to be destroyed @\"%s\"\n",
1506 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001507 unittest_path(unittest_nr, ovtype));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001508 return ret;
1509 }
1510
Wang Long9697a552015-03-11 08:36:54 +00001511 /* unittest device must be again in before state */
1512 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001513 unittest(0, "%s with device @\"%s\" %s\n",
1514 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001515 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001516 !before ? "enabled" : "disabled");
1517 return -EINVAL;
1518 }
1519
1520 return 0;
1521}
1522
1523/* test activation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001524static void __init of_unittest_overlay_0(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001525{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001526 /* device should enable */
Frank Rowand06c46972018-03-08 14:39:04 -08001527 if (of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001528 return;
1529
Wang Long9697a552015-03-11 08:36:54 +00001530 unittest(1, "overlay test %d passed\n", 0);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001531}
1532
1533/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001534static void __init of_unittest_overlay_1(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001535{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001536 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08001537 if (of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001538 return;
1539
Wang Long9697a552015-03-11 08:36:54 +00001540 unittest(1, "overlay test %d passed\n", 1);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001541}
1542
1543/* test activation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001544static void __init of_unittest_overlay_2(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001545{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001546 /* device should enable */
Frank Rowand06c46972018-03-08 14:39:04 -08001547 if (of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001548 return;
1549
Wang Long9697a552015-03-11 08:36:54 +00001550 unittest(1, "overlay test %d passed\n", 2);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001551}
1552
1553/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001554static void __init of_unittest_overlay_3(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001555{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001556 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08001557 if (of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001558 return;
1559
Wang Long9697a552015-03-11 08:36:54 +00001560 unittest(1, "overlay test %d passed\n", 3);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001561}
1562
1563/* test activation of a full device node */
Frank Rowand39a751a2018-02-12 00:19:42 -08001564static void __init of_unittest_overlay_4(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001565{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001566 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08001567 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001568 return;
1569
Wang Long9697a552015-03-11 08:36:54 +00001570 unittest(1, "overlay test %d passed\n", 4);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001571}
1572
1573/* test overlay apply/revert sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08001574static void __init of_unittest_overlay_5(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001575{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001576 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08001577 if (of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001578 return;
1579
Wang Long9697a552015-03-11 08:36:54 +00001580 unittest(1, "overlay test %d passed\n", 5);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001581}
1582
1583/* test overlay application in sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08001584static void __init of_unittest_overlay_6(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001585{
Frank Rowand06c46972018-03-08 14:39:04 -08001586 int i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00001587 int overlay_nr = 6, unittest_nr = 6;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001588 int before = 0, after = 1;
Frank Rowand39a751a2018-02-12 00:19:42 -08001589 const char *overlay_name;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001590
Wang Long9697a552015-03-11 08:36:54 +00001591 /* unittest device must be in before state */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001592 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001593 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001594 != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001595 unittest(0, "%s with device @\"%s\" %s\n",
1596 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001597 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001598 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001599 !before ? "enabled" : "disabled");
1600 return;
1601 }
1602 }
1603
1604 /* apply the overlays */
1605 for (i = 0; i < 2; i++) {
1606
Frank Rowand39a751a2018-02-12 00:19:42 -08001607 overlay_name = overlay_name_from_nr(overlay_nr + i);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001608
Frank Rowand06c46972018-03-08 14:39:04 -08001609 if (!overlay_data_apply(overlay_name, &ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001610 unittest(0, "could not apply overlay \"%s\"\n",
1611 overlay_name);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001612 return;
1613 }
Frank Rowand24789c52017-10-17 16:36:26 -07001614 ov_id[i] = ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001615 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001616 }
1617
1618 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001619 /* unittest device must be in after state */
1620 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001621 != after) {
Wang Long9697a552015-03-11 08:36:54 +00001622 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
Frank Rowand39a751a2018-02-12 00:19:42 -08001623 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001624 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001625 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001626 !after ? "enabled" : "disabled");
1627 return;
1628 }
1629 }
1630
1631 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07001632 ovcs_id = ov_id[i];
Frank Rowand06c46972018-03-08 14:39:04 -08001633 if (of_overlay_remove(&ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001634 unittest(0, "%s failed destroy @\"%s\"\n",
1635 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001636 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001637 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001638 return;
1639 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001640 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001641 }
1642
1643 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001644 /* unittest device must be again in before state */
1645 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001646 != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001647 unittest(0, "%s with device @\"%s\" %s\n",
1648 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001649 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001650 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001651 !before ? "enabled" : "disabled");
1652 return;
1653 }
1654 }
1655
Wang Long9697a552015-03-11 08:36:54 +00001656 unittest(1, "overlay test %d passed\n", 6);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001657}
1658
1659/* test overlay application in sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08001660static void __init of_unittest_overlay_8(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001661{
Frank Rowand06c46972018-03-08 14:39:04 -08001662 int i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00001663 int overlay_nr = 8, unittest_nr = 8;
Frank Rowand39a751a2018-02-12 00:19:42 -08001664 const char *overlay_name;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001665
1666 /* we don't care about device state in this test */
1667
1668 /* apply the overlays */
1669 for (i = 0; i < 2; i++) {
1670
Frank Rowand39a751a2018-02-12 00:19:42 -08001671 overlay_name = overlay_name_from_nr(overlay_nr + i);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001672
Dan Carpenterbdb70132018-03-07 09:18:08 +03001673 if (!overlay_data_apply(overlay_name, &ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001674 unittest(0, "could not apply overlay \"%s\"\n",
1675 overlay_name);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001676 return;
1677 }
Frank Rowand24789c52017-10-17 16:36:26 -07001678 ov_id[i] = ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001679 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001680 }
1681
1682 /* now try to remove first overlay (it should fail) */
Frank Rowand24789c52017-10-17 16:36:26 -07001683 ovcs_id = ov_id[0];
Frank Rowand06c46972018-03-08 14:39:04 -08001684 if (!of_overlay_remove(&ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001685 unittest(0, "%s was destroyed @\"%s\"\n",
1686 overlay_name_from_nr(overlay_nr + 0),
Wang Long9697a552015-03-11 08:36:54 +00001687 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001688 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001689 return;
1690 }
1691
1692 /* removing them in order should work */
1693 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07001694 ovcs_id = ov_id[i];
Frank Rowand06c46972018-03-08 14:39:04 -08001695 if (of_overlay_remove(&ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001696 unittest(0, "%s not destroyed @\"%s\"\n",
1697 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001698 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001699 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001700 return;
1701 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001702 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001703 }
1704
Wang Long9697a552015-03-11 08:36:54 +00001705 unittest(1, "overlay test %d passed\n", 8);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001706}
1707
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001708/* test insertion of a bus with parent devices */
Frank Rowand39a751a2018-02-12 00:19:42 -08001709static void __init of_unittest_overlay_10(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001710{
1711 int ret;
1712 char *child_path;
1713
1714 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001715 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1716 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001717 "overlay test %d failed; overlay application\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001718 return;
1719
Wang Long9697a552015-03-11 08:36:54 +00001720 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1721 unittest_path(10, PDEV_OVERLAY));
1722 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001723 return;
1724
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001725 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001726 kfree(child_path);
Frank Rowand54587be2018-03-08 14:39:05 -08001727
1728 unittest(ret, "overlay test %d failed; no child device\n", 10);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001729}
1730
1731/* test insertion of a bus with parent devices (and revert) */
Frank Rowand39a751a2018-02-12 00:19:42 -08001732static void __init of_unittest_overlay_11(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001733{
1734 int ret;
1735
1736 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001737 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001738 PDEV_OVERLAY);
Frank Rowand54587be2018-03-08 14:39:05 -08001739 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001740}
1741
Arnd Bergmann4252de32015-03-04 20:49:47 +01001742#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001743
Wang Long9697a552015-03-11 08:36:54 +00001744struct unittest_i2c_bus_data {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001745 struct platform_device *pdev;
1746 struct i2c_adapter adap;
1747};
1748
Wang Long9697a552015-03-11 08:36:54 +00001749static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001750 struct i2c_msg *msgs, int num)
1751{
Wang Long9697a552015-03-11 08:36:54 +00001752 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001753
1754 (void)std;
1755
1756 return num;
1757}
1758
Wang Long9697a552015-03-11 08:36:54 +00001759static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001760{
1761 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1762}
1763
Wang Long9697a552015-03-11 08:36:54 +00001764static const struct i2c_algorithm unittest_i2c_algo = {
1765 .master_xfer = unittest_i2c_master_xfer,
1766 .functionality = unittest_i2c_functionality,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001767};
1768
Wang Long9697a552015-03-11 08:36:54 +00001769static int unittest_i2c_bus_probe(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001770{
1771 struct device *dev = &pdev->dev;
1772 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001773 struct unittest_i2c_bus_data *std;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001774 struct i2c_adapter *adap;
1775 int ret;
1776
1777 if (np == NULL) {
1778 dev_err(dev, "No OF data for device\n");
1779 return -EINVAL;
1780
1781 }
1782
Rob Herring0d638a02017-06-01 15:50:55 -05001783 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001784
1785 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1786 if (!std) {
Wang Long9697a552015-03-11 08:36:54 +00001787 dev_err(dev, "Failed to allocate unittest i2c data\n");
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001788 return -ENOMEM;
1789 }
1790
1791 /* link them together */
1792 std->pdev = pdev;
1793 platform_set_drvdata(pdev, std);
1794
1795 adap = &std->adap;
1796 i2c_set_adapdata(adap, std);
1797 adap->nr = -1;
1798 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1799 adap->class = I2C_CLASS_DEPRECATED;
Wang Long9697a552015-03-11 08:36:54 +00001800 adap->algo = &unittest_i2c_algo;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001801 adap->dev.parent = dev;
1802 adap->dev.of_node = dev->of_node;
1803 adap->timeout = 5 * HZ;
1804 adap->retries = 3;
1805
1806 ret = i2c_add_numbered_adapter(adap);
1807 if (ret != 0) {
1808 dev_err(dev, "Failed to add I2C adapter\n");
1809 return ret;
1810 }
1811
1812 return 0;
1813}
1814
Wang Long9697a552015-03-11 08:36:54 +00001815static int unittest_i2c_bus_remove(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001816{
1817 struct device *dev = &pdev->dev;
1818 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001819 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001820
Rob Herring0d638a02017-06-01 15:50:55 -05001821 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001822 i2c_del_adapter(&std->adap);
1823
1824 return 0;
1825}
1826
Grant Likelya2166ca2015-03-29 08:59:58 +01001827static const struct of_device_id unittest_i2c_bus_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001828 { .compatible = "unittest-i2c-bus", },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001829 {},
1830};
1831
Wang Long9697a552015-03-11 08:36:54 +00001832static struct platform_driver unittest_i2c_bus_driver = {
1833 .probe = unittest_i2c_bus_probe,
1834 .remove = unittest_i2c_bus_remove,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001835 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001836 .name = "unittest-i2c-bus",
1837 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001838 },
1839};
1840
Wang Long9697a552015-03-11 08:36:54 +00001841static int unittest_i2c_dev_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001842 const struct i2c_device_id *id)
1843{
1844 struct device *dev = &client->dev;
1845 struct device_node *np = client->dev.of_node;
1846
1847 if (!np) {
1848 dev_err(dev, "No OF node\n");
1849 return -EINVAL;
1850 }
1851
Rob Herring0d638a02017-06-01 15:50:55 -05001852 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001853
1854 return 0;
1855};
1856
Wang Long9697a552015-03-11 08:36:54 +00001857static int unittest_i2c_dev_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001858{
1859 struct device *dev = &client->dev;
1860 struct device_node *np = client->dev.of_node;
1861
Rob Herring0d638a02017-06-01 15:50:55 -05001862 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001863 return 0;
1864}
1865
Wang Long9697a552015-03-11 08:36:54 +00001866static const struct i2c_device_id unittest_i2c_dev_id[] = {
1867 { .name = "unittest-i2c-dev" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001868 { }
1869};
1870
Wang Long9697a552015-03-11 08:36:54 +00001871static struct i2c_driver unittest_i2c_dev_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001872 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001873 .name = "unittest-i2c-dev",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001874 },
Wang Long9697a552015-03-11 08:36:54 +00001875 .probe = unittest_i2c_dev_probe,
1876 .remove = unittest_i2c_dev_remove,
1877 .id_table = unittest_i2c_dev_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001878};
1879
Arnd Bergmann4252de32015-03-04 20:49:47 +01001880#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001881
Peter Rosinb6e3b712016-04-20 08:42:47 +02001882static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001883{
1884 return 0;
1885}
1886
Wang Long9697a552015-03-11 08:36:54 +00001887static int unittest_i2c_mux_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001888 const struct i2c_device_id *id)
1889{
Frank Rowand06c46972018-03-08 14:39:04 -08001890 int i, nchans;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001891 struct device *dev = &client->dev;
1892 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1893 struct device_node *np = client->dev.of_node, *child;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001894 struct i2c_mux_core *muxc;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001895 u32 reg, max_reg;
1896
Rob Herring0d638a02017-06-01 15:50:55 -05001897 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001898
1899 if (!np) {
1900 dev_err(dev, "No OF node\n");
1901 return -EINVAL;
1902 }
1903
1904 max_reg = (u32)-1;
1905 for_each_child_of_node(np, child) {
Frank Rowand06c46972018-03-08 14:39:04 -08001906 if (of_property_read_u32(child, "reg", &reg))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001907 continue;
1908 if (max_reg == (u32)-1 || reg > max_reg)
1909 max_reg = reg;
1910 }
1911 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1912 if (nchans == 0) {
1913 dev_err(dev, "No channels\n");
1914 return -EINVAL;
1915 }
1916
Peter Rosinb6e3b712016-04-20 08:42:47 +02001917 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
1918 unittest_i2c_mux_select_chan, NULL);
1919 if (!muxc)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001920 return -ENOMEM;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001921 for (i = 0; i < nchans; i++) {
Frank Rowand06c46972018-03-08 14:39:04 -08001922 if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001923 dev_err(dev, "Failed to register mux #%d\n", i);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001924 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001925 return -ENODEV;
1926 }
1927 }
1928
Peter Rosinb6e3b712016-04-20 08:42:47 +02001929 i2c_set_clientdata(client, muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001930
1931 return 0;
1932};
1933
Wang Long9697a552015-03-11 08:36:54 +00001934static int unittest_i2c_mux_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001935{
1936 struct device *dev = &client->dev;
1937 struct device_node *np = client->dev.of_node;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001938 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001939
Rob Herring0d638a02017-06-01 15:50:55 -05001940 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001941 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001942 return 0;
1943}
1944
Wang Long9697a552015-03-11 08:36:54 +00001945static const struct i2c_device_id unittest_i2c_mux_id[] = {
1946 { .name = "unittest-i2c-mux" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001947 { }
1948};
1949
Wang Long9697a552015-03-11 08:36:54 +00001950static struct i2c_driver unittest_i2c_mux_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001951 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001952 .name = "unittest-i2c-mux",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001953 },
Wang Long9697a552015-03-11 08:36:54 +00001954 .probe = unittest_i2c_mux_probe,
1955 .remove = unittest_i2c_mux_remove,
1956 .id_table = unittest_i2c_mux_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001957};
1958
1959#endif
1960
Wang Long9697a552015-03-11 08:36:54 +00001961static int of_unittest_overlay_i2c_init(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001962{
1963 int ret;
1964
Wang Long9697a552015-03-11 08:36:54 +00001965 ret = i2c_add_driver(&unittest_i2c_dev_driver);
1966 if (unittest(ret == 0,
1967 "could not register unittest i2c device driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001968 return ret;
1969
Wang Long9697a552015-03-11 08:36:54 +00001970 ret = platform_driver_register(&unittest_i2c_bus_driver);
1971 if (unittest(ret == 0,
1972 "could not register unittest i2c bus driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001973 return ret;
1974
Arnd Bergmann4252de32015-03-04 20:49:47 +01001975#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001976 ret = i2c_add_driver(&unittest_i2c_mux_driver);
1977 if (unittest(ret == 0,
1978 "could not register unittest i2c mux driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001979 return ret;
1980#endif
1981
1982 return 0;
1983}
1984
Wang Long9697a552015-03-11 08:36:54 +00001985static void of_unittest_overlay_i2c_cleanup(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001986{
Arnd Bergmann4252de32015-03-04 20:49:47 +01001987#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001988 i2c_del_driver(&unittest_i2c_mux_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001989#endif
Wang Long9697a552015-03-11 08:36:54 +00001990 platform_driver_unregister(&unittest_i2c_bus_driver);
1991 i2c_del_driver(&unittest_i2c_dev_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001992}
1993
Frank Rowand39a751a2018-02-12 00:19:42 -08001994static void __init of_unittest_overlay_i2c_12(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001995{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001996 /* device should enable */
Frank Rowand06c46972018-03-08 14:39:04 -08001997 if (of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001998 return;
1999
Wang Long9697a552015-03-11 08:36:54 +00002000 unittest(1, "overlay test %d passed\n", 12);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002001}
2002
2003/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08002004static void __init of_unittest_overlay_i2c_13(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002005{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002006 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08002007 if (of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002008 return;
2009
Wang Long9697a552015-03-11 08:36:54 +00002010 unittest(1, "overlay test %d passed\n", 13);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002011}
2012
2013/* just check for i2c mux existence */
Wang Long9697a552015-03-11 08:36:54 +00002014static void of_unittest_overlay_i2c_14(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002015{
2016}
2017
Frank Rowand39a751a2018-02-12 00:19:42 -08002018static void __init of_unittest_overlay_i2c_15(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002019{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002020 /* device should enable */
Frank Rowand06c46972018-03-08 14:39:04 -08002021 if (of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002022 return;
2023
Wang Long9697a552015-03-11 08:36:54 +00002024 unittest(1, "overlay test %d passed\n", 15);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002025}
2026
2027#else
2028
Wang Long9697a552015-03-11 08:36:54 +00002029static inline void of_unittest_overlay_i2c_14(void) { }
2030static inline void of_unittest_overlay_i2c_15(void) { }
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002031
2032#endif
2033
Wang Long9697a552015-03-11 08:36:54 +00002034static void __init of_unittest_overlay(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002035{
2036 struct device_node *bus_np = NULL;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002037
Frank Rowand06c46972018-03-08 14:39:04 -08002038 if (platform_driver_register(&unittest_driver)) {
Wang Long9697a552015-03-11 08:36:54 +00002039 unittest(0, "could not register unittest driver\n");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002040 goto out;
2041 }
2042
2043 bus_np = of_find_node_by_path(bus_path);
2044 if (bus_np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00002045 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002046 goto out;
2047 }
2048
Frank Rowand06c46972018-03-08 14:39:04 -08002049 if (of_platform_default_populate(bus_np, NULL, NULL)) {
Wang Long9697a552015-03-11 08:36:54 +00002050 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002051 goto out;
2052 }
2053
Wang Long9697a552015-03-11 08:36:54 +00002054 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
2055 unittest(0, "could not find unittest0 @ \"%s\"\n",
2056 unittest_path(100, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002057 goto out;
2058 }
2059
Wang Long9697a552015-03-11 08:36:54 +00002060 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
2061 unittest(0, "unittest1 @ \"%s\" should not exist\n",
2062 unittest_path(101, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002063 goto out;
2064 }
2065
Wang Long9697a552015-03-11 08:36:54 +00002066 unittest(1, "basic infrastructure of overlays passed");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002067
2068 /* tests in sequence */
Wang Long9697a552015-03-11 08:36:54 +00002069 of_unittest_overlay_0();
2070 of_unittest_overlay_1();
2071 of_unittest_overlay_2();
2072 of_unittest_overlay_3();
2073 of_unittest_overlay_4();
2074 of_unittest_overlay_5();
2075 of_unittest_overlay_6();
2076 of_unittest_overlay_8();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002077
Wang Long9697a552015-03-11 08:36:54 +00002078 of_unittest_overlay_10();
2079 of_unittest_overlay_11();
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002080
Arnd Bergmann4252de32015-03-04 20:49:47 +01002081#if IS_BUILTIN(CONFIG_I2C)
Wang Long9697a552015-03-11 08:36:54 +00002082 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002083 goto out;
2084
Wang Long9697a552015-03-11 08:36:54 +00002085 of_unittest_overlay_i2c_12();
2086 of_unittest_overlay_i2c_13();
2087 of_unittest_overlay_i2c_14();
2088 of_unittest_overlay_i2c_15();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002089
Wang Long9697a552015-03-11 08:36:54 +00002090 of_unittest_overlay_i2c_cleanup();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002091#endif
2092
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03002093 of_unittest_destroy_tracked_overlays();
2094
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002095out:
2096 of_node_put(bus_np);
2097}
2098
2099#else
Wang Long9697a552015-03-11 08:36:54 +00002100static inline void __init of_unittest_overlay(void) { }
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002101#endif
2102
Frank Rowand60a00042017-07-19 09:25:20 -07002103#ifdef CONFIG_OF_OVERLAY
2104
Frank Rowand81d0848f2017-04-25 17:09:54 -07002105/*
2106 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2107 * in scripts/Makefile.lib
2108 */
2109
2110#define OVERLAY_INFO_EXTERN(name) \
2111 extern uint8_t __dtb_##name##_begin[]; \
2112 extern uint8_t __dtb_##name##_end[]
2113
Frank Rowand39a751a2018-02-12 00:19:42 -08002114#define OVERLAY_INFO(overlay_name, expected) \
2115{ .dtb_begin = __dtb_##overlay_name##_begin, \
2116 .dtb_end = __dtb_##overlay_name##_end, \
2117 .expected_result = expected, \
2118 .name = #overlay_name, \
Frank Rowand81d0848f2017-04-25 17:09:54 -07002119}
2120
2121struct overlay_info {
Frank Rowand39a751a2018-02-12 00:19:42 -08002122 uint8_t *dtb_begin;
2123 uint8_t *dtb_end;
2124 int expected_result;
2125 int overlay_id;
2126 char *name;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002127};
2128
2129OVERLAY_INFO_EXTERN(overlay_base);
2130OVERLAY_INFO_EXTERN(overlay);
Frank Rowand39a751a2018-02-12 00:19:42 -08002131OVERLAY_INFO_EXTERN(overlay_0);
2132OVERLAY_INFO_EXTERN(overlay_1);
2133OVERLAY_INFO_EXTERN(overlay_2);
2134OVERLAY_INFO_EXTERN(overlay_3);
2135OVERLAY_INFO_EXTERN(overlay_4);
2136OVERLAY_INFO_EXTERN(overlay_5);
2137OVERLAY_INFO_EXTERN(overlay_6);
2138OVERLAY_INFO_EXTERN(overlay_7);
2139OVERLAY_INFO_EXTERN(overlay_8);
2140OVERLAY_INFO_EXTERN(overlay_9);
2141OVERLAY_INFO_EXTERN(overlay_10);
2142OVERLAY_INFO_EXTERN(overlay_11);
2143OVERLAY_INFO_EXTERN(overlay_12);
2144OVERLAY_INFO_EXTERN(overlay_13);
2145OVERLAY_INFO_EXTERN(overlay_15);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002146OVERLAY_INFO_EXTERN(overlay_bad_phandle);
Frank Rowand60a00042017-07-19 09:25:20 -07002147OVERLAY_INFO_EXTERN(overlay_bad_symbol);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002148
2149/* order of entries is hard-coded into users of overlays[] */
2150static struct overlay_info overlays[] = {
2151 OVERLAY_INFO(overlay_base, -9999),
2152 OVERLAY_INFO(overlay, 0),
Frank Rowand39a751a2018-02-12 00:19:42 -08002153 OVERLAY_INFO(overlay_0, 0),
2154 OVERLAY_INFO(overlay_1, 0),
2155 OVERLAY_INFO(overlay_2, 0),
2156 OVERLAY_INFO(overlay_3, 0),
2157 OVERLAY_INFO(overlay_4, 0),
2158 OVERLAY_INFO(overlay_5, 0),
2159 OVERLAY_INFO(overlay_6, 0),
2160 OVERLAY_INFO(overlay_7, 0),
2161 OVERLAY_INFO(overlay_8, 0),
2162 OVERLAY_INFO(overlay_9, 0),
2163 OVERLAY_INFO(overlay_10, 0),
2164 OVERLAY_INFO(overlay_11, 0),
2165 OVERLAY_INFO(overlay_12, 0),
2166 OVERLAY_INFO(overlay_13, 0),
2167 OVERLAY_INFO(overlay_15, 0),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002168 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
Frank Rowand60a00042017-07-19 09:25:20 -07002169 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002170 {}
2171};
2172
2173static struct device_node *overlay_base_root;
2174
Rob Herring0fa1c572018-01-05 15:32:33 -06002175static void * __init dt_alloc_memory(u64 size, u64 align)
2176{
2177 return memblock_virt_alloc(size, align);
2178}
2179
Frank Rowand81d0848f2017-04-25 17:09:54 -07002180/*
2181 * Create base device tree for the overlay unittest.
2182 *
2183 * This is called from very early boot code.
2184 *
2185 * Do as much as possible the same way as done in __unflatten_device_tree
2186 * and other early boot steps for the normal FDT so that the overlay base
2187 * unflattened tree will have the same characteristics as the real tree
2188 * (such as having memory allocated by the early allocator). The goal
2189 * is to test "the real thing" as much as possible, and test "test setup
2190 * code" as little as possible.
2191 *
2192 * Have to stop before resolving phandles, because that uses kmalloc.
2193 */
2194void __init unittest_unflatten_overlay_base(void)
2195{
2196 struct overlay_info *info;
2197 u32 data_size;
Frank Rowand39a751a2018-02-12 00:19:42 -08002198 void *new_fdt;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002199 u32 size;
2200
2201 info = &overlays[0];
2202
2203 if (info->expected_result != -9999) {
2204 pr_err("No dtb 'overlay_base' to attach\n");
2205 return;
2206 }
2207
2208 data_size = info->dtb_end - info->dtb_begin;
2209 if (!data_size) {
2210 pr_err("No dtb 'overlay_base' to attach\n");
2211 return;
2212 }
2213
2214 size = fdt_totalsize(info->dtb_begin);
2215 if (size != data_size) {
2216 pr_err("dtb 'overlay_base' header totalsize != actual size");
2217 return;
2218 }
2219
Frank Rowand39a751a2018-02-12 00:19:42 -08002220 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
2221 if (!new_fdt) {
Frank Rowand81d0848f2017-04-25 17:09:54 -07002222 pr_err("alloc for dtb 'overlay_base' failed");
2223 return;
2224 }
2225
Frank Rowand39a751a2018-02-12 00:19:42 -08002226 memcpy(new_fdt, info->dtb_begin, size);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002227
Frank Rowand39a751a2018-02-12 00:19:42 -08002228 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
Rob Herring0fa1c572018-01-05 15:32:33 -06002229 dt_alloc_memory, true);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002230}
2231
2232/*
2233 * The purpose of of_unittest_overlay_data_add is to add an
2234 * overlay in the normal fashion. This is a test of the whole
2235 * picture, instead of testing individual elements.
2236 *
2237 * A secondary purpose is to be able to verify that the contents of
2238 * /proc/device-tree/ contains the updated structure and values from
2239 * the overlay. That must be verified separately in user space.
2240 *
2241 * Return 0 on unexpected error.
2242 */
Frank Rowand39a751a2018-02-12 00:19:42 -08002243static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
Frank Rowand81d0848f2017-04-25 17:09:54 -07002244{
2245 struct overlay_info *info;
Frank Rowand39a751a2018-02-12 00:19:42 -08002246 int found = 0;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002247 int k;
2248 int ret;
2249 u32 size;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002250
Frank Rowand39a751a2018-02-12 00:19:42 -08002251 for (k = 0, info = overlays; info && info->name; info++, k++) {
2252 if (!strcmp(overlay_name, info->name)) {
2253 found = 1;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002254 break;
Frank Rowand39a751a2018-02-12 00:19:42 -08002255 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07002256 }
Frank Rowand39a751a2018-02-12 00:19:42 -08002257 if (!found) {
2258 pr_err("no overlay data for %s\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002259 return 0;
Frank Rowand39a751a2018-02-12 00:19:42 -08002260 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07002261
2262 size = info->dtb_end - info->dtb_begin;
Frank Rowand54587be2018-03-08 14:39:05 -08002263 if (!size)
Frank Rowand39a751a2018-02-12 00:19:42 -08002264 pr_err("no overlay data for %s\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002265
Frank Rowand39a751a2018-02-12 00:19:42 -08002266 ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
2267 if (overlay_id)
2268 *overlay_id = info->overlay_id;
2269 if (ret < 0)
2270 goto out;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002271
Frank Rowand39a751a2018-02-12 00:19:42 -08002272 pr_debug("%s applied\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002273
2274out:
Frank Rowand39a751a2018-02-12 00:19:42 -08002275 if (ret != info->expected_result)
2276 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
2277 info->expected_result, ret, overlay_name);
2278
Frank Rowand81d0848f2017-04-25 17:09:54 -07002279 return (ret == info->expected_result);
2280}
2281
2282/*
2283 * The purpose of of_unittest_overlay_high_level is to add an overlay
2284 * in the normal fashion. This is a test of the whole picture,
2285 * instead of individual elements.
2286 *
2287 * The first part of the function is _not_ normal overlay usage; it is
2288 * finishing splicing the base overlay device tree into the live tree.
2289 */
2290static __init void of_unittest_overlay_high_level(void)
2291{
2292 struct device_node *last_sibling;
2293 struct device_node *np;
2294 struct device_node *of_symbols;
2295 struct device_node *overlay_base_symbols;
2296 struct device_node **pprev;
2297 struct property *prop;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002298
2299 if (!overlay_base_root) {
2300 unittest(0, "overlay_base_root not initialized\n");
2301 return;
2302 }
2303
2304 /*
2305 * Could not fixup phandles in unittest_unflatten_overlay_base()
2306 * because kmalloc() was not yet available.
2307 */
Frank Rowandf948d6d2017-10-17 16:36:29 -07002308 of_overlay_mutex_lock();
Frank Rowand81d0848f2017-04-25 17:09:54 -07002309 of_resolve_phandles(overlay_base_root);
Frank Rowandf948d6d2017-10-17 16:36:29 -07002310 of_overlay_mutex_unlock();
2311
Frank Rowand81d0848f2017-04-25 17:09:54 -07002312
2313 /*
2314 * do not allow overlay_base to duplicate any node already in
2315 * tree, this greatly simplifies the code
2316 */
2317
2318 /*
2319 * remove overlay_base_root node "__local_fixups", after
2320 * being used by of_resolve_phandles()
2321 */
2322 pprev = &overlay_base_root->child;
2323 for (np = overlay_base_root->child; np; np = np->sibling) {
2324 if (!of_node_cmp(np->name, "__local_fixups__")) {
2325 *pprev = np->sibling;
2326 break;
2327 }
2328 pprev = &np->sibling;
2329 }
2330
2331 /* remove overlay_base_root node "__symbols__" if in live tree */
2332 of_symbols = of_get_child_by_name(of_root, "__symbols__");
2333 if (of_symbols) {
2334 /* will have to graft properties from node into live tree */
2335 pprev = &overlay_base_root->child;
2336 for (np = overlay_base_root->child; np; np = np->sibling) {
2337 if (!of_node_cmp(np->name, "__symbols__")) {
2338 overlay_base_symbols = np;
2339 *pprev = np->sibling;
2340 break;
2341 }
2342 pprev = &np->sibling;
2343 }
2344 }
2345
2346 for (np = overlay_base_root->child; np; np = np->sibling) {
2347 if (of_get_child_by_name(of_root, np->name)) {
2348 unittest(0, "illegal node name in overlay_base %s",
2349 np->name);
2350 return;
2351 }
2352 }
2353
2354 /*
2355 * overlay 'overlay_base' is not allowed to have root
2356 * properties, so only need to splice nodes into main device tree.
2357 *
2358 * root node of *overlay_base_root will not be freed, it is lost
2359 * memory.
2360 */
2361
2362 for (np = overlay_base_root->child; np; np = np->sibling)
2363 np->parent = of_root;
2364
2365 mutex_lock(&of_mutex);
2366
Arnd Bergmannee320b32017-04-28 11:44:11 +02002367 for (last_sibling = np = of_root->child; np; np = np->sibling)
Frank Rowand81d0848f2017-04-25 17:09:54 -07002368 last_sibling = np;
2369
2370 if (last_sibling)
2371 last_sibling->sibling = overlay_base_root->child;
2372 else
2373 of_root->child = overlay_base_root->child;
2374
2375 for_each_of_allnodes_from(overlay_base_root, np)
2376 __of_attach_node_sysfs(np);
2377
2378 if (of_symbols) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002379 struct property *new_prop;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002380 for_each_property_of_node(overlay_base_symbols, prop) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002381
2382 new_prop = __of_prop_dup(prop, GFP_KERNEL);
2383 if (!new_prop) {
2384 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
Frank Rowand81d0848f2017-04-25 17:09:54 -07002385 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002386 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002387 }
Frank Rowand06c46972018-03-08 14:39:04 -08002388 if (__of_add_property(of_symbols, new_prop)) {
2389 /* "name" auto-generated by unflatten */
2390 if (!strcmp(new_prop->name, "name"))
Frank Rowand39a751a2018-02-12 00:19:42 -08002391 continue;
Frank Rowand39a751a2018-02-12 00:19:42 -08002392 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
2393 prop->name);
2394 goto err_unlock;
2395 }
Frank Rowand06c46972018-03-08 14:39:04 -08002396 if (__of_add_property_sysfs(of_symbols, new_prop)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002397 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
Frank Rowand81d0848f2017-04-25 17:09:54 -07002398 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002399 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002400 }
2401 }
2402 }
2403
2404 mutex_unlock(&of_mutex);
2405
2406
2407 /* now do the normal overlay usage test */
2408
Frank Rowand39a751a2018-02-12 00:19:42 -08002409 unittest(overlay_data_apply("overlay", NULL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002410 "Adding overlay 'overlay' failed\n");
2411
Frank Rowand39a751a2018-02-12 00:19:42 -08002412 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002413 "Adding overlay 'overlay_bad_phandle' failed\n");
Frank Rowand60a00042017-07-19 09:25:20 -07002414
Frank Rowand39a751a2018-02-12 00:19:42 -08002415 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
Frank Rowand60a00042017-07-19 09:25:20 -07002416 "Adding overlay 'overlay_bad_symbol' failed\n");
2417
Dan Carpenter8756cd12017-05-03 22:49:50 +03002418 return;
2419
2420err_unlock:
2421 mutex_unlock(&of_mutex);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002422}
2423
2424#else
2425
2426static inline __init void of_unittest_overlay_high_level(void) {}
2427
2428#endif
2429
Wang Long9697a552015-03-11 08:36:54 +00002430static int __init of_unittest(void)
Grant Likely53a42092011-12-12 09:25:57 -07002431{
2432 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002433 int res;
2434
Wang Long9697a552015-03-11 08:36:54 +00002435 /* adding data for unittest */
2436 res = unittest_data_add();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002437 if (res)
2438 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00002439 if (!of_aliases)
2440 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07002441
2442 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2443 if (!np) {
2444 pr_info("No testcase data in device tree; not running tests\n");
2445 return 0;
2446 }
2447 of_node_put(np);
2448
Wang Long9697a552015-03-11 08:36:54 +00002449 pr_info("start of unittest - you will see error messages\n");
2450 of_unittest_check_tree_linkage();
2451 of_unittest_check_phandles();
2452 of_unittest_find_node_by_name();
2453 of_unittest_dynamic();
2454 of_unittest_parse_phandle_with_args();
Stephen Boyd357aa4b2018-01-30 18:36:17 -08002455 of_unittest_parse_phandle_with_args_map();
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002456 of_unittest_printf();
Wang Long9697a552015-03-11 08:36:54 +00002457 of_unittest_property_string();
2458 of_unittest_property_copy();
2459 of_unittest_changeset();
2460 of_unittest_parse_interrupts();
2461 of_unittest_parse_interrupts_extended();
2462 of_unittest_match_node();
2463 of_unittest_platform_populate();
2464 of_unittest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002465
Grant Likelyf2051d62014-10-01 17:40:22 +01002466 /* Double check linkage after removing testcase data */
Wang Long9697a552015-03-11 08:36:54 +00002467 of_unittest_check_tree_linkage();
Grant Likelyf2051d62014-10-01 17:40:22 +01002468
Frank Rowand81d0848f2017-04-25 17:09:54 -07002469 of_unittest_overlay_high_level();
2470
Wang Long9697a552015-03-11 08:36:54 +00002471 pr_info("end of unittest - %i passed, %i failed\n",
2472 unittest_results.passed, unittest_results.failed);
Grant Likelyf2051d62014-10-01 17:40:22 +01002473
Grant Likely53a42092011-12-12 09:25:57 -07002474 return 0;
2475}
Wang Long9697a552015-03-11 08:36:54 +00002476late_initcall(of_unittest);