2
0
mirror of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git synced 2025-09-04 20:19:47 +08:00

drm/vkms: Allow to configure multiple CRTCs

Add a list of CRTCs to vkms_config and helper functions to add and
remove as many CRTCs as wanted.

For backwards compatibility, add one CRTC to the default configuration.

A future patch will allow to attach planes and CRTCs, but for the
moment there are no changes in the way the output is configured.

Reviewed-by: Louis Chauvet <louis.chauvet@bootlin.com>
Co-developed-by: Louis Chauvet <louis.chauvet@bootlin.com>
Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
Signed-off-by: José Expósito <jose.exposito89@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20250218101214.5790-10-jose.exposito89@gmail.com
Signed-off-by: Maxime Ripard <mripard@kernel.org>
This commit is contained in:
José Expósito 2025-02-18 11:12:09 +01:00 committed by Maxime Ripard
parent bc5b0d5dcc
commit 600df32dac
No known key found for this signature in database
GPG Key ID: E3EF0D6F671851C5
4 changed files with 222 additions and 5 deletions

View File

@ -690,6 +690,7 @@ ForEachMacros:
- 'v4l2_m2m_for_each_src_buf'
- 'v4l2_m2m_for_each_src_buf_safe'
- 'virtio_device_for_each_vq'
- 'vkms_config_for_each_crtc'
- 'vkms_config_for_each_plane'
- 'while_for_each_ftrace_op'
- 'xa_for_each'

View File

