1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2015 MediaTek Inc. 4 * Author: CK Hu <ck.hu@mediatek.com> 5 */ 6 7 #include <drm/drm_atomic.h> 8 #include <drm/drm_atomic_helper.h> 9 #include <drm/drm_atomic_uapi.h> 10 #include <drm/drm_blend.h> 11 #include <drm/drm_fourcc.h> 12 #include <drm/drm_framebuffer.h> 13 #include <drm/drm_gem_atomic_helper.h> 14 #include <drm/drm_print.h> 15 #include <linux/align.h> 16 17 #include "mtk_crtc.h" 18 #include "mtk_ddp_comp.h" 19 #include "mtk_drm_drv.h" 20 #include "mtk_gem.h" 21 #include "mtk_plane.h" 22 23 static const u64 modifiers[] = { 24 DRM_FORMAT_MOD_LINEAR, 25 DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_32x8 | 26 AFBC_FORMAT_MOD_SPLIT | 27 AFBC_FORMAT_MOD_SPARSE), 28 DRM_FORMAT_MOD_INVALID, 29 }; 30 31 static void mtk_plane_reset(struct drm_plane *plane) 32 { 33 struct mtk_plane_state *state; 34 35 if (plane->state) { 36 __drm_atomic_helper_plane_destroy_state(plane->state); 37 38 state = to_mtk_plane_state(plane->state); 39 memset(state, 0, sizeof(*state)); 40 } else { 41 state = kzalloc(sizeof(*state), GFP_KERNEL); 42 if (!state) 43 return; 44 } 45 46 __drm_atomic_helper_plane_reset(plane, &state->base); 47 48 state->base.plane = plane; 49 state->pending.format = DRM_FORMAT_RGB565; 50 state->pending.modifier = DRM_FORMAT_MOD_LINEAR; 51 } 52 53 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane) 54 { 55 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state); 56 struct mtk_plane_state *state; 57 58 state = kmalloc(sizeof(*state), GFP_KERNEL); 59 if (!state) 60 return NULL; 61 62 __drm_atomic_helper_plane_duplicate_state(plane, &state->base); 63 64 WARN_ON(state->base.plane != plane); 65 66 state->pending = old_state->pending; 67 68 return &state->base; 69 } 70 71 static bool mtk_plane_format_mod_supported(struct drm_plane *plane, 72 uint32_t format, 73 uint64_t modifier) 74 { 75 if (modifier == DRM_FORMAT_MOD_LINEAR) 76 return true; 77 78 if (modifier != DRM_FORMAT_MOD_ARM_AFBC( 79 AFBC_FORMAT_MOD_BLOCK_SIZE_32x8 | 80 AFBC_FORMAT_MOD_SPLIT | 81 AFBC_FORMAT_MOD_SPARSE)) 82 return false; 83 84 if (format != DRM_FORMAT_XRGB8888 && 85 format != DRM_FORMAT_ARGB8888 && 86 format != DRM_FORMAT_BGRX8888 && 87 format != DRM_FORMAT_BGRA8888 && 88 format != DRM_FORMAT_ABGR8888 && 89 format != DRM_FORMAT_XBGR8888 && 90 format != DRM_FORMAT_RGB888 && 91 format != DRM_FORMAT_BGR888) 92 return false; 93 94 return true; 95 } 96 97 static void mtk_plane_destroy_state(struct drm_plane *plane, 98 struct drm_plane_state *state) 99 { 100 __drm_atomic_helper_plane_destroy_state(state); 101 kfree(to_mtk_plane_state(state)); 102 } 103 104 static int mtk_plane_atomic_async_check(struct drm_plane *plane, 105 struct drm_atomic_state *state, bool flip) 106 { 107 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, 108 plane); 109 struct drm_crtc_state *crtc_state; 110 int ret; 111 112 if (plane != new_plane_state->crtc->cursor) 113 return -EINVAL; 114 115 if (!plane->state) 116 return -EINVAL; 117 118 if (!plane->state->fb) 119 return -EINVAL; 120 121 ret = mtk_crtc_plane_check(new_plane_state->crtc, plane, 122 to_mtk_plane_state(new_plane_state)); 123 if (ret) 124 return ret; 125 126 crtc_state = drm_atomic_get_new_crtc_state(state, 127 new_plane_state->crtc); 128 129 return drm_atomic_helper_check_plane_state(plane->state, crtc_state, 130 DRM_PLANE_NO_SCALING, 131 DRM_PLANE_NO_SCALING, 132 true, true); 133 } 134 135 static void mtk_plane_update_new_state(struct drm_plane_state *new_state, 136 struct mtk_plane_state *mtk_plane_state) 137 { 138 struct drm_framebuffer *fb = new_state->fb; 139 struct drm_gem_object *gem; 140 struct mtk_gem_obj *mtk_gem; 141 unsigned int pitch, format; 142 u64 modifier; 143 dma_addr_t addr; 144 dma_addr_t hdr_addr = 0; 145 unsigned int hdr_pitch = 0; 146 int offset; 147 148 gem = fb->obj[0]; 149 mtk_gem = to_mtk_gem_obj(gem); 150 addr = mtk_gem->dma_addr; 151 pitch = fb->pitches[0]; 152 format = fb->format->format; 153 modifier = fb->modifier; 154 155 if (modifier == DRM_FORMAT_MOD_LINEAR) { 156 /* 157 * Using dma_addr_t variable to calculate with multiplier of different types, 158 * for example: addr += (new_state->src.x1 >> 16) * fb->format->cpp[0]; 159 * may cause coverity issue with unintentional overflow. 160 */ 161 offset = (new_state->src.x1 >> 16) * fb->format->cpp[0]; 162 addr += offset; 163 offset = (new_state->src.y1 >> 16) * pitch; 164 addr += offset; 165 } else { 166 int width_in_blocks = ALIGN(fb->width, AFBC_DATA_BLOCK_WIDTH) 167 / AFBC_DATA_BLOCK_WIDTH; 168 int height_in_blocks = ALIGN(fb->height, AFBC_DATA_BLOCK_HEIGHT) 169 / AFBC_DATA_BLOCK_HEIGHT; 170 int x_offset_in_blocks = (new_state->src.x1 >> 16) / AFBC_DATA_BLOCK_WIDTH; 171 int y_offset_in_blocks = (new_state->src.y1 >> 16) / AFBC_DATA_BLOCK_HEIGHT; 172 int hdr_size, hdr_offset; 173 174 hdr_pitch = width_in_blocks * AFBC_HEADER_BLOCK_SIZE; 175 pitch = width_in_blocks * AFBC_DATA_BLOCK_WIDTH * 176 AFBC_DATA_BLOCK_HEIGHT * fb->format->cpp[0]; 177 178 hdr_size = ALIGN(hdr_pitch * height_in_blocks, AFBC_HEADER_ALIGNMENT); 179 hdr_offset = hdr_pitch * y_offset_in_blocks + 180 AFBC_HEADER_BLOCK_SIZE * x_offset_in_blocks; 181 182 /* 183 * Using dma_addr_t variable to calculate with multiplier of different types, 184 * for example: addr += hdr_pitch * y_offset_in_blocks; 185 * may cause coverity issue with unintentional overflow. 186 */ 187 hdr_addr = addr + hdr_offset; 188 189 /* The data plane is offset by 1 additional block. */ 190 offset = pitch * y_offset_in_blocks + 191 AFBC_DATA_BLOCK_WIDTH * AFBC_DATA_BLOCK_HEIGHT * 192 fb->format->cpp[0] * (x_offset_in_blocks + 1); 193 194 /* 195 * Using dma_addr_t variable to calculate with multiplier of different types, 196 * for example: addr += pitch * y_offset_in_blocks; 197 * may cause coverity issue with unintentional overflow. 198 */ 199 addr = addr + hdr_size + offset; 200 } 201 202 mtk_plane_state->pending.enable = true; 203 mtk_plane_state->pending.pitch = pitch; 204 mtk_plane_state->pending.hdr_pitch = hdr_pitch; 205 mtk_plane_state->pending.format = format; 206 mtk_plane_state->pending.modifier = modifier; 207 mtk_plane_state->pending.addr = addr; 208 mtk_plane_state->pending.hdr_addr = hdr_addr; 209 mtk_plane_state->pending.x = new_state->dst.x1; 210 mtk_plane_state->pending.y = new_state->dst.y1; 211 mtk_plane_state->pending.width = drm_rect_width(&new_state->dst); 212 mtk_plane_state->pending.height = drm_rect_height(&new_state->dst); 213 mtk_plane_state->pending.rotation = new_state->rotation; 214 mtk_plane_state->pending.color_encoding = new_state->color_encoding; 215 } 216 217 static void mtk_plane_atomic_async_update(struct drm_plane *plane, 218 struct drm_atomic_state *state) 219 { 220 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, 221 plane); 222 struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state); 223 224 plane->state->crtc_x = new_state->crtc_x; 225 plane->state->crtc_y = new_state->crtc_y; 226 plane->state->crtc_h = new_state->crtc_h; 227 plane->state->crtc_w = new_state->crtc_w; 228 plane->state->src_x = new_state->src_x; 229 plane->state->src_y = new_state->src_y; 230 plane->state->src_h = new_state->src_h; 231 plane->state->src_w = new_state->src_w; 232 plane->state->dst.x1 = new_state->dst.x1; 233 plane->state->dst.y1 = new_state->dst.y1; 234 235 mtk_plane_update_new_state(new_state, new_plane_state); 236 swap(plane->state->fb, new_state->fb); 237 wmb(); /* Make sure the above parameters are set before update */ 238 new_plane_state->pending.async_dirty = true; 239 mtk_crtc_async_update(new_state->crtc, plane, state); 240 } 241 242 static const struct drm_plane_funcs mtk_plane_funcs = { 243 .update_plane = drm_atomic_helper_update_plane, 244 .disable_plane = drm_atomic_helper_disable_plane, 245 .destroy = drm_plane_cleanup, 246 .reset = mtk_plane_reset, 247 .atomic_duplicate_state = mtk_plane_duplicate_state, 248 .atomic_destroy_state = mtk_plane_destroy_state, 249 .format_mod_supported = mtk_plane_format_mod_supported, 250 }; 251 252 static int mtk_plane_atomic_check(struct drm_plane *plane, 253 struct drm_atomic_state *state) 254 { 255 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, 256 plane); 257 struct drm_framebuffer *fb = new_plane_state->fb; 258 struct drm_crtc_state *crtc_state; 259 int ret; 260 261 if (!fb) 262 return 0; 263 264 if (WARN_ON(!new_plane_state->crtc)) 265 return 0; 266 267 ret = mtk_crtc_plane_check(new_plane_state->crtc, plane, 268 to_mtk_plane_state(new_plane_state)); 269 if (ret) 270 return ret; 271 272 crtc_state = drm_atomic_get_crtc_state(state, 273 new_plane_state->crtc); 274 if (IS_ERR(crtc_state)) 275 return PTR_ERR(crtc_state); 276 277 return drm_atomic_helper_check_plane_state(new_plane_state, 278 crtc_state, 279 DRM_PLANE_NO_SCALING, 280 DRM_PLANE_NO_SCALING, 281 true, true); 282 } 283 284 static void mtk_plane_atomic_disable(struct drm_plane *plane, 285 struct drm_atomic_state *state) 286 { 287 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, 288 plane); 289 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state); 290 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, 291 plane); 292 293 mtk_plane_state->pending.enable = false; 294 wmb(); /* Make sure the above parameter is set before update */ 295 mtk_plane_state->pending.dirty = true; 296 297 if (old_state && old_state->crtc) 298 mtk_crtc_plane_disable(old_state->crtc, plane); 299 } 300 301 static void mtk_plane_atomic_update(struct drm_plane *plane, 302 struct drm_atomic_state *state) 303 { 304 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, 305 plane); 306 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state); 307 308 if (!new_state->crtc || WARN_ON(!new_state->fb)) 309 return; 310 311 if (!new_state->visible) { 312 mtk_plane_atomic_disable(plane, state); 313 return; 314 } 315 316 mtk_plane_update_new_state(new_state, mtk_plane_state); 317 wmb(); /* Make sure the above parameters are set before update */ 318 mtk_plane_state->pending.dirty = true; 319 } 320 321 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = { 322 .atomic_check = mtk_plane_atomic_check, 323 .atomic_update = mtk_plane_atomic_update, 324 .atomic_disable = mtk_plane_atomic_disable, 325 .atomic_async_update = mtk_plane_atomic_async_update, 326 .atomic_async_check = mtk_plane_atomic_async_check, 327 }; 328 329 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane, 330 unsigned long possible_crtcs, enum drm_plane_type type, 331 unsigned int supported_rotations, const u32 blend_modes, 332 const u32 *formats, size_t num_formats, 333 bool supports_afbc, unsigned int plane_idx) 334 { 335 int err; 336 337 if (!formats || !num_formats) { 338 DRM_ERROR("no formats for plane\n"); 339 return -EINVAL; 340 } 341 342 err = drm_universal_plane_init(dev, plane, possible_crtcs, 343 &mtk_plane_funcs, formats, 344 num_formats, 345 supports_afbc ? modifiers : NULL, 346 type, NULL); 347 if (err) { 348 DRM_ERROR("failed to initialize plane\n"); 349 return err; 350 } 351 352 /* 353 * The hardware does not support repositioning planes by muxing: their 354 * Z-position is infact fixed and the only way to change the actual 355 * order is to swap the contents of the entire register set of one 356 * overlay with another, which may be more expensive than desired. 357 * 358 * With no repositioning, the caller of this function guarantees that 359 * the plane_idx is correct. This means that, for example, the PRIMARY 360 * plane fed to this function will always have plane_idx zero. 361 */ 362 err = drm_plane_create_zpos_immutable_property(plane, plane_idx); 363 if (err) { 364 DRM_ERROR("Failed to create zpos property for plane %u\n", plane_idx); 365 return err; 366 } 367 368 if (supported_rotations) { 369 err = drm_plane_create_rotation_property(plane, 370 DRM_MODE_ROTATE_0, 371 supported_rotations); 372 if (err) 373 DRM_INFO("Create rotation property failed\n"); 374 } 375 376 err = drm_plane_create_alpha_property(plane); 377 if (err) 378 DRM_ERROR("failed to create property: alpha\n"); 379 380 if (blend_modes) { 381 err = drm_plane_create_blend_mode_property(plane, blend_modes); 382 if (err) 383 DRM_ERROR("failed to create property: blend_mode\n"); 384 } 385 386 drm_plane_helper_add(plane, &mtk_plane_helper_funcs); 387 388 return 0; 389 } 390