93 lines · c
1// SPDX-License-Identifier: GPL-2.02/*3 * Test managed DeviceTree APIs4 */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 populated17 * @test: test to skip if the root node isn't populated18 */19void 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}24EXPORT_SYMBOL_GPL(of_root_kunit_skip);25 26#if defined(CONFIG_OF_OVERLAY) && defined(CONFIG_OF_EARLY_FLATTREE)27 28static 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 context36 * @overlay_fdt: device tree overlay to apply37 * @overlay_fdt_size: size in bytes of @overlay_fdt38 * @ovcs_id: identifier of overlay, used to remove the overlay39 *40 * Just like of_overlay_fdt_apply(), except the overlay is managed by the test41 * case and is automatically removed with of_overlay_remove() after the test42 * case concludes.43 *44 * Return: 0 on success, negative errno on failure45 */46int 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}68EXPORT_SYMBOL_GPL(of_overlay_fdt_apply_kunit);69 70#endif71 72KUNIT_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 context77 * @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 is80 * automatically put with of_node_put() after the test case concludes.81 */82void 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}89EXPORT_SYMBOL_GPL(of_node_put_kunit);90 91MODULE_LICENSE("GPL");92MODULE_DESCRIPTION("Test managed DeviceTree APIs");93