@ -27,6 +27,16 @@ static struct vkms_config_plane *get_first_plane(struct vkms_config *config)
return NULL;
}
static struct vkms_config_crtc *get_first_crtc(struct vkms_config *config)
{
struct vkms_config_crtc *crtc_cfg;
vkms_config_for_each_crtc(config, crtc_cfg)
return crtc_cfg;
return NULL;
}
struct default_config_case {
bool enable_cursor;
bool enable_writeback;
@ -46,6 +56,7 @@ static void vkms_config_test_empty_config(struct kunit *test)
KUNIT_EXPECT_STREQ(test, vkms_config_get_device_name(config), "test");
KUNIT_EXPECT_EQ(test, vkms_config_get_num_planes(config), 0);
KUNIT_EXPECT_EQ(test, vkms_config_get_num_crtcs(config), 0);
KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));
@ -70,6 +81,7 @@ static void vkms_config_test_default_config(struct kunit *test)
const struct default_config_case *params = test->param_value;
struct vkms_config *config;
struct vkms_config_plane *plane_cfg;
struct vkms_config_crtc *crtc_cfg;
int n_primaries = 0;
int n_cursors = 0;
int n_overlays = 0;
@ -79,8 +91,6 @@ static void vkms_config_test_default_config(struct kunit *test)
params->enable_overlay);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, config);
KUNIT_EXPECT_EQ(test, config->writeback, params->enable_writeback);
/* Planes */
vkms_config_for_each_plane(config, plane_cfg) {
switch (vkms_config_plane_get_type(plane_cfg)) {
@ -101,6 +111,13 @@ static void vkms_config_test_default_config(struct kunit *test)
KUNIT_EXPECT_EQ(test, n_cursors, params->enable_cursor ? 1 : 0);
KUNIT_EXPECT_EQ(test, n_overlays, params->enable_overlay ? 8 : 0);
/* CRTCs */
KUNIT_EXPECT_EQ(test, vkms_config_get_num_crtcs(config), 1);
crtc_cfg = get_first_crtc(config);
KUNIT_EXPECT_EQ(test, vkms_config_crtc_get_writeback(crtc_cfg),
params->enable_writeback);
KUNIT_EXPECT_TRUE(test, vkms_config_is_valid(config));
vkms_config_destroy(config);
@ -149,6 +166,43 @@ static void vkms_config_test_get_planes(struct kunit *test)
vkms_config_destroy(config);
}
static void vkms_config_test_get_crtcs(struct kunit *test)
{
struct vkms_config *config;
struct vkms_config_crtc *crtc_cfg;
struct vkms_config_crtc *crtc_cfg1, *crtc_cfg2;
config = vkms_config_create("test");
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, config);
KUNIT_ASSERT_EQ(test, vkms_config_get_num_crtcs(config), 0);
vkms_config_for_each_crtc(config, crtc_cfg)
KUNIT_FAIL(test, "Unexpected CRTC");
crtc_cfg1 = vkms_config_create_crtc(config);
KUNIT_ASSERT_EQ(test, vkms_config_get_num_crtcs(config), 1);
vkms_config_for_each_crtc(config, crtc_cfg) {
if (crtc_cfg != crtc_cfg1)
KUNIT_FAIL(test, "Unexpected CRTC");
}
crtc_cfg2 = vkms_config_create_crtc(config);
KUNIT_ASSERT_EQ(test, vkms_config_get_num_crtcs(config), 2);
vkms_config_for_each_crtc(config, crtc_cfg) {
if (crtc_cfg != crtc_cfg1 && crtc_cfg != crtc_cfg2)
KUNIT_FAIL(test, "Unexpected CRTC");
}
vkms_config_destroy_crtc(config, crtc_cfg2);
KUNIT_ASSERT_EQ(test, vkms_config_get_num_crtcs(config), 1);
vkms_config_for_each_crtc(config, crtc_cfg) {
if (crtc_cfg != crtc_cfg1)
KUNIT_FAIL(test, "Unexpected CRTC");
}
vkms_config_destroy(config);
}
static void vkms_config_test_invalid_plane_number(struct kunit *test)
{
struct vkms_config *config;
@ -213,13 +267,38 @@ static void vkms_config_test_valid_plane_type(struct kunit *test)
vkms_config_destroy(config);
}
static void vkms_config_test_invalid_crtc_number(struct kunit *test)
{
struct vkms_config *config;
struct vkms_config_crtc *crtc_cfg;
int n;
config = vkms_config_default_create(false, false, false);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, config);
/* Invalid: No CRTCs */
crtc_cfg = get_first_crtc(config);
vkms_config_destroy_crtc(config, crtc_cfg);
KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));
/* Invalid: Too many CRTCs */
for (n = 0; n <= 32; n++)
vkms_config_create_crtc(config);
KUNIT_EXPECT_FALSE(test, vkms_config_is_valid(config));
vkms_config_destroy(config);
}
static struct kunit_case vkms_config_test_cases[] = {
KUNIT_CASE(vkms_config_test_empty_config),
KUNIT_CASE_PARAM(vkms_config_test_default_config,
default_config_gen_params),
KUNIT_CASE(vkms_config_test_get_planes),
KUNIT_CASE(vkms_config_test_get_crtcs),
KUNIT_CASE(vkms_config_test_invalid_plane_number),
KUNIT_CASE(vkms_config_test_valid_plane_type),
KUNIT_CASE(vkms_config_test_invalid_crtc_number),
{}
};

View File

