1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
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>
16 #include "mtk_drm_crtc.h"
17 #include "mtk_drm_ddp_comp.h"
18 #include "mtk_drm_drv.h"
19 #include "mtk_drm_gem.h"
20 #include "mtk_drm_plane.h"
22 static const u32 formats[] = {
36 static const u64 modifiers[] = {
37 DRM_FORMAT_MOD_LINEAR,
38 DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_32x8 |
39 AFBC_FORMAT_MOD_SPLIT |
40 AFBC_FORMAT_MOD_SPARSE),
41 DRM_FORMAT_MOD_INVALID,
44 static void mtk_plane_reset(struct drm_plane *plane)
46 struct mtk_plane_state *state;
49 __drm_atomic_helper_plane_destroy_state(plane->state);
51 state = to_mtk_plane_state(plane->state);
52 memset(state, 0, sizeof(*state));
54 state = kzalloc(sizeof(*state), GFP_KERNEL);
59 __drm_atomic_helper_plane_reset(plane, &state->base);
61 state->base.plane = plane;
62 state->pending.format = DRM_FORMAT_RGB565;
63 state->pending.modifier = DRM_FORMAT_MOD_LINEAR;
66 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
68 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
69 struct mtk_plane_state *state;
71 state = kmalloc(sizeof(*state), GFP_KERNEL);
75 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
77 WARN_ON(state->base.plane != plane);
79 state->pending = old_state->pending;
84 static bool mtk_plane_format_mod_supported(struct drm_plane *plane,
88 if (modifier == DRM_FORMAT_MOD_LINEAR)
91 if (modifier != DRM_FORMAT_MOD_ARM_AFBC(
92 AFBC_FORMAT_MOD_BLOCK_SIZE_32x8 |
93 AFBC_FORMAT_MOD_SPLIT |
94 AFBC_FORMAT_MOD_SPARSE))
97 if (format != DRM_FORMAT_XRGB8888 &&
98 format != DRM_FORMAT_ARGB8888 &&
99 format != DRM_FORMAT_BGRX8888 &&
100 format != DRM_FORMAT_BGRA8888 &&
101 format != DRM_FORMAT_ABGR8888 &&
102 format != DRM_FORMAT_XBGR8888 &&
103 format != DRM_FORMAT_RGB888 &&
104 format != DRM_FORMAT_BGR888)
110 static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
111 struct drm_plane_state *state)
113 __drm_atomic_helper_plane_destroy_state(state);
114 kfree(to_mtk_plane_state(state));
117 static int mtk_plane_atomic_async_check(struct drm_plane *plane,
118 struct drm_atomic_state *state)
120 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
122 struct drm_crtc_state *crtc_state;
125 if (plane != new_plane_state->crtc->cursor)
131 if (!plane->state->fb)
134 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
135 to_mtk_plane_state(new_plane_state));
140 crtc_state = drm_atomic_get_existing_crtc_state(state,
141 new_plane_state->crtc);
142 else /* Special case for asynchronous cursor updates. */
143 crtc_state = new_plane_state->crtc->state;
145 return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
146 DRM_PLANE_NO_SCALING,
147 DRM_PLANE_NO_SCALING,
151 static void mtk_plane_update_new_state(struct drm_plane_state *new_state,
152 struct mtk_plane_state *mtk_plane_state)
154 struct drm_framebuffer *fb = new_state->fb;
155 struct drm_gem_object *gem;
156 struct mtk_drm_gem_obj *mtk_gem;
157 unsigned int pitch, format;
160 dma_addr_t hdr_addr = 0;
161 unsigned int hdr_pitch = 0;
164 mtk_gem = to_mtk_gem_obj(gem);
165 addr = mtk_gem->dma_addr;
166 pitch = fb->pitches[0];
167 format = fb->format->format;
168 modifier = fb->modifier;
170 if (modifier == DRM_FORMAT_MOD_LINEAR) {
171 addr += (new_state->src.x1 >> 16) * fb->format->cpp[0];
172 addr += (new_state->src.y1 >> 16) * pitch;
174 int width_in_blocks = ALIGN(fb->width, AFBC_DATA_BLOCK_WIDTH)
175 / AFBC_DATA_BLOCK_WIDTH;
176 int height_in_blocks = ALIGN(fb->height, AFBC_DATA_BLOCK_HEIGHT)
177 / AFBC_DATA_BLOCK_HEIGHT;
178 int x_offset_in_blocks = (new_state->src.x1 >> 16) / AFBC_DATA_BLOCK_WIDTH;
179 int y_offset_in_blocks = (new_state->src.y1 >> 16) / AFBC_DATA_BLOCK_HEIGHT;
182 hdr_pitch = width_in_blocks * AFBC_HEADER_BLOCK_SIZE;
183 pitch = width_in_blocks * AFBC_DATA_BLOCK_WIDTH *
184 AFBC_DATA_BLOCK_HEIGHT * fb->format->cpp[0];
186 hdr_size = ALIGN(hdr_pitch * height_in_blocks, AFBC_HEADER_ALIGNMENT);
188 hdr_addr = addr + hdr_pitch * y_offset_in_blocks +
189 AFBC_HEADER_BLOCK_SIZE * x_offset_in_blocks;
190 /* The data plane is offset by 1 additional block. */
191 addr = addr + hdr_size +
192 pitch * y_offset_in_blocks +
193 AFBC_DATA_BLOCK_WIDTH * AFBC_DATA_BLOCK_HEIGHT *
194 fb->format->cpp[0] * (x_offset_in_blocks + 1);
197 mtk_plane_state->pending.enable = true;
198 mtk_plane_state->pending.pitch = pitch;
199 mtk_plane_state->pending.hdr_pitch = hdr_pitch;
200 mtk_plane_state->pending.format = format;
201 mtk_plane_state->pending.modifier = modifier;
202 mtk_plane_state->pending.addr = addr;
203 mtk_plane_state->pending.hdr_addr = hdr_addr;
204 mtk_plane_state->pending.x = new_state->dst.x1;
205 mtk_plane_state->pending.y = new_state->dst.y1;
206 mtk_plane_state->pending.width = drm_rect_width(&new_state->dst);
207 mtk_plane_state->pending.height = drm_rect_height(&new_state->dst);
208 mtk_plane_state->pending.rotation = new_state->rotation;
209 mtk_plane_state->pending.color_encoding = new_state->color_encoding;
212 static void mtk_plane_atomic_async_update(struct drm_plane *plane,
213 struct drm_atomic_state *state)
215 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
217 struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state);
219 plane->state->crtc_x = new_state->crtc_x;
220 plane->state->crtc_y = new_state->crtc_y;
221 plane->state->crtc_h = new_state->crtc_h;
222 plane->state->crtc_w = new_state->crtc_w;
223 plane->state->src_x = new_state->src_x;
224 plane->state->src_y = new_state->src_y;
225 plane->state->src_h = new_state->src_h;
226 plane->state->src_w = new_state->src_w;
227 swap(plane->state->fb, new_state->fb);
229 mtk_plane_update_new_state(new_state, new_plane_state);
230 wmb(); /* Make sure the above parameters are set before update */
231 new_plane_state->pending.async_dirty = true;
232 mtk_drm_crtc_async_update(new_state->crtc, plane, state);
235 static const struct drm_plane_funcs mtk_plane_funcs = {
236 .update_plane = drm_atomic_helper_update_plane,
237 .disable_plane = drm_atomic_helper_disable_plane,
238 .destroy = drm_plane_cleanup,
239 .reset = mtk_plane_reset,
240 .atomic_duplicate_state = mtk_plane_duplicate_state,
241 .atomic_destroy_state = mtk_drm_plane_destroy_state,
242 .format_mod_supported = mtk_plane_format_mod_supported,
245 static int mtk_plane_atomic_check(struct drm_plane *plane,
246 struct drm_atomic_state *state)
248 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
250 struct drm_framebuffer *fb = new_plane_state->fb;
251 struct drm_crtc_state *crtc_state;
257 if (WARN_ON(!new_plane_state->crtc))
260 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
261 to_mtk_plane_state(new_plane_state));
265 crtc_state = drm_atomic_get_crtc_state(state,
266 new_plane_state->crtc);
267 if (IS_ERR(crtc_state))
268 return PTR_ERR(crtc_state);
270 return drm_atomic_helper_check_plane_state(new_plane_state,
272 DRM_PLANE_NO_SCALING,
273 DRM_PLANE_NO_SCALING,
277 static void mtk_plane_atomic_disable(struct drm_plane *plane,
278 struct drm_atomic_state *state)
280 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
282 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
283 mtk_plane_state->pending.enable = false;
284 wmb(); /* Make sure the above parameter is set before update */
285 mtk_plane_state->pending.dirty = true;
288 static void mtk_plane_atomic_update(struct drm_plane *plane,
289 struct drm_atomic_state *state)
291 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
293 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
295 if (!new_state->crtc || WARN_ON(!new_state->fb))
298 if (!new_state->visible) {
299 mtk_plane_atomic_disable(plane, state);
303 mtk_plane_update_new_state(new_state, mtk_plane_state);
304 wmb(); /* Make sure the above parameters are set before update */
305 mtk_plane_state->pending.dirty = true;
308 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
309 .atomic_check = mtk_plane_atomic_check,
310 .atomic_update = mtk_plane_atomic_update,
311 .atomic_disable = mtk_plane_atomic_disable,
312 .atomic_async_update = mtk_plane_atomic_async_update,
313 .atomic_async_check = mtk_plane_atomic_async_check,
316 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
317 unsigned long possible_crtcs, enum drm_plane_type type,
318 unsigned int supported_rotations)
322 err = drm_universal_plane_init(dev, plane, possible_crtcs,
323 &mtk_plane_funcs, formats,
324 ARRAY_SIZE(formats), modifiers, type, NULL);
326 DRM_ERROR("failed to initialize plane\n");
330 if (supported_rotations & ~DRM_MODE_ROTATE_0) {
331 err = drm_plane_create_rotation_property(plane,
333 supported_rotations);
335 DRM_INFO("Create rotation property failed\n");
338 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);