1 // SPDX-License-Identifier: GPL-2.0+
2
3 #include "vkms_drv.h"
4 #include <drm/drm_atomic_helper.h>
5 #include <drm/drm_edid.h>
6 #include <drm/drm_probe_helper.h>
7
8 static const struct drm_connector_funcs vkms_connector_funcs = {
9 .fill_modes = drm_helper_probe_single_connector_modes,
10 .destroy = drm_connector_cleanup,
11 .reset = drm_atomic_helper_connector_reset,
12 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
13 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
14 };
15
16 static const struct drm_encoder_funcs vkms_encoder_funcs = {
17 .destroy = drm_encoder_cleanup,
18 };
19
vkms_conn_get_modes(struct drm_connector * connector)20 static int vkms_conn_get_modes(struct drm_connector *connector)
21 {
22 int count;
23
24 /* Use the default modes list from DRM */
25 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
26 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
27
28 return count;
29 }
30
31 static const struct drm_connector_helper_funcs vkms_conn_helper_funcs = {
32 .get_modes = vkms_conn_get_modes,
33 };
34
vkms_output_init(struct vkms_device * vkmsdev)35 int vkms_output_init(struct vkms_device *vkmsdev)
36 {
37 struct vkms_output *output = &vkmsdev->output;
38 struct drm_device *dev = &vkmsdev->drm;
39 struct drm_connector *connector = &output->connector;
40 struct drm_encoder *encoder = &output->encoder;
41 struct drm_crtc *crtc = &output->crtc;
42 struct vkms_plane *primary, *overlay, *cursor = NULL;
43 int ret;
44 int writeback;
45 unsigned int n;
46
47 /*
48 * Initialize used plane. One primary plane is required to perform the composition.
49 *
50 * The overlay and cursor planes are not mandatory, but can be used to perform complex
51 * composition.
52 */
53 primary = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_PRIMARY);
54 if (IS_ERR(primary))
55 return PTR_ERR(primary);
56
57 if (vkmsdev->config->cursor) {
58 cursor = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_CURSOR);
59 if (IS_ERR(cursor))
60 return PTR_ERR(cursor);
61 }
62
63 ret = vkms_crtc_init(dev, crtc, &primary->base, &cursor->base);
64 if (ret)
65 return ret;
66
67 if (vkmsdev->config->overlay) {
68 for (n = 0; n < NUM_OVERLAY_PLANES; n++) {
69 overlay = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_OVERLAY);
70 if (IS_ERR(overlay)) {
71 DRM_DEV_ERROR(dev->dev, "Failed to init vkms plane\n");
72 return PTR_ERR(overlay);
73 }
74 overlay->base.possible_crtcs = drm_crtc_mask(crtc);
75 }
76 }
77
78 ret = drm_connector_init(dev, connector, &vkms_connector_funcs,
79 DRM_MODE_CONNECTOR_VIRTUAL);
80 if (ret) {
81 DRM_ERROR("Failed to init connector\n");
82 return ret;
83 }
84
85 drm_connector_helper_add(connector, &vkms_conn_helper_funcs);
86
87 ret = drm_encoder_init(dev, encoder, &vkms_encoder_funcs,
88 DRM_MODE_ENCODER_VIRTUAL, NULL);
89 if (ret) {
90 DRM_ERROR("Failed to init encoder\n");
91 goto err_encoder;
92 }
93 encoder->possible_crtcs = drm_crtc_mask(crtc);
94
95 ret = drm_connector_attach_encoder(connector, encoder);
96 if (ret) {
97 DRM_ERROR("Failed to attach connector to encoder\n");
98 goto err_attach;
99 }
100
101 if (vkmsdev->config->writeback) {
102 writeback = vkms_enable_writeback_connector(vkmsdev);
103 if (writeback)
104 DRM_ERROR("Failed to init writeback connector\n");
105 }
106
107 drm_mode_config_reset(dev);
108
109 return 0;
110
111 err_attach:
112 drm_encoder_cleanup(encoder);
113
114 err_encoder:
115 drm_connector_cleanup(connector);
116
117 return ret;
118 }
119