@ -23,6 +23,7 @@ struct vkms_config *vkms_config_create(const char *dev_name)
}
INIT_LIST_HEAD(&config->planes);
INIT_LIST_HEAD(&config->crtcs);
return config;
}
@ -34,19 +35,23 @@ struct vkms_config *vkms_config_default_create(bool enable_cursor,
{
struct vkms_config *config;
struct vkms_config_plane *plane_cfg;
struct vkms_config_crtc *crtc_cfg;
int n;
config = vkms_config_create(DEFAULT_DEVICE_NAME);
if (IS_ERR(config))
return config;
config->writeback = enable_writeback;
plane_cfg = vkms_config_create_plane(config);
if (IS_ERR(plane_cfg))
goto err_alloc;
vkms_config_plane_set_type(plane_cfg, DRM_PLANE_TYPE_PRIMARY);
crtc_cfg = vkms_config_create_crtc(config);
if (IS_ERR(crtc_cfg))
goto err_alloc;
vkms_config_crtc_set_writeback(crtc_cfg, enable_writeback);
if (enable_overlay) {
for (n = 0; n < NUM_OVERLAY_PLANES; n++) {
plane_cfg = vkms_config_create_plane(config);
@ -75,10 +80,14 @@ EXPORT_SYMBOL_IF_KUNIT(vkms_config_default_create);
void vkms_config_destroy(struct vkms_config *config)
{
struct vkms_config_plane *plane_cfg, *plane_tmp;
struct vkms_config_crtc *crtc_cfg, *crtc_tmp;
list_for_each_entry_safe(plane_cfg, plane_tmp, &config->planes, link)
vkms_config_destroy_plane(plane_cfg);
list_for_each_entry_safe(crtc_cfg, crtc_tmp, &config->crtcs, link)
vkms_config_destroy_crtc(config, crtc_cfg);
kfree_const(config->dev_name);
kfree(config);
}
@ -135,11 +144,28 @@ static bool valid_plane_type(const struct vkms_config *config)
return true;
}
static bool valid_crtc_number(const struct vkms_config *config)
{
struct drm_device *dev = config->dev ? &config->dev->drm : NULL;
size_t n_crtcs;
n_crtcs = list_count_nodes((struct list_head *)&config->crtcs);
if (n_crtcs <= 0 || n_crtcs >= 32) {
drm_info(dev, "The number of CRTCs must be between 1 and 31\n");
return false;
}
return true;
}
bool vkms_config_is_valid(const struct vkms_config *config)
{
if (!valid_plane_number(config))
return false;
if (!valid_crtc_number(config))
return false;
if (!valid_plane_type(config))
return false;
@ -154,10 +180,10 @@ static int vkms_config_show(struct seq_file *m, void *data)
struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
const char *dev_name;
struct vkms_config_plane *plane_cfg;
struct vkms_config_crtc *crtc_cfg;
dev_name = vkms_config_get_device_name((struct vkms_config *)vkmsdev->config);
seq_printf(m, "dev_name=%s\n", dev_name);
seq_printf(m, "writeback=%d\n", vkmsdev->config->writeback);
vkms_config_for_each_plane(vkmsdev->config, plane_cfg) {
seq_puts(m, "plane:\n");
@ -165,6 +191,12 @@ static int vkms_config_show(struct seq_file *m, void *data)
vkms_config_plane_get_type(plane_cfg));
}
vkms_config_for_each_crtc(vkmsdev->config, crtc_cfg) {
seq_puts(m, "crtc:\n");
seq_printf(m, "\twriteback=%d\n",
vkms_config_crtc_get_writeback(crtc_cfg));
}
return 0;
}
@ -201,3 +233,28 @@ void vkms_config_destroy_plane(struct vkms_config_plane *plane_cfg)
kfree(plane_cfg);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_destroy_plane);
struct vkms_config_crtc *vkms_config_create_crtc(struct vkms_config *config)
{
struct vkms_config_crtc *crtc_cfg;
crtc_cfg = kzalloc(sizeof(*crtc_cfg), GFP_KERNEL);
if (!crtc_cfg)
return ERR_PTR(-ENOMEM);
crtc_cfg->config = config;
vkms_config_crtc_set_writeback(crtc_cfg, false);
list_add_tail(&crtc_cfg->link, &config->crtcs);
return crtc_cfg;
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_create_crtc);
void vkms_config_destroy_crtc(struct vkms_config *config,
struct vkms_config_crtc *crtc_cfg)
{
list_del(&crtc_cfg->link);
kfree(crtc_cfg);
}
EXPORT_SYMBOL_IF_KUNIT(vkms_config_destroy_crtc);

View File

@ -14,12 +14,14 @@
* @dev_name: Name of the device
* @writeback: If true, a writeback buffer can be attached to the CRTC
* @planes: List of planes configured for the device
* @crtcs: List of CRTCs configured for the device
* @dev: Used to store the current VKMS device. Only set when the device is instantiated.
*/
struct vkms_config {
const char *dev_name;
bool writeback;
struct list_head planes;
struct list_head crtcs;
struct vkms_device *dev;
};
@ -45,6 +47,27 @@ struct vkms_config_plane {
struct vkms_plane *plane;
};
/**
* struct vkms_config_crtc
*
* @link: Link to the others CRTCs in vkms_config
* @config: The vkms_config this CRTC belongs to
* @writeback: If true, a writeback buffer can be attached to the CRTC
* @crtc: Internal usage. This pointer should never be considered as valid.
* It can be used to store a temporary reference to a VKMS CRTC during
* device creation. This pointer is not managed by the configuration and
* must be managed by other means.
*/
struct vkms_config_crtc {
struct list_head link;
struct vkms_config *config;
bool writeback;
/* Internal usage */
struct vkms_output *crtc;
};
/**
* vkms_config_for_each_plane - Iterate over the vkms_config planes
* @config: &struct vkms_config pointer
@ -53,6 +76,14 @@ struct vkms_config_plane {
#define vkms_config_for_each_plane(config, plane_cfg) \
list_for_each_entry((plane_cfg), &(config)->planes, link)
/**
* vkms_config_for_each_crtc - Iterate over the vkms_config CRTCs
* @config: &struct vkms_config pointer
* @crtc_cfg: &struct vkms_config_crtc pointer used as cursor
*/
#define vkms_config_for_each_crtc(config, crtc_cfg) \
list_for_each_entry((crtc_cfg), &(config)->crtcs, link)
/**
* vkms_config_create() - Create a new VKMS configuration
* @dev_name: Name of the device
@ -96,6 +127,15 @@ vkms_config_get_device_name(struct vkms_config *config)
return config->dev_name;
}
/**
* vkms_config_get_num_crtcs() - Return the number of CRTCs in the configuration
* @config: Configuration to get the number of CRTCs from
*/
static inline size_t vkms_config_get_num_crtcs(struct vkms_config *config)
{
return list_count_nodes(&config->crtcs);
}
/**
* vkms_config_is_valid() - Validate a configuration
* @config: Configuration to validate
@ -151,4 +191,44 @@ vkms_config_plane_set_type(struct vkms_config_plane *plane_cfg,
plane_cfg->type = type;
}
/**
* vkms_config_create_crtc() - Add a new CRTC configuration
* @config: Configuration to add the CRTC to
*
* Returns:
* The new CRTC configuration or an error. Call vkms_config_destroy_crtc() to
* free the returned CRTC configuration.
*/
struct vkms_config_crtc *vkms_config_create_crtc(struct vkms_config *config);
/**
* vkms_config_destroy_crtc() - Remove and free a CRTC configuration
* @config: Configuration to remove the CRTC from
* @crtc_cfg: CRTC configuration to destroy
*/
void vkms_config_destroy_crtc(struct vkms_config *config,
struct vkms_config_crtc *crtc_cfg);
/**
* vkms_config_crtc_get_writeback() - If a writeback connector will be created
* @crtc_cfg: CRTC with or without a writeback connector
*/
static inline bool
vkms_config_crtc_get_writeback(struct vkms_config_crtc *crtc_cfg)
{
return crtc_cfg->writeback;
}
/**
* vkms_config_crtc_set_writeback() - If a writeback connector will be created
* @crtc_cfg: Target CRTC
* @writeback: Enable or disable the writeback connector
*/
static inline void
vkms_config_crtc_set_writeback(struct vkms_config_crtc *crtc_cfg,
bool writeback)
{
crtc_cfg->writeback = writeback;
}
#endif /* _VKMS_CONFIG_H_ */