1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * R-Car Display Unit Writeback Support 4 * 5 * Copyright (C) 2019 Laurent Pinchart <laurent.pinchart@ideasonboard.com> 6 */ 7 8 #include <drm/drm_atomic_helper.h> 9 #include <drm/drm_device.h> 10 #include <drm/drm_edid.h> 11 #include <drm/drm_fourcc.h> 12 #include <drm/drm_framebuffer.h> 13 #include <drm/drm_probe_helper.h> 14 #include <drm/drm_writeback.h> 15 16 #include "rcar_du_crtc.h" 17 #include "rcar_du_drv.h" 18 #include "rcar_du_kms.h" 19 #include "rcar_du_writeback.h" 20 21 /** 22 * struct rcar_du_wb_conn_state - Driver-specific writeback connector state 23 * @state: base DRM connector state 24 * @format: format of the writeback framebuffer 25 */ 26 struct rcar_du_wb_conn_state { 27 struct drm_connector_state state; 28 const struct rcar_du_format_info *format; 29 }; 30 31 #define to_rcar_wb_conn_state(s) \ 32 container_of(s, struct rcar_du_wb_conn_state, state) 33 34 /** 35 * struct rcar_du_wb_job - Driver-private data for writeback jobs 36 * @sg_tables: scatter-gather tables for the framebuffer memory 37 */ 38 struct rcar_du_wb_job { 39 struct sg_table sg_tables[3]; 40 }; 41 42 static int rcar_du_wb_conn_get_modes(struct drm_connector *connector) 43 { 44 struct drm_device *dev = connector->dev; 45 46 return drm_add_modes_noedid(connector, dev->mode_config.max_width, 47 dev->mode_config.max_height); 48 } 49 50 static int rcar_du_wb_prepare_job(struct drm_writeback_connector *connector, 51 struct drm_writeback_job *job) 52 { 53 struct rcar_du_crtc *rcrtc = wb_to_rcar_crtc(connector); 54 struct rcar_du_wb_job *rjob; 55 int ret; 56 57 if (!job->fb) 58 return 0; 59 60 rjob = kzalloc(sizeof(*rjob), GFP_KERNEL); 61 if (!rjob) 62 return -ENOMEM; 63 64 /* Map the framebuffer to the VSP. */ 65 ret = rcar_du_vsp_map_fb(rcrtc->vsp, job->fb, rjob->sg_tables); 66 if (ret < 0) { 67 kfree(rjob); 68 return ret; 69 } 70 71 job->priv = rjob; 72 return 0; 73 } 74 75 static void rcar_du_wb_cleanup_job(struct drm_writeback_connector *connector, 76 struct drm_writeback_job *job) 77 { 78 struct rcar_du_crtc *rcrtc = wb_to_rcar_crtc(connector); 79 struct rcar_du_wb_job *rjob = job->priv; 80 81 if (!job->fb) 82 return; 83 84 rcar_du_vsp_unmap_fb(rcrtc->vsp, job->fb, rjob->sg_tables); 85 kfree(rjob); 86 } 87 88 static const struct drm_connector_helper_funcs rcar_du_wb_conn_helper_funcs = { 89 .get_modes = rcar_du_wb_conn_get_modes, 90 .prepare_writeback_job = rcar_du_wb_prepare_job, 91 .cleanup_writeback_job = rcar_du_wb_cleanup_job, 92 }; 93 94 static struct drm_connector_state * 95 rcar_du_wb_conn_duplicate_state(struct drm_connector *connector) 96 { 97 struct rcar_du_wb_conn_state *copy; 98 99 if (WARN_ON(!connector->state)) 100 return NULL; 101 102 copy = kzalloc(sizeof(*copy), GFP_KERNEL); 103 if (!copy) 104 return NULL; 105 106 __drm_atomic_helper_connector_duplicate_state(connector, ©->state); 107 108 return ©->state; 109 } 110 111 static void rcar_du_wb_conn_destroy_state(struct drm_connector *connector, 112 struct drm_connector_state *state) 113 { 114 __drm_atomic_helper_connector_destroy_state(state); 115 kfree(to_rcar_wb_conn_state(state)); 116 } 117 118 static void rcar_du_wb_conn_reset(struct drm_connector *connector) 119 { 120 struct rcar_du_wb_conn_state *state; 121 122 if (connector->state) { 123 rcar_du_wb_conn_destroy_state(connector, connector->state); 124 connector->state = NULL; 125 } 126 127 state = kzalloc(sizeof(*state), GFP_KERNEL); 128 if (state == NULL) 129 return; 130 131 __drm_atomic_helper_connector_reset(connector, &state->state); 132 } 133 134 static const struct drm_connector_funcs rcar_du_wb_conn_funcs = { 135 .reset = rcar_du_wb_conn_reset, 136 .fill_modes = drm_helper_probe_single_connector_modes, 137 .destroy = drm_connector_cleanup, 138 .atomic_duplicate_state = rcar_du_wb_conn_duplicate_state, 139 .atomic_destroy_state = rcar_du_wb_conn_destroy_state, 140 }; 141 142 static int rcar_du_wb_enc_atomic_check(struct drm_encoder *encoder, 143 struct drm_crtc_state *crtc_state, 144 struct drm_connector_state *conn_state) 145 { 146 struct rcar_du_wb_conn_state *wb_state = 147 to_rcar_wb_conn_state(conn_state); 148 const struct drm_display_mode *mode = &crtc_state->mode; 149 struct drm_device *dev = encoder->dev; 150 struct drm_framebuffer *fb; 151 152 if (!conn_state->writeback_job) 153 return 0; 154 155 fb = conn_state->writeback_job->fb; 156 157 /* 158 * Verify that the framebuffer format is supported and that its size 159 * matches the current mode. 160 */ 161 if (fb->width != mode->hdisplay || fb->height != mode->vdisplay) { 162 dev_dbg(dev->dev, "%s: invalid framebuffer size %ux%u\n", 163 __func__, fb->width, fb->height); 164 return -EINVAL; 165 } 166 167 wb_state->format = rcar_du_format_info(fb->format->format); 168 if (wb_state->format == NULL) { 169 dev_dbg(dev->dev, "%s: unsupported format %p4cc\n", __func__, 170 &fb->format->format); 171 return -EINVAL; 172 } 173 174 return 0; 175 } 176 177 static const struct drm_encoder_helper_funcs rcar_du_wb_enc_helper_funcs = { 178 .atomic_check = rcar_du_wb_enc_atomic_check, 179 }; 180 181 /* 182 * Only RGB formats are currently supported as the VSP outputs RGB to the DU 183 * and can't convert to YUV separately for writeback. 184 */ 185 static const u32 writeback_formats[] = { 186 DRM_FORMAT_RGB332, 187 DRM_FORMAT_ARGB4444, 188 DRM_FORMAT_XRGB4444, 189 DRM_FORMAT_ARGB1555, 190 DRM_FORMAT_XRGB1555, 191 DRM_FORMAT_RGB565, 192 DRM_FORMAT_BGR888, 193 DRM_FORMAT_RGB888, 194 DRM_FORMAT_BGRA8888, 195 DRM_FORMAT_BGRX8888, 196 DRM_FORMAT_ARGB8888, 197 DRM_FORMAT_XRGB8888, 198 }; 199 200 int rcar_du_writeback_init(struct rcar_du_device *rcdu, 201 struct rcar_du_crtc *rcrtc) 202 { 203 struct drm_writeback_connector *wb_conn = &rcrtc->writeback; 204 205 drm_connector_helper_add(&wb_conn->base, 206 &rcar_du_wb_conn_helper_funcs); 207 208 return drm_writeback_connector_init(&rcdu->ddev, wb_conn, 209 &rcar_du_wb_conn_funcs, 210 &rcar_du_wb_enc_helper_funcs, 211 writeback_formats, 212 ARRAY_SIZE(writeback_formats), 213 1 << drm_crtc_index(&rcrtc->crtc)); 214 } 215 216 void rcar_du_writeback_setup(struct rcar_du_crtc *rcrtc, 217 struct vsp1_du_writeback_config *cfg) 218 { 219 struct rcar_du_wb_conn_state *wb_state; 220 struct drm_connector_state *state; 221 struct rcar_du_wb_job *rjob; 222 struct drm_framebuffer *fb; 223 unsigned int i; 224 225 state = rcrtc->writeback.base.state; 226 if (!state || !state->writeback_job) 227 return; 228 229 fb = state->writeback_job->fb; 230 rjob = state->writeback_job->priv; 231 wb_state = to_rcar_wb_conn_state(state); 232 233 cfg->pixelformat = wb_state->format->v4l2; 234 cfg->pitch = fb->pitches[0]; 235 236 for (i = 0; i < wb_state->format->planes; ++i) 237 cfg->mem[i] = sg_dma_address(rjob->sg_tables[i].sgl) 238 + fb->offsets[i]; 239 240 drm_writeback_queue_job(&rcrtc->writeback, state); 241 } 242 243 void rcar_du_writeback_complete(struct rcar_du_crtc *rcrtc) 244 { 245 drm_writeback_signal_completion(&rcrtc->writeback, 0); 246 } 247