of_kunit_helpers.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Test managed DeviceTree APIs
  4. */
  5. #include <linux/of.h>
  6. #include <linux/of_fdt.h>
  7. #include <kunit/of.h>
  8. #include <kunit/test.h>
  9. #include <kunit/resource.h>
  10. #include "of_private.h"
  11. /**
  12. * of_root_kunit_skip() - Skip test if the root node isn't populated
  13. * @test: test to skip if the root node isn't populated
  14. */
  15. void of_root_kunit_skip(struct kunit *test)
  16. {
  17. if ((IS_ENABLED(CONFIG_ARM64) || IS_ENABLED(CONFIG_RISCV)) &&
  18. IS_ENABLED(CONFIG_ACPI) && !of_root)
  19. kunit_skip(test, "arm64/riscv+acpi doesn't populate a root node");
  20. }
  21. EXPORT_SYMBOL_GPL(of_root_kunit_skip);
  22. #if defined(CONFIG_OF_OVERLAY) && defined(CONFIG_OF_EARLY_FLATTREE)
  23. static void of_overlay_fdt_apply_kunit_exit(void *ovcs_id)
  24. {
  25. of_overlay_remove(ovcs_id);
  26. }
  27. /**
  28. * of_overlay_fdt_apply_kunit() - Test managed of_overlay_fdt_apply()
  29. * @test: test context
  30. * @overlay_fdt: device tree overlay to apply
  31. * @overlay_fdt_size: size in bytes of @overlay_fdt
  32. * @ovcs_id: identifier of overlay, used to remove the overlay
  33. *
  34. * Just like of_overlay_fdt_apply(), except the overlay is managed by the test
  35. * case and is automatically removed with of_overlay_remove() after the test
  36. * case concludes.
  37. *
  38. * Return: 0 on success, negative errno on failure
  39. */
  40. int of_overlay_fdt_apply_kunit(struct kunit *test, void *overlay_fdt,
  41. u32 overlay_fdt_size, int *ovcs_id)
  42. {
  43. int ret;
  44. int *copy_id;
  45. of_root_kunit_skip(test);
  46. copy_id = kunit_kmalloc(test, sizeof(*copy_id), GFP_KERNEL);
  47. if (!copy_id)
  48. return -ENOMEM;
  49. ret = of_overlay_fdt_apply(overlay_fdt, overlay_fdt_size,
  50. ovcs_id, NULL);
  51. if (ret)
  52. return ret;
  53. *copy_id = *ovcs_id;
  54. return kunit_add_action_or_reset(test, of_overlay_fdt_apply_kunit_exit,
  55. copy_id);
  56. }
  57. EXPORT_SYMBOL_GPL(of_overlay_fdt_apply_kunit);
  58. #endif
  59. KUNIT_DEFINE_ACTION_WRAPPER(of_node_put_wrapper, of_node_put, struct device_node *);
  60. /**
  61. * of_node_put_kunit() - Test managed of_node_put()
  62. * @test: test context
  63. * @node: node to pass to `of_node_put()`
  64. *
  65. * Just like of_node_put(), except the node is managed by the test case and is
  66. * automatically put with of_node_put() after the test case concludes.
  67. */
  68. void of_node_put_kunit(struct kunit *test, struct device_node *node)
  69. {
  70. if (kunit_add_action(test, of_node_put_wrapper, node)) {
  71. KUNIT_FAIL(test,
  72. "Can't allocate a kunit resource to put of_node\n");
  73. }
  74. }
  75. EXPORT_SYMBOL_GPL(of_node_put_kunit);
  76. MODULE_LICENSE("GPL");
  77. MODULE_DESCRIPTION("Test managed DeviceTree APIs");