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