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