1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit tests for OF APIs
5 #include <linux/ioport.h>
6 #include <linux/module.h>
9 #include <kunit/test.h>
11 #include "of_private.h"
14 * Test that the root node "/" can be found by path.
16 static void of_dtb_root_node_found_by_path(struct kunit *test)
18 struct device_node *np;
20 np = of_find_node_by_path("/");
21 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, np);
26 * Test that the 'of_root' global variable is always populated when DT code is
27 * enabled. Remove this test once of_root is removed from global access.
29 static void of_dtb_root_node_populates_of_root(struct kunit *test)
31 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, of_root);
34 static struct kunit_case of_dtb_test_cases[] = {
35 KUNIT_CASE(of_dtb_root_node_found_by_path),
36 KUNIT_CASE(of_dtb_root_node_populates_of_root),
40 static int of_dtb_test_init(struct kunit *test)
42 of_root_kunit_skip(test);
43 if (!IS_ENABLED(CONFIG_OF_EARLY_FLATTREE))
44 kunit_skip(test, "requires CONFIG_OF_EARLY_FLATTREE");
50 * Test suite to confirm a DTB is loaded.
52 static struct kunit_suite of_dtb_suite = {
54 .test_cases = of_dtb_test_cases,
55 .init = of_dtb_test_init,
58 struct of_address_resource_bounds_case {
67 static void of_address_resource_bounds_case_desc(const struct of_address_resource_bounds_case *p,
70 snprintf(name, KUNIT_PARAM_DESC_SIZE, "start=0x%016llx,size=0x%016llx", p->start, p->size);
73 static const struct of_address_resource_bounds_case of_address_resource_bounds_cases[] = {
104 .size = RESOURCE_SIZE_MAX,
107 .res_end = RESOURCE_SIZE_MAX,
110 .start = RESOURCE_SIZE_MAX,
113 .res_start = RESOURCE_SIZE_MAX,
114 .res_end = RESOURCE_SIZE_MAX,
118 .size = RESOURCE_SIZE_MAX,
122 .start = RESOURCE_SIZE_MAX,
127 .start = ULL(0x100000000),
129 .ret = sizeof(resource_size_t) > sizeof(u32) ? 0 : -EOVERFLOW,
130 .res_start = ULL(0x100000000),
131 .res_end = ULL(0x100000000),
136 .ret = sizeof(resource_size_t) > sizeof(u32) ? 0 : -EOVERFLOW,
138 .res_end = ULL(0x100000ffe),
142 KUNIT_ARRAY_PARAM(of_address_resource_bounds,
143 of_address_resource_bounds_cases, of_address_resource_bounds_case_desc);
145 static void of_address_resource_bounds(struct kunit *test)
147 const struct of_address_resource_bounds_case *param = test->param_value;
148 struct resource r; /* Intentionally uninitialized */
151 if (!IS_ENABLED(CONFIG_OF_ADDRESS))
152 kunit_skip(test, "CONFIG_OF_ADDRESS not enabled\n");
154 ret = __of_address_resource_bounds(&r, param->start, param->size);
155 KUNIT_EXPECT_EQ(test, param->ret, ret);
157 KUNIT_EXPECT_EQ(test, (resource_size_t)param->res_start, r.start);
158 KUNIT_EXPECT_EQ(test, (resource_size_t)param->res_end, r.end);
159 KUNIT_EXPECT_EQ(test, param->size, resource_size(&r));
163 static struct kunit_case of_address_test_cases[] = {
164 KUNIT_CASE_PARAM(of_address_resource_bounds, of_address_resource_bounds_gen_params),
168 static struct kunit_suite of_address_suite = {
169 .name = "of_address",
170 .test_cases = of_address_test_cases,
174 &of_dtb_suite, &of_address_suite,
176 MODULE_DESCRIPTION("KUnit tests for OF APIs");
177 MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
178 MODULE_LICENSE("GPL");