of_kunit_helpers.c 2.4 KB

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