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