1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _KUNIT_OF_H
3 #define _KUNIT_OF_H
4
5 #include <kunit/test.h>
6
7 struct device_node;
8
9 #ifdef CONFIG_OF
10
11 void of_node_put_kunit(struct kunit *test, struct device_node *node);
12
13 #else
14
15 static inline
of_node_put_kunit(struct kunit * test,struct device_node * node)16 void of_node_put_kunit(struct kunit *test, struct device_node *node)
17 {
18 kunit_skip(test, "requires CONFIG_OF");
19 }
20
21 #endif /* !CONFIG_OF */
22
23 #if defined(CONFIG_OF) && defined(CONFIG_OF_OVERLAY) && defined(CONFIG_OF_EARLY_FLATTREE)
24
25 int of_overlay_fdt_apply_kunit(struct kunit *test, void *overlay_fdt,
26 u32 overlay_fdt_size, int *ovcs_id);
27 #else
28
29 static inline int
of_overlay_fdt_apply_kunit(struct kunit * test,void * overlay_fdt,u32 overlay_fdt_size,int * ovcs_id)30 of_overlay_fdt_apply_kunit(struct kunit *test, void *overlay_fdt,
31 u32 overlay_fdt_size, int *ovcs_id)
32 {
33 kunit_skip(test, "requires CONFIG_OF and CONFIG_OF_OVERLAY and CONFIG_OF_EARLY_FLATTREE for root node");
34 return -EINVAL;
35 }
36
37 #endif
38
39 /**
40 * __of_overlay_apply_kunit() - Test managed of_overlay_fdt_apply() variant
41 * @test: test context
42 * @overlay_begin: start address of overlay to apply
43 * @overlay_end: end address of overlay to apply
44 *
45 * This is mostly internal API. See of_overlay_apply_kunit() for the wrapper
46 * that makes this easier to use.
47 *
48 * Similar to of_overlay_fdt_apply(), except the overlay is managed by the test
49 * case and is automatically removed with of_overlay_remove() after the test
50 * case concludes.
51 *
52 * Return: 0 on success, negative errno on failure
53 */
__of_overlay_apply_kunit(struct kunit * test,u8 * overlay_begin,const u8 * overlay_end)54 static inline int __of_overlay_apply_kunit(struct kunit *test,
55 u8 *overlay_begin,
56 const u8 *overlay_end)
57 {
58 int unused;
59
60 return of_overlay_fdt_apply_kunit(test, overlay_begin,
61 overlay_end - overlay_begin,
62 &unused);
63 }
64
65 #define of_overlay_begin(overlay_name) __dtbo_##overlay_name##_begin
66 #define of_overlay_end(overlay_name) __dtbo_##overlay_name##_end
67
68 #define OF_OVERLAY_DECLARE(overlay_name) \
69 extern uint8_t of_overlay_begin(overlay_name)[]; \
70 extern uint8_t of_overlay_end(overlay_name)[] \
71
72 /**
73 * of_overlay_apply_kunit() - Test managed of_overlay_fdt_apply() for built-in overlays
74 * @test: test context
75 * @overlay_name: name of overlay to apply
76 *
77 * This macro is used to apply a device tree overlay built with the
78 * cmd_dt_S_dtbo rule in scripts/Makefile.lib that has been compiled into the
79 * kernel image or KUnit test module. The overlay is automatically removed when
80 * the test is finished.
81 *
82 * Unit tests that need device tree nodes should compile an overlay file with
83 * @overlay_name\.dtbo.o in their Makefile along with their unit test and then
84 * load the overlay during their test. The @overlay_name matches the filename
85 * of the overlay without the dtbo filename extension. If CONFIG_OF_OVERLAY is
86 * not enabled, the @test will be skipped.
87 *
88 * In the Makefile
89 *
90 * .. code-block:: none
91 *
92 * obj-$(CONFIG_OF_OVERLAY_KUNIT_TEST) += overlay_test.o kunit_overlay_test.dtbo.o
93 *
94 * In the test
95 *
96 * .. code-block:: c
97 *
98 * static void of_overlay_kunit_of_overlay_apply(struct kunit *test)
99 * {
100 * struct device_node *np;
101 *
102 * KUNIT_ASSERT_EQ(test, 0,
103 * of_overlay_apply_kunit(test, kunit_overlay_test));
104 *
105 * np = of_find_node_by_name(NULL, "test-kunit");
106 * KUNIT_EXPECT_NOT_ERR_OR_NULL(test, np);
107 * of_node_put(np);
108 * }
109 *
110 * Return: 0 on success, negative errno on failure.
111 */
112 #define of_overlay_apply_kunit(test, overlay_name) \
113 ({ \
114 OF_OVERLAY_DECLARE(overlay_name); \
115 \
116 __of_overlay_apply_kunit((test), \
117 of_overlay_begin(overlay_name), \
118 of_overlay_end(overlay_name)); \
119 })
120
121 #endif
122