drm_kunit_helpers.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifndef DRM_KUNIT_HELPERS_H_
  3. #define DRM_KUNIT_HELPERS_H_
  4. #include <drm/drm_drv.h>
  5. #include <linux/device.h>
  6. #include <kunit/test.h>
  7. struct drm_connector;
  8. struct drm_crtc_funcs;
  9. struct drm_crtc_helper_funcs;
  10. struct drm_device;
  11. struct drm_plane_funcs;
  12. struct drm_plane_helper_funcs;
  13. struct kunit;
  14. struct device *drm_kunit_helper_alloc_device(struct kunit *test);
  15. void drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
  16. struct drm_device *
  17. __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
  18. struct device *dev,
  19. size_t size, size_t offset,
  20. const struct drm_driver *driver);
  21. /**
  22. * drm_kunit_helper_alloc_drm_device_with_driver - Allocates a mock DRM device for KUnit tests
  23. * @_test: The test context object
  24. * @_dev: The parent device object
  25. * @_type: the type of the struct which contains struct &drm_device
  26. * @_member: the name of the &drm_device within @_type.
  27. * @_drv: Mocked DRM device driver features
  28. *
  29. * This function creates a struct &drm_device from @_dev and @_drv.
  30. *
  31. * @_dev should be allocated using drm_kunit_helper_alloc_device().
  32. *
  33. * The driver is tied to the @_test context and will get cleaned at the
  34. * end of the test. The drm_device is allocated through
  35. * devm_drm_dev_alloc() and will thus be freed through a device-managed
  36. * resource.
  37. *
  38. * Returns:
  39. * A pointer to the new drm_device, or an ERR_PTR() otherwise.
  40. */
  41. #define drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, _type, _member, _drv) \
  42. ((_type *)__drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, \
  43. sizeof(_type), \
  44. offsetof(_type, _member), \
  45. _drv))
  46. static inline struct drm_device *
  47. __drm_kunit_helper_alloc_drm_device(struct kunit *test,
  48. struct device *dev,
  49. size_t size, size_t offset,
  50. u32 features)
  51. {
  52. struct drm_driver *driver;
  53. driver = devm_kzalloc(dev, sizeof(*driver), GFP_KERNEL);
  54. KUNIT_ASSERT_NOT_NULL(test, driver);
  55. driver->driver_features = features;
  56. return __drm_kunit_helper_alloc_drm_device_with_driver(test, dev,
  57. size, offset,
  58. driver);
  59. }
  60. /**
  61. * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
  62. * @_test: The test context object
  63. * @_dev: The parent device object
  64. * @_type: the type of the struct which contains struct &drm_device
  65. * @_member: the name of the &drm_device within @_type.
  66. * @_feat: Mocked DRM device driver features
  67. *
  68. * This function creates a struct &drm_driver and will create a struct
  69. * &drm_device from @_dev and that driver.
  70. *
  71. * @_dev should be allocated using drm_kunit_helper_alloc_device().
  72. *
  73. * The driver is tied to the @_test context and will get cleaned at the
  74. * end of the test. The drm_device is allocated through
  75. * devm_drm_dev_alloc() and will thus be freed through a device-managed
  76. * resource.
  77. *
  78. * Returns:
  79. * A pointer to the new drm_device, or an ERR_PTR() otherwise.
  80. */
  81. #define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat) \
  82. ((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev, \
  83. sizeof(_type), \
  84. offsetof(_type, _member), \
  85. _feat))
  86. struct drm_atomic_state *
  87. drm_kunit_helper_atomic_state_alloc(struct kunit *test,
  88. struct drm_device *drm,
  89. struct drm_modeset_acquire_ctx *ctx);
  90. struct drm_plane *
  91. drm_kunit_helper_create_primary_plane(struct kunit *test,
  92. struct drm_device *drm,
  93. const struct drm_plane_funcs *funcs,
  94. const struct drm_plane_helper_funcs *helper_funcs,
  95. const uint32_t *formats,
  96. unsigned int num_formats,
  97. const uint64_t *modifiers);
  98. struct drm_crtc *
  99. drm_kunit_helper_create_crtc(struct kunit *test,
  100. struct drm_device *drm,
  101. struct drm_plane *primary,
  102. struct drm_plane *cursor,
  103. const struct drm_crtc_funcs *funcs,
  104. const struct drm_crtc_helper_funcs *helper_funcs);
  105. int drm_kunit_helper_enable_crtc_connector(struct kunit *test,
  106. struct drm_device *drm,
  107. struct drm_crtc *crtc,
  108. struct drm_connector *connector,
  109. const struct drm_display_mode *mode,
  110. struct drm_modeset_acquire_ctx *ctx);
  111. int drm_kunit_add_mode_destroy_action(struct kunit *test,
  112. struct drm_display_mode *mode);
  113. struct drm_display_mode *
  114. drm_kunit_display_mode_from_cea_vic(struct kunit *test, struct drm_device *dev,
  115. u8 video_code);
  116. #endif // DRM_KUNIT_HELPERS_H_