blob: a23b54780c7dff05593a207ca30fd3b3c2d6e42e [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
Wang Long9697a552015-03-11 08:36:54 +0000458static void __init of_unittest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700459{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000460 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700461 struct device_node *np;
462 int rc;
463
Grant Likely7aff0fe2011-12-12 09:25:58 -0700464 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
465 if (!np) {
466 pr_err("No testcase data in device tree\n");
467 return;
468 }
469
470 rc = of_property_match_string(np, "phandle-list-names", "first");
Wang Long9697a552015-03-11 08:36:54 +0000471 unittest(rc == 0, "first expected:0 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700472 rc = of_property_match_string(np, "phandle-list-names", "second");
Wang Long9697a552015-03-11 08:36:54 +0000473 unittest(rc == 1, "second expected:1 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700474 rc = of_property_match_string(np, "phandle-list-names", "third");
Wang Long9697a552015-03-11 08:36:54 +0000475 unittest(rc == 2, "third expected:2 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700476 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Wang Long9697a552015-03-11 08:36:54 +0000477 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700478 rc = of_property_match_string(np, "missing-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000479 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700480 rc = of_property_match_string(np, "empty-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000481 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700482 rc = of_property_match_string(np, "unterminated-string", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000483 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000484
485 /* of_property_count_strings() tests */
486 rc = of_property_count_strings(np, "string-property");
Wang Long9697a552015-03-11 08:36:54 +0000487 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000488 rc = of_property_count_strings(np, "phandle-list-names");
Wang Long9697a552015-03-11 08:36:54 +0000489 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000490 rc = of_property_count_strings(np, "unterminated-string");
Wang Long9697a552015-03-11 08:36:54 +0000491 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000492 rc = of_property_count_strings(np, "unterminated-string-list");
Wang Long9697a552015-03-11 08:36:54 +0000493 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000494
495 /* of_property_read_string_index() tests */
496 rc = of_property_read_string_index(np, "string-property", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000497 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000498 strings[0] = NULL;
499 rc = of_property_read_string_index(np, "string-property", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000500 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000501 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000502 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000503 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000504 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000505 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
Wang Long9697a552015-03-11 08:36:54 +0000506 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000507 strings[0] = NULL;
508 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
Wang Long9697a552015-03-11 08:36:54 +0000509 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000510 strings[0] = NULL;
511 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000512 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000513 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000514 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000515 strings[0] = NULL;
516 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
Wang Long9697a552015-03-11 08:36:54 +0000517 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000518 strings[1] = NULL;
519
520 /* of_property_read_string_array() tests */
521 rc = of_property_read_string_array(np, "string-property", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000522 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000523 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000524 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000525 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000526 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000527 /* -- An incorrectly formed string should cause a failure */
528 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000529 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000530 /* -- parsing the correctly formed strings should still work: */
531 strings[2] = NULL;
532 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
Wang Long9697a552015-03-11 08:36:54 +0000533 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000534 strings[1] = NULL;
535 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
Wang Long9697a552015-03-11 08:36:54 +0000536 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 -0700537}
538
Pantelis Antoniou69843392014-07-04 19:58:47 +0300539#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
540 (p1)->value && (p2)->value && \
541 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
542 !strcmp((p1)->name, (p2)->name))
Wang Long9697a552015-03-11 08:36:54 +0000543static void __init of_unittest_property_copy(void)
Pantelis Antoniou69843392014-07-04 19:58:47 +0300544{
545#ifdef CONFIG_OF_DYNAMIC
546 struct property p1 = { .name = "p1", .length = 0, .value = "" };
547 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
548 struct property *new;
549
550 new = __of_prop_dup(&p1, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000551 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300552 kfree(new->value);
553 kfree(new->name);
554 kfree(new);
555
556 new = __of_prop_dup(&p2, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000557 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300558 kfree(new->value);
559 kfree(new->name);
560 kfree(new);
561#endif
562}
563
Wang Long9697a552015-03-11 08:36:54 +0000564static void __init of_unittest_changeset(void)
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300565{
566#ifdef CONFIG_OF_DYNAMIC
567 struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" };
568 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
569 struct property *ppremove;
Grant Likelye5179582014-11-17 22:31:32 +0000570 struct device_node *n1, *n2, *n21, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300571 struct of_changeset chgset;
572
Grant Likelye5179582014-11-17 22:31:32 +0000573 n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1");
Wang Long9697a552015-03-11 08:36:54 +0000574 unittest(n1, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000575 n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2");
Wang Long9697a552015-03-11 08:36:54 +0000576 unittest(n2, "testcase setup failure\n");
Grant Likelye5179582014-11-17 22:31:32 +0000577 n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21");
Wang Long9697a552015-03-11 08:36:54 +0000578 unittest(n21, "testcase setup failure %p\n", n21);
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300579 nremove = of_find_node_by_path("/testcase-data/changeset/node-remove");
Wang Long9697a552015-03-11 08:36:54 +0000580 unittest(nremove, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300581 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000582 unittest(ppadd, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300583 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000584 unittest(ppupdate, "testcase setup failure\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300585 parent = nremove->parent;
586 n1->parent = parent;
587 n2->parent = parent;
588 n21->parent = n2;
589 n2->child = n21;
590 ppremove = of_find_property(parent, "prop-remove", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000591 unittest(ppremove, "failed to find removal prop");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300592
593 of_changeset_init(&chgset);
Wang Long9697a552015-03-11 08:36:54 +0000594 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
595 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
596 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
597 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
598 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n");
599 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
600 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
Wang Long9697a552015-03-11 08:36:54 +0000601 unittest(!of_changeset_apply(&chgset), "apply failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300602
Grant Likelye5179582014-11-17 22:31:32 +0000603 /* Make sure node names are constructed correctly */
Wang Long9697a552015-03-11 08:36:54 +0000604 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
Rob Herring0d638a02017-06-01 15:50:55 -0500605 "'%pOF' not added\n", n21);
Markus Elfringc46ca3c2014-12-02 13:54:00 +0100606 of_node_put(np);
Grant Likelye5179582014-11-17 22:31:32 +0000607
Wang Long9697a552015-03-11 08:36:54 +0000608 unittest(!of_changeset_revert(&chgset), "revert failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300609
610 of_changeset_destroy(&chgset);
611#endif
612}
613
Wang Long9697a552015-03-11 08:36:54 +0000614static void __init of_unittest_parse_interrupts(void)
Grant Likelya9f10ca2013-10-11 22:04:23 +0100615{
616 struct device_node *np;
617 struct of_phandle_args args;
618 int i, rc;
619
620 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
621 if (!np) {
622 pr_err("missing testcase data\n");
623 return;
624 }
625
626 for (i = 0; i < 4; i++) {
627 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700628
Grant Likelya9f10ca2013-10-11 22:04:23 +0100629 args.args_count = 0;
630 rc = of_irq_parse_one(np, i, &args);
631
632 passed &= !rc;
633 passed &= (args.args_count == 1);
634 passed &= (args.args[0] == (i + 1));
635
Rob Herring0d638a02017-06-01 15:50:55 -0500636 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
637 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100638 }
639 of_node_put(np);
640
641 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
642 if (!np) {
643 pr_err("missing testcase data\n");
644 return;
645 }
646
647 for (i = 0; i < 4; i++) {
648 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700649
Grant Likelya9f10ca2013-10-11 22:04:23 +0100650 args.args_count = 0;
651 rc = of_irq_parse_one(np, i, &args);
652
653 /* Test the values from tests-phandle.dtsi */
654 switch (i) {
655 case 0:
656 passed &= !rc;
657 passed &= (args.args_count == 1);
658 passed &= (args.args[0] == 9);
659 break;
660 case 1:
661 passed &= !rc;
662 passed &= (args.args_count == 3);
663 passed &= (args.args[0] == 10);
664 passed &= (args.args[1] == 11);
665 passed &= (args.args[2] == 12);
666 break;
667 case 2:
668 passed &= !rc;
669 passed &= (args.args_count == 2);
670 passed &= (args.args[0] == 13);
671 passed &= (args.args[1] == 14);
672 break;
673 case 3:
674 passed &= !rc;
675 passed &= (args.args_count == 2);
676 passed &= (args.args[0] == 15);
677 passed &= (args.args[1] == 16);
678 break;
679 default:
680 passed = false;
681 }
Rob Herring0d638a02017-06-01 15:50:55 -0500682 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
683 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +0100684 }
685 of_node_put(np);
686}
687
Wang Long9697a552015-03-11 08:36:54 +0000688static void __init of_unittest_parse_interrupts_extended(void)
Grant Likely79d97012013-09-19 16:47:37 -0500689{
690 struct device_node *np;
691 struct of_phandle_args args;
692 int i, rc;
693
694 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
695 if (!np) {
696 pr_err("missing testcase data\n");
697 return;
698 }
699
700 for (i = 0; i < 7; i++) {
701 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700702
Grant Likely79d97012013-09-19 16:47:37 -0500703 rc = of_irq_parse_one(np, i, &args);
704
705 /* Test the values from tests-phandle.dtsi */
706 switch (i) {
707 case 0:
708 passed &= !rc;
709 passed &= (args.args_count == 1);
710 passed &= (args.args[0] == 1);
711 break;
712 case 1:
713 passed &= !rc;
714 passed &= (args.args_count == 3);
715 passed &= (args.args[0] == 2);
716 passed &= (args.args[1] == 3);
717 passed &= (args.args[2] == 4);
718 break;
719 case 2:
720 passed &= !rc;
721 passed &= (args.args_count == 2);
722 passed &= (args.args[0] == 5);
723 passed &= (args.args[1] == 6);
724 break;
725 case 3:
726 passed &= !rc;
727 passed &= (args.args_count == 1);
728 passed &= (args.args[0] == 9);
729 break;
730 case 4:
731 passed &= !rc;
732 passed &= (args.args_count == 3);
733 passed &= (args.args[0] == 10);
734 passed &= (args.args[1] == 11);
735 passed &= (args.args[2] == 12);
736 break;
737 case 5:
738 passed &= !rc;
739 passed &= (args.args_count == 2);
740 passed &= (args.args[0] == 13);
741 passed &= (args.args[1] == 14);
742 break;
743 case 6:
744 passed &= !rc;
745 passed &= (args.args_count == 1);
746 passed &= (args.args[0] == 15);
747 break;
748 default:
749 passed = false;
750 }
751
Rob Herring0d638a02017-06-01 15:50:55 -0500752 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
753 i, args.np, rc);
Grant Likely79d97012013-09-19 16:47:37 -0500754 }
755 of_node_put(np);
756}
757
Frank Rowandafaed7a2015-03-14 00:00:36 -0700758static const struct of_device_id match_node_table[] = {
Grant Likely1f42e5d2014-02-18 21:38:55 +0000759 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
760 { .data = "B", .type = "type1", }, /* followed by type alone */
761
762 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
763 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
764 { .data = "Cc", .name = "name2", .type = "type2", },
765
766 { .data = "E", .compatible = "compat3" },
767 { .data = "G", .compatible = "compat2", },
768 { .data = "H", .compatible = "compat2", .name = "name5", },
769 { .data = "I", .compatible = "compat2", .type = "type1", },
770 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
771 { .data = "K", .compatible = "compat2", .name = "name9", },
772 {}
773};
774
775static struct {
776 const char *path;
777 const char *data;
778} match_node_tests[] = {
779 { .path = "/testcase-data/match-node/name0", .data = "A", },
780 { .path = "/testcase-data/match-node/name1", .data = "B", },
781 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
782 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
783 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
784 { .path = "/testcase-data/match-node/name3", .data = "E", },
785 { .path = "/testcase-data/match-node/name4", .data = "G", },
786 { .path = "/testcase-data/match-node/name5", .data = "H", },
787 { .path = "/testcase-data/match-node/name6", .data = "G", },
788 { .path = "/testcase-data/match-node/name7", .data = "I", },
789 { .path = "/testcase-data/match-node/name8", .data = "J", },
790 { .path = "/testcase-data/match-node/name9", .data = "K", },
791};
792
Wang Long9697a552015-03-11 08:36:54 +0000793static void __init of_unittest_match_node(void)
Grant Likely1f42e5d2014-02-18 21:38:55 +0000794{
795 struct device_node *np;
796 const struct of_device_id *match;
797 int i;
798
799 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
800 np = of_find_node_by_path(match_node_tests[i].path);
801 if (!np) {
Wang Long9697a552015-03-11 08:36:54 +0000802 unittest(0, "missing testcase node %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000803 match_node_tests[i].path);
804 continue;
805 }
806
807 match = of_match_node(match_node_table, np);
808 if (!match) {
Wang Long9697a552015-03-11 08:36:54 +0000809 unittest(0, "%s didn't match anything\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000810 match_node_tests[i].path);
811 continue;
812 }
813
814 if (strcmp(match->data, match_node_tests[i].data) != 0) {
Wang Long9697a552015-03-11 08:36:54 +0000815 unittest(0, "%s got wrong match. expected %s, got %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +0000816 match_node_tests[i].path, match_node_tests[i].data,
817 (const char *)match->data);
818 continue;
819 }
Wang Long9697a552015-03-11 08:36:54 +0000820 unittest(1, "passed");
Grant Likely1f42e5d2014-02-18 21:38:55 +0000821 }
822}
823
Grant Likelyd2329fb2016-01-04 13:13:21 +0100824static struct resource test_bus_res = {
825 .start = 0xfffffff8,
826 .end = 0xfffffff9,
827 .flags = IORESOURCE_MEM,
828};
Grant Likely37791b62015-03-27 20:30:04 -0700829static const struct platform_device_info test_bus_info = {
830 .name = "unittest-bus",
Grant Likely851da972014-11-04 13:14:13 +0000831};
Wang Long9697a552015-03-11 08:36:54 +0000832static void __init of_unittest_platform_populate(void)
Rob Herring82c0f582014-04-23 17:57:40 -0500833{
Grant Likely851da972014-11-04 13:14:13 +0000834 int irq, rc;
835 struct device_node *np, *child, *grandchild;
Grant Likely37791b62015-03-27 20:30:04 -0700836 struct platform_device *pdev, *test_bus;
Frank Rowandafaed7a2015-03-14 00:00:36 -0700837 const struct of_device_id match[] = {
Rob Herringfb2caa52014-05-13 10:07:54 -0500838 { .compatible = "test-device", },
839 {}
840 };
Rob Herring82c0f582014-04-23 17:57:40 -0500841
842 np = of_find_node_by_path("/testcase-data");
Kefeng Wang146dedb2016-06-01 14:53:10 +0800843 of_platform_default_populate(np, NULL, NULL);
Rob Herring82c0f582014-04-23 17:57:40 -0500844
845 /* Test that a missing irq domain returns -EPROBE_DEFER */
846 np = of_find_node_by_path("/testcase-data/testcase-device1");
847 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +0000848 unittest(pdev, "device 1 creation failed\n");
Rob Herring7d1cdc82014-05-13 10:07:29 -0500849
Rob Herring82c0f582014-04-23 17:57:40 -0500850 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +0000851 unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500852
853 /* Test that a parsing failure does not return -EPROBE_DEFER */
854 np = of_find_node_by_path("/testcase-data/testcase-device2");
855 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +0000856 unittest(pdev, "device 2 creation failed\n");
Rob Herring82c0f582014-04-23 17:57:40 -0500857 irq = platform_get_irq(pdev, 0);
Wang Long9697a552015-03-11 08:36:54 +0000858 unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -0500859
Frank Rowand716e1d42015-03-13 23:57:40 -0700860 np = of_find_node_by_path("/testcase-data/platform-tests");
Grant Likelya2166ca2015-03-29 08:59:58 +0100861 unittest(np, "No testcase data in device tree\n");
Frank Rowand716e1d42015-03-13 23:57:40 -0700862 if (!np)
Rob Herringfb2caa52014-05-13 10:07:54 -0500863 return;
Grant Likely851da972014-11-04 13:14:13 +0000864
Grant Likely37791b62015-03-27 20:30:04 -0700865 test_bus = platform_device_register_full(&test_bus_info);
866 rc = PTR_ERR_OR_ZERO(test_bus);
Grant Likelya2166ca2015-03-29 08:59:58 +0100867 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
Frank Rowand716e1d42015-03-13 23:57:40 -0700868 if (rc)
Grant Likely851da972014-11-04 13:14:13 +0000869 return;
Grant Likely37791b62015-03-27 20:30:04 -0700870 test_bus->dev.of_node = np;
Rob Herringfb2caa52014-05-13 10:07:54 -0500871
Grant Likelyd2329fb2016-01-04 13:13:21 +0100872 /*
873 * Add a dummy resource to the test bus node after it is
874 * registered to catch problems with un-inserted resources. The
875 * DT code doesn't insert the resources, and it has caused the
876 * kernel to oops in the past. This makes sure the same bug
877 * doesn't crop up again.
878 */
879 platform_device_add_resources(test_bus, &test_bus_res, 1);
880
Grant Likely37791b62015-03-27 20:30:04 -0700881 of_platform_populate(np, match, NULL, &test_bus->dev);
Rob Herringfb2caa52014-05-13 10:07:54 -0500882 for_each_child_of_node(np, child) {
Rob Herringfb2caa52014-05-13 10:07:54 -0500883 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +0000884 unittest(of_find_device_by_node(grandchild),
Rob Herringfb2caa52014-05-13 10:07:54 -0500885 "Could not create device for node '%s'\n",
886 grandchild->name);
887 }
Grant Likely851da972014-11-04 13:14:13 +0000888
Grant Likely37791b62015-03-27 20:30:04 -0700889 of_platform_depopulate(&test_bus->dev);
Grant Likely851da972014-11-04 13:14:13 +0000890 for_each_child_of_node(np, child) {
891 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +0000892 unittest(!of_find_device_by_node(grandchild),
Grant Likely851da972014-11-04 13:14:13 +0000893 "device didn't get destroyed '%s'\n",
894 grandchild->name);
895 }
896
Grant Likely37791b62015-03-27 20:30:04 -0700897 platform_device_unregister(test_bus);
Grant Likely851da972014-11-04 13:14:13 +0000898 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -0500899}
900
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700901/**
902 * update_node_properties - adds the properties
903 * of np into dup node (present in live tree) and
904 * updates parent of children of np to dup.
905 *
906 * @np: node already present in live tree
907 * @dup: node present in live tree to be updated
908 */
909static void update_node_properties(struct device_node *np,
910 struct device_node *dup)
911{
912 struct property *prop;
913 struct device_node *child;
914
915 for_each_property_of_node(np, prop)
916 of_add_property(dup, prop);
917
918 for_each_child_of_node(np, child)
919 child->parent = dup;
920}
921
922/**
923 * attach_node_and_children - attaches nodes
924 * and its children to live tree
925 *
926 * @np: Node to attach to live tree
927 */
928static int attach_node_and_children(struct device_node *np)
929{
Grant Likely5063e252014-10-03 16:28:27 +0100930 struct device_node *next, *dup, *child;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800931 unsigned long flags;
Rob Herring0d638a02017-06-01 15:50:55 -0500932 const char *full_name;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700933
Rob Herring0d638a02017-06-01 15:50:55 -0500934 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
935 dup = of_find_node_by_path(full_name);
936 kfree(full_name);
Grant Likely5063e252014-10-03 16:28:27 +0100937 if (dup) {
938 update_node_properties(np, dup);
939 return 0;
940 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700941
Grant Likely5063e252014-10-03 16:28:27 +0100942 child = np->child;
943 np->child = NULL;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -0800944
945 mutex_lock(&of_mutex);
946 raw_spin_lock_irqsave(&devtree_lock, flags);
947 np->sibling = np->parent->child;
948 np->parent->child = np;
949 of_node_clear_flag(np, OF_DETACHED);
950 raw_spin_unlock_irqrestore(&devtree_lock, flags);
951
952 __of_attach_node_sysfs(np);
953 mutex_unlock(&of_mutex);
954
Grant Likely5063e252014-10-03 16:28:27 +0100955 while (child) {
956 next = child->sibling;
957 attach_node_and_children(child);
958 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700959 }
960
961 return 0;
962}
963
964/**
Wang Long9697a552015-03-11 08:36:54 +0000965 * unittest_data_add - Reads, copies data from
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700966 * linked tree and attaches it to the live tree
967 */
Wang Long9697a552015-03-11 08:36:54 +0000968static int __init unittest_data_add(void)
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700969{
Wang Long9697a552015-03-11 08:36:54 +0000970 void *unittest_data;
971 struct device_node *unittest_data_node, *np;
Frank Rowandc8547112015-03-14 00:04:24 -0700972 /*
973 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
974 * created by cmd_dt_S_dtb in scripts/Makefile.lib
975 */
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700976 extern uint8_t __dtb_testcases_begin[];
977 extern uint8_t __dtb_testcases_end[];
978 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +0100979 int rc;
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700980
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700981 if (!size) {
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700982 pr_warn("%s: No testcase data to attach; not running tests\n",
983 __func__);
984 return -ENODATA;
985 }
986
987 /* creating copy */
Wang Long9697a552015-03-11 08:36:54 +0000988 unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL);
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700989
Wang Long9697a552015-03-11 08:36:54 +0000990 if (!unittest_data) {
991 pr_warn("%s: Failed to allocate memory for unittest_data; "
Gaurav Minochaae9304c2014-07-16 23:09:39 -0700992 "not running tests\n", __func__);
993 return -ENOMEM;
994 }
Gavin Shanc4263232016-05-03 23:22:50 +1000995 of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node);
Wang Long9697a552015-03-11 08:36:54 +0000996 if (!unittest_data_node) {
Gaurav Minochab951f9d2014-07-26 12:48:50 -0700997 pr_warn("%s: No tree to attach; not running tests\n", __func__);
998 return -ENODATA;
999 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001000
1001 /*
Frank Rowand39a751a2018-02-12 00:19:42 -08001002 * This lock normally encloses of_resolve_phandles()
Frank Rowandf948d6d2017-10-17 16:36:29 -07001003 */
1004 of_overlay_mutex_lock();
1005
Wang Long9697a552015-03-11 08:36:54 +00001006 rc = of_resolve_phandles(unittest_data_node);
Grant Likely2eb46da2014-10-02 14:36:46 +01001007 if (rc) {
1008 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
Frank Rowandf948d6d2017-10-17 16:36:29 -07001009 of_overlay_mutex_unlock();
Grant Likely2eb46da2014-10-02 14:36:46 +01001010 return -EINVAL;
1011 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001012
Grant Likely5063e252014-10-03 16:28:27 +01001013 if (!of_root) {
Wang Long9697a552015-03-11 08:36:54 +00001014 of_root = unittest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001015 for_each_of_allnodes(np)
1016 __of_attach_node_sysfs(np);
1017 of_aliases = of_find_node_by_path("/aliases");
1018 of_chosen = of_find_node_by_path("/chosen");
Frank Rowandf948d6d2017-10-17 16:36:29 -07001019 of_overlay_mutex_unlock();
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001020 return 0;
1021 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001022
1023 /* attach the sub-tree to live tree */
Wang Long9697a552015-03-11 08:36:54 +00001024 np = unittest_data_node->child;
Grant Likely5063e252014-10-03 16:28:27 +01001025 while (np) {
1026 struct device_node *next = np->sibling;
Frank Rowand3db316d2015-03-14 00:02:31 -07001027
Grant Likely5063e252014-10-03 16:28:27 +01001028 np->parent = of_root;
1029 attach_node_and_children(np);
1030 np = next;
1031 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001032
1033 of_overlay_mutex_unlock();
1034
Grant Likely5063e252014-10-03 16:28:27 +01001035 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001036}
1037
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001038#ifdef CONFIG_OF_OVERLAY
1039
Wang Long9697a552015-03-11 08:36:54 +00001040static int unittest_probe(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001041{
1042 struct device *dev = &pdev->dev;
1043 struct device_node *np = dev->of_node;
1044
1045 if (np == NULL) {
1046 dev_err(dev, "No OF data for device\n");
1047 return -EINVAL;
1048
1049 }
1050
Rob Herring0d638a02017-06-01 15:50:55 -05001051 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001052
1053 of_platform_populate(np, NULL, NULL, &pdev->dev);
1054
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001055 return 0;
1056}
1057
Wang Long9697a552015-03-11 08:36:54 +00001058static int unittest_remove(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001059{
1060 struct device *dev = &pdev->dev;
1061 struct device_node *np = dev->of_node;
1062
Rob Herring0d638a02017-06-01 15:50:55 -05001063 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001064 return 0;
1065}
1066
Grant Likelya2166ca2015-03-29 08:59:58 +01001067static const struct of_device_id unittest_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001068 { .compatible = "unittest", },
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001069 {},
1070};
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001071
Wang Long9697a552015-03-11 08:36:54 +00001072static struct platform_driver unittest_driver = {
1073 .probe = unittest_probe,
1074 .remove = unittest_remove,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001075 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001076 .name = "unittest",
Wang Long9697a552015-03-11 08:36:54 +00001077 .of_match_table = of_match_ptr(unittest_match),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001078 },
1079};
1080
1081/* get the platform device instantiated at the path */
1082static struct platform_device *of_path_to_platform_device(const char *path)
1083{
1084 struct device_node *np;
1085 struct platform_device *pdev;
1086
1087 np = of_find_node_by_path(path);
1088 if (np == NULL)
1089 return NULL;
1090
1091 pdev = of_find_device_by_node(np);
1092 of_node_put(np);
1093
1094 return pdev;
1095}
1096
1097/* find out if a platform device exists at that path */
1098static int of_path_platform_device_exists(const char *path)
1099{
1100 struct platform_device *pdev;
1101
1102 pdev = of_path_to_platform_device(path);
1103 platform_device_put(pdev);
1104 return pdev != NULL;
1105}
1106
Arnd Bergmann4252de32015-03-04 20:49:47 +01001107#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001108
1109/* get the i2c client device instantiated at the path */
1110static struct i2c_client *of_path_to_i2c_client(const char *path)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001111{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001112 struct device_node *np;
1113 struct i2c_client *client;
1114
1115 np = of_find_node_by_path(path);
1116 if (np == NULL)
1117 return NULL;
1118
1119 client = of_find_i2c_device_by_node(np);
1120 of_node_put(np);
1121
1122 return client;
1123}
1124
1125/* find out if a i2c client device exists at that path */
1126static int of_path_i2c_client_exists(const char *path)
1127{
1128 struct i2c_client *client;
1129
1130 client = of_path_to_i2c_client(path);
1131 if (client)
1132 put_device(&client->dev);
1133 return client != NULL;
1134}
1135#else
1136static int of_path_i2c_client_exists(const char *path)
1137{
1138 return 0;
1139}
1140#endif
1141
1142enum overlay_type {
1143 PDEV_OVERLAY,
1144 I2C_OVERLAY
1145};
1146
1147static int of_path_device_type_exists(const char *path,
1148 enum overlay_type ovtype)
1149{
1150 switch (ovtype) {
1151 case PDEV_OVERLAY:
1152 return of_path_platform_device_exists(path);
1153 case I2C_OVERLAY:
1154 return of_path_i2c_client_exists(path);
1155 }
1156 return 0;
1157}
1158
Wang Long9697a552015-03-11 08:36:54 +00001159static const char *unittest_path(int nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001160{
1161 const char *base;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001162 static char buf[256];
1163
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001164 switch (ovtype) {
1165 case PDEV_OVERLAY:
1166 base = "/testcase-data/overlay-node/test-bus";
1167 break;
1168 case I2C_OVERLAY:
1169 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1170 break;
1171 default:
1172 buf[0] = '\0';
1173 return buf;
1174 }
Wang Long9697a552015-03-11 08:36:54 +00001175 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001176 buf[sizeof(buf) - 1] = '\0';
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001177 return buf;
1178}
1179
Wang Long9697a552015-03-11 08:36:54 +00001180static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001181{
1182 const char *path;
1183
Wang Long9697a552015-03-11 08:36:54 +00001184 path = unittest_path(unittest_nr, ovtype);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001185
1186 switch (ovtype) {
1187 case PDEV_OVERLAY:
1188 return of_path_platform_device_exists(path);
1189 case I2C_OVERLAY:
1190 return of_path_i2c_client_exists(path);
1191 }
1192 return 0;
1193}
1194
Frank Rowand39a751a2018-02-12 00:19:42 -08001195static const char *overlay_name_from_nr(int nr)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001196{
1197 static char buf[256];
1198
1199 snprintf(buf, sizeof(buf) - 1,
Frank Rowand39a751a2018-02-12 00:19:42 -08001200 "overlay_%d", nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001201 buf[sizeof(buf) - 1] = '\0';
1202
1203 return buf;
1204}
1205
1206static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1207
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001208/* it is guaranteed that overlay ids are assigned in sequence */
1209#define MAX_UNITTEST_OVERLAYS 256
1210static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1211static int overlay_first_id = -1;
1212
1213static void of_unittest_track_overlay(int id)
1214{
1215 if (overlay_first_id < 0)
1216 overlay_first_id = id;
1217 id -= overlay_first_id;
1218
1219 /* we shouldn't need that many */
1220 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1221 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1222}
1223
1224static void of_unittest_untrack_overlay(int id)
1225{
1226 if (overlay_first_id < 0)
1227 return;
1228 id -= overlay_first_id;
1229 BUG_ON(id >= MAX_UNITTEST_OVERLAYS);
1230 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1231}
1232
1233static void of_unittest_destroy_tracked_overlays(void)
1234{
Frank Rowand24789c52017-10-17 16:36:26 -07001235 int id, ret, defers, ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001236
1237 if (overlay_first_id < 0)
1238 return;
1239
1240 /* try until no defers */
1241 do {
1242 defers = 0;
1243 /* remove in reverse order */
1244 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
1245 if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id)))
1246 continue;
1247
Frank Rowand24789c52017-10-17 16:36:26 -07001248 ovcs_id = id + overlay_first_id;
1249 ret = of_overlay_remove(&ovcs_id);
Sergey Senozhatsky815d74b2016-03-02 20:24:49 +09001250 if (ret == -ENODEV) {
1251 pr_warn("%s: no overlay to destroy for #%d\n",
1252 __func__, id + overlay_first_id);
1253 continue;
1254 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001255 if (ret != 0) {
1256 defers++;
1257 pr_warn("%s: overlay destroy failed for #%d\n",
1258 __func__, id + overlay_first_id);
1259 continue;
1260 }
1261
1262 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1263 }
1264 } while (defers > 0);
1265}
1266
Frank Rowand39a751a2018-02-12 00:19:42 -08001267static int __init of_unittest_apply_overlay(int overlay_nr, int unittest_nr,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001268 int *overlay_id)
1269{
1270 struct device_node *np = NULL;
Frank Rowand39a751a2018-02-12 00:19:42 -08001271 const char *overlay_name;
Frank Rowand24789c52017-10-17 16:36:26 -07001272 int ret;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001273
Frank Rowand39a751a2018-02-12 00:19:42 -08001274 overlay_name = overlay_name_from_nr(overlay_nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001275
Frank Rowand39a751a2018-02-12 00:19:42 -08001276 ret = overlay_data_apply(overlay_name, overlay_id);
1277 if (!ret) {
1278 unittest(0, "could not apply overlay \"%s\"\n",
1279 overlay_name);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001280 goto out;
1281 }
Frank Rowand24789c52017-10-17 16:36:26 -07001282 of_unittest_track_overlay(*overlay_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001283
1284 ret = 0;
1285
1286out:
1287 of_node_put(np);
1288
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001289 return ret;
1290}
1291
1292/* apply an overlay while checking before and after states */
Frank Rowand39a751a2018-02-12 00:19:42 -08001293static int __init of_unittest_apply_overlay_check(int overlay_nr,
1294 int unittest_nr, int before, int after,
1295 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001296{
Frank Rowand24789c52017-10-17 16:36:26 -07001297 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001298
Wang Long9697a552015-03-11 08:36:54 +00001299 /* unittest device must not be in before state */
1300 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001301 unittest(0, "%s with device @\"%s\" %s\n",
1302 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001303 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001304 !before ? "enabled" : "disabled");
1305 return -EINVAL;
1306 }
1307
Frank Rowand24789c52017-10-17 16:36:26 -07001308 ovcs_id = 0;
1309 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001310 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001311 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001312 return ret;
1313 }
1314
Wang Long9697a552015-03-11 08:36:54 +00001315 /* unittest device must be to set to after state */
1316 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001317 unittest(0, "%s failed to create @\"%s\" %s\n",
1318 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001319 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001320 !after ? "enabled" : "disabled");
1321 return -EINVAL;
1322 }
1323
1324 return 0;
1325}
1326
1327/* apply an overlay and then revert it while checking before, after states */
Frank Rowand39a751a2018-02-12 00:19:42 -08001328static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
Wang Long9697a552015-03-11 08:36:54 +00001329 int unittest_nr, int before, int after,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001330 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001331{
Frank Rowand24789c52017-10-17 16:36:26 -07001332 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001333
Wang Long9697a552015-03-11 08:36:54 +00001334 /* unittest device must be in before state */
1335 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001336 unittest(0, "%s with device @\"%s\" %s\n",
1337 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001338 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001339 !before ? "enabled" : "disabled");
1340 return -EINVAL;
1341 }
1342
1343 /* apply the overlay */
Frank Rowand24789c52017-10-17 16:36:26 -07001344 ovcs_id = 0;
1345 ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001346 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001347 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001348 return ret;
1349 }
1350
Wang Long9697a552015-03-11 08:36:54 +00001351 /* unittest device must be in after state */
1352 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001353 unittest(0, "%s failed to create @\"%s\" %s\n",
1354 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001355 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001356 !after ? "enabled" : "disabled");
1357 return -EINVAL;
1358 }
1359
Frank Rowand24789c52017-10-17 16:36:26 -07001360 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001361 if (ret != 0) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001362 unittest(0, "%s failed to be destroyed @\"%s\"\n",
1363 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001364 unittest_path(unittest_nr, ovtype));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001365 return ret;
1366 }
1367
Wang Long9697a552015-03-11 08:36:54 +00001368 /* unittest device must be again in before state */
1369 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001370 unittest(0, "%s with device @\"%s\" %s\n",
1371 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00001372 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001373 !before ? "enabled" : "disabled");
1374 return -EINVAL;
1375 }
1376
1377 return 0;
1378}
1379
1380/* test activation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001381static void __init of_unittest_overlay_0(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001382{
1383 int ret;
1384
1385 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001386 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001387 if (ret != 0)
1388 return;
1389
Wang Long9697a552015-03-11 08:36:54 +00001390 unittest(1, "overlay test %d passed\n", 0);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001391}
1392
1393/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001394static void __init of_unittest_overlay_1(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001395{
1396 int ret;
1397
1398 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001399 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001400 if (ret != 0)
1401 return;
1402
Wang Long9697a552015-03-11 08:36:54 +00001403 unittest(1, "overlay test %d passed\n", 1);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001404}
1405
1406/* test activation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001407static void __init of_unittest_overlay_2(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001408{
1409 int ret;
1410
1411 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001412 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001413 if (ret != 0)
1414 return;
1415
Wang Long9697a552015-03-11 08:36:54 +00001416 unittest(1, "overlay test %d passed\n", 2);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001417}
1418
1419/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001420static void __init of_unittest_overlay_3(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001421{
1422 int ret;
1423
1424 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001425 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001426 if (ret != 0)
1427 return;
1428
Wang Long9697a552015-03-11 08:36:54 +00001429 unittest(1, "overlay test %d passed\n", 3);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001430}
1431
1432/* test activation of a full device node */
Frank Rowand39a751a2018-02-12 00:19:42 -08001433static void __init of_unittest_overlay_4(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001434{
1435 int ret;
1436
1437 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001438 ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001439 if (ret != 0)
1440 return;
1441
Wang Long9697a552015-03-11 08:36:54 +00001442 unittest(1, "overlay test %d passed\n", 4);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001443}
1444
1445/* test overlay apply/revert sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08001446static void __init of_unittest_overlay_5(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001447{
1448 int ret;
1449
1450 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001451 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001452 if (ret != 0)
1453 return;
1454
Wang Long9697a552015-03-11 08:36:54 +00001455 unittest(1, "overlay test %d passed\n", 5);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001456}
1457
1458/* test overlay application in sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08001459static void __init of_unittest_overlay_6(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001460{
Frank Rowand24789c52017-10-17 16:36:26 -07001461 int ret, i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00001462 int overlay_nr = 6, unittest_nr = 6;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001463 int before = 0, after = 1;
Frank Rowand39a751a2018-02-12 00:19:42 -08001464 const char *overlay_name;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001465
Wang Long9697a552015-03-11 08:36:54 +00001466 /* unittest device must be in before state */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001467 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001468 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001469 != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001470 unittest(0, "%s with device @\"%s\" %s\n",
1471 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001472 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001473 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001474 !before ? "enabled" : "disabled");
1475 return;
1476 }
1477 }
1478
1479 /* apply the overlays */
1480 for (i = 0; i < 2; i++) {
1481
Frank Rowand39a751a2018-02-12 00:19:42 -08001482 overlay_name = overlay_name_from_nr(overlay_nr + i);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001483
Frank Rowand39a751a2018-02-12 00:19:42 -08001484 ret = overlay_data_apply(overlay_name, &ovcs_id);
1485 if (!ret) {
1486 unittest(0, "could not apply overlay \"%s\"\n",
1487 overlay_name);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001488 return;
1489 }
Frank Rowand24789c52017-10-17 16:36:26 -07001490 ov_id[i] = ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001491 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001492 }
1493
1494 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001495 /* unittest device must be in after state */
1496 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001497 != after) {
Wang Long9697a552015-03-11 08:36:54 +00001498 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
Frank Rowand39a751a2018-02-12 00:19:42 -08001499 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001500 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001501 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001502 !after ? "enabled" : "disabled");
1503 return;
1504 }
1505 }
1506
1507 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07001508 ovcs_id = ov_id[i];
1509 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001510 if (ret != 0) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001511 unittest(0, "%s failed destroy @\"%s\"\n",
1512 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001513 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001514 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001515 return;
1516 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001517 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001518 }
1519
1520 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00001521 /* unittest device must be again in before state */
1522 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001523 != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001524 unittest(0, "%s with device @\"%s\" %s\n",
1525 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001526 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001527 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001528 !before ? "enabled" : "disabled");
1529 return;
1530 }
1531 }
1532
Wang Long9697a552015-03-11 08:36:54 +00001533 unittest(1, "overlay test %d passed\n", 6);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001534}
1535
1536/* test overlay application in sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08001537static void __init of_unittest_overlay_8(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001538{
Frank Rowand24789c52017-10-17 16:36:26 -07001539 int ret, i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00001540 int overlay_nr = 8, unittest_nr = 8;
Frank Rowand39a751a2018-02-12 00:19:42 -08001541 const char *overlay_name;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001542
1543 /* we don't care about device state in this test */
1544
1545 /* apply the overlays */
1546 for (i = 0; i < 2; i++) {
1547
Frank Rowand39a751a2018-02-12 00:19:42 -08001548 overlay_name = overlay_name_from_nr(overlay_nr + i);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001549
Frank Rowand39a751a2018-02-12 00:19:42 -08001550 ret = overlay_data_apply(overlay_name, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001551 if (ret < 0) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001552 unittest(0, "could not apply overlay \"%s\"\n",
1553 overlay_name);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001554 return;
1555 }
Frank Rowand24789c52017-10-17 16:36:26 -07001556 ov_id[i] = ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001557 of_unittest_track_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001558 }
1559
1560 /* now try to remove first overlay (it should fail) */
Frank Rowand24789c52017-10-17 16:36:26 -07001561 ovcs_id = ov_id[0];
1562 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001563 if (ret == 0) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001564 unittest(0, "%s was destroyed @\"%s\"\n",
1565 overlay_name_from_nr(overlay_nr + 0),
Wang Long9697a552015-03-11 08:36:54 +00001566 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001567 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001568 return;
1569 }
1570
1571 /* removing them in order should work */
1572 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07001573 ovcs_id = ov_id[i];
1574 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001575 if (ret != 0) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001576 unittest(0, "%s not destroyed @\"%s\"\n",
1577 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00001578 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001579 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001580 return;
1581 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001582 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001583 }
1584
Wang Long9697a552015-03-11 08:36:54 +00001585 unittest(1, "overlay test %d passed\n", 8);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001586}
1587
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001588/* test insertion of a bus with parent devices */
Frank Rowand39a751a2018-02-12 00:19:42 -08001589static void __init of_unittest_overlay_10(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001590{
1591 int ret;
1592 char *child_path;
1593
1594 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001595 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
1596 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001597 "overlay test %d failed; overlay application\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001598 return;
1599
Wang Long9697a552015-03-11 08:36:54 +00001600 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
1601 unittest_path(10, PDEV_OVERLAY));
1602 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001603 return;
1604
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001605 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001606 kfree(child_path);
Wang Long9697a552015-03-11 08:36:54 +00001607 if (unittest(ret, "overlay test %d failed; no child device\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001608 return;
1609}
1610
1611/* test insertion of a bus with parent devices (and revert) */
Frank Rowand39a751a2018-02-12 00:19:42 -08001612static void __init of_unittest_overlay_11(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001613{
1614 int ret;
1615
1616 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001617 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001618 PDEV_OVERLAY);
Wang Long9697a552015-03-11 08:36:54 +00001619 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001620 "overlay test %d failed; overlay application\n", 11))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001621 return;
1622}
1623
Arnd Bergmann4252de32015-03-04 20:49:47 +01001624#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001625
Wang Long9697a552015-03-11 08:36:54 +00001626struct unittest_i2c_bus_data {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001627 struct platform_device *pdev;
1628 struct i2c_adapter adap;
1629};
1630
Wang Long9697a552015-03-11 08:36:54 +00001631static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001632 struct i2c_msg *msgs, int num)
1633{
Wang Long9697a552015-03-11 08:36:54 +00001634 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001635
1636 (void)std;
1637
1638 return num;
1639}
1640
Wang Long9697a552015-03-11 08:36:54 +00001641static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001642{
1643 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1644}
1645
Wang Long9697a552015-03-11 08:36:54 +00001646static const struct i2c_algorithm unittest_i2c_algo = {
1647 .master_xfer = unittest_i2c_master_xfer,
1648 .functionality = unittest_i2c_functionality,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001649};
1650
Wang Long9697a552015-03-11 08:36:54 +00001651static int unittest_i2c_bus_probe(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001652{
1653 struct device *dev = &pdev->dev;
1654 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001655 struct unittest_i2c_bus_data *std;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001656 struct i2c_adapter *adap;
1657 int ret;
1658
1659 if (np == NULL) {
1660 dev_err(dev, "No OF data for device\n");
1661 return -EINVAL;
1662
1663 }
1664
Rob Herring0d638a02017-06-01 15:50:55 -05001665 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001666
1667 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
1668 if (!std) {
Wang Long9697a552015-03-11 08:36:54 +00001669 dev_err(dev, "Failed to allocate unittest i2c data\n");
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001670 return -ENOMEM;
1671 }
1672
1673 /* link them together */
1674 std->pdev = pdev;
1675 platform_set_drvdata(pdev, std);
1676
1677 adap = &std->adap;
1678 i2c_set_adapdata(adap, std);
1679 adap->nr = -1;
1680 strlcpy(adap->name, pdev->name, sizeof(adap->name));
1681 adap->class = I2C_CLASS_DEPRECATED;
Wang Long9697a552015-03-11 08:36:54 +00001682 adap->algo = &unittest_i2c_algo;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001683 adap->dev.parent = dev;
1684 adap->dev.of_node = dev->of_node;
1685 adap->timeout = 5 * HZ;
1686 adap->retries = 3;
1687
1688 ret = i2c_add_numbered_adapter(adap);
1689 if (ret != 0) {
1690 dev_err(dev, "Failed to add I2C adapter\n");
1691 return ret;
1692 }
1693
1694 return 0;
1695}
1696
Wang Long9697a552015-03-11 08:36:54 +00001697static int unittest_i2c_bus_remove(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001698{
1699 struct device *dev = &pdev->dev;
1700 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00001701 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001702
Rob Herring0d638a02017-06-01 15:50:55 -05001703 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001704 i2c_del_adapter(&std->adap);
1705
1706 return 0;
1707}
1708
Grant Likelya2166ca2015-03-29 08:59:58 +01001709static const struct of_device_id unittest_i2c_bus_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001710 { .compatible = "unittest-i2c-bus", },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001711 {},
1712};
1713
Wang Long9697a552015-03-11 08:36:54 +00001714static struct platform_driver unittest_i2c_bus_driver = {
1715 .probe = unittest_i2c_bus_probe,
1716 .remove = unittest_i2c_bus_remove,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001717 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001718 .name = "unittest-i2c-bus",
1719 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001720 },
1721};
1722
Wang Long9697a552015-03-11 08:36:54 +00001723static int unittest_i2c_dev_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001724 const struct i2c_device_id *id)
1725{
1726 struct device *dev = &client->dev;
1727 struct device_node *np = client->dev.of_node;
1728
1729 if (!np) {
1730 dev_err(dev, "No OF node\n");
1731 return -EINVAL;
1732 }
1733
Rob Herring0d638a02017-06-01 15:50:55 -05001734 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001735
1736 return 0;
1737};
1738
Wang Long9697a552015-03-11 08:36:54 +00001739static int unittest_i2c_dev_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001740{
1741 struct device *dev = &client->dev;
1742 struct device_node *np = client->dev.of_node;
1743
Rob Herring0d638a02017-06-01 15:50:55 -05001744 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001745 return 0;
1746}
1747
Wang Long9697a552015-03-11 08:36:54 +00001748static const struct i2c_device_id unittest_i2c_dev_id[] = {
1749 { .name = "unittest-i2c-dev" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001750 { }
1751};
1752
Wang Long9697a552015-03-11 08:36:54 +00001753static struct i2c_driver unittest_i2c_dev_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001754 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001755 .name = "unittest-i2c-dev",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001756 },
Wang Long9697a552015-03-11 08:36:54 +00001757 .probe = unittest_i2c_dev_probe,
1758 .remove = unittest_i2c_dev_remove,
1759 .id_table = unittest_i2c_dev_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001760};
1761
Arnd Bergmann4252de32015-03-04 20:49:47 +01001762#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001763
Peter Rosinb6e3b712016-04-20 08:42:47 +02001764static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001765{
1766 return 0;
1767}
1768
Wang Long9697a552015-03-11 08:36:54 +00001769static int unittest_i2c_mux_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001770 const struct i2c_device_id *id)
1771{
Peter Rosinb6e3b712016-04-20 08:42:47 +02001772 int ret, i, nchans;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001773 struct device *dev = &client->dev;
1774 struct i2c_adapter *adap = to_i2c_adapter(dev->parent);
1775 struct device_node *np = client->dev.of_node, *child;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001776 struct i2c_mux_core *muxc;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001777 u32 reg, max_reg;
1778
Rob Herring0d638a02017-06-01 15:50:55 -05001779 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001780
1781 if (!np) {
1782 dev_err(dev, "No OF node\n");
1783 return -EINVAL;
1784 }
1785
1786 max_reg = (u32)-1;
1787 for_each_child_of_node(np, child) {
1788 ret = of_property_read_u32(child, "reg", &reg);
1789 if (ret)
1790 continue;
1791 if (max_reg == (u32)-1 || reg > max_reg)
1792 max_reg = reg;
1793 }
1794 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
1795 if (nchans == 0) {
1796 dev_err(dev, "No channels\n");
1797 return -EINVAL;
1798 }
1799
Peter Rosinb6e3b712016-04-20 08:42:47 +02001800 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
1801 unittest_i2c_mux_select_chan, NULL);
1802 if (!muxc)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001803 return -ENOMEM;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001804 for (i = 0; i < nchans; i++) {
Peter Rosinb6e3b712016-04-20 08:42:47 +02001805 ret = i2c_mux_add_adapter(muxc, 0, i, 0);
1806 if (ret) {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001807 dev_err(dev, "Failed to register mux #%d\n", i);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001808 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001809 return -ENODEV;
1810 }
1811 }
1812
Peter Rosinb6e3b712016-04-20 08:42:47 +02001813 i2c_set_clientdata(client, muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001814
1815 return 0;
1816};
1817
Wang Long9697a552015-03-11 08:36:54 +00001818static int unittest_i2c_mux_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001819{
1820 struct device *dev = &client->dev;
1821 struct device_node *np = client->dev.of_node;
Peter Rosinb6e3b712016-04-20 08:42:47 +02001822 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001823
Rob Herring0d638a02017-06-01 15:50:55 -05001824 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Peter Rosinb6e3b712016-04-20 08:42:47 +02001825 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001826 return 0;
1827}
1828
Wang Long9697a552015-03-11 08:36:54 +00001829static const struct i2c_device_id unittest_i2c_mux_id[] = {
1830 { .name = "unittest-i2c-mux" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001831 { }
1832};
1833
Wang Long9697a552015-03-11 08:36:54 +00001834static struct i2c_driver unittest_i2c_mux_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001835 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001836 .name = "unittest-i2c-mux",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001837 },
Wang Long9697a552015-03-11 08:36:54 +00001838 .probe = unittest_i2c_mux_probe,
1839 .remove = unittest_i2c_mux_remove,
1840 .id_table = unittest_i2c_mux_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001841};
1842
1843#endif
1844
Wang Long9697a552015-03-11 08:36:54 +00001845static int of_unittest_overlay_i2c_init(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001846{
1847 int ret;
1848
Wang Long9697a552015-03-11 08:36:54 +00001849 ret = i2c_add_driver(&unittest_i2c_dev_driver);
1850 if (unittest(ret == 0,
1851 "could not register unittest i2c device driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001852 return ret;
1853
Wang Long9697a552015-03-11 08:36:54 +00001854 ret = platform_driver_register(&unittest_i2c_bus_driver);
1855 if (unittest(ret == 0,
1856 "could not register unittest i2c bus driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001857 return ret;
1858
Arnd Bergmann4252de32015-03-04 20:49:47 +01001859#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001860 ret = i2c_add_driver(&unittest_i2c_mux_driver);
1861 if (unittest(ret == 0,
1862 "could not register unittest i2c mux driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001863 return ret;
1864#endif
1865
1866 return 0;
1867}
1868
Wang Long9697a552015-03-11 08:36:54 +00001869static void of_unittest_overlay_i2c_cleanup(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001870{
Arnd Bergmann4252de32015-03-04 20:49:47 +01001871#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00001872 i2c_del_driver(&unittest_i2c_mux_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001873#endif
Wang Long9697a552015-03-11 08:36:54 +00001874 platform_driver_unregister(&unittest_i2c_bus_driver);
1875 i2c_del_driver(&unittest_i2c_dev_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001876}
1877
Frank Rowand39a751a2018-02-12 00:19:42 -08001878static void __init of_unittest_overlay_i2c_12(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001879{
1880 int ret;
1881
1882 /* device should enable */
Wang Long9697a552015-03-11 08:36:54 +00001883 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001884 if (ret != 0)
1885 return;
1886
Wang Long9697a552015-03-11 08:36:54 +00001887 unittest(1, "overlay test %d passed\n", 12);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001888}
1889
1890/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08001891static void __init of_unittest_overlay_i2c_13(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001892{
1893 int ret;
1894
1895 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00001896 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001897 if (ret != 0)
1898 return;
1899
Wang Long9697a552015-03-11 08:36:54 +00001900 unittest(1, "overlay test %d passed\n", 13);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001901}
1902
1903/* just check for i2c mux existence */
Wang Long9697a552015-03-11 08:36:54 +00001904static void of_unittest_overlay_i2c_14(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001905{
1906}
1907
Frank Rowand39a751a2018-02-12 00:19:42 -08001908static void __init of_unittest_overlay_i2c_15(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001909{
1910 int ret;
1911
1912 /* device should enable */
Alexander Sverdlinca7b8962017-01-19 11:06:16 +01001913 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001914 if (ret != 0)
1915 return;
1916
Wang Long9697a552015-03-11 08:36:54 +00001917 unittest(1, "overlay test %d passed\n", 15);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001918}
1919
1920#else
1921
Wang Long9697a552015-03-11 08:36:54 +00001922static inline void of_unittest_overlay_i2c_14(void) { }
1923static inline void of_unittest_overlay_i2c_15(void) { }
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001924
1925#endif
1926
Wang Long9697a552015-03-11 08:36:54 +00001927static void __init of_unittest_overlay(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001928{
1929 struct device_node *bus_np = NULL;
1930 int ret;
1931
Wang Long9697a552015-03-11 08:36:54 +00001932 ret = platform_driver_register(&unittest_driver);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001933 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001934 unittest(0, "could not register unittest driver\n");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001935 goto out;
1936 }
1937
1938 bus_np = of_find_node_by_path(bus_path);
1939 if (bus_np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00001940 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001941 goto out;
1942 }
1943
Kefeng Wang146dedb2016-06-01 14:53:10 +08001944 ret = of_platform_default_populate(bus_np, NULL, NULL);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001945 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001946 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001947 goto out;
1948 }
1949
Wang Long9697a552015-03-11 08:36:54 +00001950 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
1951 unittest(0, "could not find unittest0 @ \"%s\"\n",
1952 unittest_path(100, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001953 goto out;
1954 }
1955
Wang Long9697a552015-03-11 08:36:54 +00001956 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
1957 unittest(0, "unittest1 @ \"%s\" should not exist\n",
1958 unittest_path(101, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001959 goto out;
1960 }
1961
Wang Long9697a552015-03-11 08:36:54 +00001962 unittest(1, "basic infrastructure of overlays passed");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001963
1964 /* tests in sequence */
Wang Long9697a552015-03-11 08:36:54 +00001965 of_unittest_overlay_0();
1966 of_unittest_overlay_1();
1967 of_unittest_overlay_2();
1968 of_unittest_overlay_3();
1969 of_unittest_overlay_4();
1970 of_unittest_overlay_5();
1971 of_unittest_overlay_6();
1972 of_unittest_overlay_8();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001973
Wang Long9697a552015-03-11 08:36:54 +00001974 of_unittest_overlay_10();
1975 of_unittest_overlay_11();
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001976
Arnd Bergmann4252de32015-03-04 20:49:47 +01001977#if IS_BUILTIN(CONFIG_I2C)
Wang Long9697a552015-03-11 08:36:54 +00001978 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001979 goto out;
1980
Wang Long9697a552015-03-11 08:36:54 +00001981 of_unittest_overlay_i2c_12();
1982 of_unittest_overlay_i2c_13();
1983 of_unittest_overlay_i2c_14();
1984 of_unittest_overlay_i2c_15();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001985
Wang Long9697a552015-03-11 08:36:54 +00001986 of_unittest_overlay_i2c_cleanup();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001987#endif
1988
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001989 of_unittest_destroy_tracked_overlays();
1990
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001991out:
1992 of_node_put(bus_np);
1993}
1994
1995#else
Wang Long9697a552015-03-11 08:36:54 +00001996static inline void __init of_unittest_overlay(void) { }
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001997#endif
1998
Frank Rowand60a00042017-07-19 09:25:20 -07001999#ifdef CONFIG_OF_OVERLAY
2000
Frank Rowand81d0848f2017-04-25 17:09:54 -07002001/*
2002 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2003 * in scripts/Makefile.lib
2004 */
2005
2006#define OVERLAY_INFO_EXTERN(name) \
2007 extern uint8_t __dtb_##name##_begin[]; \
2008 extern uint8_t __dtb_##name##_end[]
2009
Frank Rowand39a751a2018-02-12 00:19:42 -08002010#define OVERLAY_INFO(overlay_name, expected) \
2011{ .dtb_begin = __dtb_##overlay_name##_begin, \
2012 .dtb_end = __dtb_##overlay_name##_end, \
2013 .expected_result = expected, \
2014 .name = #overlay_name, \
Frank Rowand81d0848f2017-04-25 17:09:54 -07002015}
2016
2017struct overlay_info {
Frank Rowand39a751a2018-02-12 00:19:42 -08002018 uint8_t *dtb_begin;
2019 uint8_t *dtb_end;
2020 int expected_result;
2021 int overlay_id;
2022 char *name;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002023};
2024
2025OVERLAY_INFO_EXTERN(overlay_base);
2026OVERLAY_INFO_EXTERN(overlay);
Frank Rowand39a751a2018-02-12 00:19:42 -08002027OVERLAY_INFO_EXTERN(overlay_0);
2028OVERLAY_INFO_EXTERN(overlay_1);
2029OVERLAY_INFO_EXTERN(overlay_2);
2030OVERLAY_INFO_EXTERN(overlay_3);
2031OVERLAY_INFO_EXTERN(overlay_4);
2032OVERLAY_INFO_EXTERN(overlay_5);
2033OVERLAY_INFO_EXTERN(overlay_6);
2034OVERLAY_INFO_EXTERN(overlay_7);
2035OVERLAY_INFO_EXTERN(overlay_8);
2036OVERLAY_INFO_EXTERN(overlay_9);
2037OVERLAY_INFO_EXTERN(overlay_10);
2038OVERLAY_INFO_EXTERN(overlay_11);
2039OVERLAY_INFO_EXTERN(overlay_12);
2040OVERLAY_INFO_EXTERN(overlay_13);
2041OVERLAY_INFO_EXTERN(overlay_15);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002042OVERLAY_INFO_EXTERN(overlay_bad_phandle);
Frank Rowand60a00042017-07-19 09:25:20 -07002043OVERLAY_INFO_EXTERN(overlay_bad_symbol);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002044
2045/* order of entries is hard-coded into users of overlays[] */
2046static struct overlay_info overlays[] = {
2047 OVERLAY_INFO(overlay_base, -9999),
2048 OVERLAY_INFO(overlay, 0),
Frank Rowand39a751a2018-02-12 00:19:42 -08002049 OVERLAY_INFO(overlay_0, 0),
2050 OVERLAY_INFO(overlay_1, 0),
2051 OVERLAY_INFO(overlay_2, 0),
2052 OVERLAY_INFO(overlay_3, 0),
2053 OVERLAY_INFO(overlay_4, 0),
2054 OVERLAY_INFO(overlay_5, 0),
2055 OVERLAY_INFO(overlay_6, 0),
2056 OVERLAY_INFO(overlay_7, 0),
2057 OVERLAY_INFO(overlay_8, 0),
2058 OVERLAY_INFO(overlay_9, 0),
2059 OVERLAY_INFO(overlay_10, 0),
2060 OVERLAY_INFO(overlay_11, 0),
2061 OVERLAY_INFO(overlay_12, 0),
2062 OVERLAY_INFO(overlay_13, 0),
2063 OVERLAY_INFO(overlay_15, 0),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002064 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
Frank Rowand60a00042017-07-19 09:25:20 -07002065 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002066 {}
2067};
2068
2069static struct device_node *overlay_base_root;
2070
Rob Herring0fa1c572018-01-05 15:32:33 -06002071static void * __init dt_alloc_memory(u64 size, u64 align)
2072{
2073 return memblock_virt_alloc(size, align);
2074}
2075
Frank Rowand81d0848f2017-04-25 17:09:54 -07002076/*
2077 * Create base device tree for the overlay unittest.
2078 *
2079 * This is called from very early boot code.
2080 *
2081 * Do as much as possible the same way as done in __unflatten_device_tree
2082 * and other early boot steps for the normal FDT so that the overlay base
2083 * unflattened tree will have the same characteristics as the real tree
2084 * (such as having memory allocated by the early allocator). The goal
2085 * is to test "the real thing" as much as possible, and test "test setup
2086 * code" as little as possible.
2087 *
2088 * Have to stop before resolving phandles, because that uses kmalloc.
2089 */
2090void __init unittest_unflatten_overlay_base(void)
2091{
2092 struct overlay_info *info;
2093 u32 data_size;
Frank Rowand39a751a2018-02-12 00:19:42 -08002094 void *new_fdt;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002095 u32 size;
2096
2097 info = &overlays[0];
2098
2099 if (info->expected_result != -9999) {
2100 pr_err("No dtb 'overlay_base' to attach\n");
2101 return;
2102 }
2103
2104 data_size = info->dtb_end - info->dtb_begin;
2105 if (!data_size) {
2106 pr_err("No dtb 'overlay_base' to attach\n");
2107 return;
2108 }
2109
2110 size = fdt_totalsize(info->dtb_begin);
2111 if (size != data_size) {
2112 pr_err("dtb 'overlay_base' header totalsize != actual size");
2113 return;
2114 }
2115
Frank Rowand39a751a2018-02-12 00:19:42 -08002116 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
2117 if (!new_fdt) {
Frank Rowand81d0848f2017-04-25 17:09:54 -07002118 pr_err("alloc for dtb 'overlay_base' failed");
2119 return;
2120 }
2121
Frank Rowand39a751a2018-02-12 00:19:42 -08002122 memcpy(new_fdt, info->dtb_begin, size);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002123
Frank Rowand39a751a2018-02-12 00:19:42 -08002124 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
Rob Herring0fa1c572018-01-05 15:32:33 -06002125 dt_alloc_memory, true);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002126}
2127
2128/*
2129 * The purpose of of_unittest_overlay_data_add is to add an
2130 * overlay in the normal fashion. This is a test of the whole
2131 * picture, instead of testing individual elements.
2132 *
2133 * A secondary purpose is to be able to verify that the contents of
2134 * /proc/device-tree/ contains the updated structure and values from
2135 * the overlay. That must be verified separately in user space.
2136 *
2137 * Return 0 on unexpected error.
2138 */
Frank Rowand39a751a2018-02-12 00:19:42 -08002139static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
Frank Rowand81d0848f2017-04-25 17:09:54 -07002140{
2141 struct overlay_info *info;
Frank Rowand39a751a2018-02-12 00:19:42 -08002142 int found = 0;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002143 int k;
2144 int ret;
2145 u32 size;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002146
Frank Rowand39a751a2018-02-12 00:19:42 -08002147 for (k = 0, info = overlays; info && info->name; info++, k++) {
2148 if (!strcmp(overlay_name, info->name)) {
2149 found = 1;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002150 break;
Frank Rowand39a751a2018-02-12 00:19:42 -08002151 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07002152 }
Frank Rowand39a751a2018-02-12 00:19:42 -08002153 if (!found) {
2154 pr_err("no overlay data for %s\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002155 return 0;
Frank Rowand39a751a2018-02-12 00:19:42 -08002156 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07002157
2158 size = info->dtb_end - info->dtb_begin;
2159 if (!size) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002160 pr_err("no overlay data for %s\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002161 ret = 0;
2162 }
2163
Frank Rowand39a751a2018-02-12 00:19:42 -08002164 ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
2165 if (overlay_id)
2166 *overlay_id = info->overlay_id;
2167 if (ret < 0)
2168 goto out;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002169
Frank Rowand39a751a2018-02-12 00:19:42 -08002170 pr_debug("%s applied\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002171
2172out:
Frank Rowand39a751a2018-02-12 00:19:42 -08002173 if (ret != info->expected_result)
2174 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
2175 info->expected_result, ret, overlay_name);
2176
Frank Rowand81d0848f2017-04-25 17:09:54 -07002177 return (ret == info->expected_result);
2178}
2179
2180/*
2181 * The purpose of of_unittest_overlay_high_level is to add an overlay
2182 * in the normal fashion. This is a test of the whole picture,
2183 * instead of individual elements.
2184 *
2185 * The first part of the function is _not_ normal overlay usage; it is
2186 * finishing splicing the base overlay device tree into the live tree.
2187 */
2188static __init void of_unittest_overlay_high_level(void)
2189{
2190 struct device_node *last_sibling;
2191 struct device_node *np;
2192 struct device_node *of_symbols;
2193 struct device_node *overlay_base_symbols;
2194 struct device_node **pprev;
2195 struct property *prop;
2196 int ret;
2197
2198 if (!overlay_base_root) {
2199 unittest(0, "overlay_base_root not initialized\n");
2200 return;
2201 }
2202
2203 /*
2204 * Could not fixup phandles in unittest_unflatten_overlay_base()
2205 * because kmalloc() was not yet available.
2206 */
Frank Rowandf948d6d2017-10-17 16:36:29 -07002207 of_overlay_mutex_lock();
Frank Rowand81d0848f2017-04-25 17:09:54 -07002208 of_resolve_phandles(overlay_base_root);
Frank Rowandf948d6d2017-10-17 16:36:29 -07002209 of_overlay_mutex_unlock();
2210
Frank Rowand81d0848f2017-04-25 17:09:54 -07002211
2212 /*
2213 * do not allow overlay_base to duplicate any node already in
2214 * tree, this greatly simplifies the code
2215 */
2216
2217 /*
2218 * remove overlay_base_root node "__local_fixups", after
2219 * being used by of_resolve_phandles()
2220 */
2221 pprev = &overlay_base_root->child;
2222 for (np = overlay_base_root->child; np; np = np->sibling) {
2223 if (!of_node_cmp(np->name, "__local_fixups__")) {
2224 *pprev = np->sibling;
2225 break;
2226 }
2227 pprev = &np->sibling;
2228 }
2229
2230 /* remove overlay_base_root node "__symbols__" if in live tree */
2231 of_symbols = of_get_child_by_name(of_root, "__symbols__");
2232 if (of_symbols) {
2233 /* will have to graft properties from node into live tree */
2234 pprev = &overlay_base_root->child;
2235 for (np = overlay_base_root->child; np; np = np->sibling) {
2236 if (!of_node_cmp(np->name, "__symbols__")) {
2237 overlay_base_symbols = np;
2238 *pprev = np->sibling;
2239 break;
2240 }
2241 pprev = &np->sibling;
2242 }
2243 }
2244
2245 for (np = overlay_base_root->child; np; np = np->sibling) {
2246 if (of_get_child_by_name(of_root, np->name)) {
2247 unittest(0, "illegal node name in overlay_base %s",
2248 np->name);
2249 return;
2250 }
2251 }
2252
2253 /*
2254 * overlay 'overlay_base' is not allowed to have root
2255 * properties, so only need to splice nodes into main device tree.
2256 *
2257 * root node of *overlay_base_root will not be freed, it is lost
2258 * memory.
2259 */
2260
2261 for (np = overlay_base_root->child; np; np = np->sibling)
2262 np->parent = of_root;
2263
2264 mutex_lock(&of_mutex);
2265
Arnd Bergmannee320b32017-04-28 11:44:11 +02002266 for (last_sibling = np = of_root->child; np; np = np->sibling)
Frank Rowand81d0848f2017-04-25 17:09:54 -07002267 last_sibling = np;
2268
2269 if (last_sibling)
2270 last_sibling->sibling = overlay_base_root->child;
2271 else
2272 of_root->child = overlay_base_root->child;
2273
2274 for_each_of_allnodes_from(overlay_base_root, np)
2275 __of_attach_node_sysfs(np);
2276
2277 if (of_symbols) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002278 struct property *new_prop;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002279 for_each_property_of_node(overlay_base_symbols, prop) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002280
2281 new_prop = __of_prop_dup(prop, GFP_KERNEL);
2282 if (!new_prop) {
2283 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
Frank Rowand81d0848f2017-04-25 17:09:54 -07002284 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002285 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002286 }
Frank Rowand39a751a2018-02-12 00:19:42 -08002287 ret = __of_add_property(of_symbols, new_prop);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002288 if (ret) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002289 if (!strcmp(new_prop->name, "name")) {
2290 /* auto-generated by unflatten */
2291 ret = 0;
2292 continue;
2293 }
2294 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
2295 prop->name);
2296 goto err_unlock;
2297 }
2298 ret = __of_add_property_sysfs(of_symbols, new_prop);
2299 if (ret) {
2300 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
Frank Rowand81d0848f2017-04-25 17:09:54 -07002301 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03002302 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002303 }
2304 }
2305 }
2306
2307 mutex_unlock(&of_mutex);
2308
2309
2310 /* now do the normal overlay usage test */
2311
Frank Rowand39a751a2018-02-12 00:19:42 -08002312 unittest(overlay_data_apply("overlay", NULL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002313 "Adding overlay 'overlay' failed\n");
2314
Frank Rowand39a751a2018-02-12 00:19:42 -08002315 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002316 "Adding overlay 'overlay_bad_phandle' failed\n");
Frank Rowand60a00042017-07-19 09:25:20 -07002317
Frank Rowand39a751a2018-02-12 00:19:42 -08002318 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
Frank Rowand60a00042017-07-19 09:25:20 -07002319 "Adding overlay 'overlay_bad_symbol' failed\n");
2320
Dan Carpenter8756cd12017-05-03 22:49:50 +03002321 return;
2322
2323err_unlock:
2324 mutex_unlock(&of_mutex);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002325}
2326
2327#else
2328
2329static inline __init void of_unittest_overlay_high_level(void) {}
2330
2331#endif
2332
Wang Long9697a552015-03-11 08:36:54 +00002333static int __init of_unittest(void)
Grant Likely53a42092011-12-12 09:25:57 -07002334{
2335 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002336 int res;
2337
Wang Long9697a552015-03-11 08:36:54 +00002338 /* adding data for unittest */
2339 res = unittest_data_add();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002340 if (res)
2341 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00002342 if (!of_aliases)
2343 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07002344
2345 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
2346 if (!np) {
2347 pr_info("No testcase data in device tree; not running tests\n");
2348 return 0;
2349 }
2350 of_node_put(np);
2351
Wang Long9697a552015-03-11 08:36:54 +00002352 pr_info("start of unittest - you will see error messages\n");
2353 of_unittest_check_tree_linkage();
2354 of_unittest_check_phandles();
2355 of_unittest_find_node_by_name();
2356 of_unittest_dynamic();
2357 of_unittest_parse_phandle_with_args();
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002358 of_unittest_printf();
Wang Long9697a552015-03-11 08:36:54 +00002359 of_unittest_property_string();
2360 of_unittest_property_copy();
2361 of_unittest_changeset();
2362 of_unittest_parse_interrupts();
2363 of_unittest_parse_interrupts_extended();
2364 of_unittest_match_node();
2365 of_unittest_platform_populate();
2366 of_unittest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07002367
Grant Likelyf2051d62014-10-01 17:40:22 +01002368 /* Double check linkage after removing testcase data */
Wang Long9697a552015-03-11 08:36:54 +00002369 of_unittest_check_tree_linkage();
Grant Likelyf2051d62014-10-01 17:40:22 +01002370
Frank Rowand81d0848f2017-04-25 17:09:54 -07002371 of_unittest_overlay_high_level();
2372
Wang Long9697a552015-03-11 08:36:54 +00002373 pr_info("end of unittest - %i passed, %i failed\n",
2374 unittest_results.passed, unittest_results.failed);
Grant Likelyf2051d62014-10-01 17:40:22 +01002375
Grant Likely53a42092011-12-12 09:25:57 -07002376 return 0;
2377}
Wang Long9697a552015-03-11 08:36:54 +00002378late_initcall(of_unittest);