1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2011 Samsung Electronics Co.Ltd 4 * Authors: Joonyoung Shim <jy0922.shim@samsung.com> 5 */ 6 7 8 #include <drm/drm_atomic.h> 9 #include <drm/drm_atomic_helper.h> 10 #include <drm/drm_blend.h> 11 #include <drm/drm_framebuffer.h> 12 #include <drm/drm_plane_helper.h> 13 #include <drm/exynos_drm.h> 14 15 #include "exynos_drm_crtc.h" 16 #include "exynos_drm_drv.h" 17 #include "exynos_drm_fb.h" 18 #include "exynos_drm_gem.h" 19 #include "exynos_drm_plane.h" 20 21 /* 22 * This function is to get X or Y size shown via screen. This needs length and 23 * start position of CRTC. 24 * 25 * <--- length ---> 26 * CRTC ---------------- 27 * ^ start ^ end 28 * 29 * There are six cases from a to f. 30 * 31 * <----- SCREEN -----> 32 * 0 last 33 * ----------|------------------|---------- 34 * CRTCs 35 * a ------- 36 * b ------- 37 * c -------------------------- 38 * d -------- 39 * e ------- 40 * f ------- 41 */ 42 static int exynos_plane_get_size(int start, unsigned length, unsigned last) 43 { 44 int end = start + length; 45 int size = 0; 46 47 if (start <= 0) { 48 if (end > 0) 49 size = min_t(unsigned, end, last); 50 } else if (start <= last) { 51 size = min_t(unsigned, last - start, length); 52 } 53 54 return size; 55 } 56 57 static void exynos_plane_mode_set(struct exynos_drm_plane_state *exynos_state) 58 { 59 struct drm_plane_state *state = &exynos_state->base; 60 struct drm_crtc *crtc = state->crtc; 61 struct drm_crtc_state *crtc_state = 62 drm_atomic_get_existing_crtc_state(state->state, crtc); 63 struct drm_display_mode *mode = &crtc_state->adjusted_mode; 64 int crtc_x, crtc_y; 65 unsigned int crtc_w, crtc_h; 66 unsigned int src_x, src_y; 67 unsigned int src_w, src_h; 68 unsigned int actual_w; 69 unsigned int actual_h; 70 71 /* 72 * The original src/dest coordinates are stored in exynos_state->base, 73 * but we want to keep another copy internal to our driver that we can 74 * clip/modify ourselves. 75 */ 76 77 crtc_x = state->crtc_x; 78 crtc_y = state->crtc_y; 79 crtc_w = state->crtc_w; 80 crtc_h = state->crtc_h; 81 82 src_x = state->src_x >> 16; 83 src_y = state->src_y >> 16; 84 src_w = state->src_w >> 16; 85 src_h = state->src_h >> 16; 86 87 /* set ratio */ 88 exynos_state->h_ratio = (src_w << 16) / crtc_w; 89 exynos_state->v_ratio = (src_h << 16) / crtc_h; 90 91 /* clip to visible area */ 92 actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay); 93 actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay); 94 95 if (crtc_x < 0) { 96 if (actual_w) 97 src_x += ((-crtc_x) * exynos_state->h_ratio) >> 16; 98 crtc_x = 0; 99 } 100 101 if (crtc_y < 0) { 102 if (actual_h) 103 src_y += ((-crtc_y) * exynos_state->v_ratio) >> 16; 104 crtc_y = 0; 105 } 106 107 /* set drm framebuffer data. */ 108 exynos_state->src.x = src_x; 109 exynos_state->src.y = src_y; 110 exynos_state->src.w = (actual_w * exynos_state->h_ratio) >> 16; 111 exynos_state->src.h = (actual_h * exynos_state->v_ratio) >> 16; 112 113 /* set plane range to be displayed. */ 114 exynos_state->crtc.x = crtc_x; 115 exynos_state->crtc.y = crtc_y; 116 exynos_state->crtc.w = actual_w; 117 exynos_state->crtc.h = actual_h; 118 119 DRM_DEV_DEBUG_KMS(crtc->dev->dev, 120 "plane : offset_x/y(%d,%d), width/height(%d,%d)", 121 exynos_state->crtc.x, exynos_state->crtc.y, 122 exynos_state->crtc.w, exynos_state->crtc.h); 123 } 124 125 static void exynos_drm_plane_reset(struct drm_plane *plane) 126 { 127 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); 128 struct exynos_drm_plane_state *exynos_state; 129 130 if (plane->state) { 131 exynos_state = to_exynos_plane_state(plane->state); 132 __drm_atomic_helper_plane_destroy_state(plane->state); 133 kfree(exynos_state); 134 plane->state = NULL; 135 } 136 137 exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL); 138 if (exynos_state) { 139 __drm_atomic_helper_plane_reset(plane, &exynos_state->base); 140 plane->state->zpos = exynos_plane->config->zpos; 141 } 142 } 143 144 static struct drm_plane_state * 145 exynos_drm_plane_duplicate_state(struct drm_plane *plane) 146 { 147 struct exynos_drm_plane_state *exynos_state; 148 struct exynos_drm_plane_state *copy; 149 150 exynos_state = to_exynos_plane_state(plane->state); 151 copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL); 152 if (!copy) 153 return NULL; 154 155 __drm_atomic_helper_plane_duplicate_state(plane, ©->base); 156 return ©->base; 157 } 158 159 static void exynos_drm_plane_destroy_state(struct drm_plane *plane, 160 struct drm_plane_state *old_state) 161 { 162 struct exynos_drm_plane_state *old_exynos_state = 163 to_exynos_plane_state(old_state); 164 __drm_atomic_helper_plane_destroy_state(old_state); 165 kfree(old_exynos_state); 166 } 167 168 static struct drm_plane_funcs exynos_plane_funcs = { 169 .update_plane = drm_atomic_helper_update_plane, 170 .disable_plane = drm_atomic_helper_disable_plane, 171 .destroy = drm_plane_cleanup, 172 .reset = exynos_drm_plane_reset, 173 .atomic_duplicate_state = exynos_drm_plane_duplicate_state, 174 .atomic_destroy_state = exynos_drm_plane_destroy_state, 175 }; 176 177 static int 178 exynos_drm_plane_check_format(const struct exynos_drm_plane_config *config, 179 struct exynos_drm_plane_state *state) 180 { 181 struct drm_framebuffer *fb = state->base.fb; 182 struct drm_device *dev = fb->dev; 183 184 switch (fb->modifier) { 185 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE: 186 if (!(config->capabilities & EXYNOS_DRM_PLANE_CAP_TILE)) 187 return -ENOTSUPP; 188 break; 189 190 case DRM_FORMAT_MOD_LINEAR: 191 break; 192 193 default: 194 DRM_DEV_ERROR(dev->dev, "unsupported pixel format modifier"); 195 return -ENOTSUPP; 196 } 197 198 return 0; 199 } 200 201 static int 202 exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config, 203 struct exynos_drm_plane_state *state) 204 { 205 struct drm_crtc *crtc = state->base.crtc; 206 bool width_ok = false, height_ok = false; 207 208 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE) 209 return 0; 210 211 if (state->src.w == state->crtc.w) 212 width_ok = true; 213 214 if (state->src.h == state->crtc.h) 215 height_ok = true; 216 217 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) && 218 state->h_ratio == (1 << 15)) 219 width_ok = true; 220 221 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) && 222 state->v_ratio == (1 << 15)) 223 height_ok = true; 224 225 if (width_ok && height_ok) 226 return 0; 227 228 DRM_DEV_DEBUG_KMS(crtc->dev->dev, "scaling mode is not supported"); 229 return -ENOTSUPP; 230 } 231 232 static int exynos_plane_atomic_check(struct drm_plane *plane, 233 struct drm_atomic_state *state) 234 { 235 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, 236 plane); 237 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); 238 struct exynos_drm_plane_state *exynos_state = 239 to_exynos_plane_state(new_plane_state); 240 int ret = 0; 241 242 if (!new_plane_state->crtc || !new_plane_state->fb) 243 return 0; 244 245 /* translate state into exynos_state */ 246 exynos_plane_mode_set(exynos_state); 247 248 ret = exynos_drm_plane_check_format(exynos_plane->config, exynos_state); 249 if (ret) 250 return ret; 251 252 ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state); 253 return ret; 254 } 255 256 static void exynos_plane_atomic_update(struct drm_plane *plane, 257 struct drm_atomic_state *state) 258 { 259 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, 260 plane); 261 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(new_state->crtc); 262 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); 263 264 if (!new_state->crtc) 265 return; 266 267 if (exynos_crtc->ops->update_plane) 268 exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane); 269 } 270 271 static void exynos_plane_atomic_disable(struct drm_plane *plane, 272 struct drm_atomic_state *state) 273 { 274 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane); 275 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); 276 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc); 277 278 if (!old_state->crtc) 279 return; 280 281 if (exynos_crtc->ops->disable_plane) 282 exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane); 283 } 284 285 static const struct drm_plane_helper_funcs plane_helper_funcs = { 286 .atomic_check = exynos_plane_atomic_check, 287 .atomic_update = exynos_plane_atomic_update, 288 .atomic_disable = exynos_plane_atomic_disable, 289 }; 290 291 static void exynos_plane_attach_zpos_property(struct drm_plane *plane, 292 int zpos, bool immutable) 293 { 294 if (immutable) 295 drm_plane_create_zpos_immutable_property(plane, zpos); 296 else 297 drm_plane_create_zpos_property(plane, zpos, 0, MAX_PLANE - 1); 298 } 299 300 int exynos_plane_init(struct drm_device *dev, 301 struct exynos_drm_plane *exynos_plane, unsigned int index, 302 const struct exynos_drm_plane_config *config) 303 { 304 int err; 305 unsigned int supported_modes = BIT(DRM_MODE_BLEND_PIXEL_NONE) | 306 BIT(DRM_MODE_BLEND_PREMULTI) | 307 BIT(DRM_MODE_BLEND_COVERAGE); 308 struct drm_plane *plane = &exynos_plane->base; 309 310 err = drm_universal_plane_init(dev, &exynos_plane->base, 311 1 << dev->mode_config.num_crtc, 312 &exynos_plane_funcs, 313 config->pixel_formats, 314 config->num_pixel_formats, 315 NULL, config->type, NULL); 316 if (err) { 317 DRM_DEV_ERROR(dev->dev, "failed to initialize plane\n"); 318 return err; 319 } 320 321 drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs); 322 323 exynos_plane->index = index; 324 exynos_plane->config = config; 325 326 exynos_plane_attach_zpos_property(&exynos_plane->base, config->zpos, 327 !(config->capabilities & EXYNOS_DRM_PLANE_CAP_ZPOS)); 328 329 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_PIX_BLEND) 330 drm_plane_create_blend_mode_property(plane, supported_modes); 331 332 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_WIN_BLEND) 333 drm_plane_create_alpha_property(plane); 334 335 return 0; 336 } 337