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