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