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
15 #include "mtk_drm_crtc.h"
16 #include "mtk_drm_ddp_comp.h"
17 #include "mtk_drm_drv.h"
18 #include "mtk_drm_gem.h"
19 #include "mtk_drm_plane.h"
20
21 static const u32 formats[] = {
22 DRM_FORMAT_XRGB8888,
23 DRM_FORMAT_ARGB8888,
24 DRM_FORMAT_BGRX8888,
25 DRM_FORMAT_BGRA8888,
26 DRM_FORMAT_ABGR8888,
27 DRM_FORMAT_XBGR8888,
28 DRM_FORMAT_RGB888,
29 DRM_FORMAT_BGR888,
30 DRM_FORMAT_RGB565,
31 DRM_FORMAT_UYVY,
32 DRM_FORMAT_YUYV,
33 };
34
mtk_plane_reset(struct drm_plane * plane)35 static void mtk_plane_reset(struct drm_plane *plane)
36 {
37 struct mtk_plane_state *state;
38
39 if (plane->state) {
40 __drm_atomic_helper_plane_destroy_state(plane->state);
41
42 state = to_mtk_plane_state(plane->state);
43 memset(state, 0, sizeof(*state));
44 } else {
45 state = kzalloc(sizeof(*state), GFP_KERNEL);
46 if (!state)
47 return;
48 }
49
50 __drm_atomic_helper_plane_reset(plane, &state->base);
51
52 state->base.plane = plane;
53 state->pending.format = DRM_FORMAT_RGB565;
54 }
55
mtk_plane_duplicate_state(struct drm_plane * plane)56 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
57 {
58 struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
59 struct mtk_plane_state *state;
60
61 state = kmalloc(sizeof(*state), GFP_KERNEL);
62 if (!state)
63 return NULL;
64
65 __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
66
67 WARN_ON(state->base.plane != plane);
68
69 state->pending = old_state->pending;
70
71 return &state->base;
72 }
73
mtk_drm_plane_destroy_state(struct drm_plane * plane,struct drm_plane_state * state)74 static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
75 struct drm_plane_state *state)
76 {
77 __drm_atomic_helper_plane_destroy_state(state);
78 kfree(to_mtk_plane_state(state));
79 }
80
mtk_plane_atomic_async_check(struct drm_plane * plane,struct drm_atomic_state * state)81 static int mtk_plane_atomic_async_check(struct drm_plane *plane,
82 struct drm_atomic_state *state)
83 {
84 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
85 plane);
86 struct drm_crtc_state *crtc_state;
87 int ret;
88
89 if (plane != new_plane_state->crtc->cursor)
90 return -EINVAL;
91
92 if (!plane->state)
93 return -EINVAL;
94
95 if (!plane->state->fb)
96 return -EINVAL;
97
98 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
99 to_mtk_plane_state(new_plane_state));
100 if (ret)
101 return ret;
102
103 if (state)
104 crtc_state = drm_atomic_get_existing_crtc_state(state,
105 new_plane_state->crtc);
106 else /* Special case for asynchronous cursor updates. */
107 crtc_state = new_plane_state->crtc->state;
108
109 return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
110 DRM_PLANE_NO_SCALING,
111 DRM_PLANE_NO_SCALING,
112 true, true);
113 }
114
mtk_plane_update_new_state(struct drm_plane_state * new_state,struct mtk_plane_state * mtk_plane_state)115 static void mtk_plane_update_new_state(struct drm_plane_state *new_state,
116 struct mtk_plane_state *mtk_plane_state)
117 {
118 struct drm_framebuffer *fb = new_state->fb;
119 struct drm_gem_object *gem;
120 struct mtk_drm_gem_obj *mtk_gem;
121 unsigned int pitch, format;
122 dma_addr_t addr;
123
124 gem = fb->obj[0];
125 mtk_gem = to_mtk_gem_obj(gem);
126 addr = mtk_gem->dma_addr;
127 pitch = fb->pitches[0];
128 format = fb->format->format;
129
130 addr += (new_state->src.x1 >> 16) * fb->format->cpp[0];
131 addr += (new_state->src.y1 >> 16) * pitch;
132
133 mtk_plane_state->pending.enable = true;
134 mtk_plane_state->pending.pitch = pitch;
135 mtk_plane_state->pending.format = format;
136 mtk_plane_state->pending.addr = addr;
137 mtk_plane_state->pending.x = new_state->dst.x1;
138 mtk_plane_state->pending.y = new_state->dst.y1;
139 mtk_plane_state->pending.width = drm_rect_width(&new_state->dst);
140 mtk_plane_state->pending.height = drm_rect_height(&new_state->dst);
141 mtk_plane_state->pending.rotation = new_state->rotation;
142 mtk_plane_state->pending.color_encoding = new_state->color_encoding;
143 }
144
mtk_plane_atomic_async_update(struct drm_plane * plane,struct drm_atomic_state * state)145 static void mtk_plane_atomic_async_update(struct drm_plane *plane,
146 struct drm_atomic_state *state)
147 {
148 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
149 plane);
150 struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state);
151
152 plane->state->crtc_x = new_state->crtc_x;
153 plane->state->crtc_y = new_state->crtc_y;
154 plane->state->crtc_h = new_state->crtc_h;
155 plane->state->crtc_w = new_state->crtc_w;
156 plane->state->src_x = new_state->src_x;
157 plane->state->src_y = new_state->src_y;
158 plane->state->src_h = new_state->src_h;
159 plane->state->src_w = new_state->src_w;
160 swap(plane->state->fb, new_state->fb);
161
162 mtk_plane_update_new_state(new_state, new_plane_state);
163 wmb(); /* Make sure the above parameters are set before update */
164 new_plane_state->pending.async_dirty = true;
165 mtk_drm_crtc_async_update(new_state->crtc, plane, state);
166 }
167
168 static const struct drm_plane_funcs mtk_plane_funcs = {
169 .update_plane = drm_atomic_helper_update_plane,
170 .disable_plane = drm_atomic_helper_disable_plane,
171 .destroy = drm_plane_cleanup,
172 .reset = mtk_plane_reset,
173 .atomic_duplicate_state = mtk_plane_duplicate_state,
174 .atomic_destroy_state = mtk_drm_plane_destroy_state,
175 };
176
mtk_plane_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)177 static int mtk_plane_atomic_check(struct drm_plane *plane,
178 struct drm_atomic_state *state)
179 {
180 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
181 plane);
182 struct drm_framebuffer *fb = new_plane_state->fb;
183 struct drm_crtc_state *crtc_state;
184 int ret;
185
186 if (!fb)
187 return 0;
188
189 if (WARN_ON(!new_plane_state->crtc))
190 return 0;
191
192 ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
193 to_mtk_plane_state(new_plane_state));
194 if (ret)
195 return ret;
196
197 crtc_state = drm_atomic_get_crtc_state(state,
198 new_plane_state->crtc);
199 if (IS_ERR(crtc_state))
200 return PTR_ERR(crtc_state);
201
202 return drm_atomic_helper_check_plane_state(new_plane_state,
203 crtc_state,
204 DRM_PLANE_NO_SCALING,
205 DRM_PLANE_NO_SCALING,
206 true, true);
207 }
208
mtk_plane_atomic_disable(struct drm_plane * plane,struct drm_atomic_state * state)209 static void mtk_plane_atomic_disable(struct drm_plane *plane,
210 struct drm_atomic_state *state)
211 {
212 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
213 plane);
214 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
215 mtk_plane_state->pending.enable = false;
216 wmb(); /* Make sure the above parameter is set before update */
217 mtk_plane_state->pending.dirty = true;
218 }
219
mtk_plane_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)220 static void mtk_plane_atomic_update(struct drm_plane *plane,
221 struct drm_atomic_state *state)
222 {
223 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
224 plane);
225 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
226
227 if (!new_state->crtc || WARN_ON(!new_state->fb))
228 return;
229
230 if (!new_state->visible) {
231 mtk_plane_atomic_disable(plane, state);
232 return;
233 }
234
235 mtk_plane_update_new_state(new_state, mtk_plane_state);
236 wmb(); /* Make sure the above parameters are set before update */
237 mtk_plane_state->pending.dirty = true;
238 }
239
240 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
241 .atomic_check = mtk_plane_atomic_check,
242 .atomic_update = mtk_plane_atomic_update,
243 .atomic_disable = mtk_plane_atomic_disable,
244 .atomic_async_update = mtk_plane_atomic_async_update,
245 .atomic_async_check = mtk_plane_atomic_async_check,
246 };
247
mtk_plane_init(struct drm_device * dev,struct drm_plane * plane,unsigned long possible_crtcs,enum drm_plane_type type,unsigned int supported_rotations)248 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
249 unsigned long possible_crtcs, enum drm_plane_type type,
250 unsigned int supported_rotations)
251 {
252 int err;
253
254 err = drm_universal_plane_init(dev, plane, possible_crtcs,
255 &mtk_plane_funcs, formats,
256 ARRAY_SIZE(formats), NULL, type, NULL);
257 if (err) {
258 DRM_ERROR("failed to initialize plane\n");
259 return err;
260 }
261
262 if (supported_rotations & ~DRM_MODE_ROTATE_0) {
263 err = drm_plane_create_rotation_property(plane,
264 DRM_MODE_ROTATE_0,
265 supported_rotations);
266 if (err)
267 DRM_INFO("Create rotation property failed\n");
268 }
269
270 drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
271
272 return 0;
273 }
274