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