Linux Audio

Check our new training course

Loading...
v6.8
  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 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
 30static 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
 52static 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
 70static 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
 96static 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
103static 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
133static 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	int offset;
145
146	gem = fb->obj[0];
147	mtk_gem = to_mtk_gem_obj(gem);
148	addr = mtk_gem->dma_addr;
149	pitch = fb->pitches[0];
150	format = fb->format->format;
151	modifier = fb->modifier;
152
153	if (modifier == DRM_FORMAT_MOD_LINEAR) {
154		/*
155		 * Using dma_addr_t variable to calculate with multiplier of different types,
156		 * for example: addr += (new_state->src.x1 >> 16) * fb->format->cpp[0];
157		 * may cause coverity issue with unintentional overflow.
158		 */
159		offset = (new_state->src.x1 >> 16) * fb->format->cpp[0];
160		addr += offset;
161		offset = (new_state->src.y1 >> 16) * pitch;
162		addr += offset;
163	} else {
164		int width_in_blocks = ALIGN(fb->width, AFBC_DATA_BLOCK_WIDTH)
165				      / AFBC_DATA_BLOCK_WIDTH;
166		int height_in_blocks = ALIGN(fb->height, AFBC_DATA_BLOCK_HEIGHT)
167				       / AFBC_DATA_BLOCK_HEIGHT;
168		int x_offset_in_blocks = (new_state->src.x1 >> 16) / AFBC_DATA_BLOCK_WIDTH;
169		int y_offset_in_blocks = (new_state->src.y1 >> 16) / AFBC_DATA_BLOCK_HEIGHT;
170		int hdr_size, hdr_offset;
171
172		hdr_pitch = width_in_blocks * AFBC_HEADER_BLOCK_SIZE;
173		pitch = width_in_blocks * AFBC_DATA_BLOCK_WIDTH *
174			AFBC_DATA_BLOCK_HEIGHT * fb->format->cpp[0];
175
176		hdr_size = ALIGN(hdr_pitch * height_in_blocks, AFBC_HEADER_ALIGNMENT);
177		hdr_offset = hdr_pitch * y_offset_in_blocks +
178			AFBC_HEADER_BLOCK_SIZE * x_offset_in_blocks;
179
180		/*
181		 * Using dma_addr_t variable to calculate with multiplier of different types,
182		 * for example: addr += hdr_pitch * y_offset_in_blocks;
183		 * may cause coverity issue with unintentional overflow.
184		 */
185		hdr_addr = addr + hdr_offset;
186
187		/* The data plane is offset by 1 additional block. */
188		offset = pitch * y_offset_in_blocks +
189			 AFBC_DATA_BLOCK_WIDTH * AFBC_DATA_BLOCK_HEIGHT *
190			 fb->format->cpp[0] * (x_offset_in_blocks + 1);
191
192		/*
193		 * Using dma_addr_t variable to calculate with multiplier of different types,
194		 * for example: addr += pitch * y_offset_in_blocks;
195		 * may cause coverity issue with unintentional overflow.
196		 */
197		addr = addr + hdr_size + offset;
198	}
199
200	mtk_plane_state->pending.enable = true;
201	mtk_plane_state->pending.pitch = pitch;
202	mtk_plane_state->pending.hdr_pitch = hdr_pitch;
203	mtk_plane_state->pending.format = format;
204	mtk_plane_state->pending.modifier = modifier;
205	mtk_plane_state->pending.addr = addr;
206	mtk_plane_state->pending.hdr_addr = hdr_addr;
207	mtk_plane_state->pending.x = new_state->dst.x1;
208	mtk_plane_state->pending.y = new_state->dst.y1;
209	mtk_plane_state->pending.width = drm_rect_width(&new_state->dst);
210	mtk_plane_state->pending.height = drm_rect_height(&new_state->dst);
211	mtk_plane_state->pending.rotation = new_state->rotation;
212	mtk_plane_state->pending.color_encoding = new_state->color_encoding;
213}
214
215static void mtk_plane_atomic_async_update(struct drm_plane *plane,
216					  struct drm_atomic_state *state)
217{
218	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
219									   plane);
220	struct mtk_plane_state *new_plane_state = to_mtk_plane_state(plane->state);
221
222	plane->state->crtc_x = new_state->crtc_x;
223	plane->state->crtc_y = new_state->crtc_y;
224	plane->state->crtc_h = new_state->crtc_h;
225	plane->state->crtc_w = new_state->crtc_w;
226	plane->state->src_x = new_state->src_x;
227	plane->state->src_y = new_state->src_y;
228	plane->state->src_h = new_state->src_h;
229	plane->state->src_w = new_state->src_w;
230
231	mtk_plane_update_new_state(new_state, new_plane_state);
232	swap(plane->state->fb, new_state->fb);
233	wmb(); /* Make sure the above parameters are set before update */
234	new_plane_state->pending.async_dirty = true;
235	mtk_drm_crtc_async_update(new_state->crtc, plane, state);
236}
237
238static const struct drm_plane_funcs mtk_plane_funcs = {
239	.update_plane = drm_atomic_helper_update_plane,
240	.disable_plane = drm_atomic_helper_disable_plane,
241	.destroy = drm_plane_cleanup,
242	.reset = mtk_plane_reset,
243	.atomic_duplicate_state = mtk_plane_duplicate_state,
244	.atomic_destroy_state = mtk_drm_plane_destroy_state,
245	.format_mod_supported = mtk_plane_format_mod_supported,
246};
247
248static int mtk_plane_atomic_check(struct drm_plane *plane,
249				  struct drm_atomic_state *state)
250{
251	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
252										 plane);
253	struct drm_framebuffer *fb = new_plane_state->fb;
254	struct drm_crtc_state *crtc_state;
255	int ret;
256
257	if (!fb)
258		return 0;
259
260	if (WARN_ON(!new_plane_state->crtc))
261		return 0;
262
263	ret = mtk_drm_crtc_plane_check(new_plane_state->crtc, plane,
264				       to_mtk_plane_state(new_plane_state));
265	if (ret)
266		return ret;
267
268	crtc_state = drm_atomic_get_crtc_state(state,
269					       new_plane_state->crtc);
270	if (IS_ERR(crtc_state))
271		return PTR_ERR(crtc_state);
272
273	return drm_atomic_helper_check_plane_state(new_plane_state,
274						   crtc_state,
275						   DRM_PLANE_NO_SCALING,
276						   DRM_PLANE_NO_SCALING,
277						   true, true);
278}
279
280static void mtk_plane_atomic_disable(struct drm_plane *plane,
281				     struct drm_atomic_state *state)
282{
283	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
284									   plane);
285	struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
286	mtk_plane_state->pending.enable = false;
287	wmb(); /* Make sure the above parameter is set before update */
288	mtk_plane_state->pending.dirty = true;
289}
290
291static void mtk_plane_atomic_update(struct drm_plane *plane,
292				    struct drm_atomic_state *state)
293{
294	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
295									   plane);
296	struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(new_state);
 
 
 
 
297
298	if (!new_state->crtc || WARN_ON(!new_state->fb))
299		return;
300
301	if (!new_state->visible) {
302		mtk_plane_atomic_disable(plane, state);
303		return;
304	}
305
306	mtk_plane_update_new_state(new_state, mtk_plane_state);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307	wmb(); /* Make sure the above parameters are set before update */
308	mtk_plane_state->pending.dirty = true;
309}
310
311static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
 
312	.atomic_check = mtk_plane_atomic_check,
313	.atomic_update = mtk_plane_atomic_update,
314	.atomic_disable = mtk_plane_atomic_disable,
315	.atomic_async_update = mtk_plane_atomic_async_update,
316	.atomic_async_check = mtk_plane_atomic_async_check,
317};
318
319int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
320		   unsigned long possible_crtcs, enum drm_plane_type type,
321		   unsigned int supported_rotations, const u32 *formats,
322		   size_t num_formats)
323{
324	int err;
325
326	if (!formats || !num_formats) {
327		DRM_ERROR("no formats for plane\n");
328		return -EINVAL;
329	}
330
331	err = drm_universal_plane_init(dev, plane, possible_crtcs,
332				       &mtk_plane_funcs, formats,
333				       num_formats, modifiers, type, NULL);
334	if (err) {
335		DRM_ERROR("failed to initialize plane\n");
336		return err;
337	}
338
339	if (supported_rotations & ~DRM_MODE_ROTATE_0) {
340		err = drm_plane_create_rotation_property(plane,
341							 DRM_MODE_ROTATE_0,
342							 supported_rotations);
343		if (err)
344			DRM_INFO("Create rotation property failed\n");
345	}
346
347	drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
348
349	return 0;
350}
v5.9
  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_fourcc.h>
 10#include <drm/drm_atomic_uapi.h>
 11#include <drm/drm_plane_helper.h>
 12#include <drm/drm_gem_framebuffer_helper.h>
 13
 14#include "mtk_drm_crtc.h"
 15#include "mtk_drm_ddp_comp.h"
 16#include "mtk_drm_drv.h"
 17#include "mtk_drm_gem.h"
 18#include "mtk_drm_plane.h"
 19
 20static const u32 formats[] = {
 21	DRM_FORMAT_XRGB8888,
 22	DRM_FORMAT_ARGB8888,
 23	DRM_FORMAT_BGRX8888,
 24	DRM_FORMAT_BGRA8888,
 25	DRM_FORMAT_ABGR8888,
 26	DRM_FORMAT_XBGR8888,
 27	DRM_FORMAT_RGB888,
 28	DRM_FORMAT_BGR888,
 29	DRM_FORMAT_RGB565,
 30	DRM_FORMAT_UYVY,
 31	DRM_FORMAT_YUYV,
 32};
 33
 34static void mtk_plane_reset(struct drm_plane *plane)
 35{
 36	struct mtk_plane_state *state;
 37
 38	if (plane->state) {
 39		__drm_atomic_helper_plane_destroy_state(plane->state);
 40
 41		state = to_mtk_plane_state(plane->state);
 42		memset(state, 0, sizeof(*state));
 43	} else {
 44		state = kzalloc(sizeof(*state), GFP_KERNEL);
 45		if (!state)
 46			return;
 47		plane->state = &state->base;
 48	}
 49
 
 
 50	state->base.plane = plane;
 51	state->pending.format = DRM_FORMAT_RGB565;
 
 52}
 53
 54static struct drm_plane_state *mtk_plane_duplicate_state(struct drm_plane *plane)
 55{
 56	struct mtk_plane_state *old_state = to_mtk_plane_state(plane->state);
 57	struct mtk_plane_state *state;
 58
 59	state = kzalloc(sizeof(*state), GFP_KERNEL);
 60	if (!state)
 61		return NULL;
 62
 63	__drm_atomic_helper_plane_duplicate_state(plane, &state->base);
 64
 65	WARN_ON(state->base.plane != plane);
 66
 67	state->pending = old_state->pending;
 68
 69	return &state->base;
 70}
 71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 72static void mtk_drm_plane_destroy_state(struct drm_plane *plane,
 73					struct drm_plane_state *state)
 74{
 75	__drm_atomic_helper_plane_destroy_state(state);
 76	kfree(to_mtk_plane_state(state));
 77}
 78
 79static int mtk_plane_atomic_async_check(struct drm_plane *plane,
 80					struct drm_plane_state *state)
 81{
 
 
 82	struct drm_crtc_state *crtc_state;
 83	int ret;
 84
 85	if (plane != state->crtc->cursor)
 86		return -EINVAL;
 87
 88	if (!plane->state)
 89		return -EINVAL;
 90
 91	if (!plane->state->fb)
 92		return -EINVAL;
 93
 94	ret = mtk_drm_crtc_plane_check(state->crtc, plane,
 95				       to_mtk_plane_state(state));
 96	if (ret)
 97		return ret;
 98
 99	if (state->state)
100		crtc_state = drm_atomic_get_existing_crtc_state(state->state,
101								state->crtc);
102	else /* Special case for asynchronous cursor updates. */
103		crtc_state = state->crtc->state;
104
105	return drm_atomic_helper_check_plane_state(plane->state, crtc_state,
106						   DRM_PLANE_HELPER_NO_SCALING,
107						   DRM_PLANE_HELPER_NO_SCALING,
108						   true, true);
109}
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111static void mtk_plane_atomic_async_update(struct drm_plane *plane,
112					  struct drm_plane_state *new_state)
113{
114	struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
 
 
115
116	plane->state->crtc_x = new_state->crtc_x;
117	plane->state->crtc_y = new_state->crtc_y;
118	plane->state->crtc_h = new_state->crtc_h;
119	plane->state->crtc_w = new_state->crtc_w;
120	plane->state->src_x = new_state->src_x;
121	plane->state->src_y = new_state->src_y;
122	plane->state->src_h = new_state->src_h;
123	plane->state->src_w = new_state->src_w;
 
 
124	swap(plane->state->fb, new_state->fb);
125	state->pending.async_dirty = true;
126
127	mtk_drm_crtc_async_update(new_state->crtc, plane, new_state);
128}
129
130static const struct drm_plane_funcs mtk_plane_funcs = {
131	.update_plane = drm_atomic_helper_update_plane,
132	.disable_plane = drm_atomic_helper_disable_plane,
133	.destroy = drm_plane_cleanup,
134	.reset = mtk_plane_reset,
135	.atomic_duplicate_state = mtk_plane_duplicate_state,
136	.atomic_destroy_state = mtk_drm_plane_destroy_state,
 
137};
138
139static int mtk_plane_atomic_check(struct drm_plane *plane,
140				  struct drm_plane_state *state)
141{
142	struct drm_framebuffer *fb = state->fb;
 
 
143	struct drm_crtc_state *crtc_state;
144	int ret;
145
146	if (!fb)
147		return 0;
148
149	if (WARN_ON(!state->crtc))
150		return 0;
151
152	ret = mtk_drm_crtc_plane_check(state->crtc, plane,
153				       to_mtk_plane_state(state));
154	if (ret)
155		return ret;
156
157	crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
 
158	if (IS_ERR(crtc_state))
159		return PTR_ERR(crtc_state);
160
161	return drm_atomic_helper_check_plane_state(state, crtc_state,
162						   DRM_PLANE_HELPER_NO_SCALING,
163						   DRM_PLANE_HELPER_NO_SCALING,
 
164						   true, true);
165}
166
167static void mtk_plane_atomic_disable(struct drm_plane *plane,
168				     struct drm_plane_state *old_state)
169{
170	struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
171
172	state->pending.enable = false;
 
173	wmb(); /* Make sure the above parameter is set before update */
174	state->pending.dirty = true;
175}
176
177static void mtk_plane_atomic_update(struct drm_plane *plane,
178				    struct drm_plane_state *old_state)
179{
180	struct mtk_plane_state *state = to_mtk_plane_state(plane->state);
181	struct drm_crtc *crtc = plane->state->crtc;
182	struct drm_framebuffer *fb = plane->state->fb;
183	struct drm_gem_object *gem;
184	struct mtk_drm_gem_obj *mtk_gem;
185	unsigned int pitch, format;
186	dma_addr_t addr;
187
188	if (!crtc || WARN_ON(!fb))
189		return;
190
191	if (!plane->state->visible) {
192		mtk_plane_atomic_disable(plane, old_state);
193		return;
194	}
195
196	gem = fb->obj[0];
197	mtk_gem = to_mtk_gem_obj(gem);
198	addr = mtk_gem->dma_addr;
199	pitch = fb->pitches[0];
200	format = fb->format->format;
201
202	addr += (plane->state->src.x1 >> 16) * fb->format->cpp[0];
203	addr += (plane->state->src.y1 >> 16) * pitch;
204
205	state->pending.enable = true;
206	state->pending.pitch = pitch;
207	state->pending.format = format;
208	state->pending.addr = addr;
209	state->pending.x = plane->state->dst.x1;
210	state->pending.y = plane->state->dst.y1;
211	state->pending.width = drm_rect_width(&plane->state->dst);
212	state->pending.height = drm_rect_height(&plane->state->dst);
213	state->pending.rotation = plane->state->rotation;
214	wmb(); /* Make sure the above parameters are set before update */
215	state->pending.dirty = true;
216}
217
218static const struct drm_plane_helper_funcs mtk_plane_helper_funcs = {
219	.prepare_fb = drm_gem_fb_prepare_fb,
220	.atomic_check = mtk_plane_atomic_check,
221	.atomic_update = mtk_plane_atomic_update,
222	.atomic_disable = mtk_plane_atomic_disable,
223	.atomic_async_update = mtk_plane_atomic_async_update,
224	.atomic_async_check = mtk_plane_atomic_async_check,
225};
226
227int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,
228		   unsigned long possible_crtcs, enum drm_plane_type type,
229		   unsigned int supported_rotations)
 
230{
231	int err;
232
 
 
 
 
 
233	err = drm_universal_plane_init(dev, plane, possible_crtcs,
234				       &mtk_plane_funcs, formats,
235				       ARRAY_SIZE(formats), NULL, type, NULL);
236	if (err) {
237		DRM_ERROR("failed to initialize plane\n");
238		return err;
239	}
240
241	if (supported_rotations & ~DRM_MODE_ROTATE_0) {
242		err = drm_plane_create_rotation_property(plane,
243							 DRM_MODE_ROTATE_0,
244							 supported_rotations);
245		if (err)
246			DRM_INFO("Create rotation property failed\n");
247	}
248
249	drm_plane_helper_add(plane, &mtk_plane_helper_funcs);
250
251	return 0;
252}