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