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 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 188 | static int __init of_selftest(void) |
| 189 | { |
| 190 | struct device_node *np; |
| 191 | |
| 192 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 193 | if (!np) { |
| 194 | pr_info("No testcase data in device tree; not running tests\n"); |
| 195 | return 0; |
| 196 | } |
| 197 | of_node_put(np); |
| 198 | |
| 199 | pr_info("start of selftest - you will see error messages\n"); |
| 200 | of_selftest_parse_phandle_with_args(); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 201 | of_selftest_property_match_string(); |
Pantelis Antoniou | f713f5e | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 202 | of_selftest_property_copy(); |
| 203 | pr_info("end of selftest - %i passed, %i failed\n", |
| 204 | selftest_results.passed, selftest_results.failed); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | late_initcall(of_selftest); |