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> |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/of_platform.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 22 | |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 23 | #include "of_private.h" |
| 24 | |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 25 | static struct selftest_results { |
| 26 | int passed; |
| 27 | int failed; |
| 28 | } selftest_results; |
| 29 | |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 30 | #define NO_OF_NODES 3 |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 31 | static struct device_node *nodes[NO_OF_NODES]; |
| 32 | static int last_node_index; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 33 | static bool selftest_live_tree; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 34 | |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 35 | #define selftest(result, fmt, ...) ({ \ |
| 36 | bool failed = !(result); \ |
| 37 | if (failed) { \ |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 38 | selftest_results.failed++; \ |
| 39 | pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 40 | } else { \ |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 41 | selftest_results.passed++; \ |
| 42 | pr_debug("pass %s():%i\n", __func__, __LINE__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 43 | } \ |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 44 | failed; \ |
| 45 | }) |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 46 | |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 47 | static void __init of_selftest_find_node_by_name(void) |
| 48 | { |
| 49 | struct device_node *np; |
| 50 | |
| 51 | np = of_find_node_by_path("/testcase-data"); |
| 52 | selftest(np && !strcmp("/testcase-data", np->full_name), |
| 53 | "find /testcase-data failed\n"); |
| 54 | of_node_put(np); |
| 55 | |
| 56 | /* Test if trailing '/' works */ |
| 57 | np = of_find_node_by_path("/testcase-data/"); |
| 58 | selftest(!np, "trailing '/' on /testcase-data/ should fail\n"); |
| 59 | |
| 60 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 61 | selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name), |
| 62 | "find /testcase-data/phandle-tests/consumer-a failed\n"); |
| 63 | of_node_put(np); |
| 64 | |
| 65 | np = of_find_node_by_path("testcase-alias"); |
| 66 | selftest(np && !strcmp("/testcase-data", np->full_name), |
| 67 | "find testcase-alias failed\n"); |
| 68 | of_node_put(np); |
| 69 | |
| 70 | /* Test if trailing '/' works on aliases */ |
| 71 | np = of_find_node_by_path("testcase-alias/"); |
| 72 | selftest(!np, "trailing '/' on testcase-alias/ should fail\n"); |
| 73 | |
| 74 | np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a"); |
| 75 | selftest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", np->full_name), |
| 76 | "find testcase-alias/phandle-tests/consumer-a failed\n"); |
| 77 | of_node_put(np); |
| 78 | |
| 79 | np = of_find_node_by_path("/testcase-data/missing-path"); |
| 80 | selftest(!np, "non-existent path returned node %s\n", np->full_name); |
| 81 | of_node_put(np); |
| 82 | |
| 83 | np = of_find_node_by_path("missing-alias"); |
| 84 | selftest(!np, "non-existent alias returned node %s\n", np->full_name); |
| 85 | of_node_put(np); |
| 86 | |
| 87 | np = of_find_node_by_path("testcase-alias/missing-path"); |
| 88 | selftest(!np, "non-existent alias with relative path returned node %s\n", np->full_name); |
| 89 | of_node_put(np); |
| 90 | } |
| 91 | |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 92 | static void __init of_selftest_dynamic(void) |
| 93 | { |
| 94 | struct device_node *np; |
| 95 | struct property *prop; |
| 96 | |
| 97 | np = of_find_node_by_path("/testcase-data"); |
| 98 | if (!np) { |
| 99 | pr_err("missing testcase data\n"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | /* Array of 4 properties for the purpose of testing */ |
| 104 | prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL); |
| 105 | if (!prop) { |
| 106 | selftest(0, "kzalloc() failed\n"); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | /* Add a new property - should pass*/ |
| 111 | prop->name = "new-property"; |
| 112 | prop->value = "new-property-data"; |
| 113 | prop->length = strlen(prop->value); |
| 114 | selftest(of_add_property(np, prop) == 0, "Adding a new property failed\n"); |
| 115 | |
| 116 | /* Try to add an existing property - should fail */ |
| 117 | prop++; |
| 118 | prop->name = "new-property"; |
| 119 | prop->value = "new-property-data-should-fail"; |
| 120 | prop->length = strlen(prop->value); |
| 121 | selftest(of_add_property(np, prop) != 0, |
| 122 | "Adding an existing property should have failed\n"); |
| 123 | |
| 124 | /* Try to modify an existing property - should pass */ |
| 125 | prop->value = "modify-property-data-should-pass"; |
| 126 | prop->length = strlen(prop->value); |
| 127 | selftest(of_update_property(np, prop) == 0, |
| 128 | "Updating an existing property should have passed\n"); |
| 129 | |
| 130 | /* Try to modify non-existent property - should pass*/ |
| 131 | prop++; |
| 132 | prop->name = "modify-property"; |
| 133 | prop->value = "modify-missing-property-data-should-pass"; |
| 134 | prop->length = strlen(prop->value); |
| 135 | selftest(of_update_property(np, prop) == 0, |
| 136 | "Updating a missing property should have passed\n"); |
| 137 | |
| 138 | /* Remove property - should pass */ |
| 139 | selftest(of_remove_property(np, prop) == 0, |
| 140 | "Removing a property should have passed\n"); |
| 141 | |
| 142 | /* Adding very large property - should pass */ |
| 143 | prop++; |
| 144 | prop->name = "large-property-PAGE_SIZEx8"; |
| 145 | prop->length = PAGE_SIZE * 8; |
| 146 | prop->value = kzalloc(prop->length, GFP_KERNEL); |
| 147 | selftest(prop->value != NULL, "Unable to allocate large buffer\n"); |
| 148 | if (prop->value) |
| 149 | selftest(of_add_property(np, prop) == 0, |
| 150 | "Adding a large property should have passed\n"); |
| 151 | } |
| 152 | |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 153 | static int __init of_selftest_check_node_linkage(struct device_node *np) |
| 154 | { |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 155 | struct device_node *child; |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 156 | int count = 0, rc; |
| 157 | |
| 158 | for_each_child_of_node(np, child) { |
| 159 | if (child->parent != np) { |
| 160 | pr_err("Child node %s links to wrong parent %s\n", |
| 161 | child->name, np->name); |
| 162 | return -EINVAL; |
| 163 | } |
| 164 | |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 165 | rc = of_selftest_check_node_linkage(child); |
| 166 | if (rc < 0) |
| 167 | return rc; |
| 168 | count += rc; |
| 169 | } |
| 170 | |
| 171 | return count + 1; |
| 172 | } |
| 173 | |
| 174 | static void __init of_selftest_check_tree_linkage(void) |
| 175 | { |
| 176 | struct device_node *np; |
| 177 | int allnode_count = 0, child_count; |
| 178 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 179 | if (!of_root) |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 180 | return; |
| 181 | |
| 182 | for_each_of_allnodes(np) |
| 183 | allnode_count++; |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 184 | child_count = of_selftest_check_node_linkage(of_root); |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 185 | |
| 186 | selftest(child_count > 0, "Device node data structure is corrupted\n"); |
| 187 | selftest(child_count == allnode_count, "allnodes list size (%i) doesn't match" |
| 188 | "sibling lists size (%i)\n", allnode_count, child_count); |
| 189 | pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count); |
| 190 | } |
| 191 | |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 192 | struct node_hash { |
| 193 | struct hlist_node node; |
| 194 | struct device_node *np; |
| 195 | }; |
| 196 | |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 197 | static DEFINE_HASHTABLE(phandle_ht, 8); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 198 | static void __init of_selftest_check_phandles(void) |
| 199 | { |
| 200 | struct device_node *np; |
| 201 | struct node_hash *nh; |
| 202 | struct hlist_node *tmp; |
| 203 | int i, dup_count = 0, phandle_count = 0; |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 204 | |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 205 | for_each_of_allnodes(np) { |
| 206 | if (!np->phandle) |
| 207 | continue; |
| 208 | |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 209 | hash_for_each_possible(phandle_ht, nh, node, np->phandle) { |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 210 | if (nh->np->phandle == np->phandle) { |
| 211 | pr_info("Duplicate phandle! %i used by %s and %s\n", |
| 212 | np->phandle, nh->np->full_name, np->full_name); |
| 213 | dup_count++; |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); |
| 219 | if (WARN_ON(!nh)) |
| 220 | return; |
| 221 | |
| 222 | nh->np = np; |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 223 | hash_add(phandle_ht, &nh->node, np->phandle); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 224 | phandle_count++; |
| 225 | } |
| 226 | selftest(dup_count == 0, "Found %i duplicates in %i phandles\n", |
| 227 | dup_count, phandle_count); |
| 228 | |
| 229 | /* Clean up */ |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 230 | hash_for_each_safe(phandle_ht, i, tmp, nh, node) { |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 231 | hash_del(&nh->node); |
| 232 | kfree(nh); |
| 233 | } |
| 234 | } |
| 235 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 236 | static void __init of_selftest_parse_phandle_with_args(void) |
| 237 | { |
| 238 | struct device_node *np; |
| 239 | struct of_phandle_args args; |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 240 | int i, rc; |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 241 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 242 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 243 | if (!np) { |
| 244 | pr_err("missing testcase data\n"); |
| 245 | return; |
| 246 | } |
| 247 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 248 | rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells"); |
| 249 | selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc); |
| 250 | |
Grant Likely | f7f951c | 2013-02-12 17:41:22 +0000 | [diff] [blame] | 251 | for (i = 0; i < 8; i++) { |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 252 | bool passed = true; |
| 253 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 254 | "#phandle-cells", i, &args); |
| 255 | |
| 256 | /* Test the values from tests-phandle.dtsi */ |
| 257 | switch (i) { |
| 258 | case 0: |
| 259 | passed &= !rc; |
| 260 | passed &= (args.args_count == 1); |
| 261 | passed &= (args.args[0] == (i + 1)); |
| 262 | break; |
| 263 | case 1: |
| 264 | passed &= !rc; |
| 265 | passed &= (args.args_count == 2); |
| 266 | passed &= (args.args[0] == (i + 1)); |
| 267 | passed &= (args.args[1] == 0); |
| 268 | break; |
| 269 | case 2: |
| 270 | passed &= (rc == -ENOENT); |
| 271 | break; |
| 272 | case 3: |
| 273 | passed &= !rc; |
| 274 | passed &= (args.args_count == 3); |
| 275 | passed &= (args.args[0] == (i + 1)); |
| 276 | passed &= (args.args[1] == 4); |
| 277 | passed &= (args.args[2] == 3); |
| 278 | break; |
| 279 | case 4: |
| 280 | passed &= !rc; |
| 281 | passed &= (args.args_count == 2); |
| 282 | passed &= (args.args[0] == (i + 1)); |
| 283 | passed &= (args.args[1] == 100); |
| 284 | break; |
| 285 | case 5: |
| 286 | passed &= !rc; |
| 287 | passed &= (args.args_count == 0); |
| 288 | break; |
| 289 | case 6: |
| 290 | passed &= !rc; |
| 291 | passed &= (args.args_count == 1); |
| 292 | passed &= (args.args[0] == (i + 1)); |
| 293 | break; |
| 294 | case 7: |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 295 | passed &= (rc == -ENOENT); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 296 | break; |
| 297 | default: |
| 298 | passed = false; |
| 299 | } |
| 300 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 301 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 302 | i, args.np->full_name, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* Check for missing list property */ |
| 306 | rc = of_parse_phandle_with_args(np, "phandle-list-missing", |
| 307 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 308 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 309 | rc = of_count_phandle_with_args(np, "phandle-list-missing", |
| 310 | "#phandle-cells"); |
| 311 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 312 | |
| 313 | /* Check for missing cells property */ |
| 314 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 315 | "#phandle-cells-missing", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 316 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 317 | rc = of_count_phandle_with_args(np, "phandle-list", |
| 318 | "#phandle-cells-missing"); |
| 319 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 320 | |
| 321 | /* Check for bad phandle in list */ |
| 322 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle", |
| 323 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 324 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 325 | rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle", |
| 326 | "#phandle-cells"); |
| 327 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 328 | |
| 329 | /* Check for incorrectly formed argument list */ |
| 330 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-args", |
| 331 | "#phandle-cells", 1, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 332 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 333 | rc = of_count_phandle_with_args(np, "phandle-list-bad-args", |
| 334 | "#phandle-cells"); |
| 335 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 338 | static void __init of_selftest_property_string(void) |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 339 | { |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 340 | const char *strings[4]; |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 341 | struct device_node *np; |
| 342 | int rc; |
| 343 | |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 344 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 345 | if (!np) { |
| 346 | pr_err("No testcase data in device tree\n"); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | rc = of_property_match_string(np, "phandle-list-names", "first"); |
| 351 | selftest(rc == 0, "first expected:0 got:%i\n", rc); |
| 352 | rc = of_property_match_string(np, "phandle-list-names", "second"); |
| 353 | selftest(rc == 1, "second expected:0 got:%i\n", rc); |
| 354 | rc = of_property_match_string(np, "phandle-list-names", "third"); |
| 355 | selftest(rc == 2, "third expected:0 got:%i\n", rc); |
| 356 | rc = of_property_match_string(np, "phandle-list-names", "fourth"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 357 | selftest(rc == -ENODATA, "unmatched string; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 358 | rc = of_property_match_string(np, "missing-property", "blah"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 359 | selftest(rc == -EINVAL, "missing property; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 360 | rc = of_property_match_string(np, "empty-property", "blah"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 361 | selftest(rc == -ENODATA, "empty property; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 362 | rc = of_property_match_string(np, "unterminated-string", "blah"); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 363 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
| 364 | |
| 365 | /* of_property_count_strings() tests */ |
| 366 | rc = of_property_count_strings(np, "string-property"); |
| 367 | selftest(rc == 1, "Incorrect string count; rc=%i\n", rc); |
| 368 | rc = of_property_count_strings(np, "phandle-list-names"); |
| 369 | selftest(rc == 3, "Incorrect string count; rc=%i\n", rc); |
| 370 | rc = of_property_count_strings(np, "unterminated-string"); |
| 371 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
| 372 | rc = of_property_count_strings(np, "unterminated-string-list"); |
| 373 | selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); |
| 374 | |
| 375 | /* of_property_read_string_index() tests */ |
| 376 | rc = of_property_read_string_index(np, "string-property", 0, strings); |
| 377 | selftest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc); |
| 378 | strings[0] = NULL; |
| 379 | rc = of_property_read_string_index(np, "string-property", 1, strings); |
| 380 | selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
| 381 | rc = of_property_read_string_index(np, "phandle-list-names", 0, strings); |
| 382 | selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); |
| 383 | rc = of_property_read_string_index(np, "phandle-list-names", 1, strings); |
| 384 | selftest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc); |
| 385 | rc = of_property_read_string_index(np, "phandle-list-names", 2, strings); |
| 386 | selftest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc); |
| 387 | strings[0] = NULL; |
| 388 | rc = of_property_read_string_index(np, "phandle-list-names", 3, strings); |
| 389 | selftest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
| 390 | strings[0] = NULL; |
| 391 | rc = of_property_read_string_index(np, "unterminated-string", 0, strings); |
| 392 | selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
| 393 | rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings); |
| 394 | selftest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); |
| 395 | strings[0] = NULL; |
| 396 | rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */ |
| 397 | selftest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
| 398 | strings[1] = NULL; |
| 399 | |
| 400 | /* of_property_read_string_array() tests */ |
| 401 | rc = of_property_read_string_array(np, "string-property", strings, 4); |
| 402 | selftest(rc == 1, "Incorrect string count; rc=%i\n", rc); |
| 403 | rc = of_property_read_string_array(np, "phandle-list-names", strings, 4); |
| 404 | selftest(rc == 3, "Incorrect string count; rc=%i\n", rc); |
| 405 | rc = of_property_read_string_array(np, "unterminated-string", strings, 4); |
| 406 | selftest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
| 407 | /* -- An incorrectly formed string should cause a failure */ |
| 408 | rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4); |
| 409 | selftest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); |
| 410 | /* -- parsing the correctly formed strings should still work: */ |
| 411 | strings[2] = NULL; |
| 412 | rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2); |
| 413 | selftest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc); |
| 414 | strings[1] = NULL; |
| 415 | rc = of_property_read_string_array(np, "phandle-list-names", strings, 1); |
| 416 | 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] | 417 | } |
| 418 | |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 419 | #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \ |
| 420 | (p1)->value && (p2)->value && \ |
| 421 | !memcmp((p1)->value, (p2)->value, (p1)->length) && \ |
| 422 | !strcmp((p1)->name, (p2)->name)) |
| 423 | static void __init of_selftest_property_copy(void) |
| 424 | { |
| 425 | #ifdef CONFIG_OF_DYNAMIC |
| 426 | struct property p1 = { .name = "p1", .length = 0, .value = "" }; |
| 427 | struct property p2 = { .name = "p2", .length = 5, .value = "abcd" }; |
| 428 | struct property *new; |
| 429 | |
| 430 | new = __of_prop_dup(&p1, GFP_KERNEL); |
| 431 | selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n"); |
| 432 | kfree(new->value); |
| 433 | kfree(new->name); |
| 434 | kfree(new); |
| 435 | |
| 436 | new = __of_prop_dup(&p2, GFP_KERNEL); |
| 437 | selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n"); |
| 438 | kfree(new->value); |
| 439 | kfree(new->name); |
| 440 | kfree(new); |
| 441 | #endif |
| 442 | } |
| 443 | |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 444 | static void __init of_selftest_changeset(void) |
| 445 | { |
| 446 | #ifdef CONFIG_OF_DYNAMIC |
| 447 | struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" }; |
| 448 | struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" }; |
| 449 | struct property *ppremove; |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 450 | struct device_node *n1, *n2, *n21, *nremove, *parent, *np; |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 451 | struct of_changeset chgset; |
| 452 | |
| 453 | of_changeset_init(&chgset); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 454 | n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 455 | selftest(n1, "testcase setup failure\n"); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 456 | n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 457 | selftest(n2, "testcase setup failure\n"); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 458 | n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 459 | selftest(n21, "testcase setup failure %p\n", n21); |
| 460 | nremove = of_find_node_by_path("/testcase-data/changeset/node-remove"); |
| 461 | selftest(nremove, "testcase setup failure\n"); |
| 462 | ppadd = __of_prop_dup(&padd, GFP_KERNEL); |
| 463 | selftest(ppadd, "testcase setup failure\n"); |
| 464 | ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL); |
| 465 | selftest(ppupdate, "testcase setup failure\n"); |
| 466 | parent = nremove->parent; |
| 467 | n1->parent = parent; |
| 468 | n2->parent = parent; |
| 469 | n21->parent = n2; |
| 470 | n2->child = n21; |
| 471 | ppremove = of_find_property(parent, "prop-remove", NULL); |
| 472 | selftest(ppremove, "failed to find removal prop"); |
| 473 | |
| 474 | of_changeset_init(&chgset); |
| 475 | selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n"); |
| 476 | selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n"); |
| 477 | selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n"); |
| 478 | selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n"); |
| 479 | selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n"); |
| 480 | selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n"); |
| 481 | selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n"); |
| 482 | mutex_lock(&of_mutex); |
| 483 | selftest(!of_changeset_apply(&chgset), "apply failed\n"); |
| 484 | mutex_unlock(&of_mutex); |
| 485 | |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 486 | /* Make sure node names are constructed correctly */ |
| 487 | selftest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")), |
| 488 | "'%s' not added\n", n21->full_name); |
| 489 | if (np) |
| 490 | of_node_put(np); |
| 491 | |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 492 | mutex_lock(&of_mutex); |
| 493 | selftest(!of_changeset_revert(&chgset), "revert failed\n"); |
| 494 | mutex_unlock(&of_mutex); |
| 495 | |
| 496 | of_changeset_destroy(&chgset); |
| 497 | #endif |
| 498 | } |
| 499 | |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 500 | static void __init of_selftest_parse_interrupts(void) |
| 501 | { |
| 502 | struct device_node *np; |
| 503 | struct of_phandle_args args; |
| 504 | int i, rc; |
| 505 | |
| 506 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts0"); |
| 507 | if (!np) { |
| 508 | pr_err("missing testcase data\n"); |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | for (i = 0; i < 4; i++) { |
| 513 | bool passed = true; |
| 514 | args.args_count = 0; |
| 515 | rc = of_irq_parse_one(np, i, &args); |
| 516 | |
| 517 | passed &= !rc; |
| 518 | passed &= (args.args_count == 1); |
| 519 | passed &= (args.args[0] == (i + 1)); |
| 520 | |
| 521 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 522 | i, args.np->full_name, rc); |
| 523 | } |
| 524 | of_node_put(np); |
| 525 | |
| 526 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts1"); |
| 527 | if (!np) { |
| 528 | pr_err("missing testcase data\n"); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | for (i = 0; i < 4; i++) { |
| 533 | bool passed = true; |
| 534 | args.args_count = 0; |
| 535 | rc = of_irq_parse_one(np, i, &args); |
| 536 | |
| 537 | /* Test the values from tests-phandle.dtsi */ |
| 538 | switch (i) { |
| 539 | case 0: |
| 540 | passed &= !rc; |
| 541 | passed &= (args.args_count == 1); |
| 542 | passed &= (args.args[0] == 9); |
| 543 | break; |
| 544 | case 1: |
| 545 | passed &= !rc; |
| 546 | passed &= (args.args_count == 3); |
| 547 | passed &= (args.args[0] == 10); |
| 548 | passed &= (args.args[1] == 11); |
| 549 | passed &= (args.args[2] == 12); |
| 550 | break; |
| 551 | case 2: |
| 552 | passed &= !rc; |
| 553 | passed &= (args.args_count == 2); |
| 554 | passed &= (args.args[0] == 13); |
| 555 | passed &= (args.args[1] == 14); |
| 556 | break; |
| 557 | case 3: |
| 558 | passed &= !rc; |
| 559 | passed &= (args.args_count == 2); |
| 560 | passed &= (args.args[0] == 15); |
| 561 | passed &= (args.args[1] == 16); |
| 562 | break; |
| 563 | default: |
| 564 | passed = false; |
| 565 | } |
| 566 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 567 | i, args.np->full_name, rc); |
| 568 | } |
| 569 | of_node_put(np); |
| 570 | } |
| 571 | |
Grant Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 572 | static void __init of_selftest_parse_interrupts_extended(void) |
| 573 | { |
| 574 | struct device_node *np; |
| 575 | struct of_phandle_args args; |
| 576 | int i, rc; |
| 577 | |
| 578 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0"); |
| 579 | if (!np) { |
| 580 | pr_err("missing testcase data\n"); |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | for (i = 0; i < 7; i++) { |
| 585 | bool passed = true; |
| 586 | rc = of_irq_parse_one(np, i, &args); |
| 587 | |
| 588 | /* Test the values from tests-phandle.dtsi */ |
| 589 | switch (i) { |
| 590 | case 0: |
| 591 | passed &= !rc; |
| 592 | passed &= (args.args_count == 1); |
| 593 | passed &= (args.args[0] == 1); |
| 594 | break; |
| 595 | case 1: |
| 596 | passed &= !rc; |
| 597 | passed &= (args.args_count == 3); |
| 598 | passed &= (args.args[0] == 2); |
| 599 | passed &= (args.args[1] == 3); |
| 600 | passed &= (args.args[2] == 4); |
| 601 | break; |
| 602 | case 2: |
| 603 | passed &= !rc; |
| 604 | passed &= (args.args_count == 2); |
| 605 | passed &= (args.args[0] == 5); |
| 606 | passed &= (args.args[1] == 6); |
| 607 | break; |
| 608 | case 3: |
| 609 | passed &= !rc; |
| 610 | passed &= (args.args_count == 1); |
| 611 | passed &= (args.args[0] == 9); |
| 612 | break; |
| 613 | case 4: |
| 614 | passed &= !rc; |
| 615 | passed &= (args.args_count == 3); |
| 616 | passed &= (args.args[0] == 10); |
| 617 | passed &= (args.args[1] == 11); |
| 618 | passed &= (args.args[2] == 12); |
| 619 | break; |
| 620 | case 5: |
| 621 | passed &= !rc; |
| 622 | passed &= (args.args_count == 2); |
| 623 | passed &= (args.args[0] == 13); |
| 624 | passed &= (args.args[1] == 14); |
| 625 | break; |
| 626 | case 6: |
| 627 | passed &= !rc; |
| 628 | passed &= (args.args_count == 1); |
| 629 | passed &= (args.args[0] == 15); |
| 630 | break; |
| 631 | default: |
| 632 | passed = false; |
| 633 | } |
| 634 | |
| 635 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 636 | i, args.np->full_name, rc); |
| 637 | } |
| 638 | of_node_put(np); |
| 639 | } |
| 640 | |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 641 | static struct of_device_id match_node_table[] = { |
| 642 | { .data = "A", .name = "name0", }, /* Name alone is lowest priority */ |
| 643 | { .data = "B", .type = "type1", }, /* followed by type alone */ |
| 644 | |
| 645 | { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */ |
| 646 | { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */ |
| 647 | { .data = "Cc", .name = "name2", .type = "type2", }, |
| 648 | |
| 649 | { .data = "E", .compatible = "compat3" }, |
| 650 | { .data = "G", .compatible = "compat2", }, |
| 651 | { .data = "H", .compatible = "compat2", .name = "name5", }, |
| 652 | { .data = "I", .compatible = "compat2", .type = "type1", }, |
| 653 | { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", }, |
| 654 | { .data = "K", .compatible = "compat2", .name = "name9", }, |
| 655 | {} |
| 656 | }; |
| 657 | |
| 658 | static struct { |
| 659 | const char *path; |
| 660 | const char *data; |
| 661 | } match_node_tests[] = { |
| 662 | { .path = "/testcase-data/match-node/name0", .data = "A", }, |
| 663 | { .path = "/testcase-data/match-node/name1", .data = "B", }, |
| 664 | { .path = "/testcase-data/match-node/a/name2", .data = "Ca", }, |
| 665 | { .path = "/testcase-data/match-node/b/name2", .data = "Cb", }, |
| 666 | { .path = "/testcase-data/match-node/c/name2", .data = "Cc", }, |
| 667 | { .path = "/testcase-data/match-node/name3", .data = "E", }, |
| 668 | { .path = "/testcase-data/match-node/name4", .data = "G", }, |
| 669 | { .path = "/testcase-data/match-node/name5", .data = "H", }, |
| 670 | { .path = "/testcase-data/match-node/name6", .data = "G", }, |
| 671 | { .path = "/testcase-data/match-node/name7", .data = "I", }, |
| 672 | { .path = "/testcase-data/match-node/name8", .data = "J", }, |
| 673 | { .path = "/testcase-data/match-node/name9", .data = "K", }, |
| 674 | }; |
| 675 | |
| 676 | static void __init of_selftest_match_node(void) |
| 677 | { |
| 678 | struct device_node *np; |
| 679 | const struct of_device_id *match; |
| 680 | int i; |
| 681 | |
| 682 | for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) { |
| 683 | np = of_find_node_by_path(match_node_tests[i].path); |
| 684 | if (!np) { |
| 685 | selftest(0, "missing testcase node %s\n", |
| 686 | match_node_tests[i].path); |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | match = of_match_node(match_node_table, np); |
| 691 | if (!match) { |
| 692 | selftest(0, "%s didn't match anything\n", |
| 693 | match_node_tests[i].path); |
| 694 | continue; |
| 695 | } |
| 696 | |
| 697 | if (strcmp(match->data, match_node_tests[i].data) != 0) { |
| 698 | selftest(0, "%s got wrong match. expected %s, got %s\n", |
| 699 | match_node_tests[i].path, match_node_tests[i].data, |
| 700 | (const char *)match->data); |
| 701 | continue; |
| 702 | } |
| 703 | selftest(1, "passed"); |
| 704 | } |
| 705 | } |
| 706 | |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 707 | struct device test_bus = { |
| 708 | .init_name = "unittest-bus", |
| 709 | }; |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 710 | static void __init of_selftest_platform_populate(void) |
| 711 | { |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 712 | int irq, rc; |
| 713 | struct device_node *np, *child, *grandchild; |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 714 | struct platform_device *pdev; |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 715 | struct of_device_id match[] = { |
| 716 | { .compatible = "test-device", }, |
| 717 | {} |
| 718 | }; |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 719 | |
| 720 | np = of_find_node_by_path("/testcase-data"); |
| 721 | of_platform_populate(np, of_default_bus_match_table, NULL, NULL); |
| 722 | |
| 723 | /* Test that a missing irq domain returns -EPROBE_DEFER */ |
| 724 | np = of_find_node_by_path("/testcase-data/testcase-device1"); |
| 725 | pdev = of_find_device_by_node(np); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 726 | selftest(pdev, "device 1 creation failed\n"); |
| 727 | |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 728 | irq = platform_get_irq(pdev, 0); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 729 | selftest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 730 | |
| 731 | /* Test that a parsing failure does not return -EPROBE_DEFER */ |
| 732 | np = of_find_node_by_path("/testcase-data/testcase-device2"); |
| 733 | pdev = of_find_device_by_node(np); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 734 | selftest(pdev, "device 2 creation failed\n"); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 735 | irq = platform_get_irq(pdev, 0); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 736 | 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] | 737 | |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 738 | if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"), |
| 739 | "No testcase data in device tree\n")); |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 740 | return; |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 741 | |
| 742 | if (selftest(!(rc = device_register(&test_bus)), |
| 743 | "testbus registration failed; rc=%i\n", rc)); |
| 744 | return; |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 745 | |
| 746 | for_each_child_of_node(np, child) { |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 747 | of_platform_populate(child, match, NULL, &test_bus); |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 748 | for_each_child_of_node(child, grandchild) |
| 749 | selftest(of_find_device_by_node(grandchild), |
| 750 | "Could not create device for node '%s'\n", |
| 751 | grandchild->name); |
| 752 | } |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 753 | |
| 754 | of_platform_depopulate(&test_bus); |
| 755 | for_each_child_of_node(np, child) { |
| 756 | for_each_child_of_node(child, grandchild) |
| 757 | selftest(!of_find_device_by_node(grandchild), |
| 758 | "device didn't get destroyed '%s'\n", |
| 759 | grandchild->name); |
| 760 | } |
| 761 | |
| 762 | device_unregister(&test_bus); |
| 763 | of_node_put(np); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 764 | } |
| 765 | |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 766 | /** |
| 767 | * update_node_properties - adds the properties |
| 768 | * of np into dup node (present in live tree) and |
| 769 | * updates parent of children of np to dup. |
| 770 | * |
| 771 | * @np: node already present in live tree |
| 772 | * @dup: node present in live tree to be updated |
| 773 | */ |
| 774 | static void update_node_properties(struct device_node *np, |
| 775 | struct device_node *dup) |
| 776 | { |
| 777 | struct property *prop; |
| 778 | struct device_node *child; |
| 779 | |
| 780 | for_each_property_of_node(np, prop) |
| 781 | of_add_property(dup, prop); |
| 782 | |
| 783 | for_each_child_of_node(np, child) |
| 784 | child->parent = dup; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * attach_node_and_children - attaches nodes |
| 789 | * and its children to live tree |
| 790 | * |
| 791 | * @np: Node to attach to live tree |
| 792 | */ |
| 793 | static int attach_node_and_children(struct device_node *np) |
| 794 | { |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 795 | struct device_node *next, *dup, *child; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 796 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 797 | dup = of_find_node_by_path(np->full_name); |
| 798 | if (dup) { |
| 799 | update_node_properties(np, dup); |
| 800 | return 0; |
| 801 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 802 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 803 | /* Children of the root need to be remembered for removal */ |
| 804 | if (np->parent == of_root) { |
Grant Likely | e66c98c | 2014-10-01 16:57:07 +0100 | [diff] [blame] | 805 | if (WARN_ON(last_node_index >= NO_OF_NODES)) |
| 806 | return -EINVAL; |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 807 | nodes[last_node_index++] = np; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 808 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 809 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 810 | child = np->child; |
| 811 | np->child = NULL; |
| 812 | np->sibling = NULL; |
| 813 | of_attach_node(np); |
| 814 | while (child) { |
| 815 | next = child->sibling; |
| 816 | attach_node_and_children(child); |
| 817 | child = next; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * selftest_data_add - Reads, copies data from |
| 825 | * linked tree and attaches it to the live tree |
| 826 | */ |
| 827 | static int __init selftest_data_add(void) |
| 828 | { |
| 829 | void *selftest_data; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 830 | struct device_node *selftest_data_node, *np; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 831 | extern uint8_t __dtb_testcases_begin[]; |
| 832 | extern uint8_t __dtb_testcases_end[]; |
| 833 | const int size = __dtb_testcases_end - __dtb_testcases_begin; |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 834 | int rc; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 835 | |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 836 | if (!size) { |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 837 | pr_warn("%s: No testcase data to attach; not running tests\n", |
| 838 | __func__); |
| 839 | return -ENODATA; |
| 840 | } |
| 841 | |
| 842 | /* creating copy */ |
| 843 | selftest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL); |
| 844 | |
| 845 | if (!selftest_data) { |
| 846 | pr_warn("%s: Failed to allocate memory for selftest_data; " |
| 847 | "not running tests\n", __func__); |
| 848 | return -ENOMEM; |
| 849 | } |
| 850 | of_fdt_unflatten_tree(selftest_data, &selftest_data_node); |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 851 | if (!selftest_data_node) { |
| 852 | pr_warn("%s: No tree to attach; not running tests\n", __func__); |
| 853 | return -ENODATA; |
| 854 | } |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 855 | of_node_set_flag(selftest_data_node, OF_DETACHED); |
| 856 | rc = of_resolve_phandles(selftest_data_node); |
| 857 | if (rc) { |
| 858 | pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc); |
| 859 | return -EINVAL; |
| 860 | } |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 861 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 862 | if (!of_root) { |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 863 | /* enabling flag for removing nodes */ |
| 864 | selftest_live_tree = true; |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 865 | of_root = selftest_data_node; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 866 | |
| 867 | for_each_of_allnodes(np) |
| 868 | __of_attach_node_sysfs(np); |
| 869 | of_aliases = of_find_node_by_path("/aliases"); |
| 870 | of_chosen = of_find_node_by_path("/chosen"); |
| 871 | return 0; |
| 872 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 873 | |
| 874 | /* attach the sub-tree to live tree */ |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 875 | np = selftest_data_node->child; |
| 876 | while (np) { |
| 877 | struct device_node *next = np->sibling; |
| 878 | np->parent = of_root; |
| 879 | attach_node_and_children(np); |
| 880 | np = next; |
| 881 | } |
| 882 | return 0; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | /** |
| 886 | * detach_node_and_children - detaches node |
| 887 | * and its children from live tree |
| 888 | * |
| 889 | * @np: Node to detach from live tree |
| 890 | */ |
| 891 | static void detach_node_and_children(struct device_node *np) |
| 892 | { |
| 893 | while (np->child) |
| 894 | detach_node_and_children(np->child); |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 895 | of_detach_node(np); |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * selftest_data_remove - removes the selftest data |
| 900 | * nodes from the live tree |
| 901 | */ |
| 902 | static void selftest_data_remove(void) |
| 903 | { |
| 904 | struct device_node *np; |
| 905 | struct property *prop; |
| 906 | |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 907 | if (selftest_live_tree) { |
| 908 | of_node_put(of_aliases); |
| 909 | of_node_put(of_chosen); |
| 910 | of_aliases = NULL; |
| 911 | of_chosen = NULL; |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 912 | for_each_child_of_node(of_root, np) |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 913 | detach_node_and_children(np); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 914 | __of_detach_node_sysfs(of_root); |
| 915 | of_root = NULL; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 916 | return; |
| 917 | } |
| 918 | |
Grant Likely | c1a2086 | 2014-11-19 16:22:32 +0000 | [diff] [blame] | 919 | while (last_node_index-- > 0) { |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 920 | if (nodes[last_node_index]) { |
| 921 | np = of_find_node_by_path(nodes[last_node_index]->full_name); |
Grant Likely | 788ec2f | 2014-11-19 17:13:44 +0000 | [diff] [blame] | 922 | if (np == nodes[last_node_index]) { |
| 923 | if (of_aliases == np) { |
| 924 | of_node_put(of_aliases); |
| 925 | of_aliases = NULL; |
| 926 | } |
Grant Likely | e66c98c | 2014-10-01 16:57:07 +0100 | [diff] [blame] | 927 | detach_node_and_children(np); |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 928 | } else { |
| 929 | for_each_property_of_node(np, prop) { |
| 930 | if (strcmp(prop->name, "testcase-alias") == 0) |
| 931 | of_remove_property(np, prop); |
| 932 | } |
| 933 | } |
| 934 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 938 | #ifdef CONFIG_OF_OVERLAY |
| 939 | |
| 940 | static int selftest_probe(struct platform_device *pdev) |
| 941 | { |
| 942 | struct device *dev = &pdev->dev; |
| 943 | struct device_node *np = dev->of_node; |
| 944 | |
| 945 | if (np == NULL) { |
| 946 | dev_err(dev, "No OF data for device\n"); |
| 947 | return -EINVAL; |
| 948 | |
| 949 | } |
| 950 | |
| 951 | dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | static int selftest_remove(struct platform_device *pdev) |
| 956 | { |
| 957 | struct device *dev = &pdev->dev; |
| 958 | struct device_node *np = dev->of_node; |
| 959 | |
| 960 | dev_dbg(dev, "%s for node @%s\n", __func__, np->full_name); |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | static struct of_device_id selftest_match[] = { |
| 965 | { .compatible = "selftest", }, |
| 966 | {}, |
| 967 | }; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 968 | |
| 969 | static struct platform_driver selftest_driver = { |
| 970 | .probe = selftest_probe, |
| 971 | .remove = selftest_remove, |
| 972 | .driver = { |
| 973 | .name = "selftest", |
| 974 | .owner = THIS_MODULE, |
| 975 | .of_match_table = of_match_ptr(selftest_match), |
| 976 | }, |
| 977 | }; |
| 978 | |
| 979 | /* get the platform device instantiated at the path */ |
| 980 | static struct platform_device *of_path_to_platform_device(const char *path) |
| 981 | { |
| 982 | struct device_node *np; |
| 983 | struct platform_device *pdev; |
| 984 | |
| 985 | np = of_find_node_by_path(path); |
| 986 | if (np == NULL) |
| 987 | return NULL; |
| 988 | |
| 989 | pdev = of_find_device_by_node(np); |
| 990 | of_node_put(np); |
| 991 | |
| 992 | return pdev; |
| 993 | } |
| 994 | |
| 995 | /* find out if a platform device exists at that path */ |
| 996 | static int of_path_platform_device_exists(const char *path) |
| 997 | { |
| 998 | struct platform_device *pdev; |
| 999 | |
| 1000 | pdev = of_path_to_platform_device(path); |
| 1001 | platform_device_put(pdev); |
| 1002 | return pdev != NULL; |
| 1003 | } |
| 1004 | |
| 1005 | static const char *selftest_path(int nr) |
| 1006 | { |
| 1007 | static char buf[256]; |
| 1008 | |
| 1009 | snprintf(buf, sizeof(buf) - 1, |
| 1010 | "/testcase-data/overlay-node/test-bus/test-selftest%d", nr); |
| 1011 | buf[sizeof(buf) - 1] = '\0'; |
| 1012 | |
| 1013 | return buf; |
| 1014 | } |
| 1015 | |
| 1016 | static const char *overlay_path(int nr) |
| 1017 | { |
| 1018 | static char buf[256]; |
| 1019 | |
| 1020 | snprintf(buf, sizeof(buf) - 1, |
| 1021 | "/testcase-data/overlay%d", nr); |
| 1022 | buf[sizeof(buf) - 1] = '\0'; |
| 1023 | |
| 1024 | return buf; |
| 1025 | } |
| 1026 | |
| 1027 | static const char *bus_path = "/testcase-data/overlay-node/test-bus"; |
| 1028 | |
| 1029 | static int of_selftest_apply_overlay(int selftest_nr, int overlay_nr, |
| 1030 | int *overlay_id) |
| 1031 | { |
| 1032 | struct device_node *np = NULL; |
| 1033 | int ret, id = -1; |
| 1034 | |
| 1035 | np = of_find_node_by_path(overlay_path(overlay_nr)); |
| 1036 | if (np == NULL) { |
| 1037 | selftest(0, "could not find overlay node @\"%s\"\n", |
| 1038 | overlay_path(overlay_nr)); |
| 1039 | ret = -EINVAL; |
| 1040 | goto out; |
| 1041 | } |
| 1042 | |
| 1043 | ret = of_overlay_create(np); |
| 1044 | if (ret < 0) { |
| 1045 | selftest(0, "could not create overlay from \"%s\"\n", |
| 1046 | overlay_path(overlay_nr)); |
| 1047 | goto out; |
| 1048 | } |
| 1049 | id = ret; |
| 1050 | |
| 1051 | ret = 0; |
| 1052 | |
| 1053 | out: |
| 1054 | of_node_put(np); |
| 1055 | |
| 1056 | if (overlay_id) |
| 1057 | *overlay_id = id; |
| 1058 | |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | /* apply an overlay while checking before and after states */ |
| 1063 | static int of_selftest_apply_overlay_check(int overlay_nr, int selftest_nr, |
| 1064 | int before, int after) |
| 1065 | { |
| 1066 | int ret; |
| 1067 | |
| 1068 | /* selftest device must not be in before state */ |
| 1069 | if (of_path_platform_device_exists(selftest_path(selftest_nr)) |
| 1070 | != before) { |
| 1071 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
| 1072 | overlay_path(overlay_nr), |
| 1073 | selftest_path(selftest_nr), |
| 1074 | !before ? "enabled" : "disabled"); |
| 1075 | return -EINVAL; |
| 1076 | } |
| 1077 | |
| 1078 | ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, NULL); |
| 1079 | if (ret != 0) { |
| 1080 | /* of_selftest_apply_overlay already called selftest() */ |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
| 1084 | /* selftest device must be to set to after state */ |
| 1085 | if (of_path_platform_device_exists(selftest_path(selftest_nr)) |
| 1086 | != after) { |
| 1087 | selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", |
| 1088 | overlay_path(overlay_nr), |
| 1089 | selftest_path(selftest_nr), |
| 1090 | !after ? "enabled" : "disabled"); |
| 1091 | return -EINVAL; |
| 1092 | } |
| 1093 | |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | /* apply an overlay and then revert it while checking before, after states */ |
| 1098 | static int of_selftest_apply_revert_overlay_check(int overlay_nr, |
| 1099 | int selftest_nr, int before, int after) |
| 1100 | { |
| 1101 | int ret, ov_id; |
| 1102 | |
| 1103 | /* selftest device must be in before state */ |
| 1104 | if (of_path_platform_device_exists(selftest_path(selftest_nr)) |
| 1105 | != before) { |
| 1106 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
| 1107 | overlay_path(overlay_nr), |
| 1108 | selftest_path(selftest_nr), |
| 1109 | !before ? "enabled" : "disabled"); |
| 1110 | return -EINVAL; |
| 1111 | } |
| 1112 | |
| 1113 | /* apply the overlay */ |
| 1114 | ret = of_selftest_apply_overlay(overlay_nr, selftest_nr, &ov_id); |
| 1115 | if (ret != 0) { |
| 1116 | /* of_selftest_apply_overlay already called selftest() */ |
| 1117 | return ret; |
| 1118 | } |
| 1119 | |
| 1120 | /* selftest device must be in after state */ |
| 1121 | if (of_path_platform_device_exists(selftest_path(selftest_nr)) |
| 1122 | != after) { |
| 1123 | selftest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", |
| 1124 | overlay_path(overlay_nr), |
| 1125 | selftest_path(selftest_nr), |
| 1126 | !after ? "enabled" : "disabled"); |
| 1127 | return -EINVAL; |
| 1128 | } |
| 1129 | |
| 1130 | ret = of_overlay_destroy(ov_id); |
| 1131 | if (ret != 0) { |
| 1132 | selftest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n", |
| 1133 | overlay_path(overlay_nr), |
| 1134 | selftest_path(selftest_nr)); |
| 1135 | return ret; |
| 1136 | } |
| 1137 | |
| 1138 | /* selftest device must be again in before state */ |
| 1139 | if (of_path_platform_device_exists(selftest_path(selftest_nr)) |
| 1140 | != before) { |
| 1141 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
| 1142 | overlay_path(overlay_nr), |
| 1143 | selftest_path(selftest_nr), |
| 1144 | !before ? "enabled" : "disabled"); |
| 1145 | return -EINVAL; |
| 1146 | } |
| 1147 | |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | /* test activation of device */ |
| 1152 | static void of_selftest_overlay_0(void) |
| 1153 | { |
| 1154 | int ret; |
| 1155 | |
| 1156 | /* device should enable */ |
| 1157 | ret = of_selftest_apply_overlay_check(0, 0, 0, 1); |
| 1158 | if (ret != 0) |
| 1159 | return; |
| 1160 | |
| 1161 | selftest(1, "overlay test %d passed\n", 0); |
| 1162 | } |
| 1163 | |
| 1164 | /* test deactivation of device */ |
| 1165 | static void of_selftest_overlay_1(void) |
| 1166 | { |
| 1167 | int ret; |
| 1168 | |
| 1169 | /* device should disable */ |
| 1170 | ret = of_selftest_apply_overlay_check(1, 1, 1, 0); |
| 1171 | if (ret != 0) |
| 1172 | return; |
| 1173 | |
| 1174 | selftest(1, "overlay test %d passed\n", 1); |
| 1175 | } |
| 1176 | |
| 1177 | /* test activation of device */ |
| 1178 | static void of_selftest_overlay_2(void) |
| 1179 | { |
| 1180 | int ret; |
| 1181 | |
| 1182 | /* device should enable */ |
| 1183 | ret = of_selftest_apply_overlay_check(2, 2, 0, 1); |
| 1184 | if (ret != 0) |
| 1185 | return; |
| 1186 | |
| 1187 | selftest(1, "overlay test %d passed\n", 2); |
| 1188 | } |
| 1189 | |
| 1190 | /* test deactivation of device */ |
| 1191 | static void of_selftest_overlay_3(void) |
| 1192 | { |
| 1193 | int ret; |
| 1194 | |
| 1195 | /* device should disable */ |
| 1196 | ret = of_selftest_apply_overlay_check(3, 3, 1, 0); |
| 1197 | if (ret != 0) |
| 1198 | return; |
| 1199 | |
| 1200 | selftest(1, "overlay test %d passed\n", 3); |
| 1201 | } |
| 1202 | |
| 1203 | /* test activation of a full device node */ |
| 1204 | static void of_selftest_overlay_4(void) |
| 1205 | { |
| 1206 | int ret; |
| 1207 | |
| 1208 | /* device should disable */ |
| 1209 | ret = of_selftest_apply_overlay_check(4, 4, 0, 1); |
| 1210 | if (ret != 0) |
| 1211 | return; |
| 1212 | |
| 1213 | selftest(1, "overlay test %d passed\n", 4); |
| 1214 | } |
| 1215 | |
| 1216 | /* test overlay apply/revert sequence */ |
| 1217 | static void of_selftest_overlay_5(void) |
| 1218 | { |
| 1219 | int ret; |
| 1220 | |
| 1221 | /* device should disable */ |
| 1222 | ret = of_selftest_apply_revert_overlay_check(5, 5, 0, 1); |
| 1223 | if (ret != 0) |
| 1224 | return; |
| 1225 | |
| 1226 | selftest(1, "overlay test %d passed\n", 5); |
| 1227 | } |
| 1228 | |
| 1229 | /* test overlay application in sequence */ |
| 1230 | static void of_selftest_overlay_6(void) |
| 1231 | { |
| 1232 | struct device_node *np; |
| 1233 | int ret, i, ov_id[2]; |
| 1234 | int overlay_nr = 6, selftest_nr = 6; |
| 1235 | int before = 0, after = 1; |
| 1236 | |
| 1237 | /* selftest device must be in before state */ |
| 1238 | for (i = 0; i < 2; i++) { |
| 1239 | if (of_path_platform_device_exists( |
| 1240 | selftest_path(selftest_nr + i)) |
| 1241 | != before) { |
| 1242 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
| 1243 | overlay_path(overlay_nr + i), |
| 1244 | selftest_path(selftest_nr + i), |
| 1245 | !before ? "enabled" : "disabled"); |
| 1246 | return; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | /* apply the overlays */ |
| 1251 | for (i = 0; i < 2; i++) { |
| 1252 | |
| 1253 | np = of_find_node_by_path(overlay_path(overlay_nr + i)); |
| 1254 | if (np == NULL) { |
| 1255 | selftest(0, "could not find overlay node @\"%s\"\n", |
| 1256 | overlay_path(overlay_nr + i)); |
| 1257 | return; |
| 1258 | } |
| 1259 | |
| 1260 | ret = of_overlay_create(np); |
| 1261 | if (ret < 0) { |
| 1262 | selftest(0, "could not create overlay from \"%s\"\n", |
| 1263 | overlay_path(overlay_nr + i)); |
| 1264 | return; |
| 1265 | } |
| 1266 | ov_id[i] = ret; |
| 1267 | } |
| 1268 | |
| 1269 | for (i = 0; i < 2; i++) { |
| 1270 | /* selftest device must be in after state */ |
| 1271 | if (of_path_platform_device_exists( |
| 1272 | selftest_path(selftest_nr + i)) |
| 1273 | != after) { |
| 1274 | selftest(0, "overlay @\"%s\" failed @\"%s\" %s\n", |
| 1275 | overlay_path(overlay_nr + i), |
| 1276 | selftest_path(selftest_nr + i), |
| 1277 | !after ? "enabled" : "disabled"); |
| 1278 | return; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | for (i = 1; i >= 0; i--) { |
| 1283 | ret = of_overlay_destroy(ov_id[i]); |
| 1284 | if (ret != 0) { |
| 1285 | selftest(0, "overlay @\"%s\" failed destroy @\"%s\"\n", |
| 1286 | overlay_path(overlay_nr + i), |
| 1287 | selftest_path(selftest_nr + i)); |
| 1288 | return; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | for (i = 0; i < 2; i++) { |
| 1293 | /* selftest device must be again in before state */ |
| 1294 | if (of_path_platform_device_exists( |
| 1295 | selftest_path(selftest_nr + i)) |
| 1296 | != before) { |
| 1297 | selftest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
| 1298 | overlay_path(overlay_nr + i), |
| 1299 | selftest_path(selftest_nr + i), |
| 1300 | !before ? "enabled" : "disabled"); |
| 1301 | return; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | selftest(1, "overlay test %d passed\n", 6); |
| 1306 | } |
| 1307 | |
| 1308 | /* test overlay application in sequence */ |
| 1309 | static void of_selftest_overlay_8(void) |
| 1310 | { |
| 1311 | struct device_node *np; |
| 1312 | int ret, i, ov_id[2]; |
| 1313 | int overlay_nr = 8, selftest_nr = 8; |
| 1314 | |
| 1315 | /* we don't care about device state in this test */ |
| 1316 | |
| 1317 | /* apply the overlays */ |
| 1318 | for (i = 0; i < 2; i++) { |
| 1319 | |
| 1320 | np = of_find_node_by_path(overlay_path(overlay_nr + i)); |
| 1321 | if (np == NULL) { |
| 1322 | selftest(0, "could not find overlay node @\"%s\"\n", |
| 1323 | overlay_path(overlay_nr + i)); |
| 1324 | return; |
| 1325 | } |
| 1326 | |
| 1327 | ret = of_overlay_create(np); |
| 1328 | if (ret < 0) { |
| 1329 | selftest(0, "could not create overlay from \"%s\"\n", |
| 1330 | overlay_path(overlay_nr + i)); |
| 1331 | return; |
| 1332 | } |
| 1333 | ov_id[i] = ret; |
| 1334 | } |
| 1335 | |
| 1336 | /* now try to remove first overlay (it should fail) */ |
| 1337 | ret = of_overlay_destroy(ov_id[0]); |
| 1338 | if (ret == 0) { |
| 1339 | selftest(0, "overlay @\"%s\" was destroyed @\"%s\"\n", |
| 1340 | overlay_path(overlay_nr + 0), |
| 1341 | selftest_path(selftest_nr)); |
| 1342 | return; |
| 1343 | } |
| 1344 | |
| 1345 | /* removing them in order should work */ |
| 1346 | for (i = 1; i >= 0; i--) { |
| 1347 | ret = of_overlay_destroy(ov_id[i]); |
| 1348 | if (ret != 0) { |
| 1349 | selftest(0, "overlay @\"%s\" not destroyed @\"%s\"\n", |
| 1350 | overlay_path(overlay_nr + i), |
| 1351 | selftest_path(selftest_nr)); |
| 1352 | return; |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | selftest(1, "overlay test %d passed\n", 8); |
| 1357 | } |
| 1358 | |
| 1359 | static void __init of_selftest_overlay(void) |
| 1360 | { |
| 1361 | struct device_node *bus_np = NULL; |
| 1362 | int ret; |
| 1363 | |
| 1364 | ret = platform_driver_register(&selftest_driver); |
| 1365 | if (ret != 0) { |
| 1366 | selftest(0, "could not register selftest driver\n"); |
| 1367 | goto out; |
| 1368 | } |
| 1369 | |
| 1370 | bus_np = of_find_node_by_path(bus_path); |
| 1371 | if (bus_np == NULL) { |
| 1372 | selftest(0, "could not find bus_path \"%s\"\n", bus_path); |
| 1373 | goto out; |
| 1374 | } |
| 1375 | |
| 1376 | ret = of_platform_populate(bus_np, of_default_bus_match_table, |
| 1377 | NULL, NULL); |
| 1378 | if (ret != 0) { |
| 1379 | selftest(0, "could not populate bus @ \"%s\"\n", bus_path); |
| 1380 | goto out; |
| 1381 | } |
| 1382 | |
| 1383 | if (!of_path_platform_device_exists(selftest_path(100))) { |
| 1384 | selftest(0, "could not find selftest0 @ \"%s\"\n", |
| 1385 | selftest_path(100)); |
| 1386 | goto out; |
| 1387 | } |
| 1388 | |
| 1389 | if (of_path_platform_device_exists(selftest_path(101))) { |
| 1390 | selftest(0, "selftest1 @ \"%s\" should not exist\n", |
| 1391 | selftest_path(101)); |
| 1392 | goto out; |
| 1393 | } |
| 1394 | |
| 1395 | selftest(1, "basic infrastructure of overlays passed"); |
| 1396 | |
| 1397 | /* tests in sequence */ |
| 1398 | of_selftest_overlay_0(); |
| 1399 | of_selftest_overlay_1(); |
| 1400 | of_selftest_overlay_2(); |
| 1401 | of_selftest_overlay_3(); |
| 1402 | of_selftest_overlay_4(); |
| 1403 | of_selftest_overlay_5(); |
| 1404 | of_selftest_overlay_6(); |
| 1405 | of_selftest_overlay_8(); |
| 1406 | |
| 1407 | out: |
| 1408 | of_node_put(bus_np); |
| 1409 | } |
| 1410 | |
| 1411 | #else |
| 1412 | static inline void __init of_selftest_overlay(void) { } |
| 1413 | #endif |
| 1414 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1415 | static int __init of_selftest(void) |
| 1416 | { |
| 1417 | struct device_node *np; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 1418 | int res; |
| 1419 | |
| 1420 | /* adding data for selftest */ |
| 1421 | res = selftest_data_add(); |
| 1422 | if (res) |
| 1423 | return res; |
Grant Likely | 788ec2f | 2014-11-19 17:13:44 +0000 | [diff] [blame] | 1424 | if (!of_aliases) |
| 1425 | of_aliases = of_find_node_by_path("/aliases"); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1426 | |
| 1427 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 1428 | if (!np) { |
| 1429 | pr_info("No testcase data in device tree; not running tests\n"); |
| 1430 | return 0; |
| 1431 | } |
| 1432 | of_node_put(np); |
| 1433 | |
| 1434 | pr_info("start of selftest - you will see error messages\n"); |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 1435 | of_selftest_check_tree_linkage(); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 1436 | of_selftest_check_phandles(); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 1437 | of_selftest_find_node_by_name(); |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 1438 | of_selftest_dynamic(); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1439 | of_selftest_parse_phandle_with_args(); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 1440 | of_selftest_property_string(); |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 1441 | of_selftest_property_copy(); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 1442 | of_selftest_changeset(); |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 1443 | of_selftest_parse_interrupts(); |
Grant Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 1444 | of_selftest_parse_interrupts_extended(); |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 1445 | of_selftest_match_node(); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 1446 | of_selftest_platform_populate(); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1447 | of_selftest_overlay(); |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 1448 | |
| 1449 | /* removing selftest data from live tree */ |
| 1450 | selftest_data_remove(); |
| 1451 | |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 1452 | /* Double check linkage after removing testcase data */ |
| 1453 | of_selftest_check_tree_linkage(); |
| 1454 | |
| 1455 | pr_info("end of selftest - %i passed, %i failed\n", |
| 1456 | selftest_results.passed, selftest_results.failed); |
| 1457 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1458 | return 0; |
| 1459 | } |
| 1460 | late_initcall(of_selftest); |