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