dma-fence: Add some more fence-merge-unwrap tests
[drm/drm-misc.git] / drivers / of / of_kunit_helpers.c
blob7b3ed5a382aaa562a07a13c30111fbda602f46a9
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Test managed DeviceTree APIs
4 */
6 #include <linux/of.h>
7 #include <linux/of_fdt.h>
9 #include <kunit/of.h>
10 #include <kunit/test.h>
11 #include <kunit/resource.h>
13 #include "of_private.h"
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
19 void of_root_kunit_skip(struct kunit *test)
21 if (IS_ENABLED(CONFIG_ARM64) && IS_ENABLED(CONFIG_ACPI) && !of_root)
22 kunit_skip(test, "arm64+acpi doesn't populate a root node");
24 EXPORT_SYMBOL_GPL(of_root_kunit_skip);
26 #if defined(CONFIG_OF_OVERLAY) && defined(CONFIG_OF_EARLY_FLATTREE)
28 static void of_overlay_fdt_apply_kunit_exit(void *ovcs_id)
30 of_overlay_remove(ovcs_id);
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
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.
44 * Return: 0 on success, negative errno on failure
46 int of_overlay_fdt_apply_kunit(struct kunit *test, void *overlay_fdt,
47 u32 overlay_fdt_size, int *ovcs_id)
49 int ret;
50 int *copy_id;
52 of_root_kunit_skip(test);
54 copy_id = kunit_kmalloc(test, sizeof(*copy_id), GFP_KERNEL);
55 if (!copy_id)
56 return -ENOMEM;
58 ret = of_overlay_fdt_apply(overlay_fdt, overlay_fdt_size,
59 ovcs_id, NULL);
60 if (ret)
61 return ret;
63 *copy_id = *ovcs_id;
65 return kunit_add_action_or_reset(test, of_overlay_fdt_apply_kunit_exit,
66 copy_id);
68 EXPORT_SYMBOL_GPL(of_overlay_fdt_apply_kunit);
70 #endif
72 KUNIT_DEFINE_ACTION_WRAPPER(of_node_put_wrapper, of_node_put, struct device_node *);
74 /**
75 * of_node_put_kunit() - Test managed of_node_put()
76 * @test: test context
77 * @node: node to pass to `of_node_put()`
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.
82 void of_node_put_kunit(struct kunit *test, struct device_node *node)
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");
89 EXPORT_SYMBOL_GPL(of_node_put_kunit);
91 MODULE_LICENSE("GPL");
92 MODULE_DESCRIPTION("Test managed DeviceTree APIs");