of.h 3.4 KB

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