xref: /linux/drivers/gpu/drm/mediatek/mtk_plane.c (revision e811c33b1f137be26a20444b79db8cbc1fca1c89)
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_crtc.h"
17 #include "mtk_ddp_comp.h"
18 #include "mtk_drm_drv.h"
19 #include "mtk_gem.h"
20 #include "mtk_plane.h"
21 
22 static const u64 modifiers[] = {
23 	DRM_FORMAT_MOD_LINEAR,
24 	DRM_FORMAT_MOD_INVALID,
25 };
26 
mtk_plane_reset(struct drm_plane * plane)27 static void mtk_plane_reset(struct drm_plane *plane)
28 {
29 	struct mtk_plane_state *state;
30 
31 	if (plane->state) {
32 		__drm_atomic_helper_plane_destroy_state(plane->state);
33 
34 		state = to_mtk_plane_state(plane->state);
35 		memset(state, 0, sizeof(*state));
36 	} else {
37 		state = kzalloc(sizeof(*state), GFP_KERNEL);
38 		if (!state)
39 			return;
40 	}
41 
42 	__drm_atomic_helper_plane_reset(plane, &state->base);
43 
44 	state->base.plane = plane;
45 	state->pending.format = DRM_FORMAT_RGB565;
46 	state->pending.modifier = DRM_FORMAT_MOD_LINEAR;
47 }
48 
mtk_plane_duplicate_state(struct drm_plane * plane)49 static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
50 {
51 	struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
52 	struct mtk_plane_state *state;
53 
54 	state = kmalloc(sizeof(*state), GFP_KERNEL);
55 	if (!state)
56 		return NULL;
57 
58 	__drm_atomic_helper_plane_duplicate_state(plane, &state->base);
59 
60 	WARN_ON(state->base.plane != plane);
61 
62 	state->pending = old_state->pending;
63 
64 	return &state->base;
65 }
66 
mtk_plane_format_mod_supported(struct drm_plane * plane,uint32_t format,uint64_t modifier)67 static bool mtk_plane_format_mod_supported(struct drm_plane *plane,
68 					   uint32_t format,
69 					   uint64_t modifier)
70 {
71 	return modifier == DRM_FORMAT_MOD_LINEAR;
72 }
73 
mtk_plane_destroy_state(struct drm_plane * plane,struct drm_plane_state * state)74 static void mtk_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,bool flip)81 static int mtk_plane_atomic_async_check(struct drm_plane *plane,
82 					struct drm_atomic_state *state, bool flip)
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_crtc_plane_check(new_plane_state->crtc, plane,
99 				   to_mtk_plane_state(new_plane_state));
100 	if (ret)
101 		return ret;
102 
103 	crtc_state = drm_atomic_get_existing_crtc_state(state, new_plane_state->crtc);
104 
105 	return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
106 						   DRM_PLANE_NO_SCALING,
107 						   DRM_PLANE_NO_SCALING,
108 						   true, true);
109 }
110 
mtk_plane_update_new_state(struct drm_plane_state * new_state,struct mtk_plane_state * mtk_plane_state)111 static void mtk_plane_update_new_state(struct drm_plane_state *new_state,
112 				       struct mtk_plane_state *mtk_plane_state)
113 {
114 	struct drm_framebuffer *fb = new_state->fb;
115 	struct drm_gem_object *gem;
116 	struct mtk_gem_obj *mtk_gem;
117 	unsigned int pitch, format;
118 	u64 modifier;
119 	dma_addr_t addr;
120 	dma_addr_t hdr_addr = 0;
121 	unsigned int hdr_pitch = 0;
122 	int offset;
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 	modifier = fb->modifier;
130 
131 	if (modifier == DRM_FORMAT_MOD_LINEAR) {
132 		/*
133 		 * Using dma_addr_t variable to calculate with multiplier of different types,
134 		 * for example: addr += (new_state->src.x1 >> 16) * fb->format->cpp[0];
135 		 * may cause coverity issue with unintentional overflow.
136 		 */
137 		offset = (new_state->src.x1 >> 16) * fb->format->cpp[0];
138 		addr += offset;
139 		offset = (new_state->src.y1 >> 16) * pitch;
140 		addr += offset;
141 	} else {
142 		int width_in_blocks = ALIGN(fb->width, AFBC_DATA_BLOCK_WIDTH)
143 				      / AFBC_DATA_BLOCK_WIDTH;
144 		int height_in_blocks = ALIGN(fb->height, AFBC_DATA_BLOCK_HEIGHT)
145 				       / AFBC_DATA_BLOCK_HEIGHT;
146 		int x_offset_in_blocks = (new_state->src.x1 >> 16) / AFBC_DATA_BLOCK_WIDTH;
147 		int y_offset_in_blocks = (new_state->src.y1 >> 16) / AFBC_DATA_BLOCK_HEIGHT;
148 		int hdr_size, hdr_offset;
149 
150 		hdr_pitch = width_in_blocks * AFBC_HEADER_BLOCK_SIZE;
151 		pitch = width_in_blocks * AFBC_DATA_BLOCK_WIDTH *
152 			AFBC_DATA_BLOCK_HEIGHT * fb->format->cpp[0];
153 
154 		hdr_size = ALIGN(hdr_pitch * height_in_blocks, AFBC_HEADER_ALIGNMENT);
155 		hdr_offset = hdr_pitch * y_offset_in_blocks +
156 			AFBC_HEADER_BLOCK_SIZE * x_offset_in_blocks;
157 
158 		/*
159 		 * Using dma_addr_t variable to calculate with multiplier of different types,
160 		 * for example: addr += hdr_pitch * y_offset_in_blocks;
161 		 * may cause coverity issue with unintentional overflow.
162 		 */
163 		hdr_addr = addr + hdr_offset;
164 
165 		/* The data plane is offset by 1 additional block. */
166 		offset = pitch * y_offset_in_blocks +
167 			 AFBC_DATA_BLOCK_WIDTH * AFBC_DATA_BLOCK_HEIGHT *
168 			 fb->format->cpp[0] * (x_offset_in_blocks + 1);
169 
170 		/*
171 		 * Using dma_addr_t variable to calculate with multiplier of different types,
172 		 * for example: addr += pitch * y_offset_in_blocks;
173 		 * may cause coverity issue with unintentional overflow.
174 		 */
175 		addr = addr + hdr_size + offset;
176 	}
177 
178 	mtk_plane_state->pending.enable = true;
179 	mtk_plane_state->pending.pitch = pitch;
180 	mtk_plane_state->pending.hdr_pitch = hdr_pitch;
181 	mtk_plane_state->pending.format = format;
182 	mtk_plane_state->pending.modifier = modifier;
183 	mtk_plane_state->pending.addr = addr;
184 	mtk_plane_state->pending.hdr_addr = hdr_addr;
185 	mtk_plane_state->pending.x = new_state->dst.x1;
186 	mtk_plane_state->pending.y = new_state->dst.y1;
187 	mtk_plane_state->pending.width = drm_rect_width(&new_state->dst);
188 	mtk_plane_state->pending.height = drm_rect_height(&new_state->dst);
189 	mtk_plane_state->pending.rotation = new_state->rotation;
190 	mtk_plane_state->pending.color_encoding = new_state->color_encoding;
191 }
192 
mtk_plane_atomic_async_update(struct drm_plane * plane,struct drm_atomic_state * state)193 static void mtk_plane_atomic_async_update(struct drm_plane *plane,
194 					  struct drm_atomic_state *state)
195 {
196 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
197 									   plane);
198 	struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state);
199 
200 	plane->state->crtc_x = new_state->crtc_x;
201 	plane->state->crtc_y = new_state->crtc_y;
202 	plane->state->crtc_h = new_state->crtc_h;
203 	plane->state->crtc_w = new_state->crtc_w;
204 	plane->state->src_x = new_state->src_x;
205 	plane->state->src_y = new_state->src_y;
206 	plane->state->src_h = new_state->src_h;
207 	plane->state->src_w = new_state->src_w;
208 	plane->state->dst.x1 = new_state->dst.x1;
209 	plane->state->dst.y1 = new_state->dst.y1;
210 
211 	mtk_plane_update_new_state(new_state, new_plane_state);
212 	swap(plane->state->fb, new_state->fb);
213 	wmb(); /* Make sure the above parameters are set before update */
214 	new_plane_state->pending.async_dirty = true;
215 	mtk_crtc_async_update(new_state->crtc, plane, state);
216 }
217 
218 static const struct drm_plane_funcs mtk_plane_funcs = {
219 	.update_plane = drm_atomic_helper_update_plane,
220 	.disable_plane = drm_atomic_helper_disable_plane,
221 	.destroy = drm_plane_cleanup,
222 	.reset = mtk_plane_reset,
223 	.atomic_duplicate_state = mtk_plane_duplicate_state,
224 	.atomic_destroy_state = mtk_plane_destroy_state,
225 	.format_mod_supported = mtk_plane_format_mod_supported,
226 };
227 
mtk_plane_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)228 static int mtk_plane_atomic_check(struct drm_plane *plane,
229 				  struct drm_atomic_state *state)
230 {
231 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
232 										 plane);
233 	struct drm_framebuffer *fb = new_plane_state->fb;
234 	struct drm_crtc_state *crtc_state;
235 	int ret;
236 
237 	if (!fb)
238 		return 0;
239 
240 	if (WARN_ON(!new_plane_state->crtc))
241 		return 0;
242 
243 	ret = mtk_crtc_plane_check(new_plane_state->crtc, plane,
244 				   to_mtk_plane_state(new_plane_state));
245 	if (ret)
246 		return ret;
247 
248 	crtc_state = drm_atomic_get_crtc_state(state,
249 					       new_plane_state->crtc);
250 	if (IS_ERR(crtc_state))
251 		return PTR_ERR(crtc_state);
252 
253 	return drm_atomic_helper_check_plane_state(new_plane_state,
254 						   crtc_state,
255 						   DRM_PLANE_NO_SCALING,
256 						   DRM_PLANE_NO_SCALING,
257 						   true, true);
258 }
259 
mtk_plane_atomic_disable(struct drm_plane * plane,struct drm_atomic_state * state)260 static void mtk_plane_atomic_disable(struct drm_plane *plane,
261 				     struct drm_atomic_state *state)
262 {
263 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
264 									   plane);
265 	struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
266 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
267 									   plane);
268 
269 	mtk_plane_state->pending.enable = false;
270 	wmb(); /* Make sure the above parameter is set before update */
271 	mtk_plane_state->pending.dirty = true;
272 
273 	if (old_state && old_state->crtc)
274 		mtk_crtc_plane_disable(old_state->crtc, plane);
275 }
276 
mtk_plane_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)277 static void mtk_plane_atomic_update(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 
284 	if (!new_state->crtc || WARN_ON(!new_state->fb))
285 		return;
286 
287 	if (!new_state->visible) {
288 		mtk_plane_atomic_disable(plane, state);
289 		return;
290 	}
291 
292 	mtk_plane_update_new_state(new_state, mtk_plane_state);
293 	wmb(); /* Make sure the above parameters are set before update */
294 	mtk_plane_state->pending.dirty = true;
295 }
296 
297 static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
298 	.atomic_check = mtk_plane_atomic_check,
299 	.atomic_update = mtk_plane_atomic_update,
300 	.atomic_disable = mtk_plane_atomic_disable,
301 	.atomic_async_update = mtk_plane_atomic_async_update,
302 	.atomic_async_check = mtk_plane_atomic_async_check,
303 };
304 
mtk_plane_init(struct drm_device * dev,struct drm_plane * plane,unsigned long possible_crtcs,enum drm_plane_type type,unsigned int supported_rotations,const u32 blend_modes,const u32 * formats,size_t num_formats,bool supports_afbc,unsigned int plane_idx)305 int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
306 		   unsigned long possible_crtcs, enum drm_plane_type type,
307 		   unsigned int supported_rotations, const u32 blend_modes,
308 		   const u32 *formats, size_t num_formats,
309 		   bool supports_afbc, unsigned int plane_idx)
310 {
311 	int err;
312 
313 	if (!formats || !num_formats) {
314 		DRM_ERROR("no formats for plane\n");
315 		return -EINVAL;
316 	}
317 
318 	err = drm_universal_plane_init(dev, plane, possible_crtcs,
319 				       &mtk_plane_funcs, formats,
320 				       num_formats,
321 				       supports_afbc ? modifiers : NULL,
322 				       type, NULL);
323 	if (err) {
324 		DRM_ERROR("failed to initialize plane\n");
325 		return err;
326 	}
327 
328 	/*
329 	 * The hardware does not support repositioning planes by muxing: their
330 	 * Z-position is infact fixed and the only way to change the actual
331 	 * order is to swap the contents of the entire register set of one
332 	 * overlay with another, which may be more expensive than desired.
333 	 *
334 	 * With no repositioning, the caller of this function guarantees that
335 	 * the plane_idx is correct. This means that, for example, the PRIMARY
336 	 * plane fed to this function will always have plane_idx zero.
337 	 */
338 	err = drm_plane_create_zpos_immutable_property(plane, plane_idx);
339 	if (err) {
340 		DRM_ERROR("Failed to create zpos property for plane %u\n", plane_idx);
341 		return err;
342 	}
343 
344 	if (supported_rotations) {
345 		err = drm_plane_create_rotation_property(plane,
346 							 DRM_MODE_ROTATE_0,
347 							 supported_rotations);
348 		if (err)
349 			DRM_INFO("Create rotation property failed\n");
350 	}
351 
352 	err = drm_plane_create_alpha_property(plane);
353 	if (err)
354 		DRM_ERROR("failed to create property: alpha\n");
355 
356 	if (blend_modes) {
357 		err = drm_plane_create_blend_mode_property(plane, blend_modes);
358 		if (err)
359 			DRM_ERROR("failed to create property: blend_mode\n");
360 	}
361 
362 	drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
363 
364 	return 0;
365 }
366