162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#ifndef DRM_KUNIT_HELPERS_H_
462306a36Sopenharmony_ci#define DRM_KUNIT_HELPERS_H_
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <drm/drm_drv.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/device.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <kunit/test.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_cistruct drm_device;
1362306a36Sopenharmony_cistruct kunit;
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cistruct device *drm_kunit_helper_alloc_device(struct kunit *test);
1662306a36Sopenharmony_civoid drm_kunit_helper_free_device(struct kunit *test, struct device *dev);
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistruct drm_device *
1962306a36Sopenharmony_ci__drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
2062306a36Sopenharmony_ci						struct device *dev,
2162306a36Sopenharmony_ci						size_t size, size_t offset,
2262306a36Sopenharmony_ci						const struct drm_driver *driver);
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/**
2562306a36Sopenharmony_ci * drm_kunit_helper_alloc_drm_device_with_driver - Allocates a mock DRM device for KUnit tests
2662306a36Sopenharmony_ci * @_test: The test context object
2762306a36Sopenharmony_ci * @_dev: The parent device object
2862306a36Sopenharmony_ci * @_type: the type of the struct which contains struct &drm_device
2962306a36Sopenharmony_ci * @_member: the name of the &drm_device within @_type.
3062306a36Sopenharmony_ci * @_drv: Mocked DRM device driver features
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * This function creates a struct &drm_device from @_dev and @_drv.
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * @_dev should be allocated using drm_kunit_helper_alloc_device().
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci * The driver is tied to the @_test context and will get cleaned at the
3762306a36Sopenharmony_ci * end of the test. The drm_device is allocated through
3862306a36Sopenharmony_ci * devm_drm_dev_alloc() and will thus be freed through a device-managed
3962306a36Sopenharmony_ci * resource.
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci * Returns:
4262306a36Sopenharmony_ci * A pointer to the new drm_device, or an ERR_PTR() otherwise.
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_ci#define drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev, _type, _member, _drv)	\
4562306a36Sopenharmony_ci	((_type *)__drm_kunit_helper_alloc_drm_device_with_driver(_test, _dev,			\
4662306a36Sopenharmony_ci						       sizeof(_type),				\
4762306a36Sopenharmony_ci						       offsetof(_type, _member),		\
4862306a36Sopenharmony_ci						       _drv))
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic inline struct drm_device *
5162306a36Sopenharmony_ci__drm_kunit_helper_alloc_drm_device(struct kunit *test,
5262306a36Sopenharmony_ci				    struct device *dev,
5362306a36Sopenharmony_ci				    size_t size, size_t offset,
5462306a36Sopenharmony_ci				    u32 features)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	struct drm_driver *driver;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	driver = devm_kzalloc(dev, sizeof(*driver), GFP_KERNEL);
5962306a36Sopenharmony_ci	KUNIT_ASSERT_NOT_NULL(test, driver);
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	driver->driver_features = features;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	return __drm_kunit_helper_alloc_drm_device_with_driver(test, dev,
6462306a36Sopenharmony_ci							       size, offset,
6562306a36Sopenharmony_ci							       driver);
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci/**
6962306a36Sopenharmony_ci * drm_kunit_helper_alloc_drm_device - Allocates a mock DRM device for KUnit tests
7062306a36Sopenharmony_ci * @_test: The test context object
7162306a36Sopenharmony_ci * @_dev: The parent device object
7262306a36Sopenharmony_ci * @_type: the type of the struct which contains struct &drm_device
7362306a36Sopenharmony_ci * @_member: the name of the &drm_device within @_type.
7462306a36Sopenharmony_ci * @_features: Mocked DRM device driver features
7562306a36Sopenharmony_ci *
7662306a36Sopenharmony_ci * This function creates a struct &drm_driver and will create a struct
7762306a36Sopenharmony_ci * &drm_device from @_dev and that driver.
7862306a36Sopenharmony_ci *
7962306a36Sopenharmony_ci * @_dev should be allocated using drm_kunit_helper_alloc_device().
8062306a36Sopenharmony_ci *
8162306a36Sopenharmony_ci * The driver is tied to the @_test context and will get cleaned at the
8262306a36Sopenharmony_ci * end of the test. The drm_device is allocated through
8362306a36Sopenharmony_ci * devm_drm_dev_alloc() and will thus be freed through a device-managed
8462306a36Sopenharmony_ci * resource.
8562306a36Sopenharmony_ci *
8662306a36Sopenharmony_ci * Returns:
8762306a36Sopenharmony_ci * A pointer to the new drm_device, or an ERR_PTR() otherwise.
8862306a36Sopenharmony_ci */
8962306a36Sopenharmony_ci#define drm_kunit_helper_alloc_drm_device(_test, _dev, _type, _member, _feat)	\
9062306a36Sopenharmony_ci	((_type *)__drm_kunit_helper_alloc_drm_device(_test, _dev,		\
9162306a36Sopenharmony_ci						      sizeof(_type),		\
9262306a36Sopenharmony_ci						      offsetof(_type, _member),	\
9362306a36Sopenharmony_ci						      _feat))
9462306a36Sopenharmony_cistruct drm_modeset_acquire_ctx *
9562306a36Sopenharmony_cidrm_kunit_helper_acquire_ctx_alloc(struct kunit *test);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistruct drm_atomic_state *
9862306a36Sopenharmony_cidrm_kunit_helper_atomic_state_alloc(struct kunit *test,
9962306a36Sopenharmony_ci				    struct drm_device *drm,
10062306a36Sopenharmony_ci				    struct drm_modeset_acquire_ctx *ctx);
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci#endif // DRM_KUNIT_HELPERS_H_
103