/* SPDX-License-Identifier: GPL-2.0 */1#ifndef _KUNIT_OF_H2#define _KUNIT_OF_H34#include <kunit/test.h>56struct device_node;78#ifdef CONFIG_OF910void of_node_put_kunit(struct kunit *test, struct device_node *node);1112#else1314static inline15void of_node_put_kunit(struct kunit *test, struct device_node *node)16{17kunit_skip(test, "requires CONFIG_OF");18}1920#endif /* !CONFIG_OF */2122#if defined(CONFIG_OF) && defined(CONFIG_OF_OVERLAY) && defined(CONFIG_OF_EARLY_FLATTREE)2324int of_overlay_fdt_apply_kunit(struct kunit *test, void *overlay_fdt,25u32 overlay_fdt_size, int *ovcs_id);26#else2728static inline int29of_overlay_fdt_apply_kunit(struct kunit *test, void *overlay_fdt,30u32 overlay_fdt_size, int *ovcs_id)31{32kunit_skip(test, "requires CONFIG_OF and CONFIG_OF_OVERLAY and CONFIG_OF_EARLY_FLATTREE for root node");33return -EINVAL;34}3536#endif3738/**39* __of_overlay_apply_kunit() - Test managed of_overlay_fdt_apply() variant40* @test: test context41* @overlay_begin: start address of overlay to apply42* @overlay_end: end address of overlay to apply43*44* This is mostly internal API. See of_overlay_apply_kunit() for the wrapper45* that makes this easier to use.46*47* Similar to of_overlay_fdt_apply(), except the overlay is managed by the test48* case and is automatically removed with of_overlay_remove() after the test49* case concludes.50*51* Return: 0 on success, negative errno on failure52*/53static inline int __of_overlay_apply_kunit(struct kunit *test,54u8 *overlay_begin,55const u8 *overlay_end)56{57int unused;5859return of_overlay_fdt_apply_kunit(test, overlay_begin,60overlay_end - overlay_begin,61&unused);62}6364#define of_overlay_begin(overlay_name) __dtbo_##overlay_name##_begin65#define of_overlay_end(overlay_name) __dtbo_##overlay_name##_end6667#define OF_OVERLAY_DECLARE(overlay_name) \68extern uint8_t of_overlay_begin(overlay_name)[]; \69extern uint8_t of_overlay_end(overlay_name)[] \7071/**72* of_overlay_apply_kunit() - Test managed of_overlay_fdt_apply() for built-in overlays73* @test: test context74* @overlay_name: name of overlay to apply75*76* This macro is used to apply a device tree overlay built with the77* cmd_dt_S_dtbo rule in scripts/Makefile.lib that has been compiled into the78* kernel image or KUnit test module. The overlay is automatically removed when79* the test is finished.80*81* Unit tests that need device tree nodes should compile an overlay file with82* @overlay_name\.dtbo.o in their Makefile along with their unit test and then83* load the overlay during their test. The @overlay_name matches the filename84* of the overlay without the dtbo filename extension. If CONFIG_OF_OVERLAY is85* not enabled, the @test will be skipped.86*87* In the Makefile88*89* .. code-block:: none90*91* obj-$(CONFIG_OF_OVERLAY_KUNIT_TEST) += overlay_test.o kunit_overlay_test.dtbo.o92*93* In the test94*95* .. code-block:: c96*97* static void of_overlay_kunit_of_overlay_apply(struct kunit *test)98* {99* struct device_node *np;100*101* KUNIT_ASSERT_EQ(test, 0,102* of_overlay_apply_kunit(test, kunit_overlay_test));103*104* np = of_find_node_by_name(NULL, "test-kunit");105* KUNIT_EXPECT_NOT_ERR_OR_NULL(test, np);106* of_node_put(np);107* }108*109* Return: 0 on success, negative errno on failure.110*/111#define of_overlay_apply_kunit(test, overlay_name) \112({ \113OF_OVERLAY_DECLARE(overlay_name); \114\115__of_overlay_apply_kunit((test), \116of_overlay_begin(overlay_name), \117of_overlay_end(overlay_name)); \118})119120#endif121122123