Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Self tests for device tree subsystem |
| 3 | */ |
| 4 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 5 | #define pr_fmt(fmt) "### dt-test ### " fmt |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 6 | |
| 7 | #include <linux/clk.h> |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/errno.h> |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 10 | #include <linux/hashtable.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/of.h> |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 13 | #include <linux/of_fdt.h> |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 14 | #include <linux/of_irq.h> |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 15 | #include <linux/of_platform.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 16 | #include <linux/list.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/device.h> |
| 20 | |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 21 | #include "of_private.h" |
| 22 | |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 23 | static struct selftest_results { |
| 24 | int passed; |
| 25 | int failed; |
| 26 | } selftest_results; |
| 27 | |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 28 | #define NO_OF_NODES 3 |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 29 | static struct device_node *nodes[NO_OF_NODES]; |
| 30 | static int last_node_index; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 31 | static bool selftest_live_tree; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 32 | |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 33 | #define selftest(result, fmt, ...) ({ \ |
| 34 | bool failed = !(result); \ |
| 35 | if (failed) { \ |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 36 | selftest_results.failed++; \ |
| 37 | pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 38 | } else { \ |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 39 | selftest_results.passed++; \ |
| 40 | pr_debug("pass %s():%i\n", __func__, __LINE__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 41 | } \ |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 42 | failed; \ |
| 43 | }) |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 44 | |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 45 | static void __init of_selftest_find_node_by_name(void) |
| 46 | { |
| 47 | struct device_node *np; |
| 48 | |
| 49 | np = of_find_node_by_path("/testcase-data"); |
| 50 | selftest(np && !strcmp("/testcase-data", np->full_name), |
| 51 | "find /testcase-data failed\n"); |
| 52 | of_node_put(np); |
| 53 | |
| 54 | /* Test if trailing '/' works */ |
| 55 | np = of_find_node_by_path("/testcase-data/"); |
| 56 | selftest(!np, "trailing '/' on /testcase-data/ should fail\n"); |
| 57 | |
| 58 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 59 | selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name), |
| 60 | "find /testcase-data/phandle-tests/consumer-a failed\n"); |
| 61 | of_node_put(np); |
| 62 | |
| 63 | np = of_find_node_by_path("testcase-alias"); |
| 64 | selftest(np && !strcmp("/testcase-data", np->full_name), |
| 65 | "find testcase-alias failed\n"); |
| 66 | of_node_put(np); |
| 67 | |
| 68 | /* Test if trailing '/' works on aliases */ |
| 69 | np = of_find_node_by_path("testcase-alias/"); |
| 70 | selftest(!np, "trailing '/' on testcase-alias/ should fail\n"); |
| 71 | |
| 72 | np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a"); |
| 73 | selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name), |
| 74 | "find testcase-alias/phandle-tests/consumer-a failed\n"); |
| 75 | of_node_put(np); |
| 76 | |
| 77 | np = of_find_node_by_path("/testcase-data/missing-path"); |
| 78 | selftest(!np, "non-existent path returned node %s\n", np->full_name); |
| 79 | of_node_put(np); |
| 80 | |
| 81 | np = of_find_node_by_path("missing-alias"); |
| 82 | selftest(!np, "non-existent alias returned node %s\n", np->full_name); |
| 83 | of_node_put(np); |
| 84 | |
| 85 | np = of_find_node_by_path("testcase-alias/missing-path"); |
| 86 | selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name); |
| 87 | of_node_put(np); |
| 88 | } |
| 89 | |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 90 | static void __init of_selftest_dynamic(void) |
| 91 | { |
| 92 | struct device_node *np; |
| 93 | struct property *prop; |
| 94 | |
| 95 | np = of_find_node_by_path("/testcase-data"); |
| 96 | if (!np) { |
| 97 | pr_err("missing testcase data\n"); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | /* Array of 4 properties for the purpose of testing */ |
| 102 | prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL); |
| 103 | if (!prop) { |
| 104 | selftest(0, "kzalloc() failed\n"); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | /* Add a new property - should pass*/ |
| 109 | prop->name = "new-property"; |
| 110 | prop->value = "new-property-data"; |
| 111 | prop->length = strlen(prop->value); |
| 112 | selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n"); |
| 113 | |
| 114 | /* Try to add an existing property - should fail */ |
| 115 | prop++; |
| 116 | prop->name = "new-property"; |
| 117 | prop->value = "new-property-data-should-fail"; |
| 118 | prop->length = strlen(prop->value); |
| 119 | selftest(of_add_property(np, prop) != 0, |
| 120 | "Adding an existing property should have failed\n"); |
| 121 | |
| 122 | /* Try to modify an existing property - should pass */ |
| 123 | prop->value = "modify-property-data-should-pass"; |
| 124 | prop->length = strlen(prop->value); |
| 125 | selftest(of_update_property(np, prop) == 0, |
| 126 | "Updating an existing property should have passed\n"); |
| 127 | |
| 128 | /* Try to modify non-existent property - should pass*/ |
| 129 | prop++; |
| 130 | prop->name = "modify-property"; |
| 131 | prop->value = "modify-missing-property-data-should-pass"; |
| 132 | prop->length = strlen(prop->value); |
| 133 | selftest(of_update_property(np, prop) == 0, |
| 134 | "Updating a missing property should have passed\n"); |
| 135 | |
| 136 | /* Remove property - should pass */ |
| 137 | selftest(of_remove_property(np, prop) == 0, |
| 138 | "Removing a property should have passed\n"); |
| 139 | |
| 140 | /* Adding very large property - should pass */ |
| 141 | prop++; |
| 142 | prop->name = "large-property-PAGE_SIZEx8"; |
| 143 | prop->length = PAGE_SIZE * 8; |
| 144 | prop->value = kzalloc(prop->length, GFP_KERNEL); |
| 145 | selftest(prop->value != NULL, "Unable to allocate large buffer\n"); |
| 146 | if (prop->value) |
| 147 | selftest(of_add_property(np, prop) == 0, |
| 148 | "Adding a large property should have passed\n"); |
| 149 | } |
| 150 | |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 151 | static int __init of_selftest_check_node_linkage(struct device_node *np) |
| 152 | { |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 153 | struct device_node *child; |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 154 | int count = 0, rc; |
| 155 | |
| 156 | for_each_child_of_node(np, child) { |
| 157 | if (child->parent != np) { |
| 158 | pr_err("Child node %s links to wrong parent %s\n", |
| 159 | child->name, np->name); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 163 | rc = of_selftest_check_node_linkage(child); |
| 164 | if (rc < 0) |
| 165 | return rc; |
| 166 | count += rc; |
| 167 | } |
| 168 | |
| 169 | return count + 1; |
| 170 | } |
| 171 | |
| 172 | static void __init of_selftest_check_tree_linkage(void) |
| 173 | { |
| 174 | struct device_node *np; |
| 175 | int allnode_count = 0, child_count; |
| 176 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 177 | if (!of_root) |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 178 | return; |
| 179 | |
| 180 | for_each_of_allnodes(np) |
| 181 | allnode_count++; |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 182 | child_count = of_selftest_check_node_linkage(of_root); |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 183 | |
| 184 | selftest(child_count > 0, "Device node data structure is corrupted\n"); |
| 185 | selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match" |
| 186 | "sibling lists size (%i)\n", allnode_count, child_count); |
| 187 | pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count); |
| 188 | } |
| 189 | |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 190 | struct node_hash { |
| 191 | struct hlist_node node; |
| 192 | struct device_node *np; |
| 193 | }; |
| 194 | |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 195 | static DEFINE_HASHTABLE(phandle_ht, 8); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 196 | static void __init of_selftest_check_phandles(void) |
| 197 | { |
| 198 | struct device_node *np; |
| 199 | struct node_hash *nh; |
| 200 | struct hlist_node *tmp; |
| 201 | int i, dup_count = 0, phandle_count = 0; |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 202 | |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 203 | for_each_of_allnodes(np) { |
| 204 | if (!np->phandle) |
| 205 | continue; |
| 206 | |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 207 | hash_for_each_possible(phandle_ht, nh, node, np->phandle) { |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 208 | if (nh->np->phandle == np->phandle) { |
| 209 | pr_info("Duplicate phandle! %i used by %s and %s\n", |
| 210 | np->phandle, nh->np->full_name, np->full_name); |
| 211 | dup_count++; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); |
| 217 | if (WARN_ON(!nh)) |
| 218 | return; |
| 219 | |
| 220 | nh->np = np; |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 221 | hash_add(phandle_ht, &nh->node, np->phandle); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 222 | phandle_count++; |
| 223 | } |
| 224 | selftest(dup_count == 0, "Found %i duplicates in %i phandles\n", |
| 225 | dup_count, phandle_count); |
| 226 | |
| 227 | /* Clean up */ |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 228 | hash_for_each_safe(phandle_ht, i, tmp, nh, node) { |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 229 | hash_del(&nh->node); |
| 230 | kfree(nh); |
| 231 | } |
| 232 | } |
| 233 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 234 | static void __init of_selftest_parse_phandle_with_args(void) |
| 235 | { |
| 236 | struct device_node *np; |
| 237 | struct of_phandle_args args; |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 238 | int i, rc; |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 239 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 240 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 241 | if (!np) { |
| 242 | pr_err("missing testcase data\n"); |
| 243 | return; |
| 244 | } |
| 245 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 246 | rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells"); |
| 247 | selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc); |
| 248 | |
Grant Likely | f7f951c | 2013-02-12 17:41:22 +0000 | [diff] [blame] | 249 | for (i = 0; i < 8; i++) { |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 250 | bool passed = true; |
| 251 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 252 | "#phandle-cells", i, &args); |
| 253 | |
| 254 | /* Test the values from tests-phandle.dtsi */ |
| 255 | switch (i) { |
| 256 | case 0: |
| 257 | passed &= !rc; |
| 258 | passed &= (args.args_count == 1); |
| 259 | passed &= (args.args[0] == (i + 1)); |
| 260 | break; |
| 261 | case 1: |
| 262 | passed &= !rc; |
| 263 | passed &= (args.args_count == 2); |
| 264 | passed &= (args.args[0] == (i + 1)); |
| 265 | passed &= (args.args[1] == 0); |
| 266 | break; |
| 267 | case 2: |
| 268 | passed &= (rc == -ENOENT); |
| 269 | break; |
| 270 | case 3: |
| 271 | passed &= !rc; |
| 272 | passed &= (args.args_count == 3); |
| 273 | passed &= (args.args[0] == (i + 1)); |
| 274 | passed &= (args.args[1] == 4); |
| 275 | passed &= (args.args[2] == 3); |
| 276 | break; |
| 277 | case 4: |
| 278 | passed &= !rc; |
| 279 | passed &= (args.args_count == 2); |
| 280 | passed &= (args.args[0] == (i + 1)); |
| 281 | passed &= (args.args[1] == 100); |
| 282 | break; |
| 283 | case 5: |
| 284 | passed &= !rc; |
| 285 | passed &= (args.args_count == 0); |
| 286 | break; |
| 287 | case 6: |
| 288 | passed &= !rc; |
| 289 | passed &= (args.args_count == 1); |
| 290 | passed &= (args.args[0] == (i + 1)); |
| 291 | break; |
| 292 | case 7: |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 293 | passed &= (rc == -ENOENT); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 294 | break; |
| 295 | default: |
| 296 | passed = false; |
| 297 | } |
| 298 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 299 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 300 | i, args.np->full_name, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Check for missing list property */ |
| 304 | rc = of_parse_phandle_with_args(np, "phandle-list-missing", |
| 305 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 306 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 307 | rc = of_count_phandle_with_args(np, "phandle-list-missing", |
| 308 | "#phandle-cells"); |
| 309 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 310 | |
| 311 | /* Check for missing cells property */ |
| 312 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 313 | "#phandle-cells-missing", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 314 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 315 | rc = of_count_phandle_with_args(np, "phandle-list", |
| 316 | "#phandle-cells-missing"); |
| 317 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 318 | |
| 319 | /* Check for bad phandle in list */ |
| 320 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle", |
| 321 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 322 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 323 | rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle", |
| 324 | "#phandle-cells"); |
| 325 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 326 | |
| 327 | /* Check for incorrectly formed argument list */ |
| 328 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-args", |
| 329 | "#phandle-cells", 1, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 330 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 331 | rc = of_count_phandle_with_args(np, "phandle-list-bad-args", |
| 332 | "#phandle-cells"); |
| 333 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 336 | static void __init of_selftest_property_string(void) |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 337 | { |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 338 | const char *strings[4]; |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 339 | struct device_node *np; |
| 340 | int rc; |
| 341 | |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 342 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 343 | if (!np) { |
| 344 | pr_err("No testcase data in device tree\n"); |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | rc = of_property_match_string(np, "phandle-list-names", "first"); |
| 349 | selftest(rc == 0, "first expected:0 got:%i\n", rc); |
| 350 | rc = of_property_match_string(np, "phandle-list-names", "second"); |
| 351 | selftest(rc == 1, "second expected:0 got:%i\n", rc); |
| 352 | rc = of_property_match_string(np, "phandle-list-names", "third"); |
| 353 | selftest(rc == 2, "third expected:0 got:%i\n", rc); |
| 354 | rc = of_property_match_string(np, "phandle-list-names", "fourth"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 355 | selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 356 | rc = of_property_match_string(np, "missing-property", "blah"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 357 | selftest(rc == -EINVAL, "missing property; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 358 | rc = of_property_match_string(np, "empty-property", "blah"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 359 | selftest(rc == -ENODATA, "empty property; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 360 | rc = of_property_match_string(np, "unterminated-string", "blah"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 361 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
| 362 | |
| 363 | /* of_property_count_strings() tests */ |
| 364 | rc = of_property_count_strings(np, "string-property"); |
| 365 | selftest(rc == 1, "Incorrect string count; rc=%i\n", rc); |
| 366 | rc = of_property_count_strings(np, "phandle-list-names"); |
| 367 | selftest(rc == 3, "Incorrect string count; rc=%i\n", rc); |
| 368 | rc = of_property_count_strings(np, "unterminated-string"); |
| 369 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
| 370 | rc = of_property_count_strings(np, "unterminated-string-list"); |
| 371 | selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); |
| 372 | |
| 373 | /* of_property_read_string_index() tests */ |
| 374 | rc = of_property_read_string_index(np, "string-property", 0, strings); |
| 375 | selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc); |
| 376 | strings[0] = NULL; |
| 377 | rc = of_property_read_string_index(np, "string-property", 1, strings); |
| 378 | selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
| 379 | rc = of_property_read_string_index(np, "phandle-list-names", 0, strings); |
| 380 | selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); |
| 381 | rc = of_property_read_string_index(np, "phandle-list-names", 1, strings); |
| 382 | selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc); |
| 383 | rc = of_property_read_string_index(np, "phandle-list-names", 2, strings); |
| 384 | selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc); |
| 385 | strings[0] = NULL; |
| 386 | rc = of_property_read_string_index(np, "phandle-list-names", 3, strings); |
| 387 | selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
| 388 | strings[0] = NULL; |
| 389 | rc = of_property_read_string_index(np, "unterminated-string", 0, strings); |
| 390 | selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
| 391 | rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings); |
| 392 | selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); |
| 393 | strings[0] = NULL; |
| 394 | rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */ |
| 395 | selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
| 396 | strings[1] = NULL; |
| 397 | |
| 398 | /* of_property_read_string_array() tests */ |
| 399 | rc = of_property_read_string_array(np, "string-property", strings, 4); |
| 400 | selftest(rc == 1, "Incorrect string count; rc=%i\n", rc); |
| 401 | rc = of_property_read_string_array(np, "phandle-list-names", strings, 4); |
| 402 | selftest(rc == 3, "Incorrect string count; rc=%i\n", rc); |
| 403 | rc = of_property_read_string_array(np, "unterminated-string", strings, 4); |
| 404 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
| 405 | /* -- An incorrectly formed string should cause a failure */ |
| 406 | rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4); |
| 407 | selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); |
| 408 | /* -- parsing the correctly formed strings should still work: */ |
| 409 | strings[2] = NULL; |
| 410 | rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2); |
| 411 | selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc); |
| 412 | strings[1] = NULL; |
| 413 | rc = of_property_read_string_array(np, "phandle-list-names", strings, 1); |
| 414 | selftest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 417 | #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \ |
| 418 | (p1)->value && (p2)->value && \ |
| 419 | !memcmp((p1)->value, (p2)->value, (p1)->length) && \ |
| 420 | !strcmp((p1)->name, (p2)->name)) |
| 421 | static void __init of_selftest_property_copy(void) |
| 422 | { |
| 423 | #ifdef CONFIG_OF_DYNAMIC |
| 424 | struct property p1 = { .name = "p1", .length = 0, .value = "" }; |
| 425 | struct property p2 = { .name = "p2", .length = 5, .value = "abcd" }; |
| 426 | struct property *new; |
| 427 | |
| 428 | new = __of_prop_dup(&p1, GFP_KERNEL); |
| 429 | selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n"); |
| 430 | kfree(new->value); |
| 431 | kfree(new->name); |
| 432 | kfree(new); |
| 433 | |
| 434 | new = __of_prop_dup(&p2, GFP_KERNEL); |
| 435 | selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n"); |
| 436 | kfree(new->value); |
| 437 | kfree(new->name); |
| 438 | kfree(new); |
| 439 | #endif |
| 440 | } |
| 441 | |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 442 | static void __init of_selftest_changeset(void) |
| 443 | { |
| 444 | #ifdef CONFIG_OF_DYNAMIC |
| 445 | struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" }; |
| 446 | struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" }; |
| 447 | struct property *ppremove; |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 448 | struct device_node *n1, *n2, *n21, *nremove, *parent, *np; |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 449 | struct of_changeset chgset; |
| 450 | |
| 451 | of_changeset_init(&chgset); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 452 | n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 453 | selftest(n1, "testcase setup failure\n"); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 454 | n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 455 | selftest(n2, "testcase setup failure\n"); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 456 | n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 457 | selftest(n21, "testcase setup failure %p\n", n21); |
| 458 | nremove = of_find_node_by_path("/testcase-data/changeset/node-remove"); |
| 459 | selftest(nremove, "testcase setup failure\n"); |
| 460 | ppadd = __of_prop_dup(&padd, GFP_KERNEL); |
| 461 | selftest(ppadd, "testcase setup failure\n"); |
| 462 | ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL); |
| 463 | selftest(ppupdate, "testcase setup failure\n"); |
| 464 | parent = nremove->parent; |
| 465 | n1->parent = parent; |
| 466 | n2->parent = parent; |
| 467 | n21->parent = n2; |
| 468 | n2->child = n21; |
| 469 | ppremove = of_find_property(parent, "prop-remove", NULL); |
| 470 | selftest(ppremove, "failed to find removal prop"); |
| 471 | |
| 472 | of_changeset_init(&chgset); |
| 473 | selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n"); |
| 474 | selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n"); |
| 475 | selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n"); |
| 476 | selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n"); |
| 477 | selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n"); |
| 478 | selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n"); |
| 479 | selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n"); |
| 480 | mutex_lock(&of_mutex); |
| 481 | selftest(!of_changeset_apply(&chgset), "apply failed\n"); |
| 482 | mutex_unlock(&of_mutex); |
| 483 | |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 484 | /* Make sure node names are constructed correctly */ |
| 485 | selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")), |
| 486 | "'%s' not added\n", n21->full_name); |
| 487 | if (np) |
| 488 | of_node_put(np); |
| 489 | |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 490 | mutex_lock(&of_mutex); |
| 491 | selftest(!of_changeset_revert(&chgset), "revert failed\n"); |
| 492 | mutex_unlock(&of_mutex); |
| 493 | |
| 494 | of_changeset_destroy(&chgset); |
| 495 | #endif |
| 496 | } |
| 497 | |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 498 | static void __init of_selftest_parse_interrupts(void) |
| 499 | { |
| 500 | struct device_node *np; |
| 501 | struct of_phandle_args args; |
| 502 | int i, rc; |
| 503 | |
| 504 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts0"); |
| 505 | if (!np) { |
| 506 | pr_err("missing testcase data\n"); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | for (i = 0; i < 4; i++) { |
| 511 | bool passed = true; |
| 512 | args.args_count = 0; |
| 513 | rc = of_irq_parse_one(np, i, &args); |
| 514 | |
| 515 | passed &= !rc; |
| 516 | passed &= (args.args_count == 1); |
| 517 | passed &= (args.args[0] == (i + 1)); |
| 518 | |
| 519 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 520 | i, args.np->full_name, rc); |
| 521 | } |
| 522 | of_node_put(np); |
| 523 | |
| 524 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts1"); |
| 525 | if (!np) { |
| 526 | pr_err("missing testcase data\n"); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | for (i = 0; i < 4; i++) { |
| 531 | bool passed = true; |
| 532 | args.args_count = 0; |
| 533 | rc = of_irq_parse_one(np, i, &args); |
| 534 | |
| 535 | /* Test the values from tests-phandle.dtsi */ |
| 536 | switch (i) { |
| 537 | case 0: |
| 538 | passed &= !rc; |
| 539 | passed &= (args.args_count == 1); |
| 540 | passed &= (args.args[0] == 9); |
| 541 | break; |
| 542 | case 1: |
| 543 | passed &= !rc; |
| 544 | passed &= (args.args_count == 3); |
| 545 | passed &= (args.args[0] == 10); |
| 546 | passed &= (args.args[1] == 11); |
| 547 | passed &= (args.args[2] == 12); |
| 548 | break; |
| 549 | case 2: |
| 550 | passed &= !rc; |
| 551 | passed &= (args.args_count == 2); |
| 552 | passed &= (args.args[0] == 13); |
| 553 | passed &= (args.args[1] == 14); |
| 554 | break; |
| 555 | case 3: |
| 556 | passed &= !rc; |
| 557 | passed &= (args.args_count == 2); |
| 558 | passed &= (args.args[0] == 15); |
| 559 | passed &= (args.args[1] == 16); |
| 560 | break; |
| 561 | default: |
| 562 | passed = false; |
| 563 | } |
| 564 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 565 | i, args.np->full_name, rc); |
| 566 | } |
| 567 | of_node_put(np); |
| 568 | } |
| 569 | |
Grant Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 570 | static void __init of_selftest_parse_interrupts_extended(void) |
| 571 | { |
| 572 | struct device_node *np; |
| 573 | struct of_phandle_args args; |
| 574 | int i, rc; |
| 575 | |
| 576 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0"); |
| 577 | if (!np) { |
| 578 | pr_err("missing testcase data\n"); |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | for (i = 0; i < 7; i++) { |
| 583 | bool passed = true; |
| 584 | rc = of_irq_parse_one(np, i, &args); |
| 585 | |
| 586 | /* Test the values from tests-phandle.dtsi */ |
| 587 | switch (i) { |
| 588 | case 0: |
| 589 | passed &= !rc; |
| 590 | passed &= (args.args_count == 1); |
| 591 | passed &= (args.args[0] == 1); |
| 592 | break; |
| 593 | case 1: |
| 594 | passed &= !rc; |
| 595 | passed &= (args.args_count == 3); |
| 596 | passed &= (args.args[0] == 2); |
| 597 | passed &= (args.args[1] == 3); |
| 598 | passed &= (args.args[2] == 4); |
| 599 | break; |
| 600 | case 2: |
| 601 | passed &= !rc; |
| 602 | passed &= (args.args_count == 2); |
| 603 | passed &= (args.args[0] == 5); |
| 604 | passed &= (args.args[1] == 6); |
| 605 | break; |
| 606 | case 3: |
| 607 | passed &= !rc; |
| 608 | passed &= (args.args_count == 1); |
| 609 | passed &= (args.args[0] == 9); |
| 610 | break; |
| 611 | case 4: |
| 612 | passed &= !rc; |
| 613 | passed &= (args.args_count == 3); |
| 614 | passed &= (args.args[0] == 10); |
| 615 | passed &= (args.args[1] == 11); |
| 616 | passed &= (args.args[2] == 12); |
| 617 | break; |
| 618 | case 5: |
| 619 | passed &= !rc; |
| 620 | passed &= (args.args_count == 2); |
| 621 | passed &= (args.args[0] == 13); |
| 622 | passed &= (args.args[1] == 14); |
| 623 | break; |
| 624 | case 6: |
| 625 | passed &= !rc; |
| 626 | passed &= (args.args_count == 1); |
| 627 | passed &= (args.args[0] == 15); |
| 628 | break; |
| 629 | default: |
| 630 | passed = false; |
| 631 | } |
| 632 | |
| 633 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 634 | i, args.np->full_name, rc); |
| 635 | } |
| 636 | of_node_put(np); |
| 637 | } |
| 638 | |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 639 | static struct of_device_id match_node_table[] = { |
| 640 | { .data = "A", .name = "name0", }, /* Name alone is lowest priority */ |
| 641 | { .data = "B", .type = "type1", }, /* followed by type alone */ |
| 642 | |
| 643 | { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */ |
| 644 | { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */ |
| 645 | { .data = "Cc", .name = "name2", .type = "type2", }, |
| 646 | |
| 647 | { .data = "E", .compatible = "compat3" }, |
| 648 | { .data = "G", .compatible = "compat2", }, |
| 649 | { .data = "H", .compatible = "compat2", .name = "name5", }, |
| 650 | { .data = "I", .compatible = "compat2", .type = "type1", }, |
| 651 | { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", }, |
| 652 | { .data = "K", .compatible = "compat2", .name = "name9", }, |
| 653 | {} |
| 654 | }; |
| 655 | |
| 656 | static struct { |
| 657 | const char *path; |
| 658 | const char *data; |
| 659 | } match_node_tests[] = { |
| 660 | { .path = "/testcase-data/match-node/name0", .data = "A", }, |
| 661 | { .path = "/testcase-data/match-node/name1", .data = "B", }, |
| 662 | { .path = "/testcase-data/match-node/a/name2", .data = "Ca", }, |
| 663 | { .path = "/testcase-data/match-node/b/name2", .data = "Cb", }, |
| 664 | { .path = "/testcase-data/match-node/c/name2", .data = "Cc", }, |
| 665 | { .path = "/testcase-data/match-node/name3", .data = "E", }, |
| 666 | { .path = "/testcase-data/match-node/name4", .data = "G", }, |
| 667 | { .path = "/testcase-data/match-node/name5", .data = "H", }, |
| 668 | { .path = "/testcase-data/match-node/name6", .data = "G", }, |
| 669 | { .path = "/testcase-data/match-node/name7", .data = "I", }, |
| 670 | { .path = "/testcase-data/match-node/name8", .data = "J", }, |
| 671 | { .path = "/testcase-data/match-node/name9", .data = "K", }, |
| 672 | }; |
| 673 | |
| 674 | static void __init of_selftest_match_node(void) |
| 675 | { |
| 676 | struct device_node *np; |
| 677 | const struct of_device_id *match; |
| 678 | int i; |
| 679 | |
| 680 | for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) { |
| 681 | np = of_find_node_by_path(match_node_tests[i].path); |
| 682 | if (!np) { |
| 683 | selftest(0, "missing testcase node %s\n", |
| 684 | match_node_tests[i].path); |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | match = of_match_node(match_node_table, np); |
| 689 | if (!match) { |
| 690 | selftest(0, "%s didn't match anything\n", |
| 691 | match_node_tests[i].path); |
| 692 | continue; |
| 693 | } |
| 694 | |
| 695 | if (strcmp(match->data, match_node_tests[i].data) != 0) { |
| 696 | selftest(0, "%s got wrong match. expected %s, got %s\n", |
| 697 | match_node_tests[i].path, match_node_tests[i].data, |
| 698 | (const char *)match->data); |
| 699 | continue; |
| 700 | } |
| 701 | selftest(1, "passed"); |
| 702 | } |
| 703 | } |
| 704 | |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 705 | struct device test_bus = { |
| 706 | .init_name = "unittest-bus", |
| 707 | }; |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 708 | static void __init of_selftest_platform_populate(void) |
| 709 | { |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 710 | int irq, rc; |
| 711 | struct device_node *np, *child, *grandchild; |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 712 | struct platform_device *pdev; |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 713 | struct of_device_id match[] = { |
| 714 | { .compatible = "test-device", }, |
| 715 | {} |
| 716 | }; |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 717 | |
| 718 | np = of_find_node_by_path("/testcase-data"); |
| 719 | of_platform_populate(np, of_default_bus_match_table, NULL, NULL); |
| 720 | |
| 721 | /* Test that a missing irq domain returns -EPROBE_DEFER */ |
| 722 | np = of_find_node_by_path("/testcase-data/testcase-device1"); |
| 723 | pdev = of_find_device_by_node(np); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 724 | selftest(pdev, "device 1 creation failed\n"); |
| 725 | |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 726 | irq = platform_get_irq(pdev, 0); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 727 | selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 728 | |
| 729 | /* Test that a parsing failure does not return -EPROBE_DEFER */ |
| 730 | np = of_find_node_by_path("/testcase-data/testcase-device2"); |
| 731 | pdev = of_find_device_by_node(np); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 732 | selftest(pdev, "device 2 creation failed\n"); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 733 | irq = platform_get_irq(pdev, 0); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 734 | selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 735 | |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 736 | if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"), |
| 737 | "No testcase data in device tree\n")); |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 738 | return; |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 739 | |
| 740 | if (selftest(!(rc = device_register(&test_bus)), |
| 741 | "testbus registration failed; rc=%i\n", rc)); |
| 742 | return; |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 743 | |
| 744 | for_each_child_of_node(np, child) { |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 745 | of_platform_populate(child, match, NULL, &test_bus); |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 746 | for_each_child_of_node(child, grandchild) |
| 747 | selftest(of_find_device_by_node(grandchild), |
| 748 | "Could not create device for node '%s'\n", |
| 749 | grandchild->name); |
| 750 | } |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 751 | |
| 752 | of_platform_depopulate(&test_bus); |
| 753 | for_each_child_of_node(np, child) { |
| 754 | for_each_child_of_node(child, grandchild) |
| 755 | selftest(!of_find_device_by_node(grandchild), |
| 756 | "device didn't get destroyed '%s'\n", |
| 757 | grandchild->name); |
| 758 | } |
| 759 | |
| 760 | device_unregister(&test_bus); |
| 761 | of_node_put(np); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 762 | } |
| 763 | |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 764 | /** |
| 765 | * update_node_properties - adds the properties |
| 766 | * of np into dup node (present in live tree) and |
| 767 | * updates parent of children of np to dup. |
| 768 | * |
| 769 | * @np: node already present in live tree |
| 770 | * @dup: node present in live tree to be updated |
| 771 | */ |
| 772 | static void update_node_properties(struct device_node *np, |
| 773 | struct device_node *dup) |
| 774 | { |
| 775 | struct property *prop; |
| 776 | struct device_node *child; |
| 777 | |
| 778 | for_each_property_of_node(np, prop) |
| 779 | of_add_property(dup, prop); |
| 780 | |
| 781 | for_each_child_of_node(np, child) |
| 782 | child->parent = dup; |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * attach_node_and_children - attaches nodes |
| 787 | * and its children to live tree |
| 788 | * |
| 789 | * @np: Node to attach to live tree |
| 790 | */ |
| 791 | static int attach_node_and_children(struct device_node *np) |
| 792 | { |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 793 | struct device_node *next, *dup, *child; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 794 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 795 | dup = of_find_node_by_path(np->full_name); |
| 796 | if (dup) { |
| 797 | update_node_properties(np, dup); |
| 798 | return 0; |
| 799 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 800 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 801 | /* Children of the root need to be remembered for removal */ |
| 802 | if (np->parent == of_root) { |
Grant Likely | e66c98c | 2014-10-01 16:57:07 +0100 | [diff] [blame] | 803 | if (WARN_ON(last_node_index >= NO_OF_NODES)) |
| 804 | return -EINVAL; |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 805 | nodes[last_node_index++] = np; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 806 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 807 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 808 | child = np->child; |
| 809 | np->child = NULL; |
| 810 | np->sibling = NULL; |
| 811 | of_attach_node(np); |
| 812 | while (child) { |
| 813 | next = child->sibling; |
| 814 | attach_node_and_children(child); |
| 815 | child = next; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * selftest_data_add - Reads, copies data from |
| 823 | * linked tree and attaches it to the live tree |
| 824 | */ |
| 825 | static int __init selftest_data_add(void) |
| 826 | { |
| 827 | void *selftest_data; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 828 | struct device_node *selftest_data_node, *np; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 829 | extern uint8_t __dtb_testcases_begin[]; |
| 830 | extern uint8_t __dtb_testcases_end[]; |
| 831 | const int size = __dtb_testcases_end - __dtb_testcases_begin; |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 832 | int rc; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 833 | |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 834 | if (!size) { |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 835 | pr_warn("%s: No testcase data to attach; not running tests\n", |
| 836 | __func__); |
| 837 | return -ENODATA; |
| 838 | } |
| 839 | |
| 840 | /* creating copy */ |
| 841 | selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL); |
| 842 | |
| 843 | if (!selftest_data) { |
| 844 | pr_warn("%s: Failed to allocate memory for selftest_data; " |
| 845 | "not running tests\n", __func__); |
| 846 | return -ENOMEM; |
| 847 | } |
| 848 | of_fdt_unflatten_tree(selftest_data, &selftest_data_node); |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 849 | if (!selftest_data_node) { |
| 850 | pr_warn("%s: No tree to attach; not running tests\n", __func__); |
| 851 | return -ENODATA; |
| 852 | } |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 853 | of_node_set_flag(selftest_data_node, OF_DETACHED); |
| 854 | rc = of_resolve_phandles(selftest_data_node); |
| 855 | if (rc) { |
| 856 | pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc); |
| 857 | return -EINVAL; |
| 858 | } |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 859 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 860 | if (!of_root) { |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 861 | /* enabling flag for removing nodes */ |
| 862 | selftest_live_tree = true; |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 863 | of_root = selftest_data_node; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 864 | |
| 865 | for_each_of_allnodes(np) |
| 866 | __of_attach_node_sysfs(np); |
| 867 | of_aliases = of_find_node_by_path("/aliases"); |
| 868 | of_chosen = of_find_node_by_path("/chosen"); |
| 869 | return 0; |
| 870 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 871 | |
| 872 | /* attach the sub-tree to live tree */ |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 873 | np = selftest_data_node->child; |
| 874 | while (np) { |
| 875 | struct device_node *next = np->sibling; |
| 876 | np->parent = of_root; |
| 877 | attach_node_and_children(np); |
| 878 | np = next; |
| 879 | } |
| 880 | return 0; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /** |
| 884 | * detach_node_and_children - detaches node |
| 885 | * and its children from live tree |
| 886 | * |
| 887 | * @np: Node to detach from live tree |
| 888 | */ |
| 889 | static void detach_node_and_children(struct device_node *np) |
| 890 | { |
| 891 | while (np->child) |
| 892 | detach_node_and_children(np->child); |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 893 | of_detach_node(np); |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * selftest_data_remove - removes the selftest data |
| 898 | * nodes from the live tree |
| 899 | */ |
| 900 | static void selftest_data_remove(void) |
| 901 | { |
| 902 | struct device_node *np; |
| 903 | struct property *prop; |
| 904 | |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 905 | if (selftest_live_tree) { |
| 906 | of_node_put(of_aliases); |
| 907 | of_node_put(of_chosen); |
| 908 | of_aliases = NULL; |
| 909 | of_chosen = NULL; |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 910 | for_each_child_of_node(of_root, np) |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 911 | detach_node_and_children(np); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 912 | __of_detach_node_sysfs(of_root); |
| 913 | of_root = NULL; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 914 | return; |
| 915 | } |
| 916 | |
Grant Likely | c1a2086 | 2014-11-19 16:22:32 +0000 | [diff] [blame] | 917 | while (last_node_index-- > 0) { |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 918 | if (nodes[last_node_index]) { |
| 919 | np = of_find_node_by_path(nodes[last_node_index]->full_name); |
Grant Likely | 788ec2f | 2014-11-19 17:13:44 +0000 | [diff] [blame] | 920 | if (np == nodes[last_node_index]) { |
| 921 | if (of_aliases == np) { |
| 922 | of_node_put(of_aliases); |
| 923 | of_aliases = NULL; |
| 924 | } |
Grant Likely | e66c98c | 2014-10-01 16:57:07 +0100 | [diff] [blame] | 925 | detach_node_and_children(np); |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 926 | } else { |
| 927 | for_each_property_of_node(np, prop) { |
| 928 | if (strcmp(prop->name, "testcase-alias") == 0) |
| 929 | of_remove_property(np, prop); |
| 930 | } |
| 931 | } |
| 932 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 936 | static int __init of_selftest(void) |
| 937 | { |
| 938 | struct device_node *np; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 939 | int res; |
| 940 | |
| 941 | /* adding data for selftest */ |
| 942 | res = selftest_data_add(); |
| 943 | if (res) |
| 944 | return res; |
Grant Likely | 788ec2f | 2014-11-19 17:13:44 +0000 | [diff] [blame] | 945 | if (!of_aliases) |
| 946 | of_aliases = of_find_node_by_path("/aliases"); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 947 | |
| 948 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 949 | if (!np) { |
| 950 | pr_info("No testcase data in device tree; not running tests\n"); |
| 951 | return 0; |
| 952 | } |
| 953 | of_node_put(np); |
| 954 | |
| 955 | pr_info("start of selftest - you will see error messages\n"); |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 956 | of_selftest_check_tree_linkage(); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 957 | of_selftest_check_phandles(); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 958 | of_selftest_find_node_by_name(); |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 959 | of_selftest_dynamic(); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 960 | of_selftest_parse_phandle_with_args(); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 961 | of_selftest_property_string(); |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 962 | of_selftest_property_copy(); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 963 | of_selftest_changeset(); |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 964 | of_selftest_parse_interrupts(); |
Grant Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 965 | of_selftest_parse_interrupts_extended(); |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 966 | of_selftest_match_node(); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 967 | of_selftest_platform_populate(); |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 968 | |
| 969 | /* removing selftest data from live tree */ |
| 970 | selftest_data_remove(); |
| 971 | |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 972 | /* Double check linkage after removing testcase data */ |
| 973 | of_selftest_check_tree_linkage(); |
| 974 | |
| 975 | pr_info("end of selftest - %i passed, %i failed\n", |
| 976 | selftest_results.passed, selftest_results.failed); |
| 977 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 978 | return 0; |
| 979 | } |
| 980 | late_initcall(of_selftest); |