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