Loading...
Note: File does not exist in v3.1.
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_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"
21
22static const u32 formats[] = {
23 DRM_FORMAT_XRGB8888,
24 DRM_FORMAT_ARGB8888,
25 DRM_FORMAT_BGRX8888,
26 DRM_FORMAT_BGRA8888,
27 DRM_FORMAT_ABGR8888,
28 DRM_FORMAT_XBGR8888,
29 DRM_FORMAT_RGB888,
30 DRM_FORMAT_BGR888,
31 DRM_FORMAT_RGB565,
32 DRM_FORMAT_UYVY,
33 DRM_FORMAT_YUYV,
34};
35
36static 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,
42};
43
44static void mtk_plane_reset(struct drm_plane *plane)
45{
46 struct mtk_plane_state *state;
47
48 if (plane->state) {
49 __drm_atomic_helper_plane_destroy_state(plane->state);
50
51 state = to_mtk_plane_state(plane->state);
52 memset(state, 0, sizeof(*state));
53 } else {
54 state = kzalloc(sizeof(*state), GFP_KERNEL);
55 if (!state)
56 return;
57 }
58
59 __drm_atomic_helper_plane_reset(plane, &state->base);
60
61 state->base.plane = plane;
62 state->pending.format = DRM_FORMAT_RGB565;
63 state->pending.modifier = DRM_FORMAT_MOD_LINEAR;
64}
65
66static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
67{
68 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
69 struct mtk_plane_state *state;
70
71 state = kmalloc(sizeof(*state), GFP_KERNEL);
72 if (!state)
73 return NULL;
74
75 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
76
77 WARN_ON(state->base.plane != plane);
78
79 state->pending = old_state->pending;
80
81 return &state->base;
82}
83
84static bool mtk_plane_format_mod_supported(struct drm_plane *plane,
85 uint32_t format,
86 uint64_t modifier)
87{
88 if (modifier == DRM_FORMAT_MOD_LINEAR)
89 return true;
90
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))
95 return false;
96
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)
105 return false;
106
107 return true;
108}
109
110static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
111 struct drm_plane_state *state)
112{
113 __drm_atomic_helper_plane_destroy_state(state);
114 kfree(to_mtk_plane_state(state));
115}
116
117static int mtk_plane_atomic_async_check(struct drm_plane *plane,
118 struct drm_atomic_state *state)
119{
120 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
121 plane);
122 struct drm_crtc_state *crtc_state;
123 int ret;
124
125 if (plane != new_plane_state->crtc->cursor)
126 return -EINVAL;
127
128 if (!plane->state)
129 return -EINVAL;
130
131 if (!plane->state->fb)
132 return -EINVAL;
133
134 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
135 to_mtk_plane_state(new_plane_state));
136 if (ret)
137 return ret;
138
139 if (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;
144
145 return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
146 DRM_PLANE_NO_SCALING,
147 DRM_PLANE_NO_SCALING,
148 true, true);
149}
150
151static void mtk_plane_update_new_state(struct drm_plane_state *new_state,
152 struct mtk_plane_state *mtk_plane_state)
153{
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;
158 u64 modifier;
159 dma_addr_t addr;
160 dma_addr_t hdr_addr = 0;
161 unsigned int hdr_pitch = 0;
162
163 gem = fb->obj[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;
169
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;
173 } else {
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;
180 int hdr_size;
181
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];
185
186 hdr_size = ALIGN(hdr_pitch * height_in_blocks, AFBC_HEADER_ALIGNMENT);
187
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);
195 }
196
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;
210}
211
212static void mtk_plane_atomic_async_update(struct drm_plane *plane,
213 struct drm_atomic_state *state)
214{
215 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
216 plane);
217 struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state);
218
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);
228
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);
233}
234
235static 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,
243};
244
245static int mtk_plane_atomic_check(struct drm_plane *plane,
246 struct drm_atomic_state *state)
247{
248 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
249 plane);
250 struct drm_framebuffer *fb = new_plane_state->fb;
251 struct drm_crtc_state *crtc_state;
252 int ret;
253
254 if (!fb)
255 return 0;
256
257 if (WARN_ON(!new_plane_state->crtc))
258 return 0;
259
260 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
261 to_mtk_plane_state(new_plane_state));
262 if (ret)
263 return ret;
264
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);
269
270 return drm_atomic_helper_check_plane_state(new_plane_state,
271 crtc_state,
272 DRM_PLANE_NO_SCALING,
273 DRM_PLANE_NO_SCALING,
274 true, true);
275}
276
277static void mtk_plane_atomic_disable(struct drm_plane *plane,
278 struct drm_atomic_state *state)
279{
280 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
281 plane);
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;
286}
287
288static void mtk_plane_atomic_update(struct drm_plane *plane,
289 struct drm_atomic_state *state)
290{
291 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
292 plane);
293 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
294
295 if (!new_state->crtc || WARN_ON(!new_state->fb))
296 return;
297
298 if (!new_state->visible) {
299 mtk_plane_atomic_disable(plane, state);
300 return;
301 }
302
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;
306}
307
308static 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,
314};
315
316int 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)
319{
320 int err;
321
322 err = drm_universal_plane_init(dev, plane, possible_crtcs,
323 &mtk_plane_funcs, formats,
324 ARRAY_SIZE(formats), modifiers, type, NULL);
325 if (err) {
326 DRM_ERROR("failed to initialize plane\n");
327 return err;
328 }
329
330 if (supported_rotations & ~DRM_MODE_ROTATE_0) {
331 err = drm_plane_create_rotation_property(plane,
332 DRM_MODE_ROTATE_0,
333 supported_rotations);
334 if (err)
335 DRM_INFO("Create rotation property failed\n");
336 }
337
338 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
339
340 return 0;
341}