xref: /linux/drivers/of/of_kunit_helpers.c (revision 6044a1ee9dca906a807ba786421dc4254191ffd5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Test managed DeviceTree APIs
4  */
5 
6 #include <linux/of.h>
7 #include <linux/of_fdt.h>
8 
9 #include <kunit/of.h>
10 #include <kunit/test.h>
11 #include <kunit/resource.h>
12 
13 #include "of_private.h"
14 
15 /**
16  * of_root_kunit_skip() - Skip test if the root node isn't populated
17  * @test: test to skip if the root node isn't populated
18  */
19 void of_root_kunit_skip(struct kunit *test)
20 {
21 	if ((IS_ENABLED(CONFIG_ARM64) || IS_ENABLED(CONFIG_RISCV)) &&
22 	    IS_ENABLED(CONFIG_ACPI) && !of_root)
23 		kunit_skip(test, "arm64/riscv+acpi doesn't populate a root node");
24 }
25 EXPORT_SYMBOL_GPL(of_root_kunit_skip);
26 
27 #if defined(CONFIG_OF_OVERLAY) && defined(CONFIG_OF_EARLY_FLATTREE)
28 
29 static void of_overlay_fdt_apply_kunit_exit(void *ovcs_id)
30 {
31 	of_overlay_remove(ovcs_id);
32 }
33 
34 /**
35  * of_overlay_fdt_apply_kunit() - Test managed of_overlay_fdt_apply()
36  * @test: test context
37  * @overlay_fdt: device tree overlay to apply
38  * @overlay_fdt_size: size in bytes of @overlay_fdt
39  * @ovcs_id: identifier of overlay, used to remove the overlay
40  *
41  * Just like of_overlay_fdt_apply(), except the overlay is managed by the test
42  * case and is automatically removed with of_overlay_remove() after the test
43  * case concludes.
44  *
45  * Return: 0 on success, negative errno on failure
46  */
47 int of_overlay_fdt_apply_kunit(struct kunit *test, void *overlay_fdt,
48 			       u32 overlay_fdt_size, int *ovcs_id)
49 {
50 	int ret;
51 	int *copy_id;
52 
53 	of_root_kunit_skip(test);
54 
55 	copy_id = kunit_kmalloc(test, sizeof(*copy_id), GFP_KERNEL);
56 	if (!copy_id)
57 		return -ENOMEM;
58 
59 	ret = of_overlay_fdt_apply(overlay_fdt, overlay_fdt_size,
60 				   ovcs_id, NULL);
61 	if (ret)
62 		return ret;
63 
64 	*copy_id = *ovcs_id;
65 
66 	return kunit_add_action_or_reset(test, of_overlay_fdt_apply_kunit_exit,
67 					 copy_id);
68 }
69 EXPORT_SYMBOL_GPL(of_overlay_fdt_apply_kunit);
70 
71 #endif
72 
73 KUNIT_DEFINE_ACTION_WRAPPER(of_node_put_wrapper, of_node_put, struct device_node *);
74 
75 /**
76  * of_node_put_kunit() - Test managed of_node_put()
77  * @test: test context
78  * @node: node to pass to `of_node_put()`
79  *
80  * Just like of_node_put(), except the node is managed by the test case and is
81  * automatically put with of_node_put() after the test case concludes.
82  */
83 void of_node_put_kunit(struct kunit *test, struct device_node *node)
84 {
85 	if (kunit_add_action(test, of_node_put_wrapper, node)) {
86 		KUNIT_FAIL(test,
87 			   "Can't allocate a kunit resource to put of_node\n");
88 	}
89 }
90 EXPORT_SYMBOL_GPL(of_node_put_kunit);
91 
92 MODULE_LICENSE("GPL");
93 MODULE_DESCRIPTION("Test managed DeviceTree APIs");
94