]> Git Repo - linux.git/blob - drivers/gpu/drm/mediatek/mtk_drm_plane.c
Merge tag 'wireless-next-2023-10-06' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / gpu / drm / mediatek / mtk_drm_plane.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 MediaTek Inc.
4  * Author: CK Hu <[email protected]>
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
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_drm_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)
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_drm_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_existing_crtc_state(state, new_plane_state->crtc);
126
127         return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
128                                                    DRM_PLANE_NO_SCALING,
129                                                    DRM_PLANE_NO_SCALING,
130                                                    true, true);
131 }
132
133 static void mtk_plane_update_new_state(struct drm_plane_state *new_state,
134                                        struct mtk_plane_state *mtk_plane_state)
135 {
136         struct drm_framebuffer *fb = new_state->fb;
137         struct drm_gem_object *gem;
138         struct mtk_drm_gem_obj *mtk_gem;
139         unsigned int pitch, format;
140         u64 modifier;
141         dma_addr_t addr;
142         dma_addr_t hdr_addr = 0;
143         unsigned int hdr_pitch = 0;
144
145         gem = fb->obj[0];
146         mtk_gem = to_mtk_gem_obj(gem);
147         addr = mtk_gem->dma_addr;
148         pitch = fb->pitches[0];
149         format = fb->format->format;
150         modifier = fb->modifier;
151
152         if (modifier == DRM_FORMAT_MOD_LINEAR) {
153                 addr += (new_state->src.x1 >> 16) * fb->format->cpp[0];
154                 addr += (new_state->src.y1 >> 16) * pitch;
155         } else {
156                 int width_in_blocks = ALIGN(fb->width, AFBC_DATA_BLOCK_WIDTH)
157                                       / AFBC_DATA_BLOCK_WIDTH;
158                 int height_in_blocks = ALIGN(fb->height, AFBC_DATA_BLOCK_HEIGHT)
159                                        / AFBC_DATA_BLOCK_HEIGHT;
160                 int x_offset_in_blocks = (new_state->src.x1 >> 16) / AFBC_DATA_BLOCK_WIDTH;
161                 int y_offset_in_blocks = (new_state->src.y1 >> 16) / AFBC_DATA_BLOCK_HEIGHT;
162                 int hdr_size;
163
164                 hdr_pitch = width_in_blocks * AFBC_HEADER_BLOCK_SIZE;
165                 pitch = width_in_blocks * AFBC_DATA_BLOCK_WIDTH *
166                         AFBC_DATA_BLOCK_HEIGHT * fb->format->cpp[0];
167
168                 hdr_size = ALIGN(hdr_pitch * height_in_blocks, AFBC_HEADER_ALIGNMENT);
169
170                 hdr_addr = addr + hdr_pitch * y_offset_in_blocks +
171                            AFBC_HEADER_BLOCK_SIZE * x_offset_in_blocks;
172                 /* The data plane is offset by 1 additional block. */
173                 addr = addr + hdr_size +
174                        pitch * y_offset_in_blocks +
175                        AFBC_DATA_BLOCK_WIDTH * AFBC_DATA_BLOCK_HEIGHT *
176                        fb->format->cpp[0] * (x_offset_in_blocks + 1);
177         }
178
179         mtk_plane_state->pending.enable = true;
180         mtk_plane_state->pending.pitch = pitch;
181         mtk_plane_state->pending.hdr_pitch = hdr_pitch;
182         mtk_plane_state->pending.format = format;
183         mtk_plane_state->pending.modifier = modifier;
184         mtk_plane_state->pending.addr = addr;
185         mtk_plane_state->pending.hdr_addr = hdr_addr;
186         mtk_plane_state->pending.x = new_state->dst.x1;
187         mtk_plane_state->pending.y = new_state->dst.y1;
188         mtk_plane_state->pending.width = drm_rect_width(&new_state->dst);
189         mtk_plane_state->pending.height = drm_rect_height(&new_state->dst);
190         mtk_plane_state->pending.rotation = new_state->rotation;
191         mtk_plane_state->pending.color_encoding = new_state->color_encoding;
192 }
193
194 static void mtk_plane_atomic_async_update(struct drm_plane *plane,
195                                           struct drm_atomic_state *state)
196 {
197         struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
198                                                                            plane);
199         struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state);
200
201         plane->state->crtc_x = new_state->crtc_x;
202         plane->state->crtc_y = new_state->crtc_y;
203         plane->state->crtc_h = new_state->crtc_h;
204         plane->state->crtc_w = new_state->crtc_w;
205         plane->state->src_x = new_state->src_x;
206         plane->state->src_y = new_state->src_y;
207         plane->state->src_h = new_state->src_h;
208         plane->state->src_w = new_state->src_w;
209         swap(plane->state->fb, new_state->fb);
210
211         mtk_plane_update_new_state(new_state, new_plane_state);
212         wmb(); /* Make sure the above parameters are set before update */
213         new_plane_state->pending.async_dirty = true;
214         mtk_drm_crtc_async_update(new_state->crtc, plane, state);
215 }
216
217 static const struct drm_plane_funcs mtk_plane_funcs = {
218         .update_plane = drm_atomic_helper_update_plane,
219         .disable_plane = drm_atomic_helper_disable_plane,
220         .destroy = drm_plane_cleanup,
221         .reset = mtk_plane_reset,
222         .atomic_duplicate_state = mtk_plane_duplicate_state,
223         .atomic_destroy_state = mtk_drm_plane_destroy_state,
224         .format_mod_supported = mtk_plane_format_mod_supported,
225 };
226
227 static int mtk_plane_atomic_check(struct drm_plane *plane,
228                                   struct drm_atomic_state *state)
229 {
230         struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
231                                                                                  plane);
232         struct drm_framebuffer *fb = new_plane_state->fb;
233         struct drm_crtc_state *crtc_state;
234         int ret;
235
236         if (!fb)
237                 return 0;
238
239         if (WARN_ON(!new_plane_state->crtc))
240                 return 0;
241
242         ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
243                                        to_mtk_plane_state(new_plane_state));
244         if (ret)
245                 return ret;
246
247         crtc_state = drm_atomic_get_crtc_state(state,
248                                                new_plane_state->crtc);
249         if (IS_ERR(crtc_state))
250                 return PTR_ERR(crtc_state);
251
252         return drm_atomic_helper_check_plane_state(new_plane_state,
253                                                    crtc_state,
254                                                    DRM_PLANE_NO_SCALING,
255                                                    DRM_PLANE_NO_SCALING,
256                                                    true, true);
257 }
258
259 static void mtk_plane_atomic_disable(struct drm_plane *plane,
260                                      struct drm_atomic_state *state)
261 {
262         struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
263                                                                            plane);
264         struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
265         mtk_plane_state->pending.enable = false;
266         wmb(); /* Make sure the above parameter is set before update */
267         mtk_plane_state->pending.dirty = true;
268 }
269
270 static void mtk_plane_atomic_update(struct drm_plane *plane,
271                                     struct drm_atomic_state *state)
272 {
273         struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
274                                                                            plane);
275         struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
276
277         if (!new_state->crtc || WARN_ON(!new_state->fb))
278                 return;
279
280         if (!new_state->visible) {
281                 mtk_plane_atomic_disable(plane, state);
282                 return;
283         }
284
285         mtk_plane_update_new_state(new_state, mtk_plane_state);
286         wmb(); /* Make sure the above parameters are set before update */
287         mtk_plane_state->pending.dirty = true;
288 }
289
290 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
291         .atomic_check = mtk_plane_atomic_check,
292         .atomic_update = mtk_plane_atomic_update,
293         .atomic_disable = mtk_plane_atomic_disable,
294         .atomic_async_update = mtk_plane_atomic_async_update,
295         .atomic_async_check = mtk_plane_atomic_async_check,
296 };
297
298 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
299                    unsigned long possible_crtcs, enum drm_plane_type type,
300                    unsigned int supported_rotations, const u32 *formats,
301                    size_t num_formats)
302 {
303         int err;
304
305         if (!formats || !num_formats) {
306                 DRM_ERROR("no formats for plane\n");
307                 return -EINVAL;
308         }
309
310         err = drm_universal_plane_init(dev, plane, possible_crtcs,
311                                        &mtk_plane_funcs, formats,
312                                        num_formats, modifiers, type, NULL);
313         if (err) {
314                 DRM_ERROR("failed to initialize plane\n");
315                 return err;
316         }
317
318         if (supported_rotations & ~DRM_MODE_ROTATE_0) {
319                 err = drm_plane_create_rotation_property(plane,
320                                                          DRM_MODE_ROTATE_0,
321                                                          supported_rotations);
322                 if (err)
323                         DRM_INFO("Create rotation property failed\n");
324         }
325
326         drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
327
328         return 0;
329 }
This page took 0.047323 seconds and 4 git commands to generate.