xref: /linux/drivers/gpu/drm/msm/disp/mdp5/mdp5_plane.c (revision db5d28c0bfe566908719bec8e25443aabecbb802)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
214be3200SRob Clark /*
314be3200SRob Clark  * Copyright (C) 2014-2015 The Linux Foundation. All rights reserved.
414be3200SRob Clark  * Copyright (C) 2013 Red Hat
514be3200SRob Clark  * Author: Rob Clark <robdclark@gmail.com>
614be3200SRob Clark  */
714be3200SRob Clark 
85ddb0bd4SMaxime Ripard #include <drm/drm_atomic.h>
990bb087fSVille Syrjälä #include <drm/drm_blend.h>
10648fdc3fSBrian Masney #include <drm/drm_damage_helper.h>
11feea39a8SSam Ravnborg #include <drm/drm_fourcc.h>
12720cf96dSVille Syrjälä #include <drm/drm_framebuffer.h>
139e4dde28SRob Clark #include <drm/drm_gem_atomic_helper.h>
1414be3200SRob Clark #include <drm/drm_print.h>
15feea39a8SSam Ravnborg 
1614be3200SRob Clark #include "mdp5_kms.h"
1714be3200SRob Clark 
1814be3200SRob Clark struct mdp5_plane {
1914be3200SRob Clark 	struct drm_plane base;
2014be3200SRob Clark };
2114be3200SRob Clark #define to_mdp5_plane(x) container_of(x, struct mdp5_plane, base)
2214be3200SRob Clark 
2314be3200SRob Clark static int mdp5_plane_mode_set(struct drm_plane *plane,
2414be3200SRob Clark 		struct drm_crtc *crtc, struct drm_framebuffer *fb,
2514be3200SRob Clark 		struct drm_rect *src, struct drm_rect *dest);
2614be3200SRob Clark 
get_kms(struct drm_plane * plane)2714be3200SRob Clark static struct mdp5_kms *get_kms(struct drm_plane *plane)
2814be3200SRob Clark {
2914be3200SRob Clark 	struct msm_drm_private *priv = plane->dev->dev_private;
3014be3200SRob Clark 	return to_mdp5_kms(to_mdp_kms(priv->kms));
3114be3200SRob Clark }
3214be3200SRob Clark 
plane_enabled(struct drm_plane_state * state)3314be3200SRob Clark static bool plane_enabled(struct drm_plane_state *state)
3414be3200SRob Clark {
3514be3200SRob Clark 	return state->visible;
3614be3200SRob Clark }
3714be3200SRob Clark 
387d36db0bSDmitry Baryshkov /* helper to install properties which are common to planes and crtcs */
mdp5_plane_install_properties(struct drm_plane * plane,struct drm_mode_object * obj)397d36db0bSDmitry Baryshkov static void mdp5_plane_install_properties(struct drm_plane *plane,
407d36db0bSDmitry Baryshkov 		struct drm_mode_object *obj)
4114be3200SRob Clark {
42c228cb34SMaxime Ripard 	unsigned int zpos;
43c228cb34SMaxime Ripard 
4414be3200SRob Clark 	drm_plane_create_rotation_property(plane,
4514be3200SRob Clark 					   DRM_MODE_ROTATE_0,
4614be3200SRob Clark 					   DRM_MODE_ROTATE_0 |
4714be3200SRob Clark 					   DRM_MODE_ROTATE_180 |
4814be3200SRob Clark 					   DRM_MODE_REFLECT_X |
4914be3200SRob Clark 					   DRM_MODE_REFLECT_Y);
50ed6b97e5SDmitry Baryshkov 	drm_plane_create_alpha_property(plane);
51ed6b97e5SDmitry Baryshkov 	drm_plane_create_blend_mode_property(plane,
52ed6b97e5SDmitry Baryshkov 			BIT(DRM_MODE_BLEND_PIXEL_NONE) |
53ed6b97e5SDmitry Baryshkov 			BIT(DRM_MODE_BLEND_PREMULTI) |
54ed6b97e5SDmitry Baryshkov 			BIT(DRM_MODE_BLEND_COVERAGE));
55c228cb34SMaxime Ripard 
56c228cb34SMaxime Ripard 	if (plane->type == DRM_PLANE_TYPE_PRIMARY)
57c228cb34SMaxime Ripard 		zpos = STAGE_BASE;
58c228cb34SMaxime Ripard 	else
59c228cb34SMaxime Ripard 		zpos = STAGE0 + drm_plane_index(plane);
60c228cb34SMaxime Ripard 	drm_plane_create_zpos_property(plane, zpos, 1, 255);
6114be3200SRob Clark }
6214be3200SRob Clark 
6314be3200SRob Clark static void
mdp5_plane_atomic_print_state(struct drm_printer * p,const struct drm_plane_state * state)6414be3200SRob Clark mdp5_plane_atomic_print_state(struct drm_printer *p,
6514be3200SRob Clark 		const struct drm_plane_state *state)
6614be3200SRob Clark {
6714be3200SRob Clark 	struct mdp5_plane_state *pstate = to_mdp5_plane_state(state);
6814be3200SRob Clark 	struct mdp5_kms *mdp5_kms = get_kms(state->plane);
6914be3200SRob Clark 
7014be3200SRob Clark 	drm_printf(p, "\thwpipe=%s\n", pstate->hwpipe ?
7114be3200SRob Clark 			pstate->hwpipe->name : "(null)");
7214be3200SRob Clark 	if (mdp5_kms->caps & MDP_CAP_SRC_SPLIT)
7314be3200SRob Clark 		drm_printf(p, "\tright-hwpipe=%s\n",
7414be3200SRob Clark 			   pstate->r_hwpipe ? pstate->r_hwpipe->name :
7514be3200SRob Clark 					      "(null)");
76a4fdc260SDmitry Baryshkov 	drm_printf(p, "\tblend_mode=%u\n", pstate->base.pixel_blend_mode);
777d36db0bSDmitry Baryshkov 	drm_printf(p, "\tzpos=%u\n", pstate->base.zpos);
787d36db0bSDmitry Baryshkov 	drm_printf(p, "\tnormalized_zpos=%u\n", pstate->base.normalized_zpos);
7921ab7e8dSDmitry Baryshkov 	drm_printf(p, "\talpha=%u\n", pstate->base.alpha);
8014be3200SRob Clark 	drm_printf(p, "\tstage=%s\n", stage2name(pstate->stage));
8114be3200SRob Clark }
8214be3200SRob Clark 
mdp5_plane_reset(struct drm_plane * plane)8314be3200SRob Clark static void mdp5_plane_reset(struct drm_plane *plane)
8414be3200SRob Clark {
8514be3200SRob Clark 	struct mdp5_plane_state *mdp5_state;
8614be3200SRob Clark 
879074b67bSDmitry Baryshkov 	if (plane->state)
889074b67bSDmitry Baryshkov 		__drm_atomic_helper_plane_destroy_state(plane->state);
8914be3200SRob Clark 
9014be3200SRob Clark 	kfree(to_mdp5_plane_state(plane->state));
91047ae665SXiaoke Wang 	plane->state = NULL;
9214be3200SRob Clark 	mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
93047ae665SXiaoke Wang 	if (!mdp5_state)
94047ae665SXiaoke Wang 		return;
959074b67bSDmitry Baryshkov 	__drm_atomic_helper_plane_reset(plane, &mdp5_state->base);
9614be3200SRob Clark }
9714be3200SRob Clark 
9814be3200SRob Clark static struct drm_plane_state *
mdp5_plane_duplicate_state(struct drm_plane * plane)9914be3200SRob Clark mdp5_plane_duplicate_state(struct drm_plane *plane)
10014be3200SRob Clark {
10114be3200SRob Clark 	struct mdp5_plane_state *mdp5_state;
10214be3200SRob Clark 
10314be3200SRob Clark 	if (WARN_ON(!plane->state))
10414be3200SRob Clark 		return NULL;
10514be3200SRob Clark 
10614be3200SRob Clark 	mdp5_state = kmemdup(to_mdp5_plane_state(plane->state),
10714be3200SRob Clark 			sizeof(*mdp5_state), GFP_KERNEL);
10814be3200SRob Clark 	if (!mdp5_state)
10914be3200SRob Clark 		return NULL;
11014be3200SRob Clark 
11114be3200SRob Clark 	__drm_atomic_helper_plane_duplicate_state(plane, &mdp5_state->base);
11214be3200SRob Clark 
11314be3200SRob Clark 	return &mdp5_state->base;
11414be3200SRob Clark }
11514be3200SRob Clark 
mdp5_plane_destroy_state(struct drm_plane * plane,struct drm_plane_state * state)11614be3200SRob Clark static void mdp5_plane_destroy_state(struct drm_plane *plane,
11714be3200SRob Clark 		struct drm_plane_state *state)
11814be3200SRob Clark {
11914be3200SRob Clark 	struct mdp5_plane_state *pstate = to_mdp5_plane_state(state);
12014be3200SRob Clark 
121fd0ad3b2SDaniel Vetter 	__drm_atomic_helper_plane_destroy_state(state);
12214be3200SRob Clark 
12314be3200SRob Clark 	kfree(pstate);
12414be3200SRob Clark }
12514be3200SRob Clark 
12614be3200SRob Clark static const struct drm_plane_funcs mdp5_plane_funcs = {
12714be3200SRob Clark 		.update_plane = drm_atomic_helper_update_plane,
12814be3200SRob Clark 		.disable_plane = drm_atomic_helper_disable_plane,
12914be3200SRob Clark 		.reset = mdp5_plane_reset,
13014be3200SRob Clark 		.atomic_duplicate_state = mdp5_plane_duplicate_state,
13114be3200SRob Clark 		.atomic_destroy_state = mdp5_plane_destroy_state,
13214be3200SRob Clark 		.atomic_print_state = mdp5_plane_atomic_print_state,
13314be3200SRob Clark };
13414be3200SRob Clark 
mdp5_plane_prepare_fb(struct drm_plane * plane,struct drm_plane_state * new_state)1359e4dde28SRob Clark static int mdp5_plane_prepare_fb(struct drm_plane *plane,
1369e4dde28SRob Clark 				 struct drm_plane_state *new_state)
1379e4dde28SRob Clark {
1389e4dde28SRob Clark 	struct msm_drm_private *priv = plane->dev->dev_private;
1399e4dde28SRob Clark 	struct msm_kms *kms = priv->kms;
1409e4dde28SRob Clark 	bool needs_dirtyfb = to_mdp5_plane_state(new_state)->needs_dirtyfb;
1419e4dde28SRob Clark 
1429e4dde28SRob Clark 	if (!new_state->fb)
1439e4dde28SRob Clark 		return 0;
1449e4dde28SRob Clark 
1459e4dde28SRob Clark 	drm_gem_plane_helper_prepare_fb(plane, new_state);
1469e4dde28SRob Clark 
1479e4dde28SRob Clark 	return msm_framebuffer_prepare(new_state->fb, kms->aspace, needs_dirtyfb);
1489e4dde28SRob Clark }
1499e4dde28SRob Clark 
mdp5_plane_cleanup_fb(struct drm_plane * plane,struct drm_plane_state * old_state)15014be3200SRob Clark static void mdp5_plane_cleanup_fb(struct drm_plane *plane,
15114be3200SRob Clark 				  struct drm_plane_state *old_state)
15214be3200SRob Clark {
15314be3200SRob Clark 	struct mdp5_kms *mdp5_kms = get_kms(plane);
15414be3200SRob Clark 	struct msm_kms *kms = &mdp5_kms->base.base;
15514be3200SRob Clark 	struct drm_framebuffer *fb = old_state->fb;
1569e4dde28SRob Clark 	bool needed_dirtyfb = to_mdp5_plane_state(old_state)->needs_dirtyfb;
15714be3200SRob Clark 
15814be3200SRob Clark 	if (!fb)
15914be3200SRob Clark 		return;
16014be3200SRob Clark 
16114be3200SRob Clark 	DBG("%s: cleanup: FB[%u]", plane->name, fb->base.id);
1629e4dde28SRob Clark 	msm_framebuffer_cleanup(fb, kms->aspace, needed_dirtyfb);
16314be3200SRob Clark }
16414be3200SRob Clark 
mdp5_plane_atomic_check_with_state(struct drm_crtc_state * crtc_state,struct drm_plane_state * state)16514be3200SRob Clark static int mdp5_plane_atomic_check_with_state(struct drm_crtc_state *crtc_state,
16614be3200SRob Clark 					      struct drm_plane_state *state)
16714be3200SRob Clark {
16814be3200SRob Clark 	struct mdp5_plane_state *mdp5_state = to_mdp5_plane_state(state);
16914be3200SRob Clark 	struct drm_plane *plane = state->plane;
17014be3200SRob Clark 	struct drm_plane_state *old_state = plane->state;
17114be3200SRob Clark 	struct mdp5_cfg *config = mdp5_cfg_get_config(get_kms(plane)->cfg);
17214be3200SRob Clark 	bool new_hwpipe = false;
17314be3200SRob Clark 	bool need_right_hwpipe = false;
17414be3200SRob Clark 	uint32_t max_width, max_height;
17514be3200SRob Clark 	bool out_of_bounds = false;
17614be3200SRob Clark 	uint32_t caps = 0;
17714be3200SRob Clark 	int min_scale, max_scale;
17814be3200SRob Clark 	int ret;
17914be3200SRob Clark 
18014be3200SRob Clark 	DBG("%s: check (%d -> %d)", plane->name,
18114be3200SRob Clark 			plane_enabled(old_state), plane_enabled(state));
18214be3200SRob Clark 
18314be3200SRob Clark 	max_width = config->hw->lm.max_width << 16;
18414be3200SRob Clark 	max_height = config->hw->lm.max_height << 16;
18514be3200SRob Clark 
18614be3200SRob Clark 	/* Make sure source dimensions are within bounds. */
18714be3200SRob Clark 	if (state->src_h > max_height)
18814be3200SRob Clark 		out_of_bounds = true;
18914be3200SRob Clark 
19014be3200SRob Clark 	if (state->src_w > max_width) {
19114be3200SRob Clark 		/* If source split is supported, we can go up to 2x
19214be3200SRob Clark 		 * the max LM width, but we'd need to stage another
19314be3200SRob Clark 		 * hwpipe to the right LM. So, the drm_plane would
19414be3200SRob Clark 		 * consist of 2 hwpipes.
19514be3200SRob Clark 		 */
19614be3200SRob Clark 		if (config->hw->mdp.caps & MDP_CAP_SRC_SPLIT &&
19714be3200SRob Clark 		    (state->src_w <= 2 * max_width))
19814be3200SRob Clark 			need_right_hwpipe = true;
19914be3200SRob Clark 		else
20014be3200SRob Clark 			out_of_bounds = true;
20114be3200SRob Clark 	}
20214be3200SRob Clark 
20314be3200SRob Clark 	if (out_of_bounds) {
20414be3200SRob Clark 		struct drm_rect src = drm_plane_state_src(state);
20514be3200SRob Clark 		DBG("Invalid source size "DRM_RECT_FP_FMT,
20614be3200SRob Clark 				DRM_RECT_FP_ARG(&src));
20714be3200SRob Clark 		return -ERANGE;
20814be3200SRob Clark 	}
20914be3200SRob Clark 
21014be3200SRob Clark 	min_scale = FRAC_16_16(1, 8);
21114be3200SRob Clark 	max_scale = FRAC_16_16(8, 1);
21214be3200SRob Clark 
213b65bd403SDave Airlie 	ret = drm_atomic_helper_check_plane_state(state, crtc_state,
21414be3200SRob Clark 						  min_scale, max_scale,
21514be3200SRob Clark 						  true, true);
21614be3200SRob Clark 	if (ret)
21714be3200SRob Clark 		return ret;
21814be3200SRob Clark 
21914be3200SRob Clark 	if (plane_enabled(state)) {
22014be3200SRob Clark 		unsigned int rotation;
2210e67f514SDmitry Baryshkov 		const struct msm_format *format;
22214be3200SRob Clark 		struct mdp5_kms *mdp5_kms = get_kms(plane);
22314be3200SRob Clark 		uint32_t blkcfg = 0;
22414be3200SRob Clark 
2250e67f514SDmitry Baryshkov 		format = msm_framebuffer_format(state->fb);
2260e67f514SDmitry Baryshkov 		if (MSM_FORMAT_IS_YUV(format))
22714be3200SRob Clark 			caps |= MDP_PIPE_CAP_SCALE | MDP_PIPE_CAP_CSC;
22814be3200SRob Clark 
22914be3200SRob Clark 		if (((state->src_w >> 16) != state->crtc_w) ||
23014be3200SRob Clark 				((state->src_h >> 16) != state->crtc_h))
23114be3200SRob Clark 			caps |= MDP_PIPE_CAP_SCALE;
23214be3200SRob Clark 
23314be3200SRob Clark 		rotation = drm_rotation_simplify(state->rotation,
23414be3200SRob Clark 						 DRM_MODE_ROTATE_0 |
23514be3200SRob Clark 						 DRM_MODE_REFLECT_X |
23614be3200SRob Clark 						 DRM_MODE_REFLECT_Y);
23714be3200SRob Clark 
23814be3200SRob Clark 		if (rotation & DRM_MODE_REFLECT_X)
23914be3200SRob Clark 			caps |= MDP_PIPE_CAP_HFLIP;
24014be3200SRob Clark 
24114be3200SRob Clark 		if (rotation & DRM_MODE_REFLECT_Y)
24214be3200SRob Clark 			caps |= MDP_PIPE_CAP_VFLIP;
24314be3200SRob Clark 
24414be3200SRob Clark 		if (plane->type == DRM_PLANE_TYPE_CURSOR)
24514be3200SRob Clark 			caps |= MDP_PIPE_CAP_CURSOR;
24614be3200SRob Clark 
24714be3200SRob Clark 		/* (re)allocate hw pipe if we don't have one or caps-mismatch: */
24814be3200SRob Clark 		if (!mdp5_state->hwpipe || (caps & ~mdp5_state->hwpipe->caps))
24914be3200SRob Clark 			new_hwpipe = true;
25014be3200SRob Clark 
25114be3200SRob Clark 		/*
25214be3200SRob Clark 		 * (re)allocte hw pipe if we're either requesting for 2 hw pipes
25314be3200SRob Clark 		 * or we're switching from 2 hw pipes to 1 hw pipe because the
25414be3200SRob Clark 		 * new src_w can be supported by 1 hw pipe itself.
25514be3200SRob Clark 		 */
25614be3200SRob Clark 		if ((need_right_hwpipe && !mdp5_state->r_hwpipe) ||
25714be3200SRob Clark 		    (!need_right_hwpipe && mdp5_state->r_hwpipe))
25814be3200SRob Clark 			new_hwpipe = true;
25914be3200SRob Clark 
26014be3200SRob Clark 		if (mdp5_kms->smp) {
2610e67f514SDmitry Baryshkov 			const struct msm_format *format =
2620e67f514SDmitry Baryshkov 				msm_framebuffer_format(state->fb);
26314be3200SRob Clark 
26414be3200SRob Clark 			blkcfg = mdp5_smp_calculate(mdp5_kms->smp, format,
26514be3200SRob Clark 					state->src_w >> 16, false);
26614be3200SRob Clark 
26714be3200SRob Clark 			if (mdp5_state->hwpipe && (mdp5_state->hwpipe->blkcfg != blkcfg))
26814be3200SRob Clark 				new_hwpipe = true;
26914be3200SRob Clark 		}
27014be3200SRob Clark 
27114be3200SRob Clark 		/* (re)assign hwpipe if needed, otherwise keep old one: */
27214be3200SRob Clark 		if (new_hwpipe) {
27314be3200SRob Clark 			/* TODO maybe we want to re-assign hwpipe sometimes
27414be3200SRob Clark 			 * in cases when we no-longer need some caps to make
27514be3200SRob Clark 			 * it available for other planes?
27614be3200SRob Clark 			 */
27714be3200SRob Clark 			struct mdp5_hw_pipe *old_hwpipe = mdp5_state->hwpipe;
27814be3200SRob Clark 			struct mdp5_hw_pipe *old_right_hwpipe =
27914be3200SRob Clark 							  mdp5_state->r_hwpipe;
28014be3200SRob Clark 			struct mdp5_hw_pipe *new_hwpipe = NULL;
28114be3200SRob Clark 			struct mdp5_hw_pipe *new_right_hwpipe = NULL;
28214be3200SRob Clark 
28314be3200SRob Clark 			ret = mdp5_pipe_assign(state->state, plane, caps,
28414be3200SRob Clark 					       blkcfg, &new_hwpipe,
28514be3200SRob Clark 					       need_right_hwpipe ?
28614be3200SRob Clark 					       &new_right_hwpipe : NULL);
28714be3200SRob Clark 			if (ret) {
28814be3200SRob Clark 				DBG("%s: failed to assign hwpipe(s)!",
28914be3200SRob Clark 				    plane->name);
29014be3200SRob Clark 				return ret;
29114be3200SRob Clark 			}
29214be3200SRob Clark 
29314be3200SRob Clark 			mdp5_state->hwpipe = new_hwpipe;
29414be3200SRob Clark 			if (need_right_hwpipe)
29514be3200SRob Clark 				mdp5_state->r_hwpipe = new_right_hwpipe;
29614be3200SRob Clark 			else
29714be3200SRob Clark 				/*
29814be3200SRob Clark 				 * set it to NULL so that the driver knows we
29914be3200SRob Clark 				 * don't have a right hwpipe when committing a
30014be3200SRob Clark 				 * new state
30114be3200SRob Clark 				 */
30214be3200SRob Clark 				mdp5_state->r_hwpipe = NULL;
30314be3200SRob Clark 
30414be3200SRob Clark 
305d59be579SJessica Zhang 			ret = mdp5_pipe_release(state->state, old_hwpipe);
306d59be579SJessica Zhang 			if (ret)
307d59be579SJessica Zhang 				return ret;
308d59be579SJessica Zhang 
309d59be579SJessica Zhang 			ret = mdp5_pipe_release(state->state, old_right_hwpipe);
310d59be579SJessica Zhang 			if (ret)
311d59be579SJessica Zhang 				return ret;
312d59be579SJessica Zhang 
31314be3200SRob Clark 		}
31414be3200SRob Clark 	} else {
315d59be579SJessica Zhang 		ret = mdp5_pipe_release(state->state, mdp5_state->hwpipe);
316d59be579SJessica Zhang 		if (ret)
317d59be579SJessica Zhang 			return ret;
318d59be579SJessica Zhang 
319d59be579SJessica Zhang 		ret = mdp5_pipe_release(state->state, mdp5_state->r_hwpipe);
320d59be579SJessica Zhang 		if (ret)
321d59be579SJessica Zhang 			return ret;
322d59be579SJessica Zhang 
32314be3200SRob Clark 		mdp5_state->hwpipe = mdp5_state->r_hwpipe = NULL;
32414be3200SRob Clark 	}
32514be3200SRob Clark 
32614be3200SRob Clark 	return 0;
32714be3200SRob Clark }
32814be3200SRob Clark 
mdp5_plane_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)32914be3200SRob Clark static int mdp5_plane_atomic_check(struct drm_plane *plane,
3307c11b99aSMaxime Ripard 				   struct drm_atomic_state *state)
33114be3200SRob Clark {
3320b6aaf9dSMaxime Ripard 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state,
3330b6aaf9dSMaxime Ripard 										 plane);
3347c11b99aSMaxime Ripard 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
3357c11b99aSMaxime Ripard 										 plane);
33614be3200SRob Clark 	struct drm_crtc *crtc;
33714be3200SRob Clark 	struct drm_crtc_state *crtc_state;
33814be3200SRob Clark 
3390b6aaf9dSMaxime Ripard 	crtc = new_plane_state->crtc ? new_plane_state->crtc : old_plane_state->crtc;
34014be3200SRob Clark 	if (!crtc)
34114be3200SRob Clark 		return 0;
34214be3200SRob Clark 
343dec92020SMaxime Ripard 	crtc_state = drm_atomic_get_existing_crtc_state(state,
344ba5c1649SMaxime Ripard 							crtc);
34514be3200SRob Clark 	if (WARN_ON(!crtc_state))
34614be3200SRob Clark 		return -EINVAL;
34714be3200SRob Clark 
348ba5c1649SMaxime Ripard 	return mdp5_plane_atomic_check_with_state(crtc_state, new_plane_state);
34914be3200SRob Clark }
35014be3200SRob Clark 
mdp5_plane_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)35114be3200SRob Clark static void mdp5_plane_atomic_update(struct drm_plane *plane,
352977697e2SMaxime Ripard 				     struct drm_atomic_state *state)
35314be3200SRob Clark {
35437418bf1SMaxime Ripard 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
35537418bf1SMaxime Ripard 									   plane);
35614be3200SRob Clark 
35714be3200SRob Clark 	DBG("%s: update", plane->name);
35814be3200SRob Clark 
35941016fe1SMaxime Ripard 	if (plane_enabled(new_state)) {
36014be3200SRob Clark 		int ret;
36114be3200SRob Clark 
36214be3200SRob Clark 		ret = mdp5_plane_mode_set(plane,
36341016fe1SMaxime Ripard 				new_state->crtc, new_state->fb,
36441016fe1SMaxime Ripard 				&new_state->src, &new_state->dst);
36514be3200SRob Clark 		/* atomic_check should have ensured that this doesn't fail */
36614be3200SRob Clark 		WARN_ON(ret < 0);
36714be3200SRob Clark 	}
36814be3200SRob Clark }
36914be3200SRob Clark 
mdp5_plane_atomic_async_check(struct drm_plane * plane,struct drm_atomic_state * state)37014be3200SRob Clark static int mdp5_plane_atomic_async_check(struct drm_plane *plane,
3715ddb0bd4SMaxime Ripard 					 struct drm_atomic_state *state)
37214be3200SRob Clark {
3735ddb0bd4SMaxime Ripard 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
3745ddb0bd4SMaxime Ripard 										 plane);
3755ddb0bd4SMaxime Ripard 	struct mdp5_plane_state *mdp5_state = to_mdp5_plane_state(new_plane_state);
37614be3200SRob Clark 	struct drm_crtc_state *crtc_state;
37714be3200SRob Clark 	int min_scale, max_scale;
37814be3200SRob Clark 	int ret;
37914be3200SRob Clark 
3805ddb0bd4SMaxime Ripard 	crtc_state = drm_atomic_get_existing_crtc_state(state,
3815ddb0bd4SMaxime Ripard 							new_plane_state->crtc);
38214be3200SRob Clark 	if (WARN_ON(!crtc_state))
38314be3200SRob Clark 		return -EINVAL;
38414be3200SRob Clark 
38514be3200SRob Clark 	if (!crtc_state->active)
38614be3200SRob Clark 		return -EINVAL;
38714be3200SRob Clark 
38814be3200SRob Clark 	/* don't use fast path if we don't have a hwpipe allocated yet */
38914be3200SRob Clark 	if (!mdp5_state->hwpipe)
39014be3200SRob Clark 		return -EINVAL;
39114be3200SRob Clark 
39214be3200SRob Clark 	/* only allow changing of position(crtc x/y or src x/y) in fast path */
3935ddb0bd4SMaxime Ripard 	if (plane->state->crtc != new_plane_state->crtc ||
3945ddb0bd4SMaxime Ripard 	    plane->state->src_w != new_plane_state->src_w ||
3955ddb0bd4SMaxime Ripard 	    plane->state->src_h != new_plane_state->src_h ||
3965ddb0bd4SMaxime Ripard 	    plane->state->crtc_w != new_plane_state->crtc_w ||
3975ddb0bd4SMaxime Ripard 	    plane->state->crtc_h != new_plane_state->crtc_h ||
39814be3200SRob Clark 	    !plane->state->fb ||
3995ddb0bd4SMaxime Ripard 	    plane->state->fb != new_plane_state->fb)
40014be3200SRob Clark 		return -EINVAL;
40114be3200SRob Clark 
40214be3200SRob Clark 	min_scale = FRAC_16_16(1, 8);
40314be3200SRob Clark 	max_scale = FRAC_16_16(8, 1);
40414be3200SRob Clark 
4055ddb0bd4SMaxime Ripard 	ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
40614be3200SRob Clark 						  min_scale, max_scale,
40714be3200SRob Clark 						  true, true);
40814be3200SRob Clark 	if (ret)
40914be3200SRob Clark 		return ret;
41014be3200SRob Clark 
41114be3200SRob Clark 	/*
41214be3200SRob Clark 	 * if the visibility of the plane changes (i.e, if the cursor is
41314be3200SRob Clark 	 * clipped out completely, we can't take the async path because
41414be3200SRob Clark 	 * we need to stage/unstage the plane from the Layer Mixer(s). We
41514be3200SRob Clark 	 * also assign/unassign the hwpipe(s) tied to the plane. We avoid
41614be3200SRob Clark 	 * taking the fast path for both these reasons.
41714be3200SRob Clark 	 */
4185ddb0bd4SMaxime Ripard 	if (new_plane_state->visible != plane->state->visible)
41914be3200SRob Clark 		return -EINVAL;
42014be3200SRob Clark 
42114be3200SRob Clark 	return 0;
42214be3200SRob Clark }
42314be3200SRob Clark 
mdp5_plane_atomic_async_update(struct drm_plane * plane,struct drm_atomic_state * state)42414be3200SRob Clark static void mdp5_plane_atomic_async_update(struct drm_plane *plane,
4255ddb0bd4SMaxime Ripard 					   struct drm_atomic_state *state)
42614be3200SRob Clark {
4275ddb0bd4SMaxime Ripard 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
4285ddb0bd4SMaxime Ripard 									   plane);
429474d952bSHelen Koike 	struct drm_framebuffer *old_fb = plane->state->fb;
430474d952bSHelen Koike 
43114be3200SRob Clark 	plane->state->src_x = new_state->src_x;
43214be3200SRob Clark 	plane->state->src_y = new_state->src_y;
43314be3200SRob Clark 	plane->state->crtc_x = new_state->crtc_x;
43414be3200SRob Clark 	plane->state->crtc_y = new_state->crtc_y;
43514be3200SRob Clark 
43614be3200SRob Clark 	if (plane_enabled(new_state)) {
43714be3200SRob Clark 		struct mdp5_ctl *ctl;
43814be3200SRob Clark 		struct mdp5_pipeline *pipeline =
43942eb2f72SVille Syrjälä 					mdp5_crtc_get_pipeline(new_state->crtc);
44014be3200SRob Clark 		int ret;
44114be3200SRob Clark 
44214be3200SRob Clark 		ret = mdp5_plane_mode_set(plane, new_state->crtc, new_state->fb,
44314be3200SRob Clark 				&new_state->src, &new_state->dst);
44414be3200SRob Clark 		WARN_ON(ret < 0);
44514be3200SRob Clark 
44614be3200SRob Clark 		ctl = mdp5_crtc_get_ctl(new_state->crtc);
44714be3200SRob Clark 
448f9cb8d8dSRob Clark 		mdp5_ctl_commit(ctl, pipeline, mdp5_plane_get_flush(plane), true);
44914be3200SRob Clark 	}
45014be3200SRob Clark 
45114be3200SRob Clark 	*to_mdp5_plane_state(plane->state) =
45214be3200SRob Clark 		*to_mdp5_plane_state(new_state);
453474d952bSHelen Koike 
454474d952bSHelen Koike 	new_state->fb = old_fb;
45514be3200SRob Clark }
45614be3200SRob Clark 
45714be3200SRob Clark static const struct drm_plane_helper_funcs mdp5_plane_helper_funcs = {
4589e4dde28SRob Clark 		.prepare_fb = mdp5_plane_prepare_fb,
45914be3200SRob Clark 		.cleanup_fb = mdp5_plane_cleanup_fb,
46014be3200SRob Clark 		.atomic_check = mdp5_plane_atomic_check,
46114be3200SRob Clark 		.atomic_update = mdp5_plane_atomic_update,
46214be3200SRob Clark 		.atomic_async_check = mdp5_plane_atomic_async_check,
46314be3200SRob Clark 		.atomic_async_update = mdp5_plane_atomic_async_update,
46414be3200SRob Clark };
46514be3200SRob Clark 
set_scanout_locked(struct mdp5_kms * mdp5_kms,enum mdp5_pipe pipe,struct drm_framebuffer * fb)46614be3200SRob Clark static void set_scanout_locked(struct mdp5_kms *mdp5_kms,
46714be3200SRob Clark 			       enum mdp5_pipe pipe,
46814be3200SRob Clark 			       struct drm_framebuffer *fb)
46914be3200SRob Clark {
47014be3200SRob Clark 	struct msm_kms *kms = &mdp5_kms->base.base;
47114be3200SRob Clark 
47214be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_A(pipe),
47314be3200SRob Clark 			MDP5_PIPE_SRC_STRIDE_A_P0(fb->pitches[0]) |
47414be3200SRob Clark 			MDP5_PIPE_SRC_STRIDE_A_P1(fb->pitches[1]));
47514be3200SRob Clark 
47614be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_B(pipe),
47714be3200SRob Clark 			MDP5_PIPE_SRC_STRIDE_B_P2(fb->pitches[2]) |
47814be3200SRob Clark 			MDP5_PIPE_SRC_STRIDE_B_P3(fb->pitches[3]));
47914be3200SRob Clark 
48014be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC0_ADDR(pipe),
48114be3200SRob Clark 			msm_framebuffer_iova(fb, kms->aspace, 0));
48214be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC1_ADDR(pipe),
48314be3200SRob Clark 			msm_framebuffer_iova(fb, kms->aspace, 1));
48414be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC2_ADDR(pipe),
48514be3200SRob Clark 			msm_framebuffer_iova(fb, kms->aspace, 2));
48614be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC3_ADDR(pipe),
48714be3200SRob Clark 			msm_framebuffer_iova(fb, kms->aspace, 3));
48814be3200SRob Clark }
48914be3200SRob Clark 
49014be3200SRob Clark /* Note: mdp5_plane->pipe_lock must be locked */
csc_disable(struct mdp5_kms * mdp5_kms,enum mdp5_pipe pipe)49114be3200SRob Clark static void csc_disable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe)
49214be3200SRob Clark {
49314be3200SRob Clark 	uint32_t value = mdp5_read(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe)) &
49414be3200SRob Clark 			 ~MDP5_PIPE_OP_MODE_CSC_1_EN;
49514be3200SRob Clark 
49614be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), value);
49714be3200SRob Clark }
49814be3200SRob Clark 
49914be3200SRob Clark /* Note: mdp5_plane->pipe_lock must be locked */
csc_enable(struct mdp5_kms * mdp5_kms,enum mdp5_pipe pipe,struct csc_cfg * csc)50014be3200SRob Clark static void csc_enable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe,
50114be3200SRob Clark 		struct csc_cfg *csc)
50214be3200SRob Clark {
50314be3200SRob Clark 	uint32_t  i, mode = 0; /* RGB, no CSC */
50414be3200SRob Clark 	uint32_t *matrix;
50514be3200SRob Clark 
50614be3200SRob Clark 	if (unlikely(!csc))
50714be3200SRob Clark 		return;
50814be3200SRob Clark 
50914be3200SRob Clark 	if ((csc->type == CSC_YUV2RGB) || (CSC_YUV2YUV == csc->type))
51014be3200SRob Clark 		mode |= MDP5_PIPE_OP_MODE_CSC_SRC_DATA_FORMAT(DATA_FORMAT_YUV);
51114be3200SRob Clark 	if ((csc->type == CSC_RGB2YUV) || (CSC_YUV2YUV == csc->type))
51214be3200SRob Clark 		mode |= MDP5_PIPE_OP_MODE_CSC_DST_DATA_FORMAT(DATA_FORMAT_YUV);
51314be3200SRob Clark 	mode |= MDP5_PIPE_OP_MODE_CSC_1_EN;
51414be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), mode);
51514be3200SRob Clark 
51614be3200SRob Clark 	matrix = csc->matrix;
51714be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_0(pipe),
51814be3200SRob Clark 			MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_11(matrix[0]) |
51914be3200SRob Clark 			MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_12(matrix[1]));
52014be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_1(pipe),
52114be3200SRob Clark 			MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_13(matrix[2]) |
52214be3200SRob Clark 			MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_21(matrix[3]));
52314be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_2(pipe),
52414be3200SRob Clark 			MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_22(matrix[4]) |
52514be3200SRob Clark 			MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_23(matrix[5]));
52614be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_3(pipe),
52714be3200SRob Clark 			MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_31(matrix[6]) |
52814be3200SRob Clark 			MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_32(matrix[7]));
52914be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_4(pipe),
53014be3200SRob Clark 			MDP5_PIPE_CSC_1_MATRIX_COEFF_4_COEFF_33(matrix[8]));
53114be3200SRob Clark 
53214be3200SRob Clark 	for (i = 0; i < ARRAY_SIZE(csc->pre_bias); i++) {
53314be3200SRob Clark 		uint32_t *pre_clamp = csc->pre_clamp;
53414be3200SRob Clark 		uint32_t *post_clamp = csc->post_clamp;
53514be3200SRob Clark 
53614be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_CLAMP(pipe, i),
53714be3200SRob Clark 			MDP5_PIPE_CSC_1_PRE_CLAMP_REG_HIGH(pre_clamp[2*i+1]) |
53814be3200SRob Clark 			MDP5_PIPE_CSC_1_PRE_CLAMP_REG_LOW(pre_clamp[2*i]));
53914be3200SRob Clark 
54014be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_CLAMP(pipe, i),
54114be3200SRob Clark 			MDP5_PIPE_CSC_1_POST_CLAMP_REG_HIGH(post_clamp[2*i+1]) |
54214be3200SRob Clark 			MDP5_PIPE_CSC_1_POST_CLAMP_REG_LOW(post_clamp[2*i]));
54314be3200SRob Clark 
54414be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_BIAS(pipe, i),
54514be3200SRob Clark 			MDP5_PIPE_CSC_1_PRE_BIAS_REG_VALUE(csc->pre_bias[i]));
54614be3200SRob Clark 
54714be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_BIAS(pipe, i),
54814be3200SRob Clark 			MDP5_PIPE_CSC_1_POST_BIAS_REG_VALUE(csc->post_bias[i]));
54914be3200SRob Clark 	}
55014be3200SRob Clark }
55114be3200SRob Clark 
55214be3200SRob Clark #define PHASE_STEP_SHIFT	21
55314be3200SRob Clark #define DOWN_SCALE_RATIO_MAX	32	/* 2^(26-21) */
55414be3200SRob Clark 
calc_phase_step(uint32_t src,uint32_t dst,uint32_t * out_phase)55514be3200SRob Clark static int calc_phase_step(uint32_t src, uint32_t dst, uint32_t *out_phase)
55614be3200SRob Clark {
55714be3200SRob Clark 	uint32_t unit;
55814be3200SRob Clark 
55914be3200SRob Clark 	if (src == 0 || dst == 0)
56014be3200SRob Clark 		return -EINVAL;
56114be3200SRob Clark 
56214be3200SRob Clark 	/*
56314be3200SRob Clark 	 * PHASE_STEP_X/Y is coded on 26 bits (25:0),
56414be3200SRob Clark 	 * where 2^21 represents the unity "1" in fixed-point hardware design.
56514be3200SRob Clark 	 * This leaves 5 bits for the integer part (downscale case):
56614be3200SRob Clark 	 *	-> maximum downscale ratio = 0b1_1111 = 31
56714be3200SRob Clark 	 */
56814be3200SRob Clark 	if (src > (dst * DOWN_SCALE_RATIO_MAX))
56914be3200SRob Clark 		return -EOVERFLOW;
57014be3200SRob Clark 
57114be3200SRob Clark 	unit = 1 << PHASE_STEP_SHIFT;
57214be3200SRob Clark 	*out_phase = mult_frac(unit, src, dst);
57314be3200SRob Clark 
57414be3200SRob Clark 	return 0;
57514be3200SRob Clark }
57614be3200SRob Clark 
calc_scalex_steps(struct drm_plane * plane,uint32_t pixel_format,uint32_t src,uint32_t dest,uint32_t phasex_steps[COMP_MAX])57714be3200SRob Clark static int calc_scalex_steps(struct drm_plane *plane,
57814be3200SRob Clark 		uint32_t pixel_format, uint32_t src, uint32_t dest,
57914be3200SRob Clark 		uint32_t phasex_steps[COMP_MAX])
58014be3200SRob Clark {
581f3e9632cSMaxime Ripard 	const struct drm_format_info *info = drm_format_info(pixel_format);
58214be3200SRob Clark 	struct mdp5_kms *mdp5_kms = get_kms(plane);
58314be3200SRob Clark 	struct device *dev = mdp5_kms->dev->dev;
58414be3200SRob Clark 	uint32_t phasex_step;
58514be3200SRob Clark 	int ret;
58614be3200SRob Clark 
58714be3200SRob Clark 	ret = calc_phase_step(src, dest, &phasex_step);
58814be3200SRob Clark 	if (ret) {
5896a41da17SMamta Shukla 		DRM_DEV_ERROR(dev, "X scaling (%d->%d) failed: %d\n", src, dest, ret);
59014be3200SRob Clark 		return ret;
59114be3200SRob Clark 	}
59214be3200SRob Clark 
59314be3200SRob Clark 	phasex_steps[COMP_0]   = phasex_step;
59414be3200SRob Clark 	phasex_steps[COMP_3]   = phasex_step;
595f3e9632cSMaxime Ripard 	phasex_steps[COMP_1_2] = phasex_step / info->hsub;
59614be3200SRob Clark 
59714be3200SRob Clark 	return 0;
59814be3200SRob Clark }
59914be3200SRob Clark 
calc_scaley_steps(struct drm_plane * plane,uint32_t pixel_format,uint32_t src,uint32_t dest,uint32_t phasey_steps[COMP_MAX])60014be3200SRob Clark static int calc_scaley_steps(struct drm_plane *plane,
60114be3200SRob Clark 		uint32_t pixel_format, uint32_t src, uint32_t dest,
60214be3200SRob Clark 		uint32_t phasey_steps[COMP_MAX])
60314be3200SRob Clark {
604f3e9632cSMaxime Ripard 	const struct drm_format_info *info = drm_format_info(pixel_format);
60514be3200SRob Clark 	struct mdp5_kms *mdp5_kms = get_kms(plane);
60614be3200SRob Clark 	struct device *dev = mdp5_kms->dev->dev;
60714be3200SRob Clark 	uint32_t phasey_step;
60814be3200SRob Clark 	int ret;
60914be3200SRob Clark 
61014be3200SRob Clark 	ret = calc_phase_step(src, dest, &phasey_step);
61114be3200SRob Clark 	if (ret) {
6126a41da17SMamta Shukla 		DRM_DEV_ERROR(dev, "Y scaling (%d->%d) failed: %d\n", src, dest, ret);
61314be3200SRob Clark 		return ret;
61414be3200SRob Clark 	}
61514be3200SRob Clark 
61614be3200SRob Clark 	phasey_steps[COMP_0]   = phasey_step;
61714be3200SRob Clark 	phasey_steps[COMP_3]   = phasey_step;
618f3e9632cSMaxime Ripard 	phasey_steps[COMP_1_2] = phasey_step / info->vsub;
61914be3200SRob Clark 
62014be3200SRob Clark 	return 0;
62114be3200SRob Clark }
62214be3200SRob Clark 
get_scale_config(const struct msm_format * format,uint32_t src,uint32_t dst,bool horz)6230e67f514SDmitry Baryshkov static uint32_t get_scale_config(const struct msm_format *format,
62414be3200SRob Clark 		uint32_t src, uint32_t dst, bool horz)
62514be3200SRob Clark {
6260e67f514SDmitry Baryshkov 	const struct drm_format_info *info = drm_format_info(format->pixel_format);
6270e67f514SDmitry Baryshkov 	bool yuv = MSM_FORMAT_IS_YUV(format);
6287120d8a0SDmitry Baryshkov 	bool scaling = yuv ? true : (src != dst);
629f3e9632cSMaxime Ripard 	uint32_t sub;
63014be3200SRob Clark 	uint32_t ya_filter, uv_filter;
63114be3200SRob Clark 
63214be3200SRob Clark 	if (!scaling)
63314be3200SRob Clark 		return 0;
63414be3200SRob Clark 
63514be3200SRob Clark 	if (yuv) {
636f3e9632cSMaxime Ripard 		sub = horz ? info->hsub : info->vsub;
63714be3200SRob Clark 		uv_filter = ((src / sub) <= dst) ?
63814be3200SRob Clark 				   SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
63914be3200SRob Clark 	}
64014be3200SRob Clark 	ya_filter = (src <= dst) ? SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
64114be3200SRob Clark 
64214be3200SRob Clark 	if (horz)
64314be3200SRob Clark 		return  MDP5_PIPE_SCALE_CONFIG_SCALEX_EN |
64414be3200SRob Clark 			MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_0(ya_filter) |
64514be3200SRob Clark 			MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_3(ya_filter) |
64614be3200SRob Clark 			COND(yuv, MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_1_2(uv_filter));
64714be3200SRob Clark 	else
64814be3200SRob Clark 		return  MDP5_PIPE_SCALE_CONFIG_SCALEY_EN |
64914be3200SRob Clark 			MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_0(ya_filter) |
65014be3200SRob Clark 			MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_3(ya_filter) |
65114be3200SRob Clark 			COND(yuv, MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_1_2(uv_filter));
65214be3200SRob Clark }
65314be3200SRob Clark 
calc_pixel_ext(const struct msm_format * format,uint32_t src,uint32_t dst,uint32_t phase_step[2],int pix_ext_edge1[COMP_MAX],int pix_ext_edge2[COMP_MAX],bool horz)6540e67f514SDmitry Baryshkov static void calc_pixel_ext(const struct msm_format *format,
65514be3200SRob Clark 		uint32_t src, uint32_t dst, uint32_t phase_step[2],
65614be3200SRob Clark 		int pix_ext_edge1[COMP_MAX], int pix_ext_edge2[COMP_MAX],
65714be3200SRob Clark 		bool horz)
65814be3200SRob Clark {
6590e67f514SDmitry Baryshkov 	bool scaling = MSM_FORMAT_IS_YUV(format) ? true : (src != dst);
66014be3200SRob Clark 	int i;
66114be3200SRob Clark 
66214be3200SRob Clark 	/*
66314be3200SRob Clark 	 * Note:
66414be3200SRob Clark 	 * We assume here that:
66514be3200SRob Clark 	 *     1. PCMN filter is used for downscale
66614be3200SRob Clark 	 *     2. bilinear filter is used for upscale
66714be3200SRob Clark 	 *     3. we are in a single pipe configuration
66814be3200SRob Clark 	 */
66914be3200SRob Clark 
67014be3200SRob Clark 	for (i = 0; i < COMP_MAX; i++) {
67114be3200SRob Clark 		pix_ext_edge1[i] = 0;
67214be3200SRob Clark 		pix_ext_edge2[i] = scaling ? 1 : 0;
67314be3200SRob Clark 	}
67414be3200SRob Clark }
67514be3200SRob Clark 
mdp5_write_pixel_ext(struct mdp5_kms * mdp5_kms,enum mdp5_pipe pipe,const struct msm_format * format,uint32_t src_w,int pe_left[COMP_MAX],int pe_right[COMP_MAX],uint32_t src_h,int pe_top[COMP_MAX],int pe_bottom[COMP_MAX])67614be3200SRob Clark static void mdp5_write_pixel_ext(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe,
6770e67f514SDmitry Baryshkov 	const struct msm_format *format,
67814be3200SRob Clark 	uint32_t src_w, int pe_left[COMP_MAX], int pe_right[COMP_MAX],
67914be3200SRob Clark 	uint32_t src_h, int pe_top[COMP_MAX], int pe_bottom[COMP_MAX])
68014be3200SRob Clark {
6810e67f514SDmitry Baryshkov 	const struct drm_format_info *info = drm_format_info(format->pixel_format);
68214be3200SRob Clark 	uint32_t lr, tb, req;
68314be3200SRob Clark 	int i;
68414be3200SRob Clark 
68514be3200SRob Clark 	for (i = 0; i < COMP_MAX; i++) {
68614be3200SRob Clark 		uint32_t roi_w = src_w;
68714be3200SRob Clark 		uint32_t roi_h = src_h;
68814be3200SRob Clark 
6890e67f514SDmitry Baryshkov 		if (MSM_FORMAT_IS_YUV(format) && i == COMP_1_2) {
690f3e9632cSMaxime Ripard 			roi_w /= info->hsub;
691f3e9632cSMaxime Ripard 			roi_h /= info->vsub;
69214be3200SRob Clark 		}
69314be3200SRob Clark 
69414be3200SRob Clark 		lr  = (pe_left[i] >= 0) ?
69514be3200SRob Clark 			MDP5_PIPE_SW_PIX_EXT_LR_LEFT_RPT(pe_left[i]) :
69614be3200SRob Clark 			MDP5_PIPE_SW_PIX_EXT_LR_LEFT_OVF(pe_left[i]);
69714be3200SRob Clark 
69814be3200SRob Clark 		lr |= (pe_right[i] >= 0) ?
69914be3200SRob Clark 			MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_RPT(pe_right[i]) :
70014be3200SRob Clark 			MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_OVF(pe_right[i]);
70114be3200SRob Clark 
70214be3200SRob Clark 		tb  = (pe_top[i] >= 0) ?
70314be3200SRob Clark 			MDP5_PIPE_SW_PIX_EXT_TB_TOP_RPT(pe_top[i]) :
70414be3200SRob Clark 			MDP5_PIPE_SW_PIX_EXT_TB_TOP_OVF(pe_top[i]);
70514be3200SRob Clark 
70614be3200SRob Clark 		tb |= (pe_bottom[i] >= 0) ?
70714be3200SRob Clark 			MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_RPT(pe_bottom[i]) :
70814be3200SRob Clark 			MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_OVF(pe_bottom[i]);
70914be3200SRob Clark 
71014be3200SRob Clark 		req  = MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_LEFT_RIGHT(roi_w +
71114be3200SRob Clark 				pe_left[i] + pe_right[i]);
71214be3200SRob Clark 
71314be3200SRob Clark 		req |= MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_TOP_BOTTOM(roi_h +
71414be3200SRob Clark 				pe_top[i] + pe_bottom[i]);
71514be3200SRob Clark 
71614be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_LR(pipe, i), lr);
71714be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_TB(pipe, i), tb);
71814be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS(pipe, i), req);
71914be3200SRob Clark 
72014be3200SRob Clark 		DBG("comp-%d (L/R): rpt=%d/%d, ovf=%d/%d, req=%d", i,
72114be3200SRob Clark 			FIELD(lr,  MDP5_PIPE_SW_PIX_EXT_LR_LEFT_RPT),
72214be3200SRob Clark 			FIELD(lr,  MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_RPT),
72314be3200SRob Clark 			FIELD(lr,  MDP5_PIPE_SW_PIX_EXT_LR_LEFT_OVF),
72414be3200SRob Clark 			FIELD(lr,  MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_OVF),
72514be3200SRob Clark 			FIELD(req, MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_LEFT_RIGHT));
72614be3200SRob Clark 
72714be3200SRob Clark 		DBG("comp-%d (T/B): rpt=%d/%d, ovf=%d/%d, req=%d", i,
72814be3200SRob Clark 			FIELD(tb,  MDP5_PIPE_SW_PIX_EXT_TB_TOP_RPT),
72914be3200SRob Clark 			FIELD(tb,  MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_RPT),
73014be3200SRob Clark 			FIELD(tb,  MDP5_PIPE_SW_PIX_EXT_TB_TOP_OVF),
73114be3200SRob Clark 			FIELD(tb,  MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_OVF),
73214be3200SRob Clark 			FIELD(req, MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_TOP_BOTTOM));
73314be3200SRob Clark 	}
73414be3200SRob Clark }
73514be3200SRob Clark 
73614be3200SRob Clark struct pixel_ext {
73714be3200SRob Clark 	int left[COMP_MAX];
73814be3200SRob Clark 	int right[COMP_MAX];
73914be3200SRob Clark 	int top[COMP_MAX];
74014be3200SRob Clark 	int bottom[COMP_MAX];
74114be3200SRob Clark };
74214be3200SRob Clark 
74314be3200SRob Clark struct phase_step {
74414be3200SRob Clark 	u32 x[COMP_MAX];
74514be3200SRob Clark 	u32 y[COMP_MAX];
74614be3200SRob Clark };
74714be3200SRob Clark 
mdp5_hwpipe_mode_set(struct mdp5_kms * mdp5_kms,struct mdp5_hw_pipe * hwpipe,struct drm_framebuffer * fb,struct phase_step * step,struct pixel_ext * pe,u32 scale_config,u32 hdecm,u32 vdecm,bool hflip,bool vflip,int crtc_x,int crtc_y,unsigned int crtc_w,unsigned int crtc_h,u32 src_img_w,u32 src_img_h,u32 src_x,u32 src_y,u32 src_w,u32 src_h)74814be3200SRob Clark static void mdp5_hwpipe_mode_set(struct mdp5_kms *mdp5_kms,
74914be3200SRob Clark 				 struct mdp5_hw_pipe *hwpipe,
75014be3200SRob Clark 				 struct drm_framebuffer *fb,
75114be3200SRob Clark 				 struct phase_step *step,
75214be3200SRob Clark 				 struct pixel_ext *pe,
75314be3200SRob Clark 				 u32 scale_config, u32 hdecm, u32 vdecm,
75414be3200SRob Clark 				 bool hflip, bool vflip,
75514be3200SRob Clark 				 int crtc_x, int crtc_y,
75614be3200SRob Clark 				 unsigned int crtc_w, unsigned int crtc_h,
75714be3200SRob Clark 				 u32 src_img_w, u32 src_img_h,
75814be3200SRob Clark 				 u32 src_x, u32 src_y,
75914be3200SRob Clark 				 u32 src_w, u32 src_h)
76014be3200SRob Clark {
76114be3200SRob Clark 	enum mdp5_pipe pipe = hwpipe->pipe;
76214be3200SRob Clark 	bool has_pe = hwpipe->caps & MDP_PIPE_CAP_SW_PIX_EXT;
7630e67f514SDmitry Baryshkov 	const struct msm_format *format =
7640e67f514SDmitry Baryshkov 			msm_framebuffer_format(fb);
76514be3200SRob Clark 
76614be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_IMG_SIZE(pipe),
76714be3200SRob Clark 			MDP5_PIPE_SRC_IMG_SIZE_WIDTH(src_img_w) |
76814be3200SRob Clark 			MDP5_PIPE_SRC_IMG_SIZE_HEIGHT(src_img_h));
76914be3200SRob Clark 
77014be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_SIZE(pipe),
77114be3200SRob Clark 			MDP5_PIPE_SRC_SIZE_WIDTH(src_w) |
77214be3200SRob Clark 			MDP5_PIPE_SRC_SIZE_HEIGHT(src_h));
77314be3200SRob Clark 
77414be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_XY(pipe),
77514be3200SRob Clark 			MDP5_PIPE_SRC_XY_X(src_x) |
77614be3200SRob Clark 			MDP5_PIPE_SRC_XY_Y(src_y));
77714be3200SRob Clark 
77814be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_SIZE(pipe),
77914be3200SRob Clark 			MDP5_PIPE_OUT_SIZE_WIDTH(crtc_w) |
78014be3200SRob Clark 			MDP5_PIPE_OUT_SIZE_HEIGHT(crtc_h));
78114be3200SRob Clark 
78214be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_XY(pipe),
78314be3200SRob Clark 			MDP5_PIPE_OUT_XY_X(crtc_x) |
78414be3200SRob Clark 			MDP5_PIPE_OUT_XY_Y(crtc_y));
78514be3200SRob Clark 
78614be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_FORMAT(pipe),
78714be3200SRob Clark 			MDP5_PIPE_SRC_FORMAT_A_BPC(format->bpc_a) |
7880e67f514SDmitry Baryshkov 			MDP5_PIPE_SRC_FORMAT_R_BPC(format->bpc_r_cr) |
7890e67f514SDmitry Baryshkov 			MDP5_PIPE_SRC_FORMAT_G_BPC(format->bpc_g_y) |
7900e67f514SDmitry Baryshkov 			MDP5_PIPE_SRC_FORMAT_B_BPC(format->bpc_b_cb) |
79114be3200SRob Clark 			COND(format->alpha_enable, MDP5_PIPE_SRC_FORMAT_ALPHA_ENABLE) |
7920e67f514SDmitry Baryshkov 			MDP5_PIPE_SRC_FORMAT_CPP(format->bpp - 1) |
79314be3200SRob Clark 			MDP5_PIPE_SRC_FORMAT_UNPACK_COUNT(format->unpack_count - 1) |
794e0925148SDmitry Baryshkov 			COND(format->flags & MSM_FORMAT_FLAG_UNPACK_TIGHT,
795e0925148SDmitry Baryshkov 			     MDP5_PIPE_SRC_FORMAT_UNPACK_TIGHT) |
79614be3200SRob Clark 			MDP5_PIPE_SRC_FORMAT_FETCH_TYPE(format->fetch_type) |
79714be3200SRob Clark 			MDP5_PIPE_SRC_FORMAT_CHROMA_SAMP(format->chroma_sample));
79814be3200SRob Clark 
79914be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_UNPACK(pipe),
8000e67f514SDmitry Baryshkov 			MDP5_PIPE_SRC_UNPACK_ELEM0(format->element[0]) |
8010e67f514SDmitry Baryshkov 			MDP5_PIPE_SRC_UNPACK_ELEM1(format->element[1]) |
8020e67f514SDmitry Baryshkov 			MDP5_PIPE_SRC_UNPACK_ELEM2(format->element[2]) |
8030e67f514SDmitry Baryshkov 			MDP5_PIPE_SRC_UNPACK_ELEM3(format->element[3]));
80414be3200SRob Clark 
80514be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_OP_MODE(pipe),
80614be3200SRob Clark 			(hflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_LR : 0) |
80714be3200SRob Clark 			(vflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_UD : 0) |
80814be3200SRob Clark 			COND(has_pe, MDP5_PIPE_SRC_OP_MODE_SW_PIX_EXT_OVERRIDE) |
80914be3200SRob Clark 			MDP5_PIPE_SRC_OP_MODE_BWC(BWC_LOSSLESS));
81014be3200SRob Clark 
81114be3200SRob Clark 	/* not using secure mode: */
81214be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_ADDR_SW_STATUS(pipe), 0);
81314be3200SRob Clark 
81414be3200SRob Clark 	if (hwpipe->caps & MDP_PIPE_CAP_SW_PIX_EXT)
81514be3200SRob Clark 		mdp5_write_pixel_ext(mdp5_kms, pipe, format,
81614be3200SRob Clark 				src_w, pe->left, pe->right,
81714be3200SRob Clark 				src_h, pe->top, pe->bottom);
81814be3200SRob Clark 
81914be3200SRob Clark 	if (hwpipe->caps & MDP_PIPE_CAP_SCALE) {
82014be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_X(pipe),
82114be3200SRob Clark 				step->x[COMP_0]);
82214be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_Y(pipe),
82314be3200SRob Clark 				step->y[COMP_0]);
82414be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_X(pipe),
82514be3200SRob Clark 				step->x[COMP_1_2]);
82614be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_Y(pipe),
82714be3200SRob Clark 				step->y[COMP_1_2]);
82814be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_DECIMATION(pipe),
82914be3200SRob Clark 				MDP5_PIPE_DECIMATION_VERT(vdecm) |
83014be3200SRob Clark 				MDP5_PIPE_DECIMATION_HORZ(hdecm));
83114be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CONFIG(pipe),
83214be3200SRob Clark 			   scale_config);
83314be3200SRob Clark 	}
83414be3200SRob Clark 
83514be3200SRob Clark 	if (hwpipe->caps & MDP_PIPE_CAP_CSC) {
8360e67f514SDmitry Baryshkov 		if (MSM_FORMAT_IS_YUV(format))
83714be3200SRob Clark 			csc_enable(mdp5_kms, pipe,
83814be3200SRob Clark 					mdp_get_default_csc_cfg(CSC_YUV2RGB));
83914be3200SRob Clark 		else
84014be3200SRob Clark 			csc_disable(mdp5_kms, pipe);
84114be3200SRob Clark 	}
84214be3200SRob Clark 
84314be3200SRob Clark 	set_scanout_locked(mdp5_kms, pipe, fb);
84414be3200SRob Clark }
84514be3200SRob Clark 
mdp5_plane_mode_set(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,struct drm_rect * src,struct drm_rect * dest)84614be3200SRob Clark static int mdp5_plane_mode_set(struct drm_plane *plane,
84714be3200SRob Clark 		struct drm_crtc *crtc, struct drm_framebuffer *fb,
84814be3200SRob Clark 		struct drm_rect *src, struct drm_rect *dest)
84914be3200SRob Clark {
85014be3200SRob Clark 	struct drm_plane_state *pstate = plane->state;
85114be3200SRob Clark 	struct mdp5_hw_pipe *hwpipe = to_mdp5_plane_state(pstate)->hwpipe;
85214be3200SRob Clark 	struct mdp5_kms *mdp5_kms = get_kms(plane);
85314be3200SRob Clark 	enum mdp5_pipe pipe = hwpipe->pipe;
85414be3200SRob Clark 	struct mdp5_hw_pipe *right_hwpipe;
8550e67f514SDmitry Baryshkov 	const struct msm_format *format;
85614be3200SRob Clark 	uint32_t nplanes, config = 0;
85714be3200SRob Clark 	struct phase_step step = { { 0 } };
85814be3200SRob Clark 	struct pixel_ext pe = { { 0 } };
85914be3200SRob Clark 	uint32_t hdecm = 0, vdecm = 0;
86014be3200SRob Clark 	uint32_t pix_format;
86114be3200SRob Clark 	unsigned int rotation;
86214be3200SRob Clark 	bool vflip, hflip;
86314be3200SRob Clark 	int crtc_x, crtc_y;
86414be3200SRob Clark 	unsigned int crtc_w, crtc_h;
86514be3200SRob Clark 	uint32_t src_x, src_y;
86614be3200SRob Clark 	uint32_t src_w, src_h;
86714be3200SRob Clark 	uint32_t src_img_w, src_img_h;
86814be3200SRob Clark 	int ret;
86914be3200SRob Clark 
87014be3200SRob Clark 	nplanes = fb->format->num_planes;
87114be3200SRob Clark 
87214be3200SRob Clark 	/* bad formats should already be rejected: */
87314be3200SRob Clark 	if (WARN_ON(nplanes > pipe2nclients(pipe)))
87414be3200SRob Clark 		return -EINVAL;
87514be3200SRob Clark 
8760e67f514SDmitry Baryshkov 	format = msm_framebuffer_format(fb);
8770e67f514SDmitry Baryshkov 	pix_format = format->pixel_format;
87814be3200SRob Clark 
87914be3200SRob Clark 	src_x = src->x1;
88014be3200SRob Clark 	src_y = src->y1;
88114be3200SRob Clark 	src_w = drm_rect_width(src);
88214be3200SRob Clark 	src_h = drm_rect_height(src);
88314be3200SRob Clark 
88414be3200SRob Clark 	crtc_x = dest->x1;
88514be3200SRob Clark 	crtc_y = dest->y1;
88614be3200SRob Clark 	crtc_w = drm_rect_width(dest);
88714be3200SRob Clark 	crtc_h = drm_rect_height(dest);
88814be3200SRob Clark 
88914be3200SRob Clark 	/* src values are in Q16 fixed point, convert to integer: */
89014be3200SRob Clark 	src_x = src_x >> 16;
89114be3200SRob Clark 	src_y = src_y >> 16;
89214be3200SRob Clark 	src_w = src_w >> 16;
89314be3200SRob Clark 	src_h = src_h >> 16;
89414be3200SRob Clark 
89514be3200SRob Clark 	src_img_w = min(fb->width, src_w);
89614be3200SRob Clark 	src_img_h = min(fb->height, src_h);
89714be3200SRob Clark 
89814be3200SRob Clark 	DBG("%s: FB[%u] %u,%u,%u,%u -> CRTC[%u] %d,%d,%u,%u", plane->name,
89914be3200SRob Clark 			fb->base.id, src_x, src_y, src_w, src_h,
90014be3200SRob Clark 			crtc->base.id, crtc_x, crtc_y, crtc_w, crtc_h);
90114be3200SRob Clark 
90214be3200SRob Clark 	right_hwpipe = to_mdp5_plane_state(pstate)->r_hwpipe;
90314be3200SRob Clark 	if (right_hwpipe) {
90414be3200SRob Clark 		/*
90514be3200SRob Clark 		 * if the plane comprises of 2 hw pipes, assume that the width
90614be3200SRob Clark 		 * is split equally across them. The only parameters that varies
90714be3200SRob Clark 		 * between the 2 pipes are src_x and crtc_x
90814be3200SRob Clark 		 */
90914be3200SRob Clark 		crtc_w /= 2;
91014be3200SRob Clark 		src_w /= 2;
91114be3200SRob Clark 		src_img_w /= 2;
91214be3200SRob Clark 	}
91314be3200SRob Clark 
91414be3200SRob Clark 	ret = calc_scalex_steps(plane, pix_format, src_w, crtc_w, step.x);
91514be3200SRob Clark 	if (ret)
91614be3200SRob Clark 		return ret;
91714be3200SRob Clark 
91814be3200SRob Clark 	ret = calc_scaley_steps(plane, pix_format, src_h, crtc_h, step.y);
91914be3200SRob Clark 	if (ret)
92014be3200SRob Clark 		return ret;
92114be3200SRob Clark 
92214be3200SRob Clark 	if (hwpipe->caps & MDP_PIPE_CAP_SW_PIX_EXT) {
92314be3200SRob Clark 		calc_pixel_ext(format, src_w, crtc_w, step.x,
92414be3200SRob Clark 			       pe.left, pe.right, true);
92514be3200SRob Clark 		calc_pixel_ext(format, src_h, crtc_h, step.y,
92614be3200SRob Clark 			       pe.top, pe.bottom, false);
92714be3200SRob Clark 	}
92814be3200SRob Clark 
92914be3200SRob Clark 	/* TODO calc hdecm, vdecm */
93014be3200SRob Clark 
93114be3200SRob Clark 	/* SCALE is used to both scale and up-sample chroma components */
93214be3200SRob Clark 	config |= get_scale_config(format, src_w, crtc_w, true);
93314be3200SRob Clark 	config |= get_scale_config(format, src_h, crtc_h, false);
93414be3200SRob Clark 	DBG("scale config = %x", config);
93514be3200SRob Clark 
93614be3200SRob Clark 	rotation = drm_rotation_simplify(pstate->rotation,
93714be3200SRob Clark 					 DRM_MODE_ROTATE_0 |
93814be3200SRob Clark 					 DRM_MODE_REFLECT_X |
93914be3200SRob Clark 					 DRM_MODE_REFLECT_Y);
94014be3200SRob Clark 	hflip = !!(rotation & DRM_MODE_REFLECT_X);
94114be3200SRob Clark 	vflip = !!(rotation & DRM_MODE_REFLECT_Y);
94214be3200SRob Clark 
94314be3200SRob Clark 	mdp5_hwpipe_mode_set(mdp5_kms, hwpipe, fb, &step, &pe,
94414be3200SRob Clark 			     config, hdecm, vdecm, hflip, vflip,
94514be3200SRob Clark 			     crtc_x, crtc_y, crtc_w, crtc_h,
94614be3200SRob Clark 			     src_img_w, src_img_h,
94714be3200SRob Clark 			     src_x, src_y, src_w, src_h);
94814be3200SRob Clark 	if (right_hwpipe)
94914be3200SRob Clark 		mdp5_hwpipe_mode_set(mdp5_kms, right_hwpipe, fb, &step, &pe,
95014be3200SRob Clark 				     config, hdecm, vdecm, hflip, vflip,
95114be3200SRob Clark 				     crtc_x + crtc_w, crtc_y, crtc_w, crtc_h,
95214be3200SRob Clark 				     src_img_w, src_img_h,
95314be3200SRob Clark 				     src_x + src_w, src_y, src_w, src_h);
95414be3200SRob Clark 
95514be3200SRob Clark 	return ret;
95614be3200SRob Clark }
95714be3200SRob Clark 
95814be3200SRob Clark /*
95914be3200SRob Clark  * Use this func and the one below only after the atomic state has been
96014be3200SRob Clark  * successfully swapped
96114be3200SRob Clark  */
mdp5_plane_pipe(struct drm_plane * plane)96214be3200SRob Clark enum mdp5_pipe mdp5_plane_pipe(struct drm_plane *plane)
96314be3200SRob Clark {
96414be3200SRob Clark 	struct mdp5_plane_state *pstate = to_mdp5_plane_state(plane->state);
96514be3200SRob Clark 
96614be3200SRob Clark 	if (WARN_ON(!pstate->hwpipe))
96714be3200SRob Clark 		return SSPP_NONE;
96814be3200SRob Clark 
96914be3200SRob Clark 	return pstate->hwpipe->pipe;
97014be3200SRob Clark }
97114be3200SRob Clark 
mdp5_plane_right_pipe(struct drm_plane * plane)97214be3200SRob Clark enum mdp5_pipe mdp5_plane_right_pipe(struct drm_plane *plane)
97314be3200SRob Clark {
97414be3200SRob Clark 	struct mdp5_plane_state *pstate = to_mdp5_plane_state(plane->state);
97514be3200SRob Clark 
97614be3200SRob Clark 	if (!pstate->r_hwpipe)
97714be3200SRob Clark 		return SSPP_NONE;
97814be3200SRob Clark 
97914be3200SRob Clark 	return pstate->r_hwpipe->pipe;
98014be3200SRob Clark }
98114be3200SRob Clark 
mdp5_plane_get_flush(struct drm_plane * plane)98214be3200SRob Clark uint32_t mdp5_plane_get_flush(struct drm_plane *plane)
98314be3200SRob Clark {
98414be3200SRob Clark 	struct mdp5_plane_state *pstate = to_mdp5_plane_state(plane->state);
98514be3200SRob Clark 	u32 mask;
98614be3200SRob Clark 
98714be3200SRob Clark 	if (WARN_ON(!pstate->hwpipe))
98814be3200SRob Clark 		return 0;
98914be3200SRob Clark 
99014be3200SRob Clark 	mask = pstate->hwpipe->flush_mask;
99114be3200SRob Clark 
99214be3200SRob Clark 	if (pstate->r_hwpipe)
99314be3200SRob Clark 		mask |= pstate->r_hwpipe->flush_mask;
99414be3200SRob Clark 
99514be3200SRob Clark 	return mask;
99614be3200SRob Clark }
99714be3200SRob Clark 
998966c5deeSDmitry Baryshkov static const uint32_t mdp5_plane_formats[] = {
999966c5deeSDmitry Baryshkov 	DRM_FORMAT_ARGB8888,
1000966c5deeSDmitry Baryshkov 	DRM_FORMAT_ABGR8888,
1001966c5deeSDmitry Baryshkov 	DRM_FORMAT_RGBA8888,
1002966c5deeSDmitry Baryshkov 	DRM_FORMAT_BGRA8888,
1003966c5deeSDmitry Baryshkov 	DRM_FORMAT_XRGB8888,
1004966c5deeSDmitry Baryshkov 	DRM_FORMAT_XBGR8888,
1005966c5deeSDmitry Baryshkov 	DRM_FORMAT_RGBX8888,
1006966c5deeSDmitry Baryshkov 	DRM_FORMAT_BGRX8888,
1007966c5deeSDmitry Baryshkov 	DRM_FORMAT_RGB888,
1008966c5deeSDmitry Baryshkov 	DRM_FORMAT_BGR888,
1009966c5deeSDmitry Baryshkov 	DRM_FORMAT_RGB565,
1010966c5deeSDmitry Baryshkov 	DRM_FORMAT_BGR565,
1011966c5deeSDmitry Baryshkov 
1012966c5deeSDmitry Baryshkov 	DRM_FORMAT_NV12,
1013966c5deeSDmitry Baryshkov 	DRM_FORMAT_NV21,
1014966c5deeSDmitry Baryshkov 	DRM_FORMAT_NV16,
1015966c5deeSDmitry Baryshkov 	DRM_FORMAT_NV61,
1016966c5deeSDmitry Baryshkov 	DRM_FORMAT_VYUY,
1017966c5deeSDmitry Baryshkov 	DRM_FORMAT_UYVY,
1018966c5deeSDmitry Baryshkov 	DRM_FORMAT_YUYV,
1019966c5deeSDmitry Baryshkov 	DRM_FORMAT_YVYU,
1020966c5deeSDmitry Baryshkov 	DRM_FORMAT_YUV420,
1021966c5deeSDmitry Baryshkov 	DRM_FORMAT_YVU420,
1022966c5deeSDmitry Baryshkov };
1023966c5deeSDmitry Baryshkov 
102414be3200SRob Clark /* initialize plane */
mdp5_plane_init(struct drm_device * dev,enum drm_plane_type type)102514be3200SRob Clark struct drm_plane *mdp5_plane_init(struct drm_device *dev,
102614be3200SRob Clark 				  enum drm_plane_type type)
102714be3200SRob Clark {
102814be3200SRob Clark 	struct drm_plane *plane = NULL;
102914be3200SRob Clark 	struct mdp5_plane *mdp5_plane;
103014be3200SRob Clark 
1031*ac8aabeeSDmitry Baryshkov 	mdp5_plane = drmm_universal_plane_alloc(dev, struct mdp5_plane, base,
1032*ac8aabeeSDmitry Baryshkov 						0xff, &mdp5_plane_funcs,
1033966c5deeSDmitry Baryshkov 						mdp5_plane_formats, ARRAY_SIZE(mdp5_plane_formats),
103414be3200SRob Clark 						NULL, type, NULL);
1035*ac8aabeeSDmitry Baryshkov 	if (IS_ERR(mdp5_plane))
1036*ac8aabeeSDmitry Baryshkov 		return ERR_CAST(mdp5_plane);
1037*ac8aabeeSDmitry Baryshkov 
1038*ac8aabeeSDmitry Baryshkov 	plane = &mdp5_plane->base;
103914be3200SRob Clark 
104014be3200SRob Clark 	drm_plane_helper_add(plane, &mdp5_plane_helper_funcs);
104114be3200SRob Clark 
104214be3200SRob Clark 	mdp5_plane_install_properties(plane, &plane->base);
104314be3200SRob Clark 
1044648fdc3fSBrian Masney 	drm_plane_enable_fb_damage_clips(plane);
1045648fdc3fSBrian Masney 
104614be3200SRob Clark 	return plane;
104714be3200SRob Clark }
1048