xref: /linux/drivers/gpu/drm/vkms/vkms_writeback.c (revision fd7d598270724cc787982ea48bbe17ad383a8b7f)
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/iosys-map.h>
4 
5 #include <drm/drm_atomic.h>
6 #include <drm/drm_edid.h>
7 #include <drm/drm_fourcc.h>
8 #include <drm/drm_writeback.h>
9 #include <drm/drm_probe_helper.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_gem_framebuffer_helper.h>
12 #include <drm/drm_gem_shmem_helper.h>
13 
14 #include "vkms_drv.h"
15 #include "vkms_formats.h"
16 
17 static const u32 vkms_wb_formats[] = {
18 	DRM_FORMAT_ARGB8888,
19 	DRM_FORMAT_XRGB8888,
20 	DRM_FORMAT_XRGB16161616,
21 	DRM_FORMAT_ARGB16161616,
22 	DRM_FORMAT_RGB565
23 };
24 
25 static const struct drm_connector_funcs vkms_wb_connector_funcs = {
26 	.fill_modes = drm_helper_probe_single_connector_modes,
27 	.destroy = drm_connector_cleanup,
28 	.reset = drm_atomic_helper_connector_reset,
29 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
30 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
31 };
32 
33 static int vkms_wb_encoder_atomic_check(struct drm_encoder *encoder,
34 					struct drm_crtc_state *crtc_state,
35 					struct drm_connector_state *conn_state)
36 {
37 	struct drm_framebuffer *fb;
38 	const struct drm_display_mode *mode = &crtc_state->mode;
39 	int ret;
40 
41 	if (!conn_state->writeback_job || !conn_state->writeback_job->fb)
42 		return 0;
43 
44 	fb = conn_state->writeback_job->fb;
45 	if (fb->width != mode->hdisplay || fb->height != mode->vdisplay) {
46 		DRM_DEBUG_KMS("Invalid framebuffer size %ux%u\n",
47 			      fb->width, fb->height);
48 		return -EINVAL;
49 	}
50 
51 	ret = drm_atomic_helper_check_wb_encoder_state(encoder, conn_state);
52 	if (ret < 0)
53 		return ret;
54 
55 	return 0;
56 }
57 
58 static const struct drm_encoder_helper_funcs vkms_wb_encoder_helper_funcs = {
59 	.atomic_check = vkms_wb_encoder_atomic_check,
60 };
61 
62 static int vkms_wb_connector_get_modes(struct drm_connector *connector)
63 {
64 	struct drm_device *dev = connector->dev;
65 
66 	return drm_add_modes_noedid(connector, dev->mode_config.max_width,
67 				    dev->mode_config.max_height);
68 }
69 
70 static int vkms_wb_prepare_job(struct drm_writeback_connector *wb_connector,
71 			       struct drm_writeback_job *job)
72 {
73 	struct vkms_writeback_job *vkmsjob;
74 	int ret;
75 
76 	if (!job->fb)
77 		return 0;
78 
79 	vkmsjob = kzalloc(sizeof(*vkmsjob), GFP_KERNEL);
80 	if (!vkmsjob)
81 		return -ENOMEM;
82 
83 	ret = drm_gem_fb_vmap(job->fb, vkmsjob->wb_frame_info.map, vkmsjob->data);
84 	if (ret) {
85 		DRM_ERROR("vmap failed: %d\n", ret);
86 		goto err_kfree;
87 	}
88 
89 	vkmsjob->wb_frame_info.fb = job->fb;
90 	drm_framebuffer_get(vkmsjob->wb_frame_info.fb);
91 
92 	job->priv = vkmsjob;
93 
94 	return 0;
95 
96 err_kfree:
97 	kfree(vkmsjob);
98 	return ret;
99 }
100 
101 static void vkms_wb_cleanup_job(struct drm_writeback_connector *connector,
102 				struct drm_writeback_job *job)
103 {
104 	struct vkms_writeback_job *vkmsjob = job->priv;
105 	struct vkms_device *vkmsdev;
106 
107 	if (!job->fb)
108 		return;
109 
110 	drm_gem_fb_vunmap(job->fb, vkmsjob->wb_frame_info.map);
111 
112 	drm_framebuffer_put(vkmsjob->wb_frame_info.fb);
113 
114 	vkmsdev = drm_device_to_vkms_device(job->fb->dev);
115 	vkms_set_composer(&vkmsdev->output, false);
116 	kfree(vkmsjob);
117 }
118 
119 static void vkms_wb_atomic_commit(struct drm_connector *conn,
120 				  struct drm_atomic_state *state)
121 {
122 	struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
123 											 conn);
124 	struct vkms_device *vkmsdev = drm_device_to_vkms_device(conn->dev);
125 	struct vkms_output *output = &vkmsdev->output;
126 	struct drm_writeback_connector *wb_conn = &output->wb_connector;
127 	struct drm_connector_state *conn_state = wb_conn->base.state;
128 	struct vkms_crtc_state *crtc_state = output->composer_state;
129 	struct drm_framebuffer *fb = connector_state->writeback_job->fb;
130 	u16 crtc_height = crtc_state->base.crtc->mode.vdisplay;
131 	u16 crtc_width = crtc_state->base.crtc->mode.hdisplay;
132 	struct vkms_writeback_job *active_wb;
133 	struct vkms_frame_info *wb_frame_info;
134 	u32 wb_format = fb->format->format;
135 
136 	if (!conn_state)
137 		return;
138 
139 	vkms_set_composer(&vkmsdev->output, true);
140 
141 	active_wb = conn_state->writeback_job->priv;
142 	wb_frame_info = &active_wb->wb_frame_info;
143 
144 	spin_lock_irq(&output->composer_lock);
145 	crtc_state->active_writeback = active_wb;
146 	crtc_state->wb_pending = true;
147 	spin_unlock_irq(&output->composer_lock);
148 
149 	wb_frame_info->offset = fb->offsets[0];
150 	wb_frame_info->pitch = fb->pitches[0];
151 	wb_frame_info->cpp = fb->format->cpp[0];
152 
153 	drm_writeback_queue_job(wb_conn, connector_state);
154 	active_wb->pixel_write = get_pixel_write_function(wb_format);
155 	drm_rect_init(&wb_frame_info->src, 0, 0, crtc_width, crtc_height);
156 	drm_rect_init(&wb_frame_info->dst, 0, 0, crtc_width, crtc_height);
157 }
158 
159 static const struct drm_connector_helper_funcs vkms_wb_conn_helper_funcs = {
160 	.get_modes = vkms_wb_connector_get_modes,
161 	.prepare_writeback_job = vkms_wb_prepare_job,
162 	.cleanup_writeback_job = vkms_wb_cleanup_job,
163 	.atomic_commit = vkms_wb_atomic_commit,
164 };
165 
166 int vkms_enable_writeback_connector(struct vkms_device *vkmsdev)
167 {
168 	struct drm_writeback_connector *wb = &vkmsdev->output.wb_connector;
169 
170 	drm_connector_helper_add(&wb->base, &vkms_wb_conn_helper_funcs);
171 
172 	return drm_writeback_connector_init(&vkmsdev->drm, wb,
173 					    &vkms_wb_connector_funcs,
174 					    &vkms_wb_encoder_helper_funcs,
175 					    vkms_wb_formats,
176 					    ARRAY_SIZE(vkms_wb_formats),
177 					    1);
178 }
179