1 // SPDX-License-Identifier: GPL-2.0+
2
3 #include "vkms_drv.h"
4 #include <drm/drm_fourcc.h>
5 #include <drm/drm_writeback.h>
6 #include <drm/drm_probe_helper.h>
7 #include <drm/drm_atomic_helper.h>
8 #include <drm/drm_gem_framebuffer_helper.h>
9
10 static const u32 vkms_wb_formats[] = {
11 DRM_FORMAT_XRGB8888,
12 };
13
14 static const struct drm_connector_funcs vkms_wb_connector_funcs = {
15 .fill_modes = drm_helper_probe_single_connector_modes,
16 .destroy = drm_connector_cleanup,
17 .reset = drm_atomic_helper_connector_reset,
18 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
19 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
20 };
21
vkms_wb_encoder_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)22 static int vkms_wb_encoder_atomic_check(struct drm_encoder *encoder,
23 struct drm_crtc_state *crtc_state,
24 struct drm_connector_state *conn_state)
25 {
26 struct drm_framebuffer *fb;
27 const struct drm_display_mode *mode = &crtc_state->mode;
28
29 if (!conn_state->writeback_job || !conn_state->writeback_job->fb)
30 return 0;
31
32 fb = conn_state->writeback_job->fb;
33 if (fb->width != mode->hdisplay || fb->height != mode->vdisplay) {
34 DRM_DEBUG_KMS("Invalid framebuffer size %ux%u\n",
35 fb->width, fb->height);
36 return -EINVAL;
37 }
38
39 if (fb->format->format != vkms_wb_formats[0]) {
40 struct drm_format_name_buf format_name;
41
42 DRM_DEBUG_KMS("Invalid pixel format %s\n",
43 drm_get_format_name(fb->format->format,
44 &format_name));
45 return -EINVAL;
46 }
47
48 return 0;
49 }
50
51 static const struct drm_encoder_helper_funcs vkms_wb_encoder_helper_funcs = {
52 .atomic_check = vkms_wb_encoder_atomic_check,
53 };
54
vkms_wb_connector_get_modes(struct drm_connector * connector)55 static int vkms_wb_connector_get_modes(struct drm_connector *connector)
56 {
57 struct drm_device *dev = connector->dev;
58
59 return drm_add_modes_noedid(connector, dev->mode_config.max_width,
60 dev->mode_config.max_height);
61 }
62
vkms_wb_prepare_job(struct drm_writeback_connector * wb_connector,struct drm_writeback_job * job)63 static int vkms_wb_prepare_job(struct drm_writeback_connector *wb_connector,
64 struct drm_writeback_job *job)
65 {
66 struct vkms_gem_object *vkms_obj;
67 struct drm_gem_object *gem_obj;
68 int ret;
69
70 if (!job->fb)
71 return 0;
72
73 gem_obj = drm_gem_fb_get_obj(job->fb, 0);
74 ret = vkms_gem_vmap(gem_obj);
75 if (ret) {
76 DRM_ERROR("vmap failed: %d\n", ret);
77 return ret;
78 }
79
80 vkms_obj = drm_gem_to_vkms_gem(gem_obj);
81 job->priv = vkms_obj->vaddr;
82
83 return 0;
84 }
85
vkms_wb_cleanup_job(struct drm_writeback_connector * connector,struct drm_writeback_job * job)86 static void vkms_wb_cleanup_job(struct drm_writeback_connector *connector,
87 struct drm_writeback_job *job)
88 {
89 struct drm_gem_object *gem_obj;
90 struct vkms_device *vkmsdev;
91
92 if (!job->fb)
93 return;
94
95 gem_obj = drm_gem_fb_get_obj(job->fb, 0);
96 vkms_gem_vunmap(gem_obj);
97
98 vkmsdev = drm_device_to_vkms_device(gem_obj->dev);
99 vkms_set_composer(&vkmsdev->output, false);
100 }
101
vkms_wb_atomic_commit(struct drm_connector * conn,struct drm_connector_state * state)102 static void vkms_wb_atomic_commit(struct drm_connector *conn,
103 struct drm_connector_state *state)
104 {
105 struct vkms_device *vkmsdev = drm_device_to_vkms_device(conn->dev);
106 struct vkms_output *output = &vkmsdev->output;
107 struct drm_writeback_connector *wb_conn = &output->wb_connector;
108 struct drm_connector_state *conn_state = wb_conn->base.state;
109 struct vkms_crtc_state *crtc_state = output->composer_state;
110
111 if (!conn_state)
112 return;
113
114 vkms_set_composer(&vkmsdev->output, true);
115
116 spin_lock_irq(&output->composer_lock);
117 crtc_state->active_writeback = conn_state->writeback_job->priv;
118 crtc_state->wb_pending = true;
119 spin_unlock_irq(&output->composer_lock);
120 drm_writeback_queue_job(wb_conn, state);
121 }
122
123 static const struct drm_connector_helper_funcs vkms_wb_conn_helper_funcs = {
124 .get_modes = vkms_wb_connector_get_modes,
125 .prepare_writeback_job = vkms_wb_prepare_job,
126 .cleanup_writeback_job = vkms_wb_cleanup_job,
127 .atomic_commit = vkms_wb_atomic_commit,
128 };
129
vkms_enable_writeback_connector(struct vkms_device * vkmsdev)130 int vkms_enable_writeback_connector(struct vkms_device *vkmsdev)
131 {
132 struct drm_writeback_connector *wb = &vkmsdev->output.wb_connector;
133
134 vkmsdev->output.wb_connector.encoder.possible_crtcs = 1;
135 drm_connector_helper_add(&wb->base, &vkms_wb_conn_helper_funcs);
136
137 return drm_writeback_connector_init(&vkmsdev->drm, wb,
138 &vkms_wb_connector_funcs,
139 &vkms_wb_encoder_helper_funcs,
140 vkms_wb_formats,
141 ARRAY_SIZE(vkms_wb_formats));
142 }
143