drm_kunit_helpers.h 4.2 KB

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