blob: 481ba8682ebf418bfd1b6d566a3cee8f1e66fd6e [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
Mike Rapoport57c8a662018-10-30 15:09:49 -07008#include <linux/memblock.h>
Grant Likely53a42092011-12-12 09:25:57 -07009#include <linux/clk.h>
Jim Quinlane0d07272020-09-17 18:43:40 +020010#include <linux/dma-direct.h> /* to test phys_to_dma/dma_to_phys */
Grant Likely53a42092011-12-12 09:25:57 -070011#include <linux/err.h>
12#include <linux/errno.h>
Grant Likely841ec212014-10-02 13:09:15 +010013#include <linux/hashtable.h>
Frank Rowand81d0848f2017-04-25 17:09:54 -070014#include <linux/libfdt.h>
Grant Likely53a42092011-12-12 09:25:57 -070015#include <linux/of.h>
Rob Herring04db93a2019-09-20 13:28:53 -050016#include <linux/of_address.h>
Gaurav Minochaae9304c2014-07-16 23:09:39 -070017#include <linux/of_fdt.h>
Grant Likelya9f10ca2013-10-11 22:04:23 +010018#include <linux/of_irq.h>
Rob Herring82c0f582014-04-23 17:57:40 -050019#include <linux/of_platform.h>
Grant Likely53a42092011-12-12 09:25:57 -070020#include <linux/list.h>
21#include <linux/mutex.h>
22#include <linux/slab.h>
23#include <linux/device.h>
Pantelis Antoniou177d2712014-10-28 22:35:59 +020024#include <linux/platform_device.h>
Frank Rowand48d499b2021-04-08 15:45:08 -050025#include <linux/kernel.h>
Grant Likely53a42092011-12-12 09:25:57 -070026
Pantelis Antonioud5e75502015-01-12 19:02:49 +020027#include <linux/i2c.h>
28#include <linux/i2c-mux.h>
Frank Rowandf4056e72020-02-20 12:40:20 -060029#include <linux/gpio/driver.h>
Pantelis Antonioud5e75502015-01-12 19:02:49 +020030
Pantelis Antoniou492a22a2015-04-07 22:23:49 +030031#include <linux/bitops.h>
32
Pantelis Antoniou69843392014-07-04 19:58:47 +030033#include "of_private.h"
34
Wang Long9697a552015-03-11 08:36:54 +000035static struct unittest_results {
Grant Likelya9f10ca2013-10-11 22:04:23 +010036 int passed;
37 int failed;
Wang Long9697a552015-03-11 08:36:54 +000038} unittest_results;
Grant Likelya9f10ca2013-10-11 22:04:23 +010039
Wang Long9697a552015-03-11 08:36:54 +000040#define unittest(result, fmt, ...) ({ \
Grant Likely851da972014-11-04 13:14:13 +000041 bool failed = !(result); \
42 if (failed) { \
Wang Long9697a552015-03-11 08:36:54 +000043 unittest_results.failed++; \
Grant Likelya9f10ca2013-10-11 22:04:23 +010044 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000045 } else { \
Wang Long9697a552015-03-11 08:36:54 +000046 unittest_results.passed++; \
Grant Likelya9f10ca2013-10-11 22:04:23 +010047 pr_debug("pass %s():%i\n", __func__, __LINE__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000048 } \
Grant Likely851da972014-11-04 13:14:13 +000049 failed; \
50})
Grant Likely53a42092011-12-12 09:25:57 -070051
Frank Rowandf4056e72020-02-20 12:40:20 -060052/*
53 * Expected message may have a message level other than KERN_INFO.
54 * Print the expected message only if the current loglevel will allow
55 * the actual message to print.
Frank Rowand0ac17432020-02-20 12:40:21 -060056 *
57 * Do not use EXPECT_BEGIN() or EXPECT_END() for messages generated by
58 * pr_debug().
Frank Rowandf4056e72020-02-20 12:40:20 -060059 */
60#define EXPECT_BEGIN(level, fmt, ...) \
61 printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
62
63#define EXPECT_END(level, fmt, ...) \
64 printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
65
Wang Long9697a552015-03-11 08:36:54 +000066static void __init of_unittest_find_node_by_name(void)
Grant Likelyae91ff72014-03-14 13:53:10 +000067{
68 struct device_node *np;
Rob Herring0d638a02017-06-01 15:50:55 -050069 const char *options, *name;
Grant Likelyae91ff72014-03-14 13:53:10 +000070
71 np = of_find_node_by_path("/testcase-data");
Rob Herring0d638a02017-06-01 15:50:55 -050072 name = kasprintf(GFP_KERNEL, "%pOF", np);
73 unittest(np && !strcmp("/testcase-data", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000074 "find /testcase-data failed\n");
75 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050076 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000077
78 /* Test if trailing '/' works */
79 np = of_find_node_by_path("/testcase-data/");
Wang Long9697a552015-03-11 08:36:54 +000080 unittest(!np, "trailing '/' on /testcase-data/ should fail\n");
Grant Likelyae91ff72014-03-14 13:53:10 +000081
82 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
Rob Herring0d638a02017-06-01 15:50:55 -050083 name = kasprintf(GFP_KERNEL, "%pOF", np);
84 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000085 "find /testcase-data/phandle-tests/consumer-a failed\n");
86 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050087 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000088
89 np = of_find_node_by_path("testcase-alias");
Rob Herring0d638a02017-06-01 15:50:55 -050090 name = kasprintf(GFP_KERNEL, "%pOF", np);
91 unittest(np && !strcmp("/testcase-data", name),
Grant Likelyae91ff72014-03-14 13:53:10 +000092 "find testcase-alias failed\n");
93 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -050094 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +000095
96 /* Test if trailing '/' works on aliases */
97 np = of_find_node_by_path("testcase-alias/");
Wang Long9697a552015-03-11 08:36:54 +000098 unittest(!np, "trailing '/' on testcase-alias/ should fail\n");
Grant Likelyae91ff72014-03-14 13:53:10 +000099
100 np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a");
Rob Herring0d638a02017-06-01 15:50:55 -0500101 name = kasprintf(GFP_KERNEL, "%pOF", np);
102 unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name),
Grant Likelyae91ff72014-03-14 13:53:10 +0000103 "find testcase-alias/phandle-tests/consumer-a failed\n");
104 of_node_put(np);
Rob Herring0d638a02017-06-01 15:50:55 -0500105 kfree(name);
Grant Likelyae91ff72014-03-14 13:53:10 +0000106
107 np = of_find_node_by_path("/testcase-data/missing-path");
Rob Herring0d638a02017-06-01 15:50:55 -0500108 unittest(!np, "non-existent path returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +0000109 of_node_put(np);
110
111 np = of_find_node_by_path("missing-alias");
Rob Herring0d638a02017-06-01 15:50:55 -0500112 unittest(!np, "non-existent alias returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +0000113 of_node_put(np);
114
115 np = of_find_node_by_path("testcase-alias/missing-path");
Rob Herring0d638a02017-06-01 15:50:55 -0500116 unittest(!np, "non-existent alias with relative path returned node %pOF\n", np);
Grant Likelyae91ff72014-03-14 13:53:10 +0000117 of_node_put(np);
Leif Lindholm75c28c02014-11-28 11:34:28 +0000118
119 np = of_find_node_opts_by_path("/testcase-data:testoption", &options);
Wang Long9697a552015-03-11 08:36:54 +0000120 unittest(np && !strcmp("testoption", options),
Leif Lindholm75c28c02014-11-28 11:34:28 +0000121 "option path test failed\n");
122 of_node_put(np);
123
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500124 np = of_find_node_opts_by_path("/testcase-data:test/option", &options);
Wang Long9697a552015-03-11 08:36:54 +0000125 unittest(np && !strcmp("test/option", options),
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500126 "option path test, subcase #1 failed\n");
127 of_node_put(np);
128
Brian Norris5ca1b0d2015-03-17 12:30:32 -0700129 np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options);
Wang Long9697a552015-03-11 08:36:54 +0000130 unittest(np && !strcmp("test/option", options),
Brian Norris5ca1b0d2015-03-17 12:30:32 -0700131 "option path test, subcase #2 failed\n");
132 of_node_put(np);
133
Leif Lindholm75c28c02014-11-28 11:34:28 +0000134 np = of_find_node_opts_by_path("/testcase-data:testoption", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000135 unittest(np, "NULL option path test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000136 of_node_put(np);
137
138 np = of_find_node_opts_by_path("testcase-alias:testaliasoption",
139 &options);
Wang Long9697a552015-03-11 08:36:54 +0000140 unittest(np && !strcmp("testaliasoption", options),
Leif Lindholm75c28c02014-11-28 11:34:28 +0000141 "option alias path test failed\n");
142 of_node_put(np);
143
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500144 np = of_find_node_opts_by_path("testcase-alias:test/alias/option",
145 &options);
Wang Long9697a552015-03-11 08:36:54 +0000146 unittest(np && !strcmp("test/alias/option", options),
Peter Hurley8cbba1a2015-03-06 13:59:59 -0500147 "option alias path test, subcase #1 failed\n");
148 of_node_put(np);
149
Leif Lindholm75c28c02014-11-28 11:34:28 +0000150 np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000151 unittest(np, "NULL option alias path test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000152 of_node_put(np);
153
154 options = "testoption";
155 np = of_find_node_opts_by_path("testcase-alias", &options);
Wang Long9697a552015-03-11 08:36:54 +0000156 unittest(np && !options, "option clearing test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000157 of_node_put(np);
158
159 options = "testoption";
160 np = of_find_node_opts_by_path("/", &options);
Wang Long9697a552015-03-11 08:36:54 +0000161 unittest(np && !options, "option clearing root node test failed\n");
Leif Lindholm75c28c02014-11-28 11:34:28 +0000162 of_node_put(np);
Grant Likelyae91ff72014-03-14 13:53:10 +0000163}
164
Wang Long9697a552015-03-11 08:36:54 +0000165static void __init of_unittest_dynamic(void)
Grant Likely7e66c5c2013-11-15 17:19:09 +0000166{
167 struct device_node *np;
168 struct property *prop;
169
170 np = of_find_node_by_path("/testcase-data");
171 if (!np) {
172 pr_err("missing testcase data\n");
173 return;
174 }
175
176 /* Array of 4 properties for the purpose of testing */
Kees Cook6396bb22018-06-12 14:03:40 -0700177 prop = kcalloc(4, sizeof(*prop), GFP_KERNEL);
Grant Likely7e66c5c2013-11-15 17:19:09 +0000178 if (!prop) {
Wang Long9697a552015-03-11 08:36:54 +0000179 unittest(0, "kzalloc() failed\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000180 return;
181 }
182
183 /* Add a new property - should pass*/
184 prop->name = "new-property";
185 prop->value = "new-property-data";
Stefan M Schaeckeler3b9cf792018-05-21 16:26:14 -0700186 prop->length = strlen(prop->value) + 1;
Wang Long9697a552015-03-11 08:36:54 +0000187 unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000188
189 /* Try to add an existing property - should fail */
190 prop++;
191 prop->name = "new-property";
192 prop->value = "new-property-data-should-fail";
Stefan M Schaeckeler3b9cf792018-05-21 16:26:14 -0700193 prop->length = strlen(prop->value) + 1;
Wang Long9697a552015-03-11 08:36:54 +0000194 unittest(of_add_property(np, prop) != 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000195 "Adding an existing property should have failed\n");
196
197 /* Try to modify an existing property - should pass */
198 prop->value = "modify-property-data-should-pass";
Stefan M Schaeckeler3b9cf792018-05-21 16:26:14 -0700199 prop->length = strlen(prop->value) + 1;
Wang Long9697a552015-03-11 08:36:54 +0000200 unittest(of_update_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000201 "Updating an existing property should have passed\n");
202
203 /* Try to modify non-existent property - should pass*/
204 prop++;
205 prop->name = "modify-property";
206 prop->value = "modify-missing-property-data-should-pass";
Stefan M Schaeckeler3b9cf792018-05-21 16:26:14 -0700207 prop->length = strlen(prop->value) + 1;
Wang Long9697a552015-03-11 08:36:54 +0000208 unittest(of_update_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000209 "Updating a missing property should have passed\n");
210
211 /* Remove property - should pass */
Wang Long9697a552015-03-11 08:36:54 +0000212 unittest(of_remove_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000213 "Removing a property should have passed\n");
214
215 /* Adding very large property - should pass */
216 prop++;
217 prop->name = "large-property-PAGE_SIZEx8";
218 prop->length = PAGE_SIZE * 8;
219 prop->value = kzalloc(prop->length, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000220 unittest(prop->value != NULL, "Unable to allocate large buffer\n");
Grant Likely7e66c5c2013-11-15 17:19:09 +0000221 if (prop->value)
Wang Long9697a552015-03-11 08:36:54 +0000222 unittest(of_add_property(np, prop) == 0,
Grant Likely7e66c5c2013-11-15 17:19:09 +0000223 "Adding a large property should have passed\n");
224}
225
Wang Long9697a552015-03-11 08:36:54 +0000226static int __init of_unittest_check_node_linkage(struct device_node *np)
Grant Likelyf2051d62014-10-01 17:40:22 +0100227{
Grant Likely5063e252014-10-03 16:28:27 +0100228 struct device_node *child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100229 int count = 0, rc;
230
231 for_each_child_of_node(np, child) {
232 if (child->parent != np) {
Rob Herringa613b262018-08-27 20:00:19 -0500233 pr_err("Child node %pOFn links to wrong parent %pOFn\n",
234 child, np);
Julia Lawall855ff282015-10-22 11:02:50 +0200235 rc = -EINVAL;
236 goto put_child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100237 }
238
Wang Long9697a552015-03-11 08:36:54 +0000239 rc = of_unittest_check_node_linkage(child);
Grant Likelyf2051d62014-10-01 17:40:22 +0100240 if (rc < 0)
Julia Lawall855ff282015-10-22 11:02:50 +0200241 goto put_child;
Grant Likelyf2051d62014-10-01 17:40:22 +0100242 count += rc;
243 }
244
245 return count + 1;
Julia Lawall855ff282015-10-22 11:02:50 +0200246put_child:
247 of_node_put(child);
248 return rc;
Grant Likelyf2051d62014-10-01 17:40:22 +0100249}
250
Wang Long9697a552015-03-11 08:36:54 +0000251static void __init of_unittest_check_tree_linkage(void)
Grant Likelyf2051d62014-10-01 17:40:22 +0100252{
253 struct device_node *np;
254 int allnode_count = 0, child_count;
255
Grant Likely5063e252014-10-03 16:28:27 +0100256 if (!of_root)
Grant Likelyf2051d62014-10-01 17:40:22 +0100257 return;
258
259 for_each_of_allnodes(np)
260 allnode_count++;
Wang Long9697a552015-03-11 08:36:54 +0000261 child_count = of_unittest_check_node_linkage(of_root);
Grant Likelyf2051d62014-10-01 17:40:22 +0100262
Wang Long9697a552015-03-11 08:36:54 +0000263 unittest(child_count > 0, "Device node data structure is corrupted\n");
Grant Likelya2166ca2015-03-29 08:59:58 +0100264 unittest(child_count == allnode_count,
Frank Rowanda6bb1212015-03-13 23:59:01 -0700265 "allnodes list size (%i) doesn't match sibling lists size (%i)\n",
266 allnode_count, child_count);
Grant Likelyf2051d62014-10-01 17:40:22 +0100267 pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count);
268}
269
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200270static void __init of_unittest_printf_one(struct device_node *np, const char *fmt,
271 const char *expected)
272{
Tobin C. Hardingd4b9e422018-03-12 15:27:23 +1100273 unsigned char *buf;
274 int buf_size;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200275 int size, i;
276
Tobin C. Hardingd4b9e422018-03-12 15:27:23 +1100277 buf_size = strlen(expected) + 10;
278 buf = kmalloc(buf_size, GFP_KERNEL);
279 if (!buf)
280 return;
281
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200282 /* Baseline; check conversion with a large size limit */
Tobin C. Hardingd4b9e422018-03-12 15:27:23 +1100283 memset(buf, 0xff, buf_size);
284 size = snprintf(buf, buf_size - 2, fmt, np);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200285
286 /* use strcmp() instead of strncmp() here to be absolutely sure strings match */
287 unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff),
288 "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n",
289 fmt, expected, buf);
290
291 /* Make sure length limits work */
292 size++;
293 for (i = 0; i < 2; i++, size--) {
294 /* Clear the buffer, and make sure it works correctly still */
Tobin C. Hardingd4b9e422018-03-12 15:27:23 +1100295 memset(buf, 0xff, buf_size);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200296 snprintf(buf, size+1, fmt, np);
297 unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff),
298 "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n",
299 size, fmt, expected, buf);
300 }
Tobin C. Hardingd4b9e422018-03-12 15:27:23 +1100301 kfree(buf);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200302}
303
304static void __init of_unittest_printf(void)
305{
306 struct device_node *np;
307 const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100";
308 char phandle_str[16] = "";
309
310 np = of_find_node_by_path(full_name);
311 if (!np) {
312 unittest(np, "testcase data missing\n");
313 return;
314 }
315
Andrei Vagind1be35c2018-04-10 16:31:16 -0700316 num_to_str(phandle_str, sizeof(phandle_str), np->phandle, 0);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200317
318 of_unittest_printf_one(np, "%pOF", full_name);
319 of_unittest_printf_one(np, "%pOFf", full_name);
Rob Herring69013782018-08-27 09:05:06 -0500320 of_unittest_printf_one(np, "%pOFn", "dev");
321 of_unittest_printf_one(np, "%2pOFn", "dev");
322 of_unittest_printf_one(np, "%5pOFn", " dev");
323 of_unittest_printf_one(np, "%pOFnc", "dev:test-sub-device");
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +0200324 of_unittest_printf_one(np, "%pOFp", phandle_str);
325 of_unittest_printf_one(np, "%pOFP", "dev@100");
326 of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC");
327 of_unittest_printf_one(np, "%10pOFP", " dev@100");
328 of_unittest_printf_one(np, "%-10pOFP", "dev@100 ");
329 of_unittest_printf_one(of_root, "%pOFP", "/");
330 of_unittest_printf_one(np, "%pOFF", "----");
331 of_unittest_printf_one(np, "%pOFPF", "dev@100:----");
332 of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device");
333 of_unittest_printf_one(np, "%pOFc", "test-sub-device");
334 of_unittest_printf_one(np, "%pOFC",
335 "\"test-sub-device\",\"test-compat2\",\"test-compat3\"");
336}
337
Grant Likely841ec212014-10-02 13:09:15 +0100338struct node_hash {
339 struct hlist_node node;
340 struct device_node *np;
341};
342
Grant Likely2118f4b2014-10-07 11:30:31 +0100343static DEFINE_HASHTABLE(phandle_ht, 8);
Wang Long9697a552015-03-11 08:36:54 +0000344static void __init of_unittest_check_phandles(void)
Grant Likely841ec212014-10-02 13:09:15 +0100345{
346 struct device_node *np;
347 struct node_hash *nh;
348 struct hlist_node *tmp;
349 int i, dup_count = 0, phandle_count = 0;
Grant Likely841ec212014-10-02 13:09:15 +0100350
Grant Likely841ec212014-10-02 13:09:15 +0100351 for_each_of_allnodes(np) {
352 if (!np->phandle)
353 continue;
354
Grant Likely2118f4b2014-10-07 11:30:31 +0100355 hash_for_each_possible(phandle_ht, nh, node, np->phandle) {
Grant Likely841ec212014-10-02 13:09:15 +0100356 if (nh->np->phandle == np->phandle) {
Rob Herring0d638a02017-06-01 15:50:55 -0500357 pr_info("Duplicate phandle! %i used by %pOF and %pOF\n",
358 np->phandle, nh->np, np);
Grant Likely841ec212014-10-02 13:09:15 +0100359 dup_count++;
360 break;
361 }
362 }
363
364 nh = kzalloc(sizeof(*nh), GFP_KERNEL);
Geert Uytterhoeven2a656cb2019-05-02 14:45:35 +0200365 if (!nh)
Grant Likely841ec212014-10-02 13:09:15 +0100366 return;
367
368 nh->np = np;
Grant Likely2118f4b2014-10-07 11:30:31 +0100369 hash_add(phandle_ht, &nh->node, np->phandle);
Grant Likely841ec212014-10-02 13:09:15 +0100370 phandle_count++;
371 }
Wang Long9697a552015-03-11 08:36:54 +0000372 unittest(dup_count == 0, "Found %i duplicates in %i phandles\n",
Grant Likely841ec212014-10-02 13:09:15 +0100373 dup_count, phandle_count);
374
375 /* Clean up */
Grant Likely2118f4b2014-10-07 11:30:31 +0100376 hash_for_each_safe(phandle_ht, i, tmp, nh, node) {
Grant Likely841ec212014-10-02 13:09:15 +0100377 hash_del(&nh->node);
378 kfree(nh);
379 }
380}
381
Wang Long9697a552015-03-11 08:36:54 +0000382static void __init of_unittest_parse_phandle_with_args(void)
Grant Likely53a42092011-12-12 09:25:57 -0700383{
384 struct device_node *np;
385 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +0000386 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -0700387
Grant Likely53a42092011-12-12 09:25:57 -0700388 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
389 if (!np) {
390 pr_err("missing testcase data\n");
391 return;
392 }
393
Grant Likelybd69f732013-02-10 22:57:21 +0000394 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000395 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000396
Grant Likelyf7f951c2013-02-12 17:41:22 +0000397 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -0700398 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -0700399
Frank Rowandeeb07c52018-10-04 20:40:21 -0700400 memset(&args, 0, sizeof(args));
Grant Likely53a42092011-12-12 09:25:57 -0700401 rc = of_parse_phandle_with_args(np, "phandle-list",
402 "#phandle-cells", i, &args);
403
404 /* Test the values from tests-phandle.dtsi */
405 switch (i) {
406 case 0:
407 passed &= !rc;
408 passed &= (args.args_count == 1);
409 passed &= (args.args[0] == (i + 1));
410 break;
411 case 1:
412 passed &= !rc;
413 passed &= (args.args_count == 2);
414 passed &= (args.args[0] == (i + 1));
415 passed &= (args.args[1] == 0);
416 break;
417 case 2:
418 passed &= (rc == -ENOENT);
419 break;
420 case 3:
421 passed &= !rc;
422 passed &= (args.args_count == 3);
423 passed &= (args.args[0] == (i + 1));
424 passed &= (args.args[1] == 4);
425 passed &= (args.args[2] == 3);
426 break;
427 case 4:
428 passed &= !rc;
429 passed &= (args.args_count == 2);
430 passed &= (args.args[0] == (i + 1));
431 passed &= (args.args[1] == 100);
432 break;
433 case 5:
434 passed &= !rc;
435 passed &= (args.args_count == 0);
436 break;
437 case 6:
438 passed &= !rc;
439 passed &= (args.args_count == 1);
440 passed &= (args.args[0] == (i + 1));
441 break;
442 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +0000443 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -0700444 break;
445 default:
446 passed = false;
447 }
448
Rob Herring0d638a02017-06-01 15:50:55 -0500449 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
450 i, args.np, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700451 }
452
453 /* Check for missing list property */
Frank Rowandeeb07c52018-10-04 20:40:21 -0700454 memset(&args, 0, sizeof(args));
Grant Likely53a42092011-12-12 09:25:57 -0700455 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
456 "#phandle-cells", 0, &args);
Wang Long9697a552015-03-11 08:36:54 +0000457 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000458 rc = of_count_phandle_with_args(np, "phandle-list-missing",
459 "#phandle-cells");
Wang Long9697a552015-03-11 08:36:54 +0000460 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700461
462 /* Check for missing cells property */
Frank Rowandeeb07c52018-10-04 20:40:21 -0700463 memset(&args, 0, sizeof(args));
Frank Rowand0ac17432020-02-20 12:40:21 -0600464
465 EXPECT_BEGIN(KERN_INFO,
466 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
467
Grant Likely53a42092011-12-12 09:25:57 -0700468 rc = of_parse_phandle_with_args(np, "phandle-list",
469 "#phandle-cells-missing", 0, &args);
Frank Rowand0ac17432020-02-20 12:40:21 -0600470
471 EXPECT_END(KERN_INFO,
472 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
473
Wang Long9697a552015-03-11 08:36:54 +0000474 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Frank Rowand0ac17432020-02-20 12:40:21 -0600475
476 EXPECT_BEGIN(KERN_INFO,
477 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
478
Grant Likelybd69f732013-02-10 22:57:21 +0000479 rc = of_count_phandle_with_args(np, "phandle-list",
480 "#phandle-cells-missing");
Frank Rowand0ac17432020-02-20 12:40:21 -0600481
482 EXPECT_END(KERN_INFO,
483 "OF: /testcase-data/phandle-tests/consumer-a: could not get #phandle-cells-missing for /testcase-data/phandle-tests/provider1");
484
Wang Long9697a552015-03-11 08:36:54 +0000485 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700486
487 /* Check for bad phandle in list */
Frank Rowandeeb07c52018-10-04 20:40:21 -0700488 memset(&args, 0, sizeof(args));
Frank Rowand0ac17432020-02-20 12:40:21 -0600489
490 EXPECT_BEGIN(KERN_INFO,
491 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
492
Grant Likely53a42092011-12-12 09:25:57 -0700493 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
494 "#phandle-cells", 0, &args);
Frank Rowand0ac17432020-02-20 12:40:21 -0600495
496 EXPECT_END(KERN_INFO,
497 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
498
Wang Long9697a552015-03-11 08:36:54 +0000499 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Frank Rowand0ac17432020-02-20 12:40:21 -0600500
501 EXPECT_BEGIN(KERN_INFO,
502 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
503
Grant Likelybd69f732013-02-10 22:57:21 +0000504 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
505 "#phandle-cells");
Frank Rowand0ac17432020-02-20 12:40:21 -0600506
507 EXPECT_END(KERN_INFO,
508 "OF: /testcase-data/phandle-tests/consumer-a: could not find phandle");
509
Wang Long9697a552015-03-11 08:36:54 +0000510 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700511
512 /* Check for incorrectly formed argument list */
Frank Rowandeeb07c52018-10-04 20:40:21 -0700513 memset(&args, 0, sizeof(args));
Frank Rowand0ac17432020-02-20 12:40:21 -0600514
515 EXPECT_BEGIN(KERN_INFO,
516 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
517
Grant Likely53a42092011-12-12 09:25:57 -0700518 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
519 "#phandle-cells", 1, &args);
Frank Rowand0ac17432020-02-20 12:40:21 -0600520
521 EXPECT_END(KERN_INFO,
522 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
523
Wang Long9697a552015-03-11 08:36:54 +0000524 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Frank Rowand0ac17432020-02-20 12:40:21 -0600525
526 EXPECT_BEGIN(KERN_INFO,
527 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
528
Grant Likelybd69f732013-02-10 22:57:21 +0000529 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
530 "#phandle-cells");
Frank Rowand0ac17432020-02-20 12:40:21 -0600531
532 EXPECT_END(KERN_INFO,
533 "OF: /testcase-data/phandle-tests/consumer-a: #phandle-cells = 3 found -1");
534
Wang Long9697a552015-03-11 08:36:54 +0000535 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700536}
537
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800538static void __init of_unittest_parse_phandle_with_args_map(void)
539{
540 struct device_node *np, *p0, *p1, *p2, *p3;
541 struct of_phandle_args args;
542 int i, rc;
543
544 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-b");
545 if (!np) {
546 pr_err("missing testcase data\n");
547 return;
548 }
549
550 p0 = of_find_node_by_path("/testcase-data/phandle-tests/provider0");
551 if (!p0) {
552 pr_err("missing testcase data\n");
553 return;
554 }
555
556 p1 = of_find_node_by_path("/testcase-data/phandle-tests/provider1");
557 if (!p1) {
558 pr_err("missing testcase data\n");
559 return;
560 }
561
562 p2 = of_find_node_by_path("/testcase-data/phandle-tests/provider2");
563 if (!p2) {
564 pr_err("missing testcase data\n");
565 return;
566 }
567
568 p3 = of_find_node_by_path("/testcase-data/phandle-tests/provider3");
569 if (!p3) {
570 pr_err("missing testcase data\n");
571 return;
572 }
573
574 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
575 unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
576
577 for (i = 0; i < 8; i++) {
578 bool passed = true;
579
Frank Rowandeeb07c52018-10-04 20:40:21 -0700580 memset(&args, 0, sizeof(args));
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800581 rc = of_parse_phandle_with_args_map(np, "phandle-list",
582 "phandle", i, &args);
583
584 /* Test the values from tests-phandle.dtsi */
585 switch (i) {
586 case 0:
587 passed &= !rc;
588 passed &= (args.np == p1);
589 passed &= (args.args_count == 1);
590 passed &= (args.args[0] == 1);
591 break;
592 case 1:
593 passed &= !rc;
594 passed &= (args.np == p3);
595 passed &= (args.args_count == 3);
596 passed &= (args.args[0] == 2);
597 passed &= (args.args[1] == 5);
598 passed &= (args.args[2] == 3);
599 break;
600 case 2:
601 passed &= (rc == -ENOENT);
602 break;
603 case 3:
604 passed &= !rc;
605 passed &= (args.np == p0);
606 passed &= (args.args_count == 0);
607 break;
608 case 4:
609 passed &= !rc;
610 passed &= (args.np == p1);
611 passed &= (args.args_count == 1);
612 passed &= (args.args[0] == 3);
613 break;
614 case 5:
615 passed &= !rc;
616 passed &= (args.np == p0);
617 passed &= (args.args_count == 0);
618 break;
619 case 6:
620 passed &= !rc;
621 passed &= (args.np == p2);
622 passed &= (args.args_count == 2);
623 passed &= (args.args[0] == 15);
624 passed &= (args.args[1] == 0x20);
625 break;
626 case 7:
627 passed &= (rc == -ENOENT);
628 break;
629 default:
630 passed = false;
631 }
632
633 unittest(passed, "index %i - data error on node %s rc=%i\n",
634 i, args.np->full_name, rc);
635 }
636
637 /* Check for missing list property */
Frank Rowandeeb07c52018-10-04 20:40:21 -0700638 memset(&args, 0, sizeof(args));
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800639 rc = of_parse_phandle_with_args_map(np, "phandle-list-missing",
640 "phandle", 0, &args);
641 unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
642
643 /* Check for missing cells,map,mask property */
Frank Rowandeeb07c52018-10-04 20:40:21 -0700644 memset(&args, 0, sizeof(args));
Frank Rowand0ac17432020-02-20 12:40:21 -0600645
646 EXPECT_BEGIN(KERN_INFO,
647 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
648
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800649 rc = of_parse_phandle_with_args_map(np, "phandle-list",
650 "phandle-missing", 0, &args);
Frank Rowand0ac17432020-02-20 12:40:21 -0600651 EXPECT_END(KERN_INFO,
652 "OF: /testcase-data/phandle-tests/consumer-b: could not get #phandle-missing-cells for /testcase-data/phandle-tests/provider1");
653
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800654 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
655
656 /* Check for bad phandle in list */
Frank Rowandeeb07c52018-10-04 20:40:21 -0700657 memset(&args, 0, sizeof(args));
Frank Rowand0ac17432020-02-20 12:40:21 -0600658
659 EXPECT_BEGIN(KERN_INFO,
660 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle");
661
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800662 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-phandle",
663 "phandle", 0, &args);
Frank Rowand0ac17432020-02-20 12:40:21 -0600664 EXPECT_END(KERN_INFO,
665 "OF: /testcase-data/phandle-tests/consumer-b: could not find phandle");
666
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800667 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
668
669 /* Check for incorrectly formed argument list */
Frank Rowandeeb07c52018-10-04 20:40:21 -0700670 memset(&args, 0, sizeof(args));
Frank Rowand0ac17432020-02-20 12:40:21 -0600671
672 EXPECT_BEGIN(KERN_INFO,
673 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found -1");
674
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800675 rc = of_parse_phandle_with_args_map(np, "phandle-list-bad-args",
676 "phandle", 1, &args);
Frank Rowand0ac17432020-02-20 12:40:21 -0600677 EXPECT_END(KERN_INFO,
678 "OF: /testcase-data/phandle-tests/consumer-b: #phandle-cells = 2 found -1");
679
Stephen Boyd357aa4b2018-01-30 18:36:17 -0800680 unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
681}
682
Wang Long9697a552015-03-11 08:36:54 +0000683static void __init of_unittest_property_string(void)
Grant Likely7aff0fe2011-12-12 09:25:58 -0700684{
Grant Likelya87fa1d2014-11-03 15:15:35 +0000685 const char *strings[4];
Grant Likely7aff0fe2011-12-12 09:25:58 -0700686 struct device_node *np;
687 int rc;
688
Grant Likely7aff0fe2011-12-12 09:25:58 -0700689 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
690 if (!np) {
691 pr_err("No testcase data in device tree\n");
692 return;
693 }
694
695 rc = of_property_match_string(np, "phandle-list-names", "first");
Wang Long9697a552015-03-11 08:36:54 +0000696 unittest(rc == 0, "first expected:0 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700697 rc = of_property_match_string(np, "phandle-list-names", "second");
Wang Long9697a552015-03-11 08:36:54 +0000698 unittest(rc == 1, "second expected:1 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700699 rc = of_property_match_string(np, "phandle-list-names", "third");
Wang Long9697a552015-03-11 08:36:54 +0000700 unittest(rc == 2, "third expected:2 got:%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700701 rc = of_property_match_string(np, "phandle-list-names", "fourth");
Wang Long9697a552015-03-11 08:36:54 +0000702 unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700703 rc = of_property_match_string(np, "missing-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000704 unittest(rc == -EINVAL, "missing property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700705 rc = of_property_match_string(np, "empty-property", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000706 unittest(rc == -ENODATA, "empty property; rc=%i\n", rc);
Grant Likely7aff0fe2011-12-12 09:25:58 -0700707 rc = of_property_match_string(np, "unterminated-string", "blah");
Wang Long9697a552015-03-11 08:36:54 +0000708 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000709
710 /* of_property_count_strings() tests */
711 rc = of_property_count_strings(np, "string-property");
Wang Long9697a552015-03-11 08:36:54 +0000712 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000713 rc = of_property_count_strings(np, "phandle-list-names");
Wang Long9697a552015-03-11 08:36:54 +0000714 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000715 rc = of_property_count_strings(np, "unterminated-string");
Wang Long9697a552015-03-11 08:36:54 +0000716 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000717 rc = of_property_count_strings(np, "unterminated-string-list");
Wang Long9697a552015-03-11 08:36:54 +0000718 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000719
720 /* of_property_read_string_index() tests */
721 rc = of_property_read_string_index(np, "string-property", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000722 unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000723 strings[0] = NULL;
724 rc = of_property_read_string_index(np, "string-property", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000725 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000726 rc = of_property_read_string_index(np, "phandle-list-names", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000727 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000728 rc = of_property_read_string_index(np, "phandle-list-names", 1, strings);
Wang Long9697a552015-03-11 08:36:54 +0000729 unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000730 rc = of_property_read_string_index(np, "phandle-list-names", 2, strings);
Wang Long9697a552015-03-11 08:36:54 +0000731 unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000732 strings[0] = NULL;
733 rc = of_property_read_string_index(np, "phandle-list-names", 3, strings);
Wang Long9697a552015-03-11 08:36:54 +0000734 unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000735 strings[0] = NULL;
736 rc = of_property_read_string_index(np, "unterminated-string", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000737 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000738 rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings);
Wang Long9697a552015-03-11 08:36:54 +0000739 unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000740 strings[0] = NULL;
741 rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */
Wang Long9697a552015-03-11 08:36:54 +0000742 unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000743 strings[1] = NULL;
744
745 /* of_property_read_string_array() tests */
746 rc = of_property_read_string_array(np, "string-property", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000747 unittest(rc == 1, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000748 rc = of_property_read_string_array(np, "phandle-list-names", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000749 unittest(rc == 3, "Incorrect string count; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000750 rc = of_property_read_string_array(np, "unterminated-string", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000751 unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000752 /* -- An incorrectly formed string should cause a failure */
753 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4);
Wang Long9697a552015-03-11 08:36:54 +0000754 unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000755 /* -- parsing the correctly formed strings should still work: */
756 strings[2] = NULL;
757 rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2);
Wang Long9697a552015-03-11 08:36:54 +0000758 unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc);
Grant Likelya87fa1d2014-11-03 15:15:35 +0000759 strings[1] = NULL;
760 rc = of_property_read_string_array(np, "phandle-list-names", strings, 1);
Wang Long9697a552015-03-11 08:36:54 +0000761 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 -0700762}
763
Pantelis Antoniou69843392014-07-04 19:58:47 +0300764#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
765 (p1)->value && (p2)->value && \
766 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
767 !strcmp((p1)->name, (p2)->name))
Wang Long9697a552015-03-11 08:36:54 +0000768static void __init of_unittest_property_copy(void)
Pantelis Antoniou69843392014-07-04 19:58:47 +0300769{
770#ifdef CONFIG_OF_DYNAMIC
771 struct property p1 = { .name = "p1", .length = 0, .value = "" };
772 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
773 struct property *new;
774
775 new = __of_prop_dup(&p1, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000776 unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300777 kfree(new->value);
778 kfree(new->name);
779 kfree(new);
780
781 new = __of_prop_dup(&p2, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000782 unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
Pantelis Antoniou69843392014-07-04 19:58:47 +0300783 kfree(new->value);
784 kfree(new->name);
785 kfree(new);
786#endif
787}
788
Wang Long9697a552015-03-11 08:36:54 +0000789static void __init of_unittest_changeset(void)
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300790{
791#ifdef CONFIG_OF_DYNAMIC
Frank Rowanda4f91f02018-02-26 14:01:22 -0800792 struct property *ppadd, padd = { .name = "prop-add", .length = 1, .value = "" };
793 struct property *ppname_n1, pname_n1 = { .name = "name", .length = 3, .value = "n1" };
794 struct property *ppname_n2, pname_n2 = { .name = "name", .length = 3, .value = "n2" };
795 struct property *ppname_n21, pname_n21 = { .name = "name", .length = 3, .value = "n21" };
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300796 struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" };
797 struct property *ppremove;
Frank Rowanda4f91f02018-02-26 14:01:22 -0800798 struct device_node *n1, *n2, *n21, *nchangeset, *nremove, *parent, *np;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300799 struct of_changeset chgset;
800
Frank Rowandb89dae12018-02-26 14:01:23 -0800801 n1 = __of_node_dup(NULL, "n1");
Wang Long9697a552015-03-11 08:36:54 +0000802 unittest(n1, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800803
Frank Rowandb89dae12018-02-26 14:01:23 -0800804 n2 = __of_node_dup(NULL, "n2");
Wang Long9697a552015-03-11 08:36:54 +0000805 unittest(n2, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800806
Frank Rowandb89dae12018-02-26 14:01:23 -0800807 n21 = __of_node_dup(NULL, "n21");
Wang Long9697a552015-03-11 08:36:54 +0000808 unittest(n21, "testcase setup failure %p\n", n21);
Frank Rowanda4f91f02018-02-26 14:01:22 -0800809
810 nchangeset = of_find_node_by_path("/testcase-data/changeset");
811 nremove = of_get_child_by_name(nchangeset, "node-remove");
Wang Long9697a552015-03-11 08:36:54 +0000812 unittest(nremove, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800813
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300814 ppadd = __of_prop_dup(&padd, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000815 unittest(ppadd, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800816
817 ppname_n1 = __of_prop_dup(&pname_n1, GFP_KERNEL);
818 unittest(ppname_n1, "testcase setup failure\n");
819
820 ppname_n2 = __of_prop_dup(&pname_n2, GFP_KERNEL);
821 unittest(ppname_n2, "testcase setup failure\n");
822
823 ppname_n21 = __of_prop_dup(&pname_n21, GFP_KERNEL);
824 unittest(ppname_n21, "testcase setup failure\n");
825
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300826 ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL);
Wang Long9697a552015-03-11 08:36:54 +0000827 unittest(ppupdate, "testcase setup failure\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800828
829 parent = nchangeset;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300830 n1->parent = parent;
831 n2->parent = parent;
832 n21->parent = n2;
Frank Rowanda4f91f02018-02-26 14:01:22 -0800833
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300834 ppremove = of_find_property(parent, "prop-remove", NULL);
Wang Long9697a552015-03-11 08:36:54 +0000835 unittest(ppremove, "failed to find removal prop");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300836
837 of_changeset_init(&chgset);
Frank Rowanda4f91f02018-02-26 14:01:22 -0800838
Wang Long9697a552015-03-11 08:36:54 +0000839 unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800840 unittest(!of_changeset_add_property(&chgset, n1, ppname_n1), "fail add prop name\n");
841
Wang Long9697a552015-03-11 08:36:54 +0000842 unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800843 unittest(!of_changeset_add_property(&chgset, n2, ppname_n2), "fail add prop name\n");
844
Wang Long9697a552015-03-11 08:36:54 +0000845 unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800846 unittest(!of_changeset_add_property(&chgset, n21, ppname_n21), "fail add prop name\n");
847
Wang Long9697a552015-03-11 08:36:54 +0000848 unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800849
850 unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop prop-add\n");
Wang Long9697a552015-03-11 08:36:54 +0000851 unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n");
852 unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n");
Frank Rowanda4f91f02018-02-26 14:01:22 -0800853
Wang Long9697a552015-03-11 08:36:54 +0000854 unittest(!of_changeset_apply(&chgset), "apply failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300855
Frank Rowanda4f91f02018-02-26 14:01:22 -0800856 of_node_put(nchangeset);
857
Grant Likelye5179582014-11-17 22:31:32 +0000858 /* Make sure node names are constructed correctly */
Wang Long9697a552015-03-11 08:36:54 +0000859 unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")),
Rob Herring0d638a02017-06-01 15:50:55 -0500860 "'%pOF' not added\n", n21);
Markus Elfringc46ca3c2014-12-02 13:54:00 +0100861 of_node_put(np);
Grant Likelye5179582014-11-17 22:31:32 +0000862
Wang Long9697a552015-03-11 08:36:54 +0000863 unittest(!of_changeset_revert(&chgset), "revert failed\n");
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300864
865 of_changeset_destroy(&chgset);
Frank Rowandb3fb36e2020-04-16 16:42:46 -0500866
867 of_node_put(n1);
868 of_node_put(n2);
869 of_node_put(n21);
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300870#endif
871}
872
Nicolas Saenz Julienne07d13a12020-11-19 18:53:56 +0100873static void __init of_unittest_dma_get_max_cpu_address(void)
874{
875 struct device_node *np;
876 phys_addr_t cpu_addr;
877
Catalin Marinasaed50412020-12-01 12:47:25 +0000878 if (!IS_ENABLED(CONFIG_OF_ADDRESS))
879 return;
880
Nicolas Saenz Julienne07d13a12020-11-19 18:53:56 +0100881 np = of_find_node_by_path("/testcase-data/address-tests");
882 if (!np) {
883 pr_err("missing testcase data\n");
884 return;
885 }
886
887 cpu_addr = of_dma_get_max_cpu_address(np);
888 unittest(cpu_addr == 0x4fffffff,
889 "of_dma_get_max_cpu_address: wrong CPU addr %pad (expecting %x)\n",
890 &cpu_addr, 0x4fffffff);
891}
892
Rob Herring04db93a2019-09-20 13:28:53 -0500893static void __init of_unittest_dma_ranges_one(const char *path,
Jim Quinlane0d07272020-09-17 18:43:40 +0200894 u64 expect_dma_addr, u64 expect_paddr)
Rob Herring04db93a2019-09-20 13:28:53 -0500895{
Jim Quinlane0d07272020-09-17 18:43:40 +0200896#ifdef CONFIG_HAS_DMA
Rob Herring04db93a2019-09-20 13:28:53 -0500897 struct device_node *np;
Jim Quinlane0d07272020-09-17 18:43:40 +0200898 const struct bus_dma_region *map = NULL;
Rob Herring04db93a2019-09-20 13:28:53 -0500899 int rc;
900
901 np = of_find_node_by_path(path);
902 if (!np) {
903 pr_err("missing testcase data\n");
904 return;
905 }
906
Jim Quinlane0d07272020-09-17 18:43:40 +0200907 rc = of_dma_get_range(np, &map);
Rob Herring04db93a2019-09-20 13:28:53 -0500908
909 unittest(!rc, "of_dma_get_range failed on node %pOF rc=%i\n", np, rc);
Jim Quinlane0d07272020-09-17 18:43:40 +0200910
Rob Herring04db93a2019-09-20 13:28:53 -0500911 if (!rc) {
Jim Quinlane0d07272020-09-17 18:43:40 +0200912 phys_addr_t paddr;
913 dma_addr_t dma_addr;
914 struct device dev_bogus;
915
916 dev_bogus.dma_range_map = map;
917 paddr = dma_to_phys(&dev_bogus, expect_dma_addr);
918 dma_addr = phys_to_dma(&dev_bogus, expect_paddr);
919
Rob Herring04db93a2019-09-20 13:28:53 -0500920 unittest(paddr == expect_paddr,
Jim Quinlane0d07272020-09-17 18:43:40 +0200921 "of_dma_get_range: wrong phys addr %pap (expecting %llx) on node %pOF\n",
922 &paddr, expect_paddr, np);
Rob Herring04db93a2019-09-20 13:28:53 -0500923 unittest(dma_addr == expect_dma_addr,
Jim Quinlane0d07272020-09-17 18:43:40 +0200924 "of_dma_get_range: wrong DMA addr %pad (expecting %llx) on node %pOF\n",
925 &dma_addr, expect_dma_addr, np);
926
927 kfree(map);
Rob Herring04db93a2019-09-20 13:28:53 -0500928 }
929 of_node_put(np);
Jim Quinlane0d07272020-09-17 18:43:40 +0200930#endif
Rob Herring04db93a2019-09-20 13:28:53 -0500931}
932
933static void __init of_unittest_parse_dma_ranges(void)
934{
935 of_unittest_dma_ranges_one("/testcase-data/address-tests/device@70000000",
Jim Quinlane0d07272020-09-17 18:43:40 +0200936 0x0, 0x20000000);
Rob Herring04db93a2019-09-20 13:28:53 -0500937 of_unittest_dma_ranges_one("/testcase-data/address-tests/bus@80000000/device@1000",
Jim Quinlane0d07272020-09-17 18:43:40 +0200938 0x100000000, 0x20000000);
Rob Herring04db93a2019-09-20 13:28:53 -0500939 of_unittest_dma_ranges_one("/testcase-data/address-tests/pci@90000000",
Jim Quinlane0d07272020-09-17 18:43:40 +0200940 0x80000000, 0x20000000);
Rob Herring04db93a2019-09-20 13:28:53 -0500941}
942
943static void __init of_unittest_pci_dma_ranges(void)
944{
945 struct device_node *np;
946 struct of_pci_range range;
947 struct of_pci_range_parser parser;
948 int i = 0;
949
950 if (!IS_ENABLED(CONFIG_PCI))
951 return;
952
953 np = of_find_node_by_path("/testcase-data/address-tests/pci@90000000");
954 if (!np) {
955 pr_err("missing testcase data\n");
956 return;
957 }
958
959 if (of_pci_dma_range_parser_init(&parser, np)) {
960 pr_err("missing dma-ranges property\n");
961 return;
962 }
963
964 /*
965 * Get the dma-ranges from the device tree
966 */
967 for_each_of_pci_range(&parser, &range) {
968 if (!i) {
969 unittest(range.size == 0x10000000,
970 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
971 np, range.size);
972 unittest(range.cpu_addr == 0x20000000,
973 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
974 range.cpu_addr, np);
975 unittest(range.pci_addr == 0x80000000,
976 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
977 range.pci_addr, np);
978 } else {
979 unittest(range.size == 0x10000000,
980 "for_each_of_pci_range wrong size on node %pOF size=%llx\n",
981 np, range.size);
982 unittest(range.cpu_addr == 0x40000000,
983 "for_each_of_pci_range wrong CPU addr (%llx) on node %pOF",
984 range.cpu_addr, np);
985 unittest(range.pci_addr == 0xc0000000,
986 "for_each_of_pci_range wrong DMA addr (%llx) on node %pOF",
987 range.pci_addr, np);
988 }
989 i++;
990 }
991
992 of_node_put(np);
993}
994
Wang Long9697a552015-03-11 08:36:54 +0000995static void __init of_unittest_parse_interrupts(void)
Grant Likelya9f10ca2013-10-11 22:04:23 +0100996{
997 struct device_node *np;
998 struct of_phandle_args args;
999 int i, rc;
1000
Guenter Roeckda08d8c2018-09-25 21:06:24 -07001001 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1002 return;
1003
Grant Likelya9f10ca2013-10-11 22:04:23 +01001004 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
1005 if (!np) {
1006 pr_err("missing testcase data\n");
1007 return;
1008 }
1009
1010 for (i = 0; i < 4; i++) {
1011 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -07001012
Frank Rowandeeb07c52018-10-04 20:40:21 -07001013 memset(&args, 0, sizeof(args));
Grant Likelya9f10ca2013-10-11 22:04:23 +01001014 rc = of_irq_parse_one(np, i, &args);
1015
1016 passed &= !rc;
1017 passed &= (args.args_count == 1);
1018 passed &= (args.args[0] == (i + 1));
1019
Rob Herring0d638a02017-06-01 15:50:55 -05001020 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1021 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +01001022 }
1023 of_node_put(np);
1024
1025 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
1026 if (!np) {
1027 pr_err("missing testcase data\n");
1028 return;
1029 }
1030
1031 for (i = 0; i < 4; i++) {
1032 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -07001033
Frank Rowandeeb07c52018-10-04 20:40:21 -07001034 memset(&args, 0, sizeof(args));
Grant Likelya9f10ca2013-10-11 22:04:23 +01001035 rc = of_irq_parse_one(np, i, &args);
1036
1037 /* Test the values from tests-phandle.dtsi */
1038 switch (i) {
1039 case 0:
1040 passed &= !rc;
1041 passed &= (args.args_count == 1);
1042 passed &= (args.args[0] == 9);
1043 break;
1044 case 1:
1045 passed &= !rc;
1046 passed &= (args.args_count == 3);
1047 passed &= (args.args[0] == 10);
1048 passed &= (args.args[1] == 11);
1049 passed &= (args.args[2] == 12);
1050 break;
1051 case 2:
1052 passed &= !rc;
1053 passed &= (args.args_count == 2);
1054 passed &= (args.args[0] == 13);
1055 passed &= (args.args[1] == 14);
1056 break;
1057 case 3:
1058 passed &= !rc;
1059 passed &= (args.args_count == 2);
1060 passed &= (args.args[0] == 15);
1061 passed &= (args.args[1] == 16);
1062 break;
1063 default:
1064 passed = false;
1065 }
Rob Herring0d638a02017-06-01 15:50:55 -05001066 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1067 i, args.np, rc);
Grant Likelya9f10ca2013-10-11 22:04:23 +01001068 }
1069 of_node_put(np);
1070}
1071
Wang Long9697a552015-03-11 08:36:54 +00001072static void __init of_unittest_parse_interrupts_extended(void)
Grant Likely79d97012013-09-19 16:47:37 -05001073{
1074 struct device_node *np;
1075 struct of_phandle_args args;
1076 int i, rc;
1077
Guenter Roeckda08d8c2018-09-25 21:06:24 -07001078 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
1079 return;
1080
Grant Likely79d97012013-09-19 16:47:37 -05001081 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
1082 if (!np) {
1083 pr_err("missing testcase data\n");
1084 return;
1085 }
1086
1087 for (i = 0; i < 7; i++) {
1088 bool passed = true;
Frank Rowand3db316d2015-03-14 00:02:31 -07001089
Frank Rowandeeb07c52018-10-04 20:40:21 -07001090 memset(&args, 0, sizeof(args));
Grant Likely79d97012013-09-19 16:47:37 -05001091 rc = of_irq_parse_one(np, i, &args);
1092
1093 /* Test the values from tests-phandle.dtsi */
1094 switch (i) {
1095 case 0:
1096 passed &= !rc;
1097 passed &= (args.args_count == 1);
1098 passed &= (args.args[0] == 1);
1099 break;
1100 case 1:
1101 passed &= !rc;
1102 passed &= (args.args_count == 3);
1103 passed &= (args.args[0] == 2);
1104 passed &= (args.args[1] == 3);
1105 passed &= (args.args[2] == 4);
1106 break;
1107 case 2:
1108 passed &= !rc;
1109 passed &= (args.args_count == 2);
1110 passed &= (args.args[0] == 5);
1111 passed &= (args.args[1] == 6);
1112 break;
1113 case 3:
1114 passed &= !rc;
1115 passed &= (args.args_count == 1);
1116 passed &= (args.args[0] == 9);
1117 break;
1118 case 4:
1119 passed &= !rc;
1120 passed &= (args.args_count == 3);
1121 passed &= (args.args[0] == 10);
1122 passed &= (args.args[1] == 11);
1123 passed &= (args.args[2] == 12);
1124 break;
1125 case 5:
1126 passed &= !rc;
1127 passed &= (args.args_count == 2);
1128 passed &= (args.args[0] == 13);
1129 passed &= (args.args[1] == 14);
1130 break;
1131 case 6:
Frank Rowand95265652021-10-29 20:10:39 -05001132 /*
1133 * Tests child node that is missing property
1134 * #address-cells. See the comments in
1135 * drivers/of/unittest-data/tests-interrupts.dtsi
1136 * nodes intmap1 and interrupts-extended0
1137 */
Grant Likely79d97012013-09-19 16:47:37 -05001138 passed &= !rc;
1139 passed &= (args.args_count == 1);
1140 passed &= (args.args[0] == 15);
1141 break;
1142 default:
1143 passed = false;
1144 }
1145
Rob Herring0d638a02017-06-01 15:50:55 -05001146 unittest(passed, "index %i - data error on node %pOF rc=%i\n",
1147 i, args.np, rc);
Grant Likely79d97012013-09-19 16:47:37 -05001148 }
1149 of_node_put(np);
1150}
1151
Frank Rowandafaed7a2015-03-14 00:00:36 -07001152static const struct of_device_id match_node_table[] = {
Grant Likely1f42e5d2014-02-18 21:38:55 +00001153 { .data = "A", .name = "name0", }, /* Name alone is lowest priority */
1154 { .data = "B", .type = "type1", }, /* followed by type alone */
1155
1156 { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */
1157 { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */
1158 { .data = "Cc", .name = "name2", .type = "type2", },
1159
1160 { .data = "E", .compatible = "compat3" },
1161 { .data = "G", .compatible = "compat2", },
1162 { .data = "H", .compatible = "compat2", .name = "name5", },
1163 { .data = "I", .compatible = "compat2", .type = "type1", },
1164 { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", },
1165 { .data = "K", .compatible = "compat2", .name = "name9", },
1166 {}
1167};
1168
1169static struct {
1170 const char *path;
1171 const char *data;
1172} match_node_tests[] = {
1173 { .path = "/testcase-data/match-node/name0", .data = "A", },
1174 { .path = "/testcase-data/match-node/name1", .data = "B", },
1175 { .path = "/testcase-data/match-node/a/name2", .data = "Ca", },
1176 { .path = "/testcase-data/match-node/b/name2", .data = "Cb", },
1177 { .path = "/testcase-data/match-node/c/name2", .data = "Cc", },
1178 { .path = "/testcase-data/match-node/name3", .data = "E", },
1179 { .path = "/testcase-data/match-node/name4", .data = "G", },
1180 { .path = "/testcase-data/match-node/name5", .data = "H", },
1181 { .path = "/testcase-data/match-node/name6", .data = "G", },
1182 { .path = "/testcase-data/match-node/name7", .data = "I", },
1183 { .path = "/testcase-data/match-node/name8", .data = "J", },
1184 { .path = "/testcase-data/match-node/name9", .data = "K", },
1185};
1186
Wang Long9697a552015-03-11 08:36:54 +00001187static void __init of_unittest_match_node(void)
Grant Likely1f42e5d2014-02-18 21:38:55 +00001188{
1189 struct device_node *np;
1190 const struct of_device_id *match;
1191 int i;
1192
1193 for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) {
1194 np = of_find_node_by_path(match_node_tests[i].path);
1195 if (!np) {
Wang Long9697a552015-03-11 08:36:54 +00001196 unittest(0, "missing testcase node %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +00001197 match_node_tests[i].path);
1198 continue;
1199 }
1200
1201 match = of_match_node(match_node_table, np);
1202 if (!match) {
Wang Long9697a552015-03-11 08:36:54 +00001203 unittest(0, "%s didn't match anything\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +00001204 match_node_tests[i].path);
1205 continue;
1206 }
1207
1208 if (strcmp(match->data, match_node_tests[i].data) != 0) {
Wang Long9697a552015-03-11 08:36:54 +00001209 unittest(0, "%s got wrong match. expected %s, got %s\n",
Grant Likely1f42e5d2014-02-18 21:38:55 +00001210 match_node_tests[i].path, match_node_tests[i].data,
1211 (const char *)match->data);
1212 continue;
1213 }
Wang Long9697a552015-03-11 08:36:54 +00001214 unittest(1, "passed");
Grant Likely1f42e5d2014-02-18 21:38:55 +00001215 }
1216}
1217
Zhen Lei67c54652021-06-01 14:22:23 +08001218static struct resource test_bus_res = DEFINE_RES_MEM(0xfffffff8, 2);
Grant Likely37791b62015-03-27 20:30:04 -07001219static const struct platform_device_info test_bus_info = {
1220 .name = "unittest-bus",
Grant Likely851da972014-11-04 13:14:13 +00001221};
Wang Long9697a552015-03-11 08:36:54 +00001222static void __init of_unittest_platform_populate(void)
Rob Herring82c0f582014-04-23 17:57:40 -05001223{
Grant Likely851da972014-11-04 13:14:13 +00001224 int irq, rc;
1225 struct device_node *np, *child, *grandchild;
Grant Likely37791b62015-03-27 20:30:04 -07001226 struct platform_device *pdev, *test_bus;
Frank Rowandafaed7a2015-03-14 00:00:36 -07001227 const struct of_device_id match[] = {
Rob Herringfb2caa52014-05-13 10:07:54 -05001228 { .compatible = "test-device", },
1229 {}
1230 };
Rob Herring82c0f582014-04-23 17:57:40 -05001231
1232 np = of_find_node_by_path("/testcase-data");
Kefeng Wang146dedb2016-06-01 14:53:10 +08001233 of_platform_default_populate(np, NULL, NULL);
Rob Herring82c0f582014-04-23 17:57:40 -05001234
1235 /* Test that a missing irq domain returns -EPROBE_DEFER */
1236 np = of_find_node_by_path("/testcase-data/testcase-device1");
1237 pdev = of_find_device_by_node(np);
Wang Long9697a552015-03-11 08:36:54 +00001238 unittest(pdev, "device 1 creation failed\n");
Rob Herring7d1cdc82014-05-13 10:07:29 -05001239
Guenter Roeckda08d8c2018-09-25 21:06:24 -07001240 if (!(of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)) {
1241 irq = platform_get_irq(pdev, 0);
1242 unittest(irq == -EPROBE_DEFER,
1243 "device deferred probe failed - %d\n", irq);
Rob Herring82c0f582014-04-23 17:57:40 -05001244
Guenter Roeckda08d8c2018-09-25 21:06:24 -07001245 /* Test that a parsing failure does not return -EPROBE_DEFER */
1246 np = of_find_node_by_path("/testcase-data/testcase-device2");
1247 pdev = of_find_device_by_node(np);
1248 unittest(pdev, "device 2 creation failed\n");
Frank Rowand0ac17432020-02-20 12:40:21 -06001249
1250 EXPECT_BEGIN(KERN_INFO,
1251 "platform testcase-data:testcase-device2: IRQ index 0 not found");
1252
Guenter Roeckda08d8c2018-09-25 21:06:24 -07001253 irq = platform_get_irq(pdev, 0);
Frank Rowand0ac17432020-02-20 12:40:21 -06001254
1255 EXPECT_END(KERN_INFO,
1256 "platform testcase-data:testcase-device2: IRQ index 0 not found");
1257
Guenter Roeckda08d8c2018-09-25 21:06:24 -07001258 unittest(irq < 0 && irq != -EPROBE_DEFER,
1259 "device parsing error failed - %d\n", irq);
1260 }
Rob Herring82c0f582014-04-23 17:57:40 -05001261
Frank Rowand716e1d42015-03-13 23:57:40 -07001262 np = of_find_node_by_path("/testcase-data/platform-tests");
Grant Likelya2166ca2015-03-29 08:59:58 +01001263 unittest(np, "No testcase data in device tree\n");
Frank Rowand716e1d42015-03-13 23:57:40 -07001264 if (!np)
Rob Herringfb2caa52014-05-13 10:07:54 -05001265 return;
Grant Likely851da972014-11-04 13:14:13 +00001266
Grant Likely37791b62015-03-27 20:30:04 -07001267 test_bus = platform_device_register_full(&test_bus_info);
1268 rc = PTR_ERR_OR_ZERO(test_bus);
Grant Likelya2166ca2015-03-29 08:59:58 +01001269 unittest(!rc, "testbus registration failed; rc=%i\n", rc);
Nishka Dasguptaa7bcae52019-08-15 11:52:18 +05301270 if (rc) {
1271 of_node_put(np);
Grant Likely851da972014-11-04 13:14:13 +00001272 return;
Nishka Dasguptaa7bcae52019-08-15 11:52:18 +05301273 }
Grant Likely37791b62015-03-27 20:30:04 -07001274 test_bus->dev.of_node = np;
Rob Herringfb2caa52014-05-13 10:07:54 -05001275
Grant Likelyd2329fb2016-01-04 13:13:21 +01001276 /*
1277 * Add a dummy resource to the test bus node after it is
1278 * registered to catch problems with un-inserted resources. The
1279 * DT code doesn't insert the resources, and it has caused the
1280 * kernel to oops in the past. This makes sure the same bug
1281 * doesn't crop up again.
1282 */
1283 platform_device_add_resources(test_bus, &test_bus_res, 1);
1284
Grant Likely37791b62015-03-27 20:30:04 -07001285 of_platform_populate(np, match, NULL, &test_bus->dev);
Rob Herringfb2caa52014-05-13 10:07:54 -05001286 for_each_child_of_node(np, child) {
Frank Rowand216830d2020-04-16 16:42:47 -05001287 for_each_child_of_node(child, grandchild) {
1288 pdev = of_find_device_by_node(grandchild);
1289 unittest(pdev,
Rob Herringa613b262018-08-27 20:00:19 -05001290 "Could not create device for node '%pOFn'\n",
1291 grandchild);
Rob Herring83c4a4e2021-02-11 17:27:44 -06001292 platform_device_put(pdev);
Frank Rowand216830d2020-04-16 16:42:47 -05001293 }
Rob Herringfb2caa52014-05-13 10:07:54 -05001294 }
Grant Likely851da972014-11-04 13:14:13 +00001295
Grant Likely37791b62015-03-27 20:30:04 -07001296 of_platform_depopulate(&test_bus->dev);
Grant Likely851da972014-11-04 13:14:13 +00001297 for_each_child_of_node(np, child) {
1298 for_each_child_of_node(child, grandchild)
Wang Long9697a552015-03-11 08:36:54 +00001299 unittest(!of_find_device_by_node(grandchild),
Rob Herringa613b262018-08-27 20:00:19 -05001300 "device didn't get destroyed '%pOFn'\n",
1301 grandchild);
Grant Likely851da972014-11-04 13:14:13 +00001302 }
1303
Grant Likely37791b62015-03-27 20:30:04 -07001304 platform_device_unregister(test_bus);
Grant Likely851da972014-11-04 13:14:13 +00001305 of_node_put(np);
Rob Herring82c0f582014-04-23 17:57:40 -05001306}
1307
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001308/**
1309 * update_node_properties - adds the properties
1310 * of np into dup node (present in live tree) and
1311 * updates parent of children of np to dup.
1312 *
Frank Rowand5babefb2018-10-12 19:38:26 -07001313 * @np: node whose properties are being added to the live tree
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001314 * @dup: node present in live tree to be updated
1315 */
1316static void update_node_properties(struct device_node *np,
1317 struct device_node *dup)
1318{
1319 struct property *prop;
Frank Rowand5babefb2018-10-12 19:38:26 -07001320 struct property *save_next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001321 struct device_node *child;
Frank Rowand5babefb2018-10-12 19:38:26 -07001322 int ret;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001323
1324 for_each_child_of_node(np, child)
1325 child->parent = dup;
Frank Rowand5babefb2018-10-12 19:38:26 -07001326
1327 /*
1328 * "unittest internal error: unable to add testdata property"
1329 *
1330 * If this message reports a property in node '/__symbols__' then
1331 * the respective unittest overlay contains a label that has the
1332 * same name as a label in the live devicetree. The label will
1333 * be in the live devicetree only if the devicetree source was
1334 * compiled with the '-@' option. If you encounter this error,
1335 * please consider renaming __all__ of the labels in the unittest
1336 * overlay dts files with an odd prefix that is unlikely to be
1337 * used in a real devicetree.
1338 */
1339
1340 /*
1341 * open code for_each_property_of_node() because of_add_property()
1342 * sets prop->next to NULL
1343 */
1344 for (prop = np->properties; prop != NULL; prop = save_next) {
1345 save_next = prop->next;
1346 ret = of_add_property(dup, prop);
Frank Rowandfd25ffd2019-01-24 15:22:13 -08001347 if (ret) {
1348 if (ret == -EEXIST && !strcmp(prop->name, "name"))
1349 continue;
Frank Rowand5babefb2018-10-12 19:38:26 -07001350 pr_err("unittest internal error: unable to add testdata property %pOF/%s",
1351 np, prop->name);
Frank Rowandfd25ffd2019-01-24 15:22:13 -08001352 }
Frank Rowand5babefb2018-10-12 19:38:26 -07001353 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001354}
1355
1356/**
1357 * attach_node_and_children - attaches nodes
Frank Rowand89716dc2019-01-24 15:22:14 -08001358 * and its children to live tree.
1359 * CAUTION: misleading function name - if node @np already exists in
1360 * the live tree then children of @np are *not* attached to the live
1361 * tree. This works for the current test devicetree nodes because such
1362 * nodes do not have child nodes.
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001363 *
1364 * @np: Node to attach to live tree
1365 */
Frank Rowand5babefb2018-10-12 19:38:26 -07001366static void attach_node_and_children(struct device_node *np)
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001367{
Grant Likely5063e252014-10-03 16:28:27 +01001368 struct device_node *next, *dup, *child;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -08001369 unsigned long flags;
Rob Herring0d638a02017-06-01 15:50:55 -05001370 const char *full_name;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001371
Rob Herring0d638a02017-06-01 15:50:55 -05001372 full_name = kasprintf(GFP_KERNEL, "%pOF", np);
Frank Rowand5babefb2018-10-12 19:38:26 -07001373
1374 if (!strcmp(full_name, "/__local_fixups__") ||
Erhard Furtner2aacace2019-11-26 02:48:04 +01001375 !strcmp(full_name, "/__fixups__")) {
1376 kfree(full_name);
Frank Rowand5babefb2018-10-12 19:38:26 -07001377 return;
Erhard Furtner2aacace2019-11-26 02:48:04 +01001378 }
Frank Rowand5babefb2018-10-12 19:38:26 -07001379
Rob Herring0d638a02017-06-01 15:50:55 -05001380 dup = of_find_node_by_path(full_name);
1381 kfree(full_name);
Grant Likely5063e252014-10-03 16:28:27 +01001382 if (dup) {
1383 update_node_properties(np, dup);
Frank Rowand5babefb2018-10-12 19:38:26 -07001384 return;
Grant Likely5063e252014-10-03 16:28:27 +01001385 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001386
Grant Likely5063e252014-10-03 16:28:27 +01001387 child = np->child;
1388 np->child = NULL;
Gaurav Minocha3ce04b42015-01-10 23:19:51 -08001389
1390 mutex_lock(&of_mutex);
1391 raw_spin_lock_irqsave(&devtree_lock, flags);
1392 np->sibling = np->parent->child;
1393 np->parent->child = np;
1394 of_node_clear_flag(np, OF_DETACHED);
1395 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1396
1397 __of_attach_node_sysfs(np);
1398 mutex_unlock(&of_mutex);
1399
Grant Likely5063e252014-10-03 16:28:27 +01001400 while (child) {
1401 next = child->sibling;
1402 attach_node_and_children(child);
1403 child = next;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001404 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001405}
1406
1407/**
Wang Long9697a552015-03-11 08:36:54 +00001408 * unittest_data_add - Reads, copies data from
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001409 * linked tree and attaches it to the live tree
1410 */
Wang Long9697a552015-03-11 08:36:54 +00001411static int __init unittest_data_add(void)
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001412{
Wang Long9697a552015-03-11 08:36:54 +00001413 void *unittest_data;
Frank Rowand48d499b2021-04-08 15:45:08 -05001414 void *unittest_data_align;
Frank Rowand649cab52021-04-04 22:28:45 -05001415 struct device_node *unittest_data_node = NULL, *np;
Frank Rowandc8547112015-03-14 00:04:24 -07001416 /*
1417 * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically
1418 * created by cmd_dt_S_dtb in scripts/Makefile.lib
1419 */
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001420 extern uint8_t __dtb_testcases_begin[];
1421 extern uint8_t __dtb_testcases_end[];
1422 const int size = __dtb_testcases_end - __dtb_testcases_begin;
Grant Likely2eb46da2014-10-02 14:36:46 +01001423 int rc;
Frank Rowand649cab52021-04-04 22:28:45 -05001424 void *ret;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001425
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001426 if (!size) {
Frank Rowand649cab52021-04-04 22:28:45 -05001427 pr_warn("%s: testcases is empty\n", __func__);
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001428 return -ENODATA;
1429 }
1430
1431 /* creating copy */
Frank Rowand48d499b2021-04-08 15:45:08 -05001432 unittest_data = kmalloc(size + FDT_ALIGN_SIZE, GFP_KERNEL);
Geert Uytterhoeven2a656cb2019-05-02 14:45:35 +02001433 if (!unittest_data)
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001434 return -ENOMEM;
Geert Uytterhoeven2a656cb2019-05-02 14:45:35 +02001435
Frank Rowand48d499b2021-04-08 15:45:08 -05001436 unittest_data_align = PTR_ALIGN(unittest_data, FDT_ALIGN_SIZE);
1437 memcpy(unittest_data_align, __dtb_testcases_begin, size);
1438
1439 ret = of_fdt_unflatten_tree(unittest_data_align, NULL, &unittest_data_node);
Frank Rowand649cab52021-04-04 22:28:45 -05001440 if (!ret) {
1441 pr_warn("%s: unflatten testcases tree failed\n", __func__);
1442 kfree(unittest_data);
1443 return -ENODATA;
1444 }
Wang Long9697a552015-03-11 08:36:54 +00001445 if (!unittest_data_node) {
Frank Rowand649cab52021-04-04 22:28:45 -05001446 pr_warn("%s: testcases tree is empty\n", __func__);
Navid Emamdooste13de8f2019-10-04 13:58:43 -05001447 kfree(unittest_data);
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001448 return -ENODATA;
1449 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001450
1451 /*
Frank Rowand39a751a2018-02-12 00:19:42 -08001452 * This lock normally encloses of_resolve_phandles()
Frank Rowandf948d6d2017-10-17 16:36:29 -07001453 */
1454 of_overlay_mutex_lock();
1455
Wang Long9697a552015-03-11 08:36:54 +00001456 rc = of_resolve_phandles(unittest_data_node);
Grant Likely2eb46da2014-10-02 14:36:46 +01001457 if (rc) {
1458 pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc);
Frank Rowandf948d6d2017-10-17 16:36:29 -07001459 of_overlay_mutex_unlock();
Grant Likely2eb46da2014-10-02 14:36:46 +01001460 return -EINVAL;
1461 }
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001462
Grant Likely5063e252014-10-03 16:28:27 +01001463 if (!of_root) {
Wang Long9697a552015-03-11 08:36:54 +00001464 of_root = unittest_data_node;
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001465 for_each_of_allnodes(np)
1466 __of_attach_node_sysfs(np);
1467 of_aliases = of_find_node_by_path("/aliases");
1468 of_chosen = of_find_node_by_path("/chosen");
Frank Rowandf948d6d2017-10-17 16:36:29 -07001469 of_overlay_mutex_unlock();
Gaurav Minochab951f9d2014-07-26 12:48:50 -07001470 return 0;
1471 }
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001472
Frank Rowand0ac17432020-02-20 12:40:21 -06001473 EXPECT_BEGIN(KERN_INFO,
1474 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1475
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001476 /* attach the sub-tree to live tree */
Wang Long9697a552015-03-11 08:36:54 +00001477 np = unittest_data_node->child;
Grant Likely5063e252014-10-03 16:28:27 +01001478 while (np) {
1479 struct device_node *next = np->sibling;
Frank Rowand3db316d2015-03-14 00:02:31 -07001480
Grant Likely5063e252014-10-03 16:28:27 +01001481 np->parent = of_root;
1482 attach_node_and_children(np);
1483 np = next;
1484 }
Frank Rowandf948d6d2017-10-17 16:36:29 -07001485
Frank Rowand0ac17432020-02-20 12:40:21 -06001486 EXPECT_END(KERN_INFO,
1487 "Duplicate name in testcase-data, renamed to \"duplicate-name#1\"");
1488
Frank Rowandf948d6d2017-10-17 16:36:29 -07001489 of_overlay_mutex_unlock();
1490
Grant Likely5063e252014-10-03 16:28:27 +01001491 return 0;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07001492}
1493
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001494#ifdef CONFIG_OF_OVERLAY
Arnd Bergmann202fbf42018-03-13 14:15:42 +01001495static int __init overlay_data_apply(const char *overlay_name, int *overlay_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001496
Wang Long9697a552015-03-11 08:36:54 +00001497static int unittest_probe(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001498{
1499 struct device *dev = &pdev->dev;
1500 struct device_node *np = dev->of_node;
1501
1502 if (np == NULL) {
1503 dev_err(dev, "No OF data for device\n");
1504 return -EINVAL;
1505
1506 }
1507
Rob Herring0d638a02017-06-01 15:50:55 -05001508 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02001509
1510 of_platform_populate(np, NULL, NULL, &pdev->dev);
1511
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001512 return 0;
1513}
1514
Wang Long9697a552015-03-11 08:36:54 +00001515static int unittest_remove(struct platform_device *pdev)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001516{
1517 struct device *dev = &pdev->dev;
1518 struct device_node *np = dev->of_node;
1519
Rob Herring0d638a02017-06-01 15:50:55 -05001520 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001521 return 0;
1522}
1523
Grant Likelya2166ca2015-03-29 08:59:58 +01001524static const struct of_device_id unittest_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00001525 { .compatible = "unittest", },
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001526 {},
1527};
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001528
Wang Long9697a552015-03-11 08:36:54 +00001529static struct platform_driver unittest_driver = {
1530 .probe = unittest_probe,
1531 .remove = unittest_remove,
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001532 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00001533 .name = "unittest",
Wang Long9697a552015-03-11 08:36:54 +00001534 .of_match_table = of_match_ptr(unittest_match),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001535 },
1536};
1537
1538/* get the platform device instantiated at the path */
1539static struct platform_device *of_path_to_platform_device(const char *path)
1540{
1541 struct device_node *np;
1542 struct platform_device *pdev;
1543
1544 np = of_find_node_by_path(path);
1545 if (np == NULL)
1546 return NULL;
1547
1548 pdev = of_find_device_by_node(np);
1549 of_node_put(np);
1550
1551 return pdev;
1552}
1553
1554/* find out if a platform device exists at that path */
1555static int of_path_platform_device_exists(const char *path)
1556{
1557 struct platform_device *pdev;
1558
1559 pdev = of_path_to_platform_device(path);
1560 platform_device_put(pdev);
1561 return pdev != NULL;
1562}
1563
Frank Rowand485bb192020-02-27 22:16:29 -06001564#ifdef CONFIG_OF_GPIO
1565
1566struct unittest_gpio_dev {
1567 struct gpio_chip chip;
1568};
1569
1570static int unittest_gpio_chip_request_count;
1571static int unittest_gpio_probe_count;
1572static int unittest_gpio_probe_pass_count;
1573
1574static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
1575{
1576 unittest_gpio_chip_request_count++;
1577
1578 pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
1579 unittest_gpio_chip_request_count);
1580 return 0;
1581}
1582
1583static int unittest_gpio_probe(struct platform_device *pdev)
1584{
1585 struct unittest_gpio_dev *devptr;
1586 int ret;
1587
1588 unittest_gpio_probe_count++;
1589
1590 devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
1591 if (!devptr)
1592 return -ENOMEM;
1593
1594 platform_set_drvdata(pdev, devptr);
1595
1596 devptr->chip.of_node = pdev->dev.of_node;
1597 devptr->chip.label = "of-unittest-gpio";
1598 devptr->chip.base = -1; /* dynamic allocation */
1599 devptr->chip.ngpio = 5;
1600 devptr->chip.request = unittest_gpio_chip_request;
1601
1602 ret = gpiochip_add_data(&devptr->chip, NULL);
1603
1604 unittest(!ret,
1605 "gpiochip_add_data() for node @%pOF failed, ret = %d\n", devptr->chip.of_node, ret);
1606
1607 if (!ret)
1608 unittest_gpio_probe_pass_count++;
1609 return ret;
1610}
1611
1612static int unittest_gpio_remove(struct platform_device *pdev)
1613{
1614 struct unittest_gpio_dev *gdev = platform_get_drvdata(pdev);
1615 struct device *dev = &pdev->dev;
1616 struct device_node *np = pdev->dev.of_node;
1617
1618 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
1619
1620 if (!gdev)
1621 return -EINVAL;
1622
1623 if (gdev->chip.base != -1)
1624 gpiochip_remove(&gdev->chip);
1625
1626 platform_set_drvdata(pdev, NULL);
Frank Rowandfb227f52020-03-25 20:45:30 -05001627 kfree(gdev);
Frank Rowand485bb192020-02-27 22:16:29 -06001628
1629 return 0;
1630}
1631
1632static const struct of_device_id unittest_gpio_id[] = {
1633 { .compatible = "unittest-gpio", },
1634 {}
1635};
1636
1637static struct platform_driver unittest_gpio_driver = {
1638 .probe = unittest_gpio_probe,
1639 .remove = unittest_gpio_remove,
1640 .driver = {
1641 .name = "unittest-gpio",
1642 .of_match_table = of_match_ptr(unittest_gpio_id),
1643 },
1644};
1645
1646static void __init of_unittest_overlay_gpio(void)
1647{
1648 int chip_request_count;
1649 int probe_pass_count;
1650 int ret;
1651
1652 /*
1653 * tests: apply overlays before registering driver
1654 * Similar to installing a driver as a module, the
1655 * driver is registered after applying the overlays.
1656 *
Frank Rowand1adc8672020-03-25 20:45:31 -05001657 * The overlays are applied by overlay_data_apply()
1658 * instead of of_unittest_apply_overlay() so that they
1659 * will not be tracked. Thus they will not be removed
1660 * by of_unittest_destroy_tracked_overlays().
1661 *
Frank Rowand485bb192020-02-27 22:16:29 -06001662 * - apply overlay_gpio_01
1663 * - apply overlay_gpio_02a
1664 * - apply overlay_gpio_02b
1665 * - register driver
1666 *
1667 * register driver will result in
1668 * - probe and processing gpio hog for overlay_gpio_01
1669 * - probe for overlay_gpio_02a
1670 * - processing gpio for overlay_gpio_02b
1671 */
1672
1673 probe_pass_count = unittest_gpio_probe_pass_count;
1674 chip_request_count = unittest_gpio_chip_request_count;
1675
1676 /*
1677 * overlay_gpio_01 contains gpio node and child gpio hog node
1678 * overlay_gpio_02a contains gpio node
1679 * overlay_gpio_02b contains child gpio hog node
1680 */
1681
1682 unittest(overlay_data_apply("overlay_gpio_01", NULL),
1683 "Adding overlay 'overlay_gpio_01' failed\n");
1684
1685 unittest(overlay_data_apply("overlay_gpio_02a", NULL),
1686 "Adding overlay 'overlay_gpio_02a' failed\n");
1687
1688 unittest(overlay_data_apply("overlay_gpio_02b", NULL),
1689 "Adding overlay 'overlay_gpio_02b' failed\n");
1690
1691 /*
1692 * messages are the result of the probes, after the
1693 * driver is registered
1694 */
1695
1696 EXPECT_BEGIN(KERN_INFO,
Frank Rowande85860e2021-10-28 20:32:25 -05001697 "gpio-<<int>> (line-B-input): hogged as input\n");
Frank Rowand485bb192020-02-27 22:16:29 -06001698
1699 EXPECT_BEGIN(KERN_INFO,
Frank Rowande85860e2021-10-28 20:32:25 -05001700 "gpio-<<int>> (line-A-input): hogged as input\n");
Frank Rowand485bb192020-02-27 22:16:29 -06001701
1702 ret = platform_driver_register(&unittest_gpio_driver);
1703 if (unittest(ret == 0, "could not register unittest gpio driver\n"))
1704 return;
1705
1706 EXPECT_END(KERN_INFO,
Frank Rowande85860e2021-10-28 20:32:25 -05001707 "gpio-<<int>> (line-A-input): hogged as input\n");
Frank Rowand485bb192020-02-27 22:16:29 -06001708 EXPECT_END(KERN_INFO,
Frank Rowande85860e2021-10-28 20:32:25 -05001709 "gpio-<<int>> (line-B-input): hogged as input\n");
Frank Rowand485bb192020-02-27 22:16:29 -06001710
1711 unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
1712 "unittest_gpio_probe() failed or not called\n");
1713
1714 unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
1715 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1716 unittest_gpio_chip_request_count - chip_request_count);
1717
1718 /*
1719 * tests: apply overlays after registering driver
1720 *
1721 * Similar to a driver built-in to the kernel, the
1722 * driver is registered before applying the overlays.
1723 *
1724 * overlay_gpio_03 contains gpio node and child gpio hog node
1725 *
1726 * - apply overlay_gpio_03
1727 *
1728 * apply overlay will result in
1729 * - probe and processing gpio hog.
1730 */
1731
1732 probe_pass_count = unittest_gpio_probe_pass_count;
1733 chip_request_count = unittest_gpio_chip_request_count;
1734
1735 EXPECT_BEGIN(KERN_INFO,
Frank Rowande85860e2021-10-28 20:32:25 -05001736 "gpio-<<int>> (line-D-input): hogged as input\n");
Frank Rowand485bb192020-02-27 22:16:29 -06001737
1738 /* overlay_gpio_03 contains gpio node and child gpio hog node */
1739
1740 unittest(overlay_data_apply("overlay_gpio_03", NULL),
1741 "Adding overlay 'overlay_gpio_03' failed\n");
1742
1743 EXPECT_END(KERN_INFO,
Frank Rowande85860e2021-10-28 20:32:25 -05001744 "gpio-<<int>> (line-D-input): hogged as input\n");
Frank Rowand485bb192020-02-27 22:16:29 -06001745
1746 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1747 "unittest_gpio_probe() failed or not called\n");
1748
1749 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1750 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1751 unittest_gpio_chip_request_count - chip_request_count);
1752
1753 /*
1754 * overlay_gpio_04a contains gpio node
1755 *
1756 * - apply overlay_gpio_04a
1757 *
1758 * apply the overlay will result in
1759 * - probe for overlay_gpio_04a
1760 */
1761
1762 probe_pass_count = unittest_gpio_probe_pass_count;
1763 chip_request_count = unittest_gpio_chip_request_count;
1764
1765 /* overlay_gpio_04a contains gpio node */
1766
1767 unittest(overlay_data_apply("overlay_gpio_04a", NULL),
1768 "Adding overlay 'overlay_gpio_04a' failed\n");
1769
1770 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
1771 "unittest_gpio_probe() failed or not called\n");
1772
1773 /*
1774 * overlay_gpio_04b contains child gpio hog node
1775 *
1776 * - apply overlay_gpio_04b
1777 *
1778 * apply the overlay will result in
1779 * - processing gpio for overlay_gpio_04b
1780 */
1781
1782 EXPECT_BEGIN(KERN_INFO,
Frank Rowande85860e2021-10-28 20:32:25 -05001783 "gpio-<<int>> (line-C-input): hogged as input\n");
Frank Rowand485bb192020-02-27 22:16:29 -06001784
1785 /* overlay_gpio_04b contains child gpio hog node */
1786
1787 unittest(overlay_data_apply("overlay_gpio_04b", NULL),
1788 "Adding overlay 'overlay_gpio_04b' failed\n");
1789
1790 EXPECT_END(KERN_INFO,
Frank Rowande85860e2021-10-28 20:32:25 -05001791 "gpio-<<int>> (line-C-input): hogged as input\n");
Frank Rowand485bb192020-02-27 22:16:29 -06001792
1793 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
1794 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
1795 unittest_gpio_chip_request_count - chip_request_count);
1796}
1797
1798#else
1799
1800static void __init of_unittest_overlay_gpio(void)
1801{
1802 /* skip tests */
1803}
1804
1805#endif
1806
Arnd Bergmann4252de32015-03-04 20:49:47 +01001807#if IS_BUILTIN(CONFIG_I2C)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001808
1809/* get the i2c client device instantiated at the path */
1810static struct i2c_client *of_path_to_i2c_client(const char *path)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001811{
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001812 struct device_node *np;
1813 struct i2c_client *client;
1814
1815 np = of_find_node_by_path(path);
1816 if (np == NULL)
1817 return NULL;
1818
1819 client = of_find_i2c_device_by_node(np);
1820 of_node_put(np);
1821
1822 return client;
1823}
1824
1825/* find out if a i2c client device exists at that path */
1826static int of_path_i2c_client_exists(const char *path)
1827{
1828 struct i2c_client *client;
1829
1830 client = of_path_to_i2c_client(path);
1831 if (client)
1832 put_device(&client->dev);
1833 return client != NULL;
1834}
1835#else
1836static int of_path_i2c_client_exists(const char *path)
1837{
1838 return 0;
1839}
1840#endif
1841
1842enum overlay_type {
1843 PDEV_OVERLAY,
1844 I2C_OVERLAY
1845};
1846
1847static int of_path_device_type_exists(const char *path,
1848 enum overlay_type ovtype)
1849{
1850 switch (ovtype) {
1851 case PDEV_OVERLAY:
1852 return of_path_platform_device_exists(path);
1853 case I2C_OVERLAY:
1854 return of_path_i2c_client_exists(path);
1855 }
1856 return 0;
1857}
1858
Wang Long9697a552015-03-11 08:36:54 +00001859static const char *unittest_path(int nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001860{
1861 const char *base;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001862 static char buf[256];
1863
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001864 switch (ovtype) {
1865 case PDEV_OVERLAY:
1866 base = "/testcase-data/overlay-node/test-bus";
1867 break;
1868 case I2C_OVERLAY:
1869 base = "/testcase-data/overlay-node/test-bus/i2c-test-bus";
1870 break;
1871 default:
1872 buf[0] = '\0';
1873 return buf;
1874 }
Wang Long9697a552015-03-11 08:36:54 +00001875 snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001876 buf[sizeof(buf) - 1] = '\0';
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001877 return buf;
1878}
1879
Wang Long9697a552015-03-11 08:36:54 +00001880static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001881{
1882 const char *path;
1883
Wang Long9697a552015-03-11 08:36:54 +00001884 path = unittest_path(unittest_nr, ovtype);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02001885
1886 switch (ovtype) {
1887 case PDEV_OVERLAY:
1888 return of_path_platform_device_exists(path);
1889 case I2C_OVERLAY:
1890 return of_path_i2c_client_exists(path);
1891 }
1892 return 0;
1893}
1894
Frank Rowand39a751a2018-02-12 00:19:42 -08001895static const char *overlay_name_from_nr(int nr)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001896{
1897 static char buf[256];
1898
1899 snprintf(buf, sizeof(buf) - 1,
Frank Rowand39a751a2018-02-12 00:19:42 -08001900 "overlay_%d", nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001901 buf[sizeof(buf) - 1] = '\0';
1902
1903 return buf;
1904}
1905
1906static const char *bus_path = "/testcase-data/overlay-node/test-bus";
1907
Frank Rowand1adc8672020-03-25 20:45:31 -05001908/* FIXME: it is NOT guaranteed that overlay ids are assigned in sequence */
1909
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001910#define MAX_UNITTEST_OVERLAYS 256
1911static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)];
1912static int overlay_first_id = -1;
1913
Frank Rowand1adc8672020-03-25 20:45:31 -05001914static long of_unittest_overlay_tracked(int id)
1915{
1916 if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
1917 return 0;
1918 return overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id);
1919}
1920
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001921static void of_unittest_track_overlay(int id)
1922{
1923 if (overlay_first_id < 0)
1924 overlay_first_id = id;
1925 id -= overlay_first_id;
1926
Frank Rowand1adc8672020-03-25 20:45:31 -05001927 if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
1928 return;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001929 overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id);
1930}
1931
1932static void of_unittest_untrack_overlay(int id)
1933{
1934 if (overlay_first_id < 0)
1935 return;
1936 id -= overlay_first_id;
Frank Rowand1adc8672020-03-25 20:45:31 -05001937 if (WARN_ON(id >= MAX_UNITTEST_OVERLAYS))
1938 return;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001939 overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id);
1940}
1941
1942static void of_unittest_destroy_tracked_overlays(void)
1943{
Frank Rowand24789c52017-10-17 16:36:26 -07001944 int id, ret, defers, ovcs_id;
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001945
1946 if (overlay_first_id < 0)
1947 return;
1948
1949 /* try until no defers */
1950 do {
1951 defers = 0;
1952 /* remove in reverse order */
1953 for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) {
Frank Rowand1adc8672020-03-25 20:45:31 -05001954 if (!of_unittest_overlay_tracked(id))
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001955 continue;
1956
Frank Rowand24789c52017-10-17 16:36:26 -07001957 ovcs_id = id + overlay_first_id;
1958 ret = of_overlay_remove(&ovcs_id);
Sergey Senozhatsky815d74b2016-03-02 20:24:49 +09001959 if (ret == -ENODEV) {
1960 pr_warn("%s: no overlay to destroy for #%d\n",
1961 __func__, id + overlay_first_id);
1962 continue;
1963 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001964 if (ret != 0) {
1965 defers++;
1966 pr_warn("%s: overlay destroy failed for #%d\n",
1967 __func__, id + overlay_first_id);
1968 continue;
1969 }
1970
Frank Rowand1adc8672020-03-25 20:45:31 -05001971 of_unittest_untrack_overlay(id);
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03001972 }
1973 } while (defers > 0);
1974}
1975
Frank Rowand8c329652018-10-04 20:39:24 -07001976static int __init of_unittest_apply_overlay(int overlay_nr, int *overlay_id)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001977{
Frank Rowand39a751a2018-02-12 00:19:42 -08001978 const char *overlay_name;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001979
Frank Rowand39a751a2018-02-12 00:19:42 -08001980 overlay_name = overlay_name_from_nr(overlay_nr);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001981
Dan Carpenter82747322018-03-14 23:08:28 +03001982 if (!overlay_data_apply(overlay_name, overlay_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08001983 unittest(0, "could not apply overlay \"%s\"\n",
1984 overlay_name);
Dan Carpenter82747322018-03-14 23:08:28 +03001985 return -EFAULT;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001986 }
Frank Rowand24789c52017-10-17 16:36:26 -07001987 of_unittest_track_overlay(*overlay_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001988
Frank Rowand54587be2018-03-08 14:39:05 -08001989 return 0;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001990}
1991
1992/* apply an overlay while checking before and after states */
Frank Rowand39a751a2018-02-12 00:19:42 -08001993static int __init of_unittest_apply_overlay_check(int overlay_nr,
1994 int unittest_nr, int before, int after,
1995 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001996{
Frank Rowand24789c52017-10-17 16:36:26 -07001997 int ret, ovcs_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02001998
Wang Long9697a552015-03-11 08:36:54 +00001999 /* unittest device must not be in before state */
2000 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002001 unittest(0, "%s with device @\"%s\" %s\n",
2002 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00002003 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002004 !before ? "enabled" : "disabled");
2005 return -EINVAL;
2006 }
2007
Frank Rowand24789c52017-10-17 16:36:26 -07002008 ovcs_id = 0;
Frank Rowand8c329652018-10-04 20:39:24 -07002009 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002010 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00002011 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002012 return ret;
2013 }
2014
Wang Long9697a552015-03-11 08:36:54 +00002015 /* unittest device must be to set to after state */
2016 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002017 unittest(0, "%s failed to create @\"%s\" %s\n",
2018 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00002019 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002020 !after ? "enabled" : "disabled");
2021 return -EINVAL;
2022 }
2023
2024 return 0;
2025}
2026
2027/* apply an overlay and then revert it while checking before, after states */
Frank Rowand39a751a2018-02-12 00:19:42 -08002028static int __init of_unittest_apply_revert_overlay_check(int overlay_nr,
Wang Long9697a552015-03-11 08:36:54 +00002029 int unittest_nr, int before, int after,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002030 enum overlay_type ovtype)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002031{
Frank Rowand1adc8672020-03-25 20:45:31 -05002032 int ret, ovcs_id, save_id;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002033
Wang Long9697a552015-03-11 08:36:54 +00002034 /* unittest device must be in before state */
2035 if (of_unittest_device_exists(unittest_nr, ovtype) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002036 unittest(0, "%s with device @\"%s\" %s\n",
2037 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00002038 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002039 !before ? "enabled" : "disabled");
2040 return -EINVAL;
2041 }
2042
2043 /* apply the overlay */
Frank Rowand24789c52017-10-17 16:36:26 -07002044 ovcs_id = 0;
Frank Rowand8c329652018-10-04 20:39:24 -07002045 ret = of_unittest_apply_overlay(overlay_nr, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002046 if (ret != 0) {
Wang Long9697a552015-03-11 08:36:54 +00002047 /* of_unittest_apply_overlay already called unittest() */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002048 return ret;
2049 }
2050
Wang Long9697a552015-03-11 08:36:54 +00002051 /* unittest device must be in after state */
2052 if (of_unittest_device_exists(unittest_nr, ovtype) != after) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002053 unittest(0, "%s failed to create @\"%s\" %s\n",
2054 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00002055 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002056 !after ? "enabled" : "disabled");
2057 return -EINVAL;
2058 }
2059
Frank Rowand1adc8672020-03-25 20:45:31 -05002060 save_id = ovcs_id;
Frank Rowand24789c52017-10-17 16:36:26 -07002061 ret = of_overlay_remove(&ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002062 if (ret != 0) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002063 unittest(0, "%s failed to be destroyed @\"%s\"\n",
2064 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00002065 unittest_path(unittest_nr, ovtype));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002066 return ret;
2067 }
Frank Rowand1adc8672020-03-25 20:45:31 -05002068 of_unittest_untrack_overlay(save_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002069
Wang Long9697a552015-03-11 08:36:54 +00002070 /* unittest device must be again in before state */
2071 if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002072 unittest(0, "%s with device @\"%s\" %s\n",
2073 overlay_name_from_nr(overlay_nr),
Wang Long9697a552015-03-11 08:36:54 +00002074 unittest_path(unittest_nr, ovtype),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002075 !before ? "enabled" : "disabled");
2076 return -EINVAL;
2077 }
2078
2079 return 0;
2080}
2081
2082/* test activation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08002083static void __init of_unittest_overlay_0(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002084{
Frank Rowand0ac17432020-02-20 12:40:21 -06002085 int ret;
2086
2087 EXPECT_BEGIN(KERN_INFO,
2088 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2089
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002090 /* device should enable */
Frank Rowand0ac17432020-02-20 12:40:21 -06002091 ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY);
2092
2093 EXPECT_END(KERN_INFO,
2094 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest0/status");
2095
2096 if (ret)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002097 return;
2098
Wang Long9697a552015-03-11 08:36:54 +00002099 unittest(1, "overlay test %d passed\n", 0);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002100}
2101
2102/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08002103static void __init of_unittest_overlay_1(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002104{
Frank Rowand0ac17432020-02-20 12:40:21 -06002105 int ret;
2106
2107 EXPECT_BEGIN(KERN_INFO,
2108 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2109
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002110 /* device should disable */
Frank Rowand0ac17432020-02-20 12:40:21 -06002111 ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY);
2112
2113 EXPECT_END(KERN_INFO,
2114 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest1/status");
2115
2116 if (ret)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002117 return;
2118
Wang Long9697a552015-03-11 08:36:54 +00002119 unittest(1, "overlay test %d passed\n", 1);
Frank Rowand0ac17432020-02-20 12:40:21 -06002120
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002121}
2122
2123/* test activation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08002124static void __init of_unittest_overlay_2(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002125{
Frank Rowand0ac17432020-02-20 12:40:21 -06002126 int ret;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002127
Frank Rowand0ac17432020-02-20 12:40:21 -06002128 EXPECT_BEGIN(KERN_INFO,
2129 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2130
2131 /* device should enable */
2132 ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY);
2133
2134 EXPECT_END(KERN_INFO,
2135 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest2/status");
2136
2137 if (ret)
2138 return;
Wang Long9697a552015-03-11 08:36:54 +00002139 unittest(1, "overlay test %d passed\n", 2);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002140}
2141
2142/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08002143static void __init of_unittest_overlay_3(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002144{
Frank Rowand0ac17432020-02-20 12:40:21 -06002145 int ret;
2146
2147 EXPECT_BEGIN(KERN_INFO,
2148 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2149
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002150 /* device should disable */
Frank Rowand0ac17432020-02-20 12:40:21 -06002151 ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY);
2152
2153 EXPECT_END(KERN_INFO,
2154 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest3/status");
2155
2156 if (ret)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002157 return;
2158
Wang Long9697a552015-03-11 08:36:54 +00002159 unittest(1, "overlay test %d passed\n", 3);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002160}
2161
2162/* test activation of a full device node */
Frank Rowand39a751a2018-02-12 00:19:42 -08002163static void __init of_unittest_overlay_4(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002164{
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002165 /* device should disable */
Frank Rowand06c46972018-03-08 14:39:04 -08002166 if (of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY))
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002167 return;
2168
Wang Long9697a552015-03-11 08:36:54 +00002169 unittest(1, "overlay test %d passed\n", 4);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002170}
2171
2172/* test overlay apply/revert sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08002173static void __init of_unittest_overlay_5(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002174{
Frank Rowand0ac17432020-02-20 12:40:21 -06002175 int ret;
2176
2177 EXPECT_BEGIN(KERN_INFO,
2178 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2179
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002180 /* device should disable */
Frank Rowand0ac17432020-02-20 12:40:21 -06002181 ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY);
2182
2183 EXPECT_END(KERN_INFO,
2184 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest5/status");
2185
2186 if (ret)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002187 return;
2188
Wang Long9697a552015-03-11 08:36:54 +00002189 unittest(1, "overlay test %d passed\n", 5);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002190}
2191
2192/* test overlay application in sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08002193static void __init of_unittest_overlay_6(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002194{
Frank Rowand06c46972018-03-08 14:39:04 -08002195 int i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00002196 int overlay_nr = 6, unittest_nr = 6;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002197 int before = 0, after = 1;
Frank Rowand39a751a2018-02-12 00:19:42 -08002198 const char *overlay_name;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002199
Frank Rowand0ac17432020-02-20 12:40:21 -06002200 int ret;
2201
Wang Long9697a552015-03-11 08:36:54 +00002202 /* unittest device must be in before state */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002203 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00002204 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002205 != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002206 unittest(0, "%s with device @\"%s\" %s\n",
2207 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00002208 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002209 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002210 !before ? "enabled" : "disabled");
2211 return;
2212 }
2213 }
2214
2215 /* apply the overlays */
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002216
Frank Rowand0ac17432020-02-20 12:40:21 -06002217 EXPECT_BEGIN(KERN_INFO,
2218 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002219
Frank Rowand0ac17432020-02-20 12:40:21 -06002220 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2221
2222 ret = overlay_data_apply(overlay_name, &ovcs_id);
2223
2224 if (!ret) {
2225 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002226 return;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002227 }
Frank Rowand0ac17432020-02-20 12:40:21 -06002228 ov_id[0] = ovcs_id;
2229 of_unittest_track_overlay(ov_id[0]);
2230
2231 EXPECT_END(KERN_INFO,
2232 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest6/status");
2233
2234 EXPECT_BEGIN(KERN_INFO,
2235 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2236
2237 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2238
2239 ret = overlay_data_apply(overlay_name, &ovcs_id);
2240
2241 if (!ret) {
2242 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2243 return;
2244 }
2245 ov_id[1] = ovcs_id;
2246 of_unittest_track_overlay(ov_id[1]);
2247
2248 EXPECT_END(KERN_INFO,
2249 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest7/status");
2250
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002251
2252 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00002253 /* unittest device must be in after state */
2254 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002255 != after) {
Wang Long9697a552015-03-11 08:36:54 +00002256 unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n",
Frank Rowand39a751a2018-02-12 00:19:42 -08002257 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00002258 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002259 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002260 !after ? "enabled" : "disabled");
2261 return;
2262 }
2263 }
2264
2265 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07002266 ovcs_id = ov_id[i];
Frank Rowand06c46972018-03-08 14:39:04 -08002267 if (of_overlay_remove(&ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002268 unittest(0, "%s failed destroy @\"%s\"\n",
2269 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00002270 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002271 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002272 return;
2273 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03002274 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002275 }
2276
2277 for (i = 0; i < 2; i++) {
Wang Long9697a552015-03-11 08:36:54 +00002278 /* unittest device must be again in before state */
2279 if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002280 != before) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002281 unittest(0, "%s with device @\"%s\" %s\n",
2282 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00002283 unittest_path(unittest_nr + i,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002284 PDEV_OVERLAY),
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002285 !before ? "enabled" : "disabled");
2286 return;
2287 }
2288 }
2289
Wang Long9697a552015-03-11 08:36:54 +00002290 unittest(1, "overlay test %d passed\n", 6);
Frank Rowand0ac17432020-02-20 12:40:21 -06002291
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002292}
2293
2294/* test overlay application in sequence */
Frank Rowand39a751a2018-02-12 00:19:42 -08002295static void __init of_unittest_overlay_8(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002296{
Frank Rowand06c46972018-03-08 14:39:04 -08002297 int i, ov_id[2], ovcs_id;
Wang Long9697a552015-03-11 08:36:54 +00002298 int overlay_nr = 8, unittest_nr = 8;
Frank Rowand39a751a2018-02-12 00:19:42 -08002299 const char *overlay_name;
Frank Rowand0ac17432020-02-20 12:40:21 -06002300 int ret;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002301
2302 /* we don't care about device state in this test */
2303
Frank Rowand0ac17432020-02-20 12:40:21 -06002304 EXPECT_BEGIN(KERN_INFO,
2305 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2306
2307 overlay_name = overlay_name_from_nr(overlay_nr + 0);
2308
2309 ret = overlay_data_apply(overlay_name, &ovcs_id);
2310 if (!ret)
2311 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2312
2313 EXPECT_END(KERN_INFO,
2314 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/status");
2315
2316 if (!ret)
2317 return;
2318
2319 ov_id[0] = ovcs_id;
2320 of_unittest_track_overlay(ov_id[0]);
2321
2322 overlay_name = overlay_name_from_nr(overlay_nr + 1);
2323
2324 EXPECT_BEGIN(KERN_INFO,
2325 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
2326
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002327 /* apply the overlays */
Frank Rowand0ac17432020-02-20 12:40:21 -06002328 ret = overlay_data_apply(overlay_name, &ovcs_id);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002329
Frank Rowand0ac17432020-02-20 12:40:21 -06002330 EXPECT_END(KERN_INFO,
2331 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/test-unittest8/property-foo");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002332
Frank Rowand0ac17432020-02-20 12:40:21 -06002333 if (!ret) {
2334 unittest(0, "could not apply overlay \"%s\"\n", overlay_name);
2335 return;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002336 }
2337
Frank Rowand0ac17432020-02-20 12:40:21 -06002338 ov_id[1] = ovcs_id;
2339 of_unittest_track_overlay(ov_id[1]);
2340
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002341 /* now try to remove first overlay (it should fail) */
Frank Rowand24789c52017-10-17 16:36:26 -07002342 ovcs_id = ov_id[0];
Frank Rowand0ac17432020-02-20 12:40:21 -06002343
2344 EXPECT_BEGIN(KERN_INFO,
2345 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2346
2347 EXPECT_BEGIN(KERN_INFO,
2348 "OF: overlay: overlay #6 is not topmost");
2349
2350 ret = of_overlay_remove(&ovcs_id);
2351
2352 EXPECT_END(KERN_INFO,
2353 "OF: overlay: overlay #6 is not topmost");
2354
2355 EXPECT_END(KERN_INFO,
2356 "OF: overlay: node_overlaps_later_cs: #6 overlaps with #7 @/testcase-data/overlay-node/test-bus/test-unittest8");
2357
2358 if (!ret) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002359 unittest(0, "%s was destroyed @\"%s\"\n",
2360 overlay_name_from_nr(overlay_nr + 0),
Wang Long9697a552015-03-11 08:36:54 +00002361 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002362 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002363 return;
2364 }
2365
2366 /* removing them in order should work */
2367 for (i = 1; i >= 0; i--) {
Frank Rowand24789c52017-10-17 16:36:26 -07002368 ovcs_id = ov_id[i];
Frank Rowand06c46972018-03-08 14:39:04 -08002369 if (of_overlay_remove(&ovcs_id)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08002370 unittest(0, "%s not destroyed @\"%s\"\n",
2371 overlay_name_from_nr(overlay_nr + i),
Wang Long9697a552015-03-11 08:36:54 +00002372 unittest_path(unittest_nr,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002373 PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002374 return;
2375 }
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03002376 of_unittest_untrack_overlay(ov_id[i]);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002377 }
2378
Wang Long9697a552015-03-11 08:36:54 +00002379 unittest(1, "overlay test %d passed\n", 8);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002380}
2381
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002382/* test insertion of a bus with parent devices */
Frank Rowand39a751a2018-02-12 00:19:42 -08002383static void __init of_unittest_overlay_10(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002384{
2385 int ret;
2386 char *child_path;
2387
2388 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00002389 ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY);
Frank Rowand0ac17432020-02-20 12:40:21 -06002390
Wang Long9697a552015-03-11 08:36:54 +00002391 if (unittest(ret == 0,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002392 "overlay test %d failed; overlay application\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002393 return;
2394
Wang Long9697a552015-03-11 08:36:54 +00002395 child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101",
2396 unittest_path(10, PDEV_OVERLAY));
2397 if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10))
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002398 return;
2399
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002400 ret = of_path_device_type_exists(child_path, PDEV_OVERLAY);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002401 kfree(child_path);
Frank Rowand54587be2018-03-08 14:39:05 -08002402
2403 unittest(ret, "overlay test %d failed; no child device\n", 10);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002404}
2405
2406/* test insertion of a bus with parent devices (and revert) */
Frank Rowand39a751a2018-02-12 00:19:42 -08002407static void __init of_unittest_overlay_11(void)
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002408{
2409 int ret;
2410
2411 /* device should disable */
Wang Long9697a552015-03-11 08:36:54 +00002412 ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002413 PDEV_OVERLAY);
Frank Rowand0ac17432020-02-20 12:40:21 -06002414
Frank Rowand54587be2018-03-08 14:39:05 -08002415 unittest(ret == 0, "overlay test %d failed; overlay apply\n", 11);
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002416}
2417
Arnd Bergmann4252de32015-03-04 20:49:47 +01002418#if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002419
Wang Long9697a552015-03-11 08:36:54 +00002420struct unittest_i2c_bus_data {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002421 struct platform_device *pdev;
2422 struct i2c_adapter adap;
2423};
2424
Wang Long9697a552015-03-11 08:36:54 +00002425static int unittest_i2c_master_xfer(struct i2c_adapter *adap,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002426 struct i2c_msg *msgs, int num)
2427{
Wang Long9697a552015-03-11 08:36:54 +00002428 struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002429
2430 (void)std;
2431
2432 return num;
2433}
2434
Wang Long9697a552015-03-11 08:36:54 +00002435static u32 unittest_i2c_functionality(struct i2c_adapter *adap)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002436{
2437 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
2438}
2439
Wang Long9697a552015-03-11 08:36:54 +00002440static const struct i2c_algorithm unittest_i2c_algo = {
2441 .master_xfer = unittest_i2c_master_xfer,
2442 .functionality = unittest_i2c_functionality,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002443};
2444
Wang Long9697a552015-03-11 08:36:54 +00002445static int unittest_i2c_bus_probe(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002446{
2447 struct device *dev = &pdev->dev;
2448 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00002449 struct unittest_i2c_bus_data *std;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002450 struct i2c_adapter *adap;
2451 int ret;
2452
2453 if (np == NULL) {
2454 dev_err(dev, "No OF data for device\n");
2455 return -EINVAL;
2456
2457 }
2458
Rob Herring0d638a02017-06-01 15:50:55 -05002459 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002460
2461 std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL);
Geert Uytterhoeven2a656cb2019-05-02 14:45:35 +02002462 if (!std)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002463 return -ENOMEM;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002464
2465 /* link them together */
2466 std->pdev = pdev;
2467 platform_set_drvdata(pdev, std);
2468
2469 adap = &std->adap;
2470 i2c_set_adapdata(adap, std);
2471 adap->nr = -1;
2472 strlcpy(adap->name, pdev->name, sizeof(adap->name));
2473 adap->class = I2C_CLASS_DEPRECATED;
Wang Long9697a552015-03-11 08:36:54 +00002474 adap->algo = &unittest_i2c_algo;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002475 adap->dev.parent = dev;
2476 adap->dev.of_node = dev->of_node;
2477 adap->timeout = 5 * HZ;
2478 adap->retries = 3;
2479
2480 ret = i2c_add_numbered_adapter(adap);
2481 if (ret != 0) {
2482 dev_err(dev, "Failed to add I2C adapter\n");
2483 return ret;
2484 }
2485
2486 return 0;
2487}
2488
Wang Long9697a552015-03-11 08:36:54 +00002489static int unittest_i2c_bus_remove(struct platform_device *pdev)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002490{
2491 struct device *dev = &pdev->dev;
2492 struct device_node *np = dev->of_node;
Wang Long9697a552015-03-11 08:36:54 +00002493 struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002494
Rob Herring0d638a02017-06-01 15:50:55 -05002495 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002496 i2c_del_adapter(&std->adap);
2497
2498 return 0;
2499}
2500
Grant Likelya2166ca2015-03-29 08:59:58 +01002501static const struct of_device_id unittest_i2c_bus_match[] = {
Wang Long9697a552015-03-11 08:36:54 +00002502 { .compatible = "unittest-i2c-bus", },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002503 {},
2504};
2505
Wang Long9697a552015-03-11 08:36:54 +00002506static struct platform_driver unittest_i2c_bus_driver = {
2507 .probe = unittest_i2c_bus_probe,
2508 .remove = unittest_i2c_bus_remove,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002509 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00002510 .name = "unittest-i2c-bus",
2511 .of_match_table = of_match_ptr(unittest_i2c_bus_match),
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002512 },
2513};
2514
Wang Long9697a552015-03-11 08:36:54 +00002515static int unittest_i2c_dev_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002516 const struct i2c_device_id *id)
2517{
2518 struct device *dev = &client->dev;
2519 struct device_node *np = client->dev.of_node;
2520
2521 if (!np) {
2522 dev_err(dev, "No OF node\n");
2523 return -EINVAL;
2524 }
2525
Rob Herring0d638a02017-06-01 15:50:55 -05002526 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002527
2528 return 0;
2529};
2530
Wang Long9697a552015-03-11 08:36:54 +00002531static int unittest_i2c_dev_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002532{
2533 struct device *dev = &client->dev;
2534 struct device_node *np = client->dev.of_node;
2535
Rob Herring0d638a02017-06-01 15:50:55 -05002536 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002537 return 0;
2538}
2539
Wang Long9697a552015-03-11 08:36:54 +00002540static const struct i2c_device_id unittest_i2c_dev_id[] = {
2541 { .name = "unittest-i2c-dev" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002542 { }
2543};
2544
Wang Long9697a552015-03-11 08:36:54 +00002545static struct i2c_driver unittest_i2c_dev_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002546 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00002547 .name = "unittest-i2c-dev",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002548 },
Wang Long9697a552015-03-11 08:36:54 +00002549 .probe = unittest_i2c_dev_probe,
2550 .remove = unittest_i2c_dev_remove,
2551 .id_table = unittest_i2c_dev_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002552};
2553
Arnd Bergmann4252de32015-03-04 20:49:47 +01002554#if IS_BUILTIN(CONFIG_I2C_MUX)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002555
Peter Rosinb6e3b712016-04-20 08:42:47 +02002556static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002557{
2558 return 0;
2559}
2560
Wang Long9697a552015-03-11 08:36:54 +00002561static int unittest_i2c_mux_probe(struct i2c_client *client,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002562 const struct i2c_device_id *id)
2563{
Frank Rowand06c46972018-03-08 14:39:04 -08002564 int i, nchans;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002565 struct device *dev = &client->dev;
Wolfram Sang272d28b2019-06-10 11:51:56 +02002566 struct i2c_adapter *adap = client->adapter;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002567 struct device_node *np = client->dev.of_node, *child;
Peter Rosinb6e3b712016-04-20 08:42:47 +02002568 struct i2c_mux_core *muxc;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002569 u32 reg, max_reg;
2570
Rob Herring0d638a02017-06-01 15:50:55 -05002571 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002572
2573 if (!np) {
2574 dev_err(dev, "No OF node\n");
2575 return -EINVAL;
2576 }
2577
2578 max_reg = (u32)-1;
2579 for_each_child_of_node(np, child) {
Frank Rowand06c46972018-03-08 14:39:04 -08002580 if (of_property_read_u32(child, "reg", &reg))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002581 continue;
2582 if (max_reg == (u32)-1 || reg > max_reg)
2583 max_reg = reg;
2584 }
2585 nchans = max_reg == (u32)-1 ? 0 : max_reg + 1;
2586 if (nchans == 0) {
2587 dev_err(dev, "No channels\n");
2588 return -EINVAL;
2589 }
2590
Peter Rosinb6e3b712016-04-20 08:42:47 +02002591 muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0,
2592 unittest_i2c_mux_select_chan, NULL);
2593 if (!muxc)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002594 return -ENOMEM;
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002595 for (i = 0; i < nchans; i++) {
Frank Rowand06c46972018-03-08 14:39:04 -08002596 if (i2c_mux_add_adapter(muxc, 0, i, 0)) {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002597 dev_err(dev, "Failed to register mux #%d\n", i);
Peter Rosinb6e3b712016-04-20 08:42:47 +02002598 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002599 return -ENODEV;
2600 }
2601 }
2602
Peter Rosinb6e3b712016-04-20 08:42:47 +02002603 i2c_set_clientdata(client, muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002604
2605 return 0;
2606};
2607
Wang Long9697a552015-03-11 08:36:54 +00002608static int unittest_i2c_mux_remove(struct i2c_client *client)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002609{
2610 struct device *dev = &client->dev;
2611 struct device_node *np = client->dev.of_node;
Peter Rosinb6e3b712016-04-20 08:42:47 +02002612 struct i2c_mux_core *muxc = i2c_get_clientdata(client);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002613
Rob Herring0d638a02017-06-01 15:50:55 -05002614 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
Peter Rosinb6e3b712016-04-20 08:42:47 +02002615 i2c_mux_del_adapters(muxc);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002616 return 0;
2617}
2618
Wang Long9697a552015-03-11 08:36:54 +00002619static const struct i2c_device_id unittest_i2c_mux_id[] = {
2620 { .name = "unittest-i2c-mux" },
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002621 { }
2622};
2623
Wang Long9697a552015-03-11 08:36:54 +00002624static struct i2c_driver unittest_i2c_mux_driver = {
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002625 .driver = {
Wang Long9697a552015-03-11 08:36:54 +00002626 .name = "unittest-i2c-mux",
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002627 },
Wang Long9697a552015-03-11 08:36:54 +00002628 .probe = unittest_i2c_mux_probe,
2629 .remove = unittest_i2c_mux_remove,
2630 .id_table = unittest_i2c_mux_id,
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002631};
2632
2633#endif
2634
Wang Long9697a552015-03-11 08:36:54 +00002635static int of_unittest_overlay_i2c_init(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002636{
2637 int ret;
2638
Wang Long9697a552015-03-11 08:36:54 +00002639 ret = i2c_add_driver(&unittest_i2c_dev_driver);
2640 if (unittest(ret == 0,
2641 "could not register unittest i2c device driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002642 return ret;
2643
Wang Long9697a552015-03-11 08:36:54 +00002644 ret = platform_driver_register(&unittest_i2c_bus_driver);
Frank Rowand0ac17432020-02-20 12:40:21 -06002645
Wang Long9697a552015-03-11 08:36:54 +00002646 if (unittest(ret == 0,
2647 "could not register unittest i2c bus driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002648 return ret;
2649
Arnd Bergmann4252de32015-03-04 20:49:47 +01002650#if IS_BUILTIN(CONFIG_I2C_MUX)
Frank Rowand0ac17432020-02-20 12:40:21 -06002651
2652 EXPECT_BEGIN(KERN_INFO,
2653 "i2c i2c-1: Added multiplexed i2c bus 2");
2654
Wang Long9697a552015-03-11 08:36:54 +00002655 ret = i2c_add_driver(&unittest_i2c_mux_driver);
Frank Rowand0ac17432020-02-20 12:40:21 -06002656
2657 EXPECT_END(KERN_INFO,
2658 "i2c i2c-1: Added multiplexed i2c bus 2");
2659
Wang Long9697a552015-03-11 08:36:54 +00002660 if (unittest(ret == 0,
2661 "could not register unittest i2c mux driver\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002662 return ret;
2663#endif
2664
2665 return 0;
2666}
2667
Wang Long9697a552015-03-11 08:36:54 +00002668static void of_unittest_overlay_i2c_cleanup(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002669{
Arnd Bergmann4252de32015-03-04 20:49:47 +01002670#if IS_BUILTIN(CONFIG_I2C_MUX)
Wang Long9697a552015-03-11 08:36:54 +00002671 i2c_del_driver(&unittest_i2c_mux_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002672#endif
Wang Long9697a552015-03-11 08:36:54 +00002673 platform_driver_unregister(&unittest_i2c_bus_driver);
2674 i2c_del_driver(&unittest_i2c_dev_driver);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002675}
2676
Frank Rowand39a751a2018-02-12 00:19:42 -08002677static void __init of_unittest_overlay_i2c_12(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002678{
Frank Rowand0ac17432020-02-20 12:40:21 -06002679 int ret;
2680
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002681 /* device should enable */
Frank Rowand0ac17432020-02-20 12:40:21 -06002682 EXPECT_BEGIN(KERN_INFO,
2683 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2684
2685 ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY);
2686
2687 EXPECT_END(KERN_INFO,
2688 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest12/status");
2689
2690 if (ret)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002691 return;
2692
Wang Long9697a552015-03-11 08:36:54 +00002693 unittest(1, "overlay test %d passed\n", 12);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002694}
2695
2696/* test deactivation of device */
Frank Rowand39a751a2018-02-12 00:19:42 -08002697static void __init of_unittest_overlay_i2c_13(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002698{
Frank Rowand0ac17432020-02-20 12:40:21 -06002699 int ret;
2700
2701 EXPECT_BEGIN(KERN_INFO,
2702 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2703
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002704 /* device should disable */
Frank Rowand0ac17432020-02-20 12:40:21 -06002705 ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY);
2706
2707 EXPECT_END(KERN_INFO,
2708 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data/overlay-node/test-bus/i2c-test-bus/test-unittest13/status");
2709
2710 if (ret)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002711 return;
2712
Wang Long9697a552015-03-11 08:36:54 +00002713 unittest(1, "overlay test %d passed\n", 13);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002714}
2715
2716/* just check for i2c mux existence */
Wang Long9697a552015-03-11 08:36:54 +00002717static void of_unittest_overlay_i2c_14(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002718{
2719}
2720
Frank Rowand39a751a2018-02-12 00:19:42 -08002721static void __init of_unittest_overlay_i2c_15(void)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002722{
Frank Rowand0ac17432020-02-20 12:40:21 -06002723 int ret;
2724
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002725 /* device should enable */
Frank Rowand0ac17432020-02-20 12:40:21 -06002726 EXPECT_BEGIN(KERN_INFO,
2727 "i2c i2c-1: Added multiplexed i2c bus 3");
2728
2729 ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY);
2730
2731 EXPECT_END(KERN_INFO,
2732 "i2c i2c-1: Added multiplexed i2c bus 3");
2733
2734 if (ret)
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002735 return;
2736
Wang Long9697a552015-03-11 08:36:54 +00002737 unittest(1, "overlay test %d passed\n", 15);
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002738}
2739
2740#else
2741
Wang Long9697a552015-03-11 08:36:54 +00002742static inline void of_unittest_overlay_i2c_14(void) { }
2743static inline void of_unittest_overlay_i2c_15(void) { }
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002744
2745#endif
2746
Wang Long9697a552015-03-11 08:36:54 +00002747static void __init of_unittest_overlay(void)
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002748{
2749 struct device_node *bus_np = NULL;
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002750
Frank Rowand06c46972018-03-08 14:39:04 -08002751 if (platform_driver_register(&unittest_driver)) {
Wang Long9697a552015-03-11 08:36:54 +00002752 unittest(0, "could not register unittest driver\n");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002753 goto out;
2754 }
2755
2756 bus_np = of_find_node_by_path(bus_path);
2757 if (bus_np == NULL) {
Wang Long9697a552015-03-11 08:36:54 +00002758 unittest(0, "could not find bus_path \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002759 goto out;
2760 }
2761
Frank Rowand06c46972018-03-08 14:39:04 -08002762 if (of_platform_default_populate(bus_np, NULL, NULL)) {
Wang Long9697a552015-03-11 08:36:54 +00002763 unittest(0, "could not populate bus @ \"%s\"\n", bus_path);
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002764 goto out;
2765 }
2766
Wang Long9697a552015-03-11 08:36:54 +00002767 if (!of_unittest_device_exists(100, PDEV_OVERLAY)) {
2768 unittest(0, "could not find unittest0 @ \"%s\"\n",
2769 unittest_path(100, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002770 goto out;
2771 }
2772
Wang Long9697a552015-03-11 08:36:54 +00002773 if (of_unittest_device_exists(101, PDEV_OVERLAY)) {
2774 unittest(0, "unittest1 @ \"%s\" should not exist\n",
2775 unittest_path(101, PDEV_OVERLAY));
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002776 goto out;
2777 }
2778
Wang Long9697a552015-03-11 08:36:54 +00002779 unittest(1, "basic infrastructure of overlays passed");
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002780
2781 /* tests in sequence */
Wang Long9697a552015-03-11 08:36:54 +00002782 of_unittest_overlay_0();
2783 of_unittest_overlay_1();
2784 of_unittest_overlay_2();
2785 of_unittest_overlay_3();
2786 of_unittest_overlay_4();
2787 of_unittest_overlay_5();
2788 of_unittest_overlay_6();
2789 of_unittest_overlay_8();
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002790
Wang Long9697a552015-03-11 08:36:54 +00002791 of_unittest_overlay_10();
2792 of_unittest_overlay_11();
Pantelis Antoniou6b1271d2014-12-19 14:34:34 +02002793
Arnd Bergmann4252de32015-03-04 20:49:47 +01002794#if IS_BUILTIN(CONFIG_I2C)
Wang Long9697a552015-03-11 08:36:54 +00002795 if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n"))
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002796 goto out;
2797
Wang Long9697a552015-03-11 08:36:54 +00002798 of_unittest_overlay_i2c_12();
2799 of_unittest_overlay_i2c_13();
2800 of_unittest_overlay_i2c_14();
2801 of_unittest_overlay_i2c_15();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002802
Wang Long9697a552015-03-11 08:36:54 +00002803 of_unittest_overlay_i2c_cleanup();
Pantelis Antonioud5e75502015-01-12 19:02:49 +02002804#endif
2805
Frank Rowandf4056e72020-02-20 12:40:20 -06002806 of_unittest_overlay_gpio();
2807
Pantelis Antoniou492a22a2015-04-07 22:23:49 +03002808 of_unittest_destroy_tracked_overlays();
2809
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002810out:
2811 of_node_put(bus_np);
2812}
2813
2814#else
Wang Long9697a552015-03-11 08:36:54 +00002815static inline void __init of_unittest_overlay(void) { }
Pantelis Antoniou177d2712014-10-28 22:35:59 +02002816#endif
2817
Frank Rowand60a00042017-07-19 09:25:20 -07002818#ifdef CONFIG_OF_OVERLAY
2819
Frank Rowand81d0848f2017-04-25 17:09:54 -07002820/*
2821 * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb
2822 * in scripts/Makefile.lib
2823 */
2824
2825#define OVERLAY_INFO_EXTERN(name) \
2826 extern uint8_t __dtb_##name##_begin[]; \
2827 extern uint8_t __dtb_##name##_end[]
2828
Frank Rowand39a751a2018-02-12 00:19:42 -08002829#define OVERLAY_INFO(overlay_name, expected) \
2830{ .dtb_begin = __dtb_##overlay_name##_begin, \
2831 .dtb_end = __dtb_##overlay_name##_end, \
2832 .expected_result = expected, \
2833 .name = #overlay_name, \
Frank Rowand81d0848f2017-04-25 17:09:54 -07002834}
2835
2836struct overlay_info {
Frank Rowand39a751a2018-02-12 00:19:42 -08002837 uint8_t *dtb_begin;
2838 uint8_t *dtb_end;
2839 int expected_result;
2840 int overlay_id;
2841 char *name;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002842};
2843
2844OVERLAY_INFO_EXTERN(overlay_base);
2845OVERLAY_INFO_EXTERN(overlay);
Frank Rowand39a751a2018-02-12 00:19:42 -08002846OVERLAY_INFO_EXTERN(overlay_0);
2847OVERLAY_INFO_EXTERN(overlay_1);
2848OVERLAY_INFO_EXTERN(overlay_2);
2849OVERLAY_INFO_EXTERN(overlay_3);
2850OVERLAY_INFO_EXTERN(overlay_4);
2851OVERLAY_INFO_EXTERN(overlay_5);
2852OVERLAY_INFO_EXTERN(overlay_6);
2853OVERLAY_INFO_EXTERN(overlay_7);
2854OVERLAY_INFO_EXTERN(overlay_8);
2855OVERLAY_INFO_EXTERN(overlay_9);
2856OVERLAY_INFO_EXTERN(overlay_10);
2857OVERLAY_INFO_EXTERN(overlay_11);
2858OVERLAY_INFO_EXTERN(overlay_12);
2859OVERLAY_INFO_EXTERN(overlay_13);
2860OVERLAY_INFO_EXTERN(overlay_15);
Frank Rowandf4056e72020-02-20 12:40:20 -06002861OVERLAY_INFO_EXTERN(overlay_gpio_01);
2862OVERLAY_INFO_EXTERN(overlay_gpio_02a);
2863OVERLAY_INFO_EXTERN(overlay_gpio_02b);
2864OVERLAY_INFO_EXTERN(overlay_gpio_03);
2865OVERLAY_INFO_EXTERN(overlay_gpio_04a);
2866OVERLAY_INFO_EXTERN(overlay_gpio_04b);
Frank Rowanda68238a2018-10-04 20:34:33 -07002867OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
Frank Rowand2fe0e872018-10-04 20:36:18 -07002868OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002869OVERLAY_INFO_EXTERN(overlay_bad_phandle);
Frank Rowand60a00042017-07-19 09:25:20 -07002870OVERLAY_INFO_EXTERN(overlay_bad_symbol);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002871
Frank Rowand160b1d42018-10-04 20:41:03 -07002872/* entries found by name */
Frank Rowand81d0848f2017-04-25 17:09:54 -07002873static struct overlay_info overlays[] = {
2874 OVERLAY_INFO(overlay_base, -9999),
2875 OVERLAY_INFO(overlay, 0),
Frank Rowand39a751a2018-02-12 00:19:42 -08002876 OVERLAY_INFO(overlay_0, 0),
2877 OVERLAY_INFO(overlay_1, 0),
2878 OVERLAY_INFO(overlay_2, 0),
2879 OVERLAY_INFO(overlay_3, 0),
2880 OVERLAY_INFO(overlay_4, 0),
2881 OVERLAY_INFO(overlay_5, 0),
2882 OVERLAY_INFO(overlay_6, 0),
2883 OVERLAY_INFO(overlay_7, 0),
2884 OVERLAY_INFO(overlay_8, 0),
2885 OVERLAY_INFO(overlay_9, 0),
2886 OVERLAY_INFO(overlay_10, 0),
2887 OVERLAY_INFO(overlay_11, 0),
2888 OVERLAY_INFO(overlay_12, 0),
2889 OVERLAY_INFO(overlay_13, 0),
2890 OVERLAY_INFO(overlay_15, 0),
Frank Rowandf4056e72020-02-20 12:40:20 -06002891 OVERLAY_INFO(overlay_gpio_01, 0),
2892 OVERLAY_INFO(overlay_gpio_02a, 0),
2893 OVERLAY_INFO(overlay_gpio_02b, 0),
2894 OVERLAY_INFO(overlay_gpio_03, 0),
2895 OVERLAY_INFO(overlay_gpio_04a, 0),
2896 OVERLAY_INFO(overlay_gpio_04b, 0),
Frank Rowanda68238a2018-10-04 20:34:33 -07002897 OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL),
Frank Rowand2fe0e872018-10-04 20:36:18 -07002898 OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07002899 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
Frank Rowand60a00042017-07-19 09:25:20 -07002900 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
Frank Rowand160b1d42018-10-04 20:41:03 -07002901 /* end marker */
2902 {.dtb_begin = NULL, .dtb_end = NULL, .expected_result = 0, .name = NULL}
Frank Rowand81d0848f2017-04-25 17:09:54 -07002903};
2904
2905static struct device_node *overlay_base_root;
2906
Rob Herring0fa1c572018-01-05 15:32:33 -06002907static void * __init dt_alloc_memory(u64 size, u64 align)
2908{
Mike Rapoport8a7f97b2019-03-11 23:30:31 -07002909 void *ptr = memblock_alloc(size, align);
2910
2911 if (!ptr)
2912 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
2913 __func__, size, align);
2914
2915 return ptr;
Rob Herring0fa1c572018-01-05 15:32:33 -06002916}
2917
Frank Rowand81d0848f2017-04-25 17:09:54 -07002918/*
2919 * Create base device tree for the overlay unittest.
2920 *
2921 * This is called from very early boot code.
2922 *
2923 * Do as much as possible the same way as done in __unflatten_device_tree
2924 * and other early boot steps for the normal FDT so that the overlay base
2925 * unflattened tree will have the same characteristics as the real tree
2926 * (such as having memory allocated by the early allocator). The goal
2927 * is to test "the real thing" as much as possible, and test "test setup
2928 * code" as little as possible.
2929 *
2930 * Have to stop before resolving phandles, because that uses kmalloc.
2931 */
2932void __init unittest_unflatten_overlay_base(void)
2933{
2934 struct overlay_info *info;
2935 u32 data_size;
Frank Rowand39a751a2018-02-12 00:19:42 -08002936 void *new_fdt;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002937 u32 size;
Frank Rowand160b1d42018-10-04 20:41:03 -07002938 int found = 0;
2939 const char *overlay_name = "overlay_base";
2940
2941 for (info = overlays; info && info->name; info++) {
2942 if (!strcmp(overlay_name, info->name)) {
2943 found = 1;
2944 break;
2945 }
2946 }
2947 if (!found) {
2948 pr_err("no overlay data for %s\n", overlay_name);
2949 return;
2950 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07002951
2952 info = &overlays[0];
2953
2954 if (info->expected_result != -9999) {
2955 pr_err("No dtb 'overlay_base' to attach\n");
2956 return;
2957 }
2958
2959 data_size = info->dtb_end - info->dtb_begin;
2960 if (!data_size) {
2961 pr_err("No dtb 'overlay_base' to attach\n");
2962 return;
2963 }
2964
2965 size = fdt_totalsize(info->dtb_begin);
2966 if (size != data_size) {
2967 pr_err("dtb 'overlay_base' header totalsize != actual size");
2968 return;
2969 }
2970
Frank Rowand39a751a2018-02-12 00:19:42 -08002971 new_fdt = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE));
2972 if (!new_fdt) {
Frank Rowand81d0848f2017-04-25 17:09:54 -07002973 pr_err("alloc for dtb 'overlay_base' failed");
2974 return;
2975 }
2976
Frank Rowand39a751a2018-02-12 00:19:42 -08002977 memcpy(new_fdt, info->dtb_begin, size);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002978
Frank Rowand39a751a2018-02-12 00:19:42 -08002979 __unflatten_device_tree(new_fdt, NULL, &overlay_base_root,
Rob Herring0fa1c572018-01-05 15:32:33 -06002980 dt_alloc_memory, true);
Frank Rowand81d0848f2017-04-25 17:09:54 -07002981}
2982
2983/*
2984 * The purpose of of_unittest_overlay_data_add is to add an
2985 * overlay in the normal fashion. This is a test of the whole
2986 * picture, instead of testing individual elements.
2987 *
2988 * A secondary purpose is to be able to verify that the contents of
2989 * /proc/device-tree/ contains the updated structure and values from
2990 * the overlay. That must be verified separately in user space.
2991 *
2992 * Return 0 on unexpected error.
2993 */
Frank Rowand39a751a2018-02-12 00:19:42 -08002994static int __init overlay_data_apply(const char *overlay_name, int *overlay_id)
Frank Rowand81d0848f2017-04-25 17:09:54 -07002995{
2996 struct overlay_info *info;
Frank Rowand39a751a2018-02-12 00:19:42 -08002997 int found = 0;
Frank Rowand81d0848f2017-04-25 17:09:54 -07002998 int ret;
2999 u32 size;
Frank Rowand81d0848f2017-04-25 17:09:54 -07003000
Frank Rowand160b1d42018-10-04 20:41:03 -07003001 for (info = overlays; info && info->name; info++) {
Frank Rowand39a751a2018-02-12 00:19:42 -08003002 if (!strcmp(overlay_name, info->name)) {
3003 found = 1;
Frank Rowand81d0848f2017-04-25 17:09:54 -07003004 break;
Frank Rowand39a751a2018-02-12 00:19:42 -08003005 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07003006 }
Frank Rowand39a751a2018-02-12 00:19:42 -08003007 if (!found) {
3008 pr_err("no overlay data for %s\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07003009 return 0;
Frank Rowand39a751a2018-02-12 00:19:42 -08003010 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07003011
3012 size = info->dtb_end - info->dtb_begin;
Frank Rowand54587be2018-03-08 14:39:05 -08003013 if (!size)
Frank Rowand39a751a2018-02-12 00:19:42 -08003014 pr_err("no overlay data for %s\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07003015
Frank Rowand39a751a2018-02-12 00:19:42 -08003016 ret = of_overlay_fdt_apply(info->dtb_begin, size, &info->overlay_id);
3017 if (overlay_id)
3018 *overlay_id = info->overlay_id;
3019 if (ret < 0)
3020 goto out;
Frank Rowand81d0848f2017-04-25 17:09:54 -07003021
Frank Rowand39a751a2018-02-12 00:19:42 -08003022 pr_debug("%s applied\n", overlay_name);
Frank Rowand81d0848f2017-04-25 17:09:54 -07003023
3024out:
Frank Rowand39a751a2018-02-12 00:19:42 -08003025 if (ret != info->expected_result)
3026 pr_err("of_overlay_fdt_apply() expected %d, ret=%d, %s\n",
3027 info->expected_result, ret, overlay_name);
3028
Frank Rowand81d0848f2017-04-25 17:09:54 -07003029 return (ret == info->expected_result);
3030}
3031
3032/*
3033 * The purpose of of_unittest_overlay_high_level is to add an overlay
3034 * in the normal fashion. This is a test of the whole picture,
3035 * instead of individual elements.
3036 *
3037 * The first part of the function is _not_ normal overlay usage; it is
3038 * finishing splicing the base overlay device tree into the live tree.
3039 */
3040static __init void of_unittest_overlay_high_level(void)
3041{
3042 struct device_node *last_sibling;
3043 struct device_node *np;
3044 struct device_node *of_symbols;
3045 struct device_node *overlay_base_symbols;
3046 struct device_node **pprev;
3047 struct property *prop;
Frank Rowand0ac17432020-02-20 12:40:21 -06003048 int ret;
Frank Rowand81d0848f2017-04-25 17:09:54 -07003049
3050 if (!overlay_base_root) {
3051 unittest(0, "overlay_base_root not initialized\n");
3052 return;
3053 }
3054
3055 /*
3056 * Could not fixup phandles in unittest_unflatten_overlay_base()
3057 * because kmalloc() was not yet available.
3058 */
Frank Rowandf948d6d2017-10-17 16:36:29 -07003059 of_overlay_mutex_lock();
Frank Rowand81d0848f2017-04-25 17:09:54 -07003060 of_resolve_phandles(overlay_base_root);
Frank Rowandf948d6d2017-10-17 16:36:29 -07003061 of_overlay_mutex_unlock();
3062
Frank Rowand81d0848f2017-04-25 17:09:54 -07003063
3064 /*
3065 * do not allow overlay_base to duplicate any node already in
3066 * tree, this greatly simplifies the code
3067 */
3068
3069 /*
3070 * remove overlay_base_root node "__local_fixups", after
3071 * being used by of_resolve_phandles()
3072 */
3073 pprev = &overlay_base_root->child;
3074 for (np = overlay_base_root->child; np; np = np->sibling) {
Rob Herringb3e46d12018-08-27 08:37:06 -05003075 if (of_node_name_eq(np, "__local_fixups__")) {
Frank Rowand81d0848f2017-04-25 17:09:54 -07003076 *pprev = np->sibling;
3077 break;
3078 }
3079 pprev = &np->sibling;
3080 }
3081
3082 /* remove overlay_base_root node "__symbols__" if in live tree */
3083 of_symbols = of_get_child_by_name(of_root, "__symbols__");
3084 if (of_symbols) {
3085 /* will have to graft properties from node into live tree */
3086 pprev = &overlay_base_root->child;
3087 for (np = overlay_base_root->child; np; np = np->sibling) {
Rob Herringb3e46d12018-08-27 08:37:06 -05003088 if (of_node_name_eq(np, "__symbols__")) {
Frank Rowand81d0848f2017-04-25 17:09:54 -07003089 overlay_base_symbols = np;
3090 *pprev = np->sibling;
3091 break;
3092 }
3093 pprev = &np->sibling;
3094 }
3095 }
3096
Rob Herringb610e2f2018-08-27 08:38:08 -05003097 for_each_child_of_node(overlay_base_root, np) {
3098 struct device_node *base_child;
3099 for_each_child_of_node(of_root, base_child) {
3100 if (!strcmp(np->full_name, base_child->full_name)) {
3101 unittest(0, "illegal node name in overlay_base %pOFn",
3102 np);
Wan Jiabingf925a972021-10-15 04:26:58 -04003103 of_node_put(np);
3104 of_node_put(base_child);
Rob Herringb610e2f2018-08-27 08:38:08 -05003105 return;
3106 }
Frank Rowand81d0848f2017-04-25 17:09:54 -07003107 }
3108 }
3109
3110 /*
3111 * overlay 'overlay_base' is not allowed to have root
3112 * properties, so only need to splice nodes into main device tree.
3113 *
3114 * root node of *overlay_base_root will not be freed, it is lost
3115 * memory.
3116 */
3117
3118 for (np = overlay_base_root->child; np; np = np->sibling)
3119 np->parent = of_root;
3120
3121 mutex_lock(&of_mutex);
3122
Arnd Bergmannee320b32017-04-28 11:44:11 +02003123 for (last_sibling = np = of_root->child; np; np = np->sibling)
Frank Rowand81d0848f2017-04-25 17:09:54 -07003124 last_sibling = np;
3125
3126 if (last_sibling)
3127 last_sibling->sibling = overlay_base_root->child;
3128 else
3129 of_root->child = overlay_base_root->child;
3130
3131 for_each_of_allnodes_from(overlay_base_root, np)
3132 __of_attach_node_sysfs(np);
3133
3134 if (of_symbols) {
Frank Rowand39a751a2018-02-12 00:19:42 -08003135 struct property *new_prop;
Frank Rowand81d0848f2017-04-25 17:09:54 -07003136 for_each_property_of_node(overlay_base_symbols, prop) {
Frank Rowand39a751a2018-02-12 00:19:42 -08003137
3138 new_prop = __of_prop_dup(prop, GFP_KERNEL);
3139 if (!new_prop) {
3140 unittest(0, "__of_prop_dup() of '%s' from overlay_base node __symbols__",
Frank Rowand81d0848f2017-04-25 17:09:54 -07003141 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03003142 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07003143 }
Frank Rowand06c46972018-03-08 14:39:04 -08003144 if (__of_add_property(of_symbols, new_prop)) {
Frank Rowand145fc132020-04-16 16:42:48 -05003145 kfree(new_prop->name);
3146 kfree(new_prop->value);
3147 kfree(new_prop);
Frank Rowand06c46972018-03-08 14:39:04 -08003148 /* "name" auto-generated by unflatten */
Frank Rowand145fc132020-04-16 16:42:48 -05003149 if (!strcmp(prop->name, "name"))
Frank Rowand39a751a2018-02-12 00:19:42 -08003150 continue;
Frank Rowand39a751a2018-02-12 00:19:42 -08003151 unittest(0, "duplicate property '%s' in overlay_base node __symbols__",
3152 prop->name);
3153 goto err_unlock;
3154 }
Frank Rowand06c46972018-03-08 14:39:04 -08003155 if (__of_add_property_sysfs(of_symbols, new_prop)) {
Frank Rowand39a751a2018-02-12 00:19:42 -08003156 unittest(0, "unable to add property '%s' in overlay_base node __symbols__ to sysfs",
Frank Rowand81d0848f2017-04-25 17:09:54 -07003157 prop->name);
Dan Carpenter8756cd12017-05-03 22:49:50 +03003158 goto err_unlock;
Frank Rowand81d0848f2017-04-25 17:09:54 -07003159 }
3160 }
3161 }
3162
3163 mutex_unlock(&of_mutex);
3164
3165
3166 /* now do the normal overlay usage test */
3167
Frank Rowand0ac17432020-02-20 12:40:21 -06003168 EXPECT_BEGIN(KERN_ERR,
3169 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3170 EXPECT_BEGIN(KERN_ERR,
3171 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3172 EXPECT_BEGIN(KERN_ERR,
3173 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3174 EXPECT_BEGIN(KERN_ERR,
3175 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3176 EXPECT_BEGIN(KERN_ERR,
3177 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3178 EXPECT_BEGIN(KERN_ERR,
3179 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3180 EXPECT_BEGIN(KERN_ERR,
3181 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3182 EXPECT_BEGIN(KERN_ERR,
3183 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3184 EXPECT_BEGIN(KERN_ERR,
3185 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3186 EXPECT_BEGIN(KERN_ERR,
3187 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3188 EXPECT_BEGIN(KERN_ERR,
3189 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3190
3191 ret = overlay_data_apply("overlay", NULL);
3192
3193 EXPECT_END(KERN_ERR,
3194 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_right");
3195 EXPECT_END(KERN_ERR,
3196 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200_left");
3197 EXPECT_END(KERN_ERR,
3198 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/ride_200");
3199 EXPECT_END(KERN_ERR,
3200 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /__symbols__/hvac_2");
3201 EXPECT_END(KERN_ERR,
3202 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/rate");
3203 EXPECT_END(KERN_ERR,
3204 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/color");
3205 EXPECT_END(KERN_ERR,
3206 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/lights@40000/status");
3207 EXPECT_END(KERN_ERR,
3208 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@40/incline-up");
3209 EXPECT_END(KERN_ERR,
3210 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/ride@100/track@30/incline-up");
3211 EXPECT_END(KERN_ERR,
3212 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/fairway-1/status");
3213 EXPECT_END(KERN_ERR,
3214 "OF: overlay: WARNING: memory leak will occur if overlay removed, property: /testcase-data-2/substation@100/status");
3215
3216 unittest(ret, "Adding overlay 'overlay' failed\n");
3217
3218 EXPECT_BEGIN(KERN_ERR,
3219 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3220 EXPECT_BEGIN(KERN_ERR,
3221 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
Frank Rowand81d0848f2017-04-25 17:09:54 -07003222
Frank Rowanda68238a2018-10-04 20:34:33 -07003223 unittest(overlay_data_apply("overlay_bad_add_dup_node", NULL),
3224 "Adding overlay 'overlay_bad_add_dup_node' failed\n");
3225
Frank Rowand0ac17432020-02-20 12:40:21 -06003226 EXPECT_END(KERN_ERR,
3227 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/controller/name");
3228 EXPECT_END(KERN_ERR,
3229 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/controller");
3230
3231 EXPECT_BEGIN(KERN_ERR,
Frank Rowand29acfb62020-04-16 16:42:50 -05003232 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
Frank Rowand0ac17432020-02-20 12:40:21 -06003233 EXPECT_BEGIN(KERN_ERR,
Frank Rowand29acfb62020-04-16 16:42:50 -05003234 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
Frank Rowand0ac17432020-02-20 12:40:21 -06003235 EXPECT_BEGIN(KERN_ERR,
Frank Rowand29acfb62020-04-16 16:42:50 -05003236 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
Frank Rowand0ac17432020-02-20 12:40:21 -06003237
Frank Rowand2fe0e872018-10-04 20:36:18 -07003238 unittest(overlay_data_apply("overlay_bad_add_dup_prop", NULL),
3239 "Adding overlay 'overlay_bad_add_dup_prop' failed\n");
3240
Frank Rowand0ac17432020-02-20 12:40:21 -06003241 EXPECT_END(KERN_ERR,
Frank Rowand29acfb62020-04-16 16:42:50 -05003242 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/name");
Frank Rowand0ac17432020-02-20 12:40:21 -06003243 EXPECT_END(KERN_ERR,
Frank Rowand29acfb62020-04-16 16:42:50 -05003244 "OF: overlay: ERROR: multiple fragments add, update, and/or delete property /testcase-data-2/substation@100/motor-1/electric/rpm_avail");
Frank Rowand0ac17432020-02-20 12:40:21 -06003245 EXPECT_END(KERN_ERR,
Frank Rowand29acfb62020-04-16 16:42:50 -05003246 "OF: overlay: ERROR: multiple fragments add and/or delete node /testcase-data-2/substation@100/motor-1/electric");
Frank Rowand0ac17432020-02-20 12:40:21 -06003247
Frank Rowand39a751a2018-02-12 00:19:42 -08003248 unittest(overlay_data_apply("overlay_bad_phandle", NULL),
Frank Rowand81d0848f2017-04-25 17:09:54 -07003249 "Adding overlay 'overlay_bad_phandle' failed\n");
Frank Rowand60a00042017-07-19 09:25:20 -07003250
Frank Rowand39a751a2018-02-12 00:19:42 -08003251 unittest(overlay_data_apply("overlay_bad_symbol", NULL),
Frank Rowand60a00042017-07-19 09:25:20 -07003252 "Adding overlay 'overlay_bad_symbol' failed\n");
3253
Dan Carpenter8756cd12017-05-03 22:49:50 +03003254 return;
3255
3256err_unlock:
3257 mutex_unlock(&of_mutex);
Frank Rowand81d0848f2017-04-25 17:09:54 -07003258}
3259
3260#else
3261
3262static inline __init void of_unittest_overlay_high_level(void) {}
3263
3264#endif
3265
Wang Long9697a552015-03-11 08:36:54 +00003266static int __init of_unittest(void)
Grant Likely53a42092011-12-12 09:25:57 -07003267{
3268 struct device_node *np;
Gaurav Minochaae9304c2014-07-16 23:09:39 -07003269 int res;
3270
Frank Rowand0ac17432020-02-20 12:40:21 -06003271 pr_info("start of unittest - you will see error messages\n");
3272
Wang Long9697a552015-03-11 08:36:54 +00003273 /* adding data for unittest */
Brendan Higgins935665c2019-02-19 15:54:22 -08003274
3275 if (IS_ENABLED(CONFIG_UML))
3276 unittest_unflatten_overlay_base();
3277
Wang Long9697a552015-03-11 08:36:54 +00003278 res = unittest_data_add();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07003279 if (res)
3280 return res;
Grant Likely788ec2f2014-11-19 17:13:44 +00003281 if (!of_aliases)
3282 of_aliases = of_find_node_by_path("/aliases");
Grant Likely53a42092011-12-12 09:25:57 -07003283
3284 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
3285 if (!np) {
3286 pr_info("No testcase data in device tree; not running tests\n");
3287 return 0;
3288 }
3289 of_node_put(np);
3290
Wang Long9697a552015-03-11 08:36:54 +00003291 of_unittest_check_tree_linkage();
3292 of_unittest_check_phandles();
3293 of_unittest_find_node_by_name();
3294 of_unittest_dynamic();
3295 of_unittest_parse_phandle_with_args();
Stephen Boyd357aa4b2018-01-30 18:36:17 -08003296 of_unittest_parse_phandle_with_args_map();
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02003297 of_unittest_printf();
Wang Long9697a552015-03-11 08:36:54 +00003298 of_unittest_property_string();
3299 of_unittest_property_copy();
3300 of_unittest_changeset();
3301 of_unittest_parse_interrupts();
3302 of_unittest_parse_interrupts_extended();
Nicolas Saenz Julienne07d13a12020-11-19 18:53:56 +01003303 of_unittest_dma_get_max_cpu_address();
Rob Herring04db93a2019-09-20 13:28:53 -05003304 of_unittest_parse_dma_ranges();
3305 of_unittest_pci_dma_ranges();
Wang Long9697a552015-03-11 08:36:54 +00003306 of_unittest_match_node();
3307 of_unittest_platform_populate();
3308 of_unittest_overlay();
Gaurav Minochaae9304c2014-07-16 23:09:39 -07003309
Grant Likelyf2051d62014-10-01 17:40:22 +01003310 /* Double check linkage after removing testcase data */
Wang Long9697a552015-03-11 08:36:54 +00003311 of_unittest_check_tree_linkage();
Grant Likelyf2051d62014-10-01 17:40:22 +01003312
Frank Rowand81d0848f2017-04-25 17:09:54 -07003313 of_unittest_overlay_high_level();
3314
Wang Long9697a552015-03-11 08:36:54 +00003315 pr_info("end of unittest - %i passed, %i failed\n",
3316 unittest_results.passed, unittest_results.failed);
Grant Likelyf2051d62014-10-01 17:40:22 +01003317
Grant Likely53a42092011-12-12 09:25:57 -07003318 return 0;
3319}
Wang Long9697a552015-03-11 08:36:54 +00003320late_initcall(of_unittest);