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