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> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/device.h> |
| 16 | |
Pantelis Antoniou | f713f5e | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 17 | #include "of_private.h" |
| 18 | |
| 19 | static struct selftest_results { |
| 20 | int passed; |
| 21 | int failed; |
| 22 | } selftest_results; |
| 23 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 24 | #define selftest(result, fmt, ...) { \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 25 | if (!(result)) { \ |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 26 | pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \ |
Pantelis Antoniou | f713f5e | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 27 | selftest_results.passed = false; \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 28 | } else { \ |
| 29 | pr_info("pass %s:%i\n", __FILE__, __LINE__); \ |
| 30 | } \ |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static void __init of_selftest_parse_phandle_with_args(void) |
| 34 | { |
| 35 | struct device_node *np; |
| 36 | struct of_phandle_args args; |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 37 | int i, rc; |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 38 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 39 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 40 | if (!np) { |
| 41 | pr_err("missing testcase data\n"); |
| 42 | return; |
| 43 | } |
| 44 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 45 | rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells"); |
| 46 | selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc); |
| 47 | |
Grant Likely | f7f951c | 2013-02-12 17:41:22 +0000 | [diff] [blame] | 48 | for (i = 0; i < 8; i++) { |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 49 | bool passed = true; |
| 50 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 51 | "#phandle-cells", i, &args); |
| 52 | |
| 53 | /* Test the values from tests-phandle.dtsi */ |
| 54 | switch (i) { |
| 55 | case 0: |
| 56 | passed &= !rc; |
| 57 | passed &= (args.args_count == 1); |
| 58 | passed &= (args.args[0] == (i + 1)); |
| 59 | break; |
| 60 | case 1: |
| 61 | passed &= !rc; |
| 62 | passed &= (args.args_count == 2); |
| 63 | passed &= (args.args[0] == (i + 1)); |
| 64 | passed &= (args.args[1] == 0); |
| 65 | break; |
| 66 | case 2: |
| 67 | passed &= (rc == -ENOENT); |
| 68 | break; |
| 69 | case 3: |
| 70 | passed &= !rc; |
| 71 | passed &= (args.args_count == 3); |
| 72 | passed &= (args.args[0] == (i + 1)); |
| 73 | passed &= (args.args[1] == 4); |
| 74 | passed &= (args.args[2] == 3); |
| 75 | break; |
| 76 | case 4: |
| 77 | passed &= !rc; |
| 78 | passed &= (args.args_count == 2); |
| 79 | passed &= (args.args[0] == (i + 1)); |
| 80 | passed &= (args.args[1] == 100); |
| 81 | break; |
| 82 | case 5: |
| 83 | passed &= !rc; |
| 84 | passed &= (args.args_count == 0); |
| 85 | break; |
| 86 | case 6: |
| 87 | passed &= !rc; |
| 88 | passed &= (args.args_count == 1); |
| 89 | passed &= (args.args[0] == (i + 1)); |
| 90 | break; |
| 91 | case 7: |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 92 | passed &= (rc == -ENOENT); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 93 | break; |
| 94 | default: |
| 95 | passed = false; |
| 96 | } |
| 97 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 98 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 99 | i, args.np->full_name, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* Check for missing list property */ |
| 103 | rc = of_parse_phandle_with_args(np, "phandle-list-missing", |
| 104 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 105 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 106 | rc = of_count_phandle_with_args(np, "phandle-list-missing", |
| 107 | "#phandle-cells"); |
| 108 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 109 | |
| 110 | /* Check for missing cells property */ |
| 111 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 112 | "#phandle-cells-missing", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 113 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 114 | rc = of_count_phandle_with_args(np, "phandle-list", |
| 115 | "#phandle-cells-missing"); |
| 116 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 117 | |
| 118 | /* Check for bad phandle in list */ |
| 119 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle", |
| 120 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 121 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 122 | rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle", |
| 123 | "#phandle-cells"); |
| 124 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 125 | |
| 126 | /* Check for incorrectly formed argument list */ |
| 127 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-args", |
| 128 | "#phandle-cells", 1, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 129 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 130 | rc = of_count_phandle_with_args(np, "phandle-list-bad-args", |
| 131 | "#phandle-cells"); |
| 132 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 135 | static void __init of_selftest_property_match_string(void) |
| 136 | { |
| 137 | struct device_node *np; |
| 138 | int rc; |
| 139 | |
| 140 | pr_info("start\n"); |
| 141 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 142 | if (!np) { |
| 143 | pr_err("No testcase data in device tree\n"); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | rc = of_property_match_string(np, "phandle-list-names", "first"); |
| 148 | selftest(rc == 0, "first expected:0 got:%i\n", rc); |
| 149 | rc = of_property_match_string(np, "phandle-list-names", "second"); |
| 150 | selftest(rc == 1, "second expected:0 got:%i\n", rc); |
| 151 | rc = of_property_match_string(np, "phandle-list-names", "third"); |
| 152 | selftest(rc == 2, "third expected:0 got:%i\n", rc); |
| 153 | rc = of_property_match_string(np, "phandle-list-names", "fourth"); |
| 154 | selftest(rc == -ENODATA, "unmatched string; rc=%i", rc); |
| 155 | rc = of_property_match_string(np, "missing-property", "blah"); |
| 156 | selftest(rc == -EINVAL, "missing property; rc=%i", rc); |
| 157 | rc = of_property_match_string(np, "empty-property", "blah"); |
| 158 | selftest(rc == -ENODATA, "empty property; rc=%i", rc); |
| 159 | rc = of_property_match_string(np, "unterminated-string", "blah"); |
| 160 | selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc); |
| 161 | } |
| 162 | |
Pantelis Antoniou | f713f5e | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 163 | #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \ |
| 164 | (p1)->value && (p2)->value && \ |
| 165 | !memcmp((p1)->value, (p2)->value, (p1)->length) && \ |
| 166 | !strcmp((p1)->name, (p2)->name)) |
| 167 | static void __init of_selftest_property_copy(void) |
| 168 | { |
| 169 | #ifdef CONFIG_OF_DYNAMIC |
| 170 | struct property p1 = { .name = "p1", .length = 0, .value = "" }; |
| 171 | struct property p2 = { .name = "p2", .length = 5, .value = "abcd" }; |
| 172 | struct property *new; |
| 173 | |
| 174 | new = __of_prop_dup(&p1, GFP_KERNEL); |
| 175 | selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n"); |
| 176 | kfree(new->value); |
| 177 | kfree(new->name); |
| 178 | kfree(new); |
| 179 | |
| 180 | new = __of_prop_dup(&p2, GFP_KERNEL); |
| 181 | selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n"); |
| 182 | kfree(new->value); |
| 183 | kfree(new->name); |
| 184 | kfree(new); |
| 185 | #endif |
| 186 | } |
| 187 | |
Pantelis Antoniou | 67cf773 | 2014-07-04 19:58:49 +0300 | [diff] [blame^] | 188 | static void __init of_selftest_changeset(void) |
| 189 | { |
| 190 | #ifdef CONFIG_OF_DYNAMIC |
| 191 | struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" }; |
| 192 | struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" }; |
| 193 | struct property *ppremove; |
| 194 | struct device_node *n1, *n2, *n21, *nremove, *parent; |
| 195 | struct of_changeset chgset; |
| 196 | |
| 197 | of_changeset_init(&chgset); |
| 198 | n1 = __of_node_alloc("/testcase-data/changeset/n1", GFP_KERNEL); |
| 199 | selftest(n1, "testcase setup failure\n"); |
| 200 | n2 = __of_node_alloc("/testcase-data/changeset/n2", GFP_KERNEL); |
| 201 | selftest(n2, "testcase setup failure\n"); |
| 202 | n21 = __of_node_alloc("/testcase-data/changeset/n2/n21", GFP_KERNEL); |
| 203 | selftest(n21, "testcase setup failure %p\n", n21); |
| 204 | nremove = of_find_node_by_path("/testcase-data/changeset/node-remove"); |
| 205 | selftest(nremove, "testcase setup failure\n"); |
| 206 | ppadd = __of_prop_dup(&padd, GFP_KERNEL); |
| 207 | selftest(ppadd, "testcase setup failure\n"); |
| 208 | ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL); |
| 209 | selftest(ppupdate, "testcase setup failure\n"); |
| 210 | parent = nremove->parent; |
| 211 | n1->parent = parent; |
| 212 | n2->parent = parent; |
| 213 | n21->parent = n2; |
| 214 | n2->child = n21; |
| 215 | ppremove = of_find_property(parent, "prop-remove", NULL); |
| 216 | selftest(ppremove, "failed to find removal prop"); |
| 217 | |
| 218 | of_changeset_init(&chgset); |
| 219 | selftest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n"); |
| 220 | selftest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n"); |
| 221 | selftest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n"); |
| 222 | selftest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n"); |
| 223 | selftest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n"); |
| 224 | selftest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n"); |
| 225 | selftest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n"); |
| 226 | mutex_lock(&of_mutex); |
| 227 | selftest(!of_changeset_apply(&chgset), "apply failed\n"); |
| 228 | mutex_unlock(&of_mutex); |
| 229 | |
| 230 | mutex_lock(&of_mutex); |
| 231 | selftest(!of_changeset_revert(&chgset), "revert failed\n"); |
| 232 | mutex_unlock(&of_mutex); |
| 233 | |
| 234 | of_changeset_destroy(&chgset); |
| 235 | #endif |
| 236 | } |
| 237 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 238 | static int __init of_selftest(void) |
| 239 | { |
| 240 | struct device_node *np; |
| 241 | |
| 242 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 243 | if (!np) { |
| 244 | pr_info("No testcase data in device tree; not running tests\n"); |
| 245 | return 0; |
| 246 | } |
| 247 | of_node_put(np); |
| 248 | |
| 249 | pr_info("start of selftest - you will see error messages\n"); |
| 250 | of_selftest_parse_phandle_with_args(); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 251 | of_selftest_property_match_string(); |
Pantelis Antoniou | f713f5e | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 252 | of_selftest_property_copy(); |
Pantelis Antoniou | 67cf773 | 2014-07-04 19:58:49 +0300 | [diff] [blame^] | 253 | of_selftest_changeset(); |
Pantelis Antoniou | f713f5e | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 254 | pr_info("end of selftest - %i passed, %i failed\n", |
| 255 | selftest_results.passed, selftest_results.failed); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | late_initcall(of_selftest); |