xref: /linux/drivers/gpu/drm/msm/disp/mdp5/mdp5_ctl.c (revision 4db102dcb0396a4ccf89b1eac0f4eb3fd167a080)
197fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
214be3200SRob Clark /*
314be3200SRob Clark  * Copyright (c) 2014-2015 The Linux Foundation. All rights reserved.
414be3200SRob Clark  */
514be3200SRob Clark 
614be3200SRob Clark #include "mdp5_kms.h"
714be3200SRob Clark #include "mdp5_ctl.h"
814be3200SRob Clark 
914be3200SRob Clark /*
1014be3200SRob Clark  * CTL - MDP Control Pool Manager
1114be3200SRob Clark  *
1214be3200SRob Clark  * Controls are shared between all display interfaces.
1314be3200SRob Clark  *
1414be3200SRob Clark  * They are intended to be used for data path configuration.
1514be3200SRob Clark  * The top level register programming describes the complete data path for
1614be3200SRob Clark  * a specific data path ID - REG_MDP5_CTL_*(<id>, ...)
1714be3200SRob Clark  *
1814be3200SRob Clark  * Hardware capabilities determine the number of concurrent data paths
1914be3200SRob Clark  *
2014be3200SRob Clark  * In certain use cases (high-resolution dual pipe), one single CTL can be
2114be3200SRob Clark  * shared across multiple CRTCs.
2214be3200SRob Clark  */
2314be3200SRob Clark 
2414be3200SRob Clark #define CTL_STAT_BUSY		0x1
2514be3200SRob Clark #define CTL_STAT_BOOKED	0x2
2614be3200SRob Clark 
2714be3200SRob Clark struct mdp5_ctl {
2814be3200SRob Clark 	struct mdp5_ctl_manager *ctlm;
2914be3200SRob Clark 
3014be3200SRob Clark 	u32 id;
3114be3200SRob Clark 
3214be3200SRob Clark 	/* CTL status bitmask */
3314be3200SRob Clark 	u32 status;
3414be3200SRob Clark 
3514be3200SRob Clark 	bool encoder_enabled;
36f9cb8d8dSRob Clark 
37f9cb8d8dSRob Clark 	/* pending flush_mask bits */
38f9cb8d8dSRob Clark 	u32 flush_mask;
3914be3200SRob Clark 
4014be3200SRob Clark 	/* REG_MDP5_CTL_*(<id>) registers access info + lock: */
4114be3200SRob Clark 	spinlock_t hw_lock;
4214be3200SRob Clark 	u32 reg_offset;
4314be3200SRob Clark 
4414be3200SRob Clark 	/* when do CTL registers need to be flushed? (mask of trigger bits) */
4514be3200SRob Clark 	u32 pending_ctl_trigger;
4614be3200SRob Clark 
4714be3200SRob Clark 	bool cursor_on;
4814be3200SRob Clark 
4914be3200SRob Clark 	/* True if the current CTL has FLUSH bits pending for single FLUSH. */
5014be3200SRob Clark 	bool flush_pending;
5114be3200SRob Clark 
5214be3200SRob Clark 	struct mdp5_ctl *pair; /* Paired CTL to be flushed together */
5314be3200SRob Clark };
5414be3200SRob Clark 
5514be3200SRob Clark struct mdp5_ctl_manager {
5614be3200SRob Clark 	struct drm_device *dev;
5714be3200SRob Clark 
5814be3200SRob Clark 	/* number of CTL / Layer Mixers in this hw config: */
5914be3200SRob Clark 	u32 nlm;
6014be3200SRob Clark 	u32 nctl;
6114be3200SRob Clark 
6214be3200SRob Clark 	/* to filter out non-present bits in the current hardware config */
6314be3200SRob Clark 	u32 flush_hw_mask;
6414be3200SRob Clark 
6514be3200SRob Clark 	/* status for single FLUSH */
6614be3200SRob Clark 	bool single_flush_supported;
6714be3200SRob Clark 	u32 single_flush_pending_mask;
6814be3200SRob Clark 
6914be3200SRob Clark 	/* pool of CTLs + lock to protect resource allocation (ctls[i].busy) */
7014be3200SRob Clark 	spinlock_t pool_lock;
7114be3200SRob Clark 	struct mdp5_ctl ctls[MAX_CTL];
7214be3200SRob Clark };
7314be3200SRob Clark 
7414be3200SRob Clark static inline
get_kms(struct mdp5_ctl_manager * ctl_mgr)7514be3200SRob Clark struct mdp5_kms *get_kms(struct mdp5_ctl_manager *ctl_mgr)
7614be3200SRob Clark {
7714be3200SRob Clark 	struct msm_drm_private *priv = ctl_mgr->dev->dev_private;
7814be3200SRob Clark 
7914be3200SRob Clark 	return to_mdp5_kms(to_mdp_kms(priv->kms));
8014be3200SRob Clark }
8114be3200SRob Clark 
8214be3200SRob Clark static inline
ctl_write(struct mdp5_ctl * ctl,u32 reg,u32 data)8314be3200SRob Clark void ctl_write(struct mdp5_ctl *ctl, u32 reg, u32 data)
8414be3200SRob Clark {
8514be3200SRob Clark 	struct mdp5_kms *mdp5_kms = get_kms(ctl->ctlm);
8614be3200SRob Clark 
8714be3200SRob Clark 	(void)ctl->reg_offset; /* TODO use this instead of mdp5_write */
8814be3200SRob Clark 	mdp5_write(mdp5_kms, reg, data);
8914be3200SRob Clark }
9014be3200SRob Clark 
9114be3200SRob Clark static inline
ctl_read(struct mdp5_ctl * ctl,u32 reg)9214be3200SRob Clark u32 ctl_read(struct mdp5_ctl *ctl, u32 reg)
9314be3200SRob Clark {
9414be3200SRob Clark 	struct mdp5_kms *mdp5_kms = get_kms(ctl->ctlm);
9514be3200SRob Clark 
9614be3200SRob Clark 	(void)ctl->reg_offset; /* TODO use this instead of mdp5_write */
9714be3200SRob Clark 	return mdp5_read(mdp5_kms, reg);
9814be3200SRob Clark }
9914be3200SRob Clark 
set_display_intf(struct mdp5_kms * mdp5_kms,struct mdp5_interface * intf)10014be3200SRob Clark static void set_display_intf(struct mdp5_kms *mdp5_kms,
10114be3200SRob Clark 		struct mdp5_interface *intf)
10214be3200SRob Clark {
10314be3200SRob Clark 	unsigned long flags;
10414be3200SRob Clark 	u32 intf_sel;
10514be3200SRob Clark 
10614be3200SRob Clark 	spin_lock_irqsave(&mdp5_kms->resource_lock, flags);
10714be3200SRob Clark 	intf_sel = mdp5_read(mdp5_kms, REG_MDP5_DISP_INTF_SEL);
10814be3200SRob Clark 
10914be3200SRob Clark 	switch (intf->num) {
11014be3200SRob Clark 	case 0:
11114be3200SRob Clark 		intf_sel &= ~MDP5_DISP_INTF_SEL_INTF0__MASK;
11214be3200SRob Clark 		intf_sel |= MDP5_DISP_INTF_SEL_INTF0(intf->type);
11314be3200SRob Clark 		break;
11414be3200SRob Clark 	case 1:
11514be3200SRob Clark 		intf_sel &= ~MDP5_DISP_INTF_SEL_INTF1__MASK;
11614be3200SRob Clark 		intf_sel |= MDP5_DISP_INTF_SEL_INTF1(intf->type);
11714be3200SRob Clark 		break;
11814be3200SRob Clark 	case 2:
11914be3200SRob Clark 		intf_sel &= ~MDP5_DISP_INTF_SEL_INTF2__MASK;
12014be3200SRob Clark 		intf_sel |= MDP5_DISP_INTF_SEL_INTF2(intf->type);
12114be3200SRob Clark 		break;
12214be3200SRob Clark 	case 3:
12314be3200SRob Clark 		intf_sel &= ~MDP5_DISP_INTF_SEL_INTF3__MASK;
12414be3200SRob Clark 		intf_sel |= MDP5_DISP_INTF_SEL_INTF3(intf->type);
12514be3200SRob Clark 		break;
12614be3200SRob Clark 	default:
12714be3200SRob Clark 		BUG();
12814be3200SRob Clark 		break;
12914be3200SRob Clark 	}
13014be3200SRob Clark 
13114be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_DISP_INTF_SEL, intf_sel);
13214be3200SRob Clark 	spin_unlock_irqrestore(&mdp5_kms->resource_lock, flags);
13314be3200SRob Clark }
13414be3200SRob Clark 
set_ctl_op(struct mdp5_ctl * ctl,struct mdp5_pipeline * pipeline)13514be3200SRob Clark static void set_ctl_op(struct mdp5_ctl *ctl, struct mdp5_pipeline *pipeline)
13614be3200SRob Clark {
13714be3200SRob Clark 	unsigned long flags;
13814be3200SRob Clark 	struct mdp5_interface *intf = pipeline->intf;
13914be3200SRob Clark 	u32 ctl_op = 0;
14014be3200SRob Clark 
14114be3200SRob Clark 	if (!mdp5_cfg_intf_is_virtual(intf->type))
14214be3200SRob Clark 		ctl_op |= MDP5_CTL_OP_INTF_NUM(INTF0 + intf->num);
14314be3200SRob Clark 
14414be3200SRob Clark 	switch (intf->type) {
14514be3200SRob Clark 	case INTF_DSI:
14614be3200SRob Clark 		if (intf->mode == MDP5_INTF_DSI_MODE_COMMAND)
14714be3200SRob Clark 			ctl_op |= MDP5_CTL_OP_CMD_MODE;
14814be3200SRob Clark 		break;
14914be3200SRob Clark 
15014be3200SRob Clark 	case INTF_WB:
15114be3200SRob Clark 		if (intf->mode == MDP5_INTF_WB_MODE_LINE)
15214be3200SRob Clark 			ctl_op |= MDP5_CTL_OP_MODE(MODE_WB_2_LINE);
15314be3200SRob Clark 		break;
15414be3200SRob Clark 
15514be3200SRob Clark 	default:
15614be3200SRob Clark 		break;
15714be3200SRob Clark 	}
15814be3200SRob Clark 
15914be3200SRob Clark 	if (pipeline->r_mixer)
16014be3200SRob Clark 		ctl_op |= MDP5_CTL_OP_PACK_3D_ENABLE |
16114be3200SRob Clark 			  MDP5_CTL_OP_PACK_3D(1);
16214be3200SRob Clark 
16314be3200SRob Clark 	spin_lock_irqsave(&ctl->hw_lock, flags);
16414be3200SRob Clark 	ctl_write(ctl, REG_MDP5_CTL_OP(ctl->id), ctl_op);
16514be3200SRob Clark 	spin_unlock_irqrestore(&ctl->hw_lock, flags);
16614be3200SRob Clark }
16714be3200SRob Clark 
mdp5_ctl_set_pipeline(struct mdp5_ctl * ctl,struct mdp5_pipeline * pipeline)16814be3200SRob Clark int mdp5_ctl_set_pipeline(struct mdp5_ctl *ctl, struct mdp5_pipeline *pipeline)
16914be3200SRob Clark {
170f9cb8d8dSRob Clark 	struct mdp5_kms *mdp5_kms = get_kms(ctl->ctlm);
17114be3200SRob Clark 	struct mdp5_interface *intf = pipeline->intf;
17214be3200SRob Clark 
17314be3200SRob Clark 	/* Virtual interfaces need not set a display intf (e.g.: Writeback) */
17414be3200SRob Clark 	if (!mdp5_cfg_intf_is_virtual(intf->type))
17514be3200SRob Clark 		set_display_intf(mdp5_kms, intf);
17614be3200SRob Clark 
17714be3200SRob Clark 	set_ctl_op(ctl, pipeline);
17814be3200SRob Clark 
17914be3200SRob Clark 	return 0;
18014be3200SRob Clark }
18114be3200SRob Clark 
start_signal_needed(struct mdp5_ctl * ctl,struct mdp5_pipeline * pipeline)18214be3200SRob Clark static bool start_signal_needed(struct mdp5_ctl *ctl,
18314be3200SRob Clark 				struct mdp5_pipeline *pipeline)
18414be3200SRob Clark {
18514be3200SRob Clark 	struct mdp5_interface *intf = pipeline->intf;
18614be3200SRob Clark 
187f9cb8d8dSRob Clark 	if (!ctl->encoder_enabled)
18814be3200SRob Clark 		return false;
18914be3200SRob Clark 
19014be3200SRob Clark 	switch (intf->type) {
19114be3200SRob Clark 	case INTF_WB:
19214be3200SRob Clark 		return true;
19314be3200SRob Clark 	case INTF_DSI:
19414be3200SRob Clark 		return intf->mode == MDP5_INTF_DSI_MODE_COMMAND;
19514be3200SRob Clark 	default:
19614be3200SRob Clark 		return false;
19714be3200SRob Clark 	}
19814be3200SRob Clark }
19914be3200SRob Clark 
20014be3200SRob Clark /*
20114be3200SRob Clark  * send_start_signal() - Overlay Processor Start Signal
20214be3200SRob Clark  *
20314be3200SRob Clark  * For a given control operation (display pipeline), a START signal needs to be
20414be3200SRob Clark  * executed in order to kick off operation and activate all layers.
20514be3200SRob Clark  * e.g.: DSI command mode, Writeback
20614be3200SRob Clark  */
send_start_signal(struct mdp5_ctl * ctl)20714be3200SRob Clark static void send_start_signal(struct mdp5_ctl *ctl)
20814be3200SRob Clark {
20914be3200SRob Clark 	unsigned long flags;
21014be3200SRob Clark 
21114be3200SRob Clark 	spin_lock_irqsave(&ctl->hw_lock, flags);
21214be3200SRob Clark 	ctl_write(ctl, REG_MDP5_CTL_START(ctl->id), 1);
21314be3200SRob Clark 	spin_unlock_irqrestore(&ctl->hw_lock, flags);
21414be3200SRob Clark }
21514be3200SRob Clark 
21614be3200SRob Clark /**
21714be3200SRob Clark  * mdp5_ctl_set_encoder_state() - set the encoder state
21814be3200SRob Clark  *
21903b6f2d6SRob Clark  * @ctl:      the CTL instance
22003b6f2d6SRob Clark  * @pipeline: the encoder's INTF + MIXER configuration
22103b6f2d6SRob Clark  * @enabled:  true, when encoder is ready for data streaming; false, otherwise.
22214be3200SRob Clark  *
22314be3200SRob Clark  * Note:
22414be3200SRob Clark  * This encoder state is needed to trigger START signal (data path kickoff).
22514be3200SRob Clark  */
mdp5_ctl_set_encoder_state(struct mdp5_ctl * ctl,struct mdp5_pipeline * pipeline,bool enabled)22614be3200SRob Clark int mdp5_ctl_set_encoder_state(struct mdp5_ctl *ctl,
22714be3200SRob Clark 			       struct mdp5_pipeline *pipeline,
22814be3200SRob Clark 			       bool enabled)
22914be3200SRob Clark {
23014be3200SRob Clark 	struct mdp5_interface *intf = pipeline->intf;
23114be3200SRob Clark 
23214be3200SRob Clark 	if (WARN_ON(!ctl))
23314be3200SRob Clark 		return -EINVAL;
23414be3200SRob Clark 
23514be3200SRob Clark 	ctl->encoder_enabled = enabled;
23614be3200SRob Clark 	DBG("intf_%d: %s", intf->num, enabled ? "on" : "off");
23714be3200SRob Clark 
23814be3200SRob Clark 	if (start_signal_needed(ctl, pipeline)) {
23914be3200SRob Clark 		send_start_signal(ctl);
24014be3200SRob Clark 	}
24114be3200SRob Clark 
24214be3200SRob Clark 	return 0;
24314be3200SRob Clark }
24414be3200SRob Clark 
24514be3200SRob Clark /*
24614be3200SRob Clark  * Note:
24714be3200SRob Clark  * CTL registers need to be flushed after calling this function
24814be3200SRob Clark  * (call mdp5_ctl_commit() with mdp_ctl_flush_mask_ctl() mask)
24914be3200SRob Clark  */
mdp5_ctl_set_cursor(struct mdp5_ctl * ctl,struct mdp5_pipeline * pipeline,int cursor_id,bool enable)25014be3200SRob Clark int mdp5_ctl_set_cursor(struct mdp5_ctl *ctl, struct mdp5_pipeline *pipeline,
25114be3200SRob Clark 			int cursor_id, bool enable)
25214be3200SRob Clark {
25314be3200SRob Clark 	struct mdp5_ctl_manager *ctl_mgr = ctl->ctlm;
25414be3200SRob Clark 	unsigned long flags;
25514be3200SRob Clark 	u32 blend_cfg;
25614be3200SRob Clark 	struct mdp5_hw_mixer *mixer = pipeline->mixer;
25714be3200SRob Clark 
258c044e86fSDenis Efremov 	if (WARN_ON(!mixer)) {
2596a41da17SMamta Shukla 		DRM_DEV_ERROR(ctl_mgr->dev->dev, "CTL %d cannot find LM",
26014be3200SRob Clark 			ctl->id);
26114be3200SRob Clark 		return -EINVAL;
26214be3200SRob Clark 	}
26314be3200SRob Clark 
26414be3200SRob Clark 	if (pipeline->r_mixer) {
2656a41da17SMamta Shukla 		DRM_DEV_ERROR(ctl_mgr->dev->dev, "unsupported configuration");
26614be3200SRob Clark 		return -EINVAL;
26714be3200SRob Clark 	}
26814be3200SRob Clark 
26914be3200SRob Clark 	spin_lock_irqsave(&ctl->hw_lock, flags);
27014be3200SRob Clark 
27114be3200SRob Clark 	blend_cfg = ctl_read(ctl, REG_MDP5_CTL_LAYER_REG(ctl->id, mixer->lm));
27214be3200SRob Clark 
27314be3200SRob Clark 	if (enable)
27414be3200SRob Clark 		blend_cfg |=  MDP5_CTL_LAYER_REG_CURSOR_OUT;
27514be3200SRob Clark 	else
27614be3200SRob Clark 		blend_cfg &= ~MDP5_CTL_LAYER_REG_CURSOR_OUT;
27714be3200SRob Clark 
27814be3200SRob Clark 	ctl_write(ctl, REG_MDP5_CTL_LAYER_REG(ctl->id, mixer->lm), blend_cfg);
27914be3200SRob Clark 	ctl->cursor_on = enable;
28014be3200SRob Clark 
28114be3200SRob Clark 	spin_unlock_irqrestore(&ctl->hw_lock, flags);
28214be3200SRob Clark 
28314be3200SRob Clark 	ctl->pending_ctl_trigger = mdp_ctl_flush_mask_cursor(cursor_id);
28414be3200SRob Clark 
28514be3200SRob Clark 	return 0;
28614be3200SRob Clark }
28714be3200SRob Clark 
mdp_ctl_blend_mask(enum mdp5_pipe pipe,enum mdp_mixer_stage_id stage)28814be3200SRob Clark static u32 mdp_ctl_blend_mask(enum mdp5_pipe pipe,
28914be3200SRob Clark 		enum mdp_mixer_stage_id stage)
29014be3200SRob Clark {
29114be3200SRob Clark 	switch (pipe) {
29214be3200SRob Clark 	case SSPP_VIG0: return MDP5_CTL_LAYER_REG_VIG0(stage);
29314be3200SRob Clark 	case SSPP_VIG1: return MDP5_CTL_LAYER_REG_VIG1(stage);
29414be3200SRob Clark 	case SSPP_VIG2: return MDP5_CTL_LAYER_REG_VIG2(stage);
29514be3200SRob Clark 	case SSPP_RGB0: return MDP5_CTL_LAYER_REG_RGB0(stage);
29614be3200SRob Clark 	case SSPP_RGB1: return MDP5_CTL_LAYER_REG_RGB1(stage);
29714be3200SRob Clark 	case SSPP_RGB2: return MDP5_CTL_LAYER_REG_RGB2(stage);
29814be3200SRob Clark 	case SSPP_DMA0: return MDP5_CTL_LAYER_REG_DMA0(stage);
29914be3200SRob Clark 	case SSPP_DMA1: return MDP5_CTL_LAYER_REG_DMA1(stage);
30014be3200SRob Clark 	case SSPP_VIG3: return MDP5_CTL_LAYER_REG_VIG3(stage);
30114be3200SRob Clark 	case SSPP_RGB3: return MDP5_CTL_LAYER_REG_RGB3(stage);
30214be3200SRob Clark 	case SSPP_CURSOR0:
30314be3200SRob Clark 	case SSPP_CURSOR1:
30414be3200SRob Clark 	default:	return 0;
30514be3200SRob Clark 	}
30614be3200SRob Clark }
30714be3200SRob Clark 
mdp_ctl_blend_ext_mask(enum mdp5_pipe pipe,enum mdp_mixer_stage_id stage)30814be3200SRob Clark static u32 mdp_ctl_blend_ext_mask(enum mdp5_pipe pipe,
30914be3200SRob Clark 		enum mdp_mixer_stage_id stage)
31014be3200SRob Clark {
31114be3200SRob Clark 	if (stage < STAGE6 && (pipe != SSPP_CURSOR0 && pipe != SSPP_CURSOR1))
31214be3200SRob Clark 		return 0;
31314be3200SRob Clark 
31414be3200SRob Clark 	switch (pipe) {
31514be3200SRob Clark 	case SSPP_VIG0: return MDP5_CTL_LAYER_EXT_REG_VIG0_BIT3;
31614be3200SRob Clark 	case SSPP_VIG1: return MDP5_CTL_LAYER_EXT_REG_VIG1_BIT3;
31714be3200SRob Clark 	case SSPP_VIG2: return MDP5_CTL_LAYER_EXT_REG_VIG2_BIT3;
31814be3200SRob Clark 	case SSPP_RGB0: return MDP5_CTL_LAYER_EXT_REG_RGB0_BIT3;
31914be3200SRob Clark 	case SSPP_RGB1: return MDP5_CTL_LAYER_EXT_REG_RGB1_BIT3;
32014be3200SRob Clark 	case SSPP_RGB2: return MDP5_CTL_LAYER_EXT_REG_RGB2_BIT3;
32114be3200SRob Clark 	case SSPP_DMA0: return MDP5_CTL_LAYER_EXT_REG_DMA0_BIT3;
32214be3200SRob Clark 	case SSPP_DMA1: return MDP5_CTL_LAYER_EXT_REG_DMA1_BIT3;
32314be3200SRob Clark 	case SSPP_VIG3: return MDP5_CTL_LAYER_EXT_REG_VIG3_BIT3;
32414be3200SRob Clark 	case SSPP_RGB3: return MDP5_CTL_LAYER_EXT_REG_RGB3_BIT3;
32514be3200SRob Clark 	case SSPP_CURSOR0: return MDP5_CTL_LAYER_EXT_REG_CURSOR0(stage);
32614be3200SRob Clark 	case SSPP_CURSOR1: return MDP5_CTL_LAYER_EXT_REG_CURSOR1(stage);
32714be3200SRob Clark 	default:	return 0;
32814be3200SRob Clark 	}
32914be3200SRob Clark }
33014be3200SRob Clark 
mdp5_ctl_reset_blend_regs(struct mdp5_ctl * ctl)33114be3200SRob Clark static void mdp5_ctl_reset_blend_regs(struct mdp5_ctl *ctl)
33214be3200SRob Clark {
33314be3200SRob Clark 	unsigned long flags;
33414be3200SRob Clark 	struct mdp5_ctl_manager *ctl_mgr = ctl->ctlm;
33514be3200SRob Clark 	int i;
33614be3200SRob Clark 
33714be3200SRob Clark 	spin_lock_irqsave(&ctl->hw_lock, flags);
33814be3200SRob Clark 
33914be3200SRob Clark 	for (i = 0; i < ctl_mgr->nlm; i++) {
34014be3200SRob Clark 		ctl_write(ctl, REG_MDP5_CTL_LAYER_REG(ctl->id, i), 0x0);
34114be3200SRob Clark 		ctl_write(ctl, REG_MDP5_CTL_LAYER_EXT_REG(ctl->id, i), 0x0);
34214be3200SRob Clark 	}
34314be3200SRob Clark 
34414be3200SRob Clark 	spin_unlock_irqrestore(&ctl->hw_lock, flags);
34514be3200SRob Clark }
34614be3200SRob Clark 
34714be3200SRob Clark #define PIPE_LEFT	0
34814be3200SRob Clark #define PIPE_RIGHT	1
mdp5_ctl_blend(struct mdp5_ctl * ctl,struct mdp5_pipeline * pipeline,enum mdp5_pipe stage[][MAX_PIPE_STAGE],enum mdp5_pipe r_stage[][MAX_PIPE_STAGE],u32 stage_cnt,u32 ctl_blend_op_flags)34914be3200SRob Clark int mdp5_ctl_blend(struct mdp5_ctl *ctl, struct mdp5_pipeline *pipeline,
35014be3200SRob Clark 		   enum mdp5_pipe stage[][MAX_PIPE_STAGE],
35114be3200SRob Clark 		   enum mdp5_pipe r_stage[][MAX_PIPE_STAGE],
35214be3200SRob Clark 		   u32 stage_cnt, u32 ctl_blend_op_flags)
35314be3200SRob Clark {
35414be3200SRob Clark 	struct mdp5_hw_mixer *mixer = pipeline->mixer;
35514be3200SRob Clark 	struct mdp5_hw_mixer *r_mixer = pipeline->r_mixer;
35614be3200SRob Clark 	unsigned long flags;
35714be3200SRob Clark 	u32 blend_cfg = 0, blend_ext_cfg = 0;
35814be3200SRob Clark 	u32 r_blend_cfg = 0, r_blend_ext_cfg = 0;
35914be3200SRob Clark 	int i, start_stage;
36014be3200SRob Clark 
36114be3200SRob Clark 	mdp5_ctl_reset_blend_regs(ctl);
36214be3200SRob Clark 
36314be3200SRob Clark 	if (ctl_blend_op_flags & MDP5_CTL_BLEND_OP_FLAG_BORDER_OUT) {
36414be3200SRob Clark 		start_stage = STAGE0;
36514be3200SRob Clark 		blend_cfg |= MDP5_CTL_LAYER_REG_BORDER_COLOR;
36614be3200SRob Clark 		if (r_mixer)
36714be3200SRob Clark 			r_blend_cfg |= MDP5_CTL_LAYER_REG_BORDER_COLOR;
36814be3200SRob Clark 	} else {
36914be3200SRob Clark 		start_stage = STAGE_BASE;
37014be3200SRob Clark 	}
37114be3200SRob Clark 
37214be3200SRob Clark 	for (i = start_stage; stage_cnt && i <= STAGE_MAX; i++) {
37314be3200SRob Clark 		blend_cfg |=
37414be3200SRob Clark 			mdp_ctl_blend_mask(stage[i][PIPE_LEFT], i) |
37514be3200SRob Clark 			mdp_ctl_blend_mask(stage[i][PIPE_RIGHT], i);
37614be3200SRob Clark 		blend_ext_cfg |=
37714be3200SRob Clark 			mdp_ctl_blend_ext_mask(stage[i][PIPE_LEFT], i) |
37814be3200SRob Clark 			mdp_ctl_blend_ext_mask(stage[i][PIPE_RIGHT], i);
37914be3200SRob Clark 		if (r_mixer) {
38014be3200SRob Clark 			r_blend_cfg |=
38114be3200SRob Clark 				mdp_ctl_blend_mask(r_stage[i][PIPE_LEFT], i) |
38214be3200SRob Clark 				mdp_ctl_blend_mask(r_stage[i][PIPE_RIGHT], i);
38314be3200SRob Clark 			r_blend_ext_cfg |=
38414be3200SRob Clark 			     mdp_ctl_blend_ext_mask(r_stage[i][PIPE_LEFT], i) |
38514be3200SRob Clark 			     mdp_ctl_blend_ext_mask(r_stage[i][PIPE_RIGHT], i);
38614be3200SRob Clark 		}
38714be3200SRob Clark 	}
38814be3200SRob Clark 
38914be3200SRob Clark 	spin_lock_irqsave(&ctl->hw_lock, flags);
39014be3200SRob Clark 	if (ctl->cursor_on)
39114be3200SRob Clark 		blend_cfg |=  MDP5_CTL_LAYER_REG_CURSOR_OUT;
39214be3200SRob Clark 
39314be3200SRob Clark 	ctl_write(ctl, REG_MDP5_CTL_LAYER_REG(ctl->id, mixer->lm), blend_cfg);
39414be3200SRob Clark 	ctl_write(ctl, REG_MDP5_CTL_LAYER_EXT_REG(ctl->id, mixer->lm),
39514be3200SRob Clark 		  blend_ext_cfg);
39614be3200SRob Clark 	if (r_mixer) {
39714be3200SRob Clark 		ctl_write(ctl, REG_MDP5_CTL_LAYER_REG(ctl->id, r_mixer->lm),
39814be3200SRob Clark 			  r_blend_cfg);
39914be3200SRob Clark 		ctl_write(ctl, REG_MDP5_CTL_LAYER_EXT_REG(ctl->id, r_mixer->lm),
40014be3200SRob Clark 			  r_blend_ext_cfg);
40114be3200SRob Clark 	}
40214be3200SRob Clark 	spin_unlock_irqrestore(&ctl->hw_lock, flags);
40314be3200SRob Clark 
40414be3200SRob Clark 	ctl->pending_ctl_trigger = mdp_ctl_flush_mask_lm(mixer->lm);
40514be3200SRob Clark 	if (r_mixer)
40614be3200SRob Clark 		ctl->pending_ctl_trigger |= mdp_ctl_flush_mask_lm(r_mixer->lm);
40714be3200SRob Clark 
40814be3200SRob Clark 	DBG("lm%d: blend config = 0x%08x. ext_cfg = 0x%08x", mixer->lm,
40914be3200SRob Clark 		blend_cfg, blend_ext_cfg);
41014be3200SRob Clark 	if (r_mixer)
41114be3200SRob Clark 		DBG("lm%d: blend config = 0x%08x. ext_cfg = 0x%08x",
41214be3200SRob Clark 		    r_mixer->lm, r_blend_cfg, r_blend_ext_cfg);
41314be3200SRob Clark 
41414be3200SRob Clark 	return 0;
41514be3200SRob Clark }
41614be3200SRob Clark 
mdp_ctl_flush_mask_encoder(struct mdp5_interface * intf)41714be3200SRob Clark u32 mdp_ctl_flush_mask_encoder(struct mdp5_interface *intf)
41814be3200SRob Clark {
41914be3200SRob Clark 	if (intf->type == INTF_WB)
42014be3200SRob Clark 		return MDP5_CTL_FLUSH_WB;
42114be3200SRob Clark 
42214be3200SRob Clark 	switch (intf->num) {
42314be3200SRob Clark 	case 0: return MDP5_CTL_FLUSH_TIMING_0;
42414be3200SRob Clark 	case 1: return MDP5_CTL_FLUSH_TIMING_1;
42514be3200SRob Clark 	case 2: return MDP5_CTL_FLUSH_TIMING_2;
42614be3200SRob Clark 	case 3: return MDP5_CTL_FLUSH_TIMING_3;
42714be3200SRob Clark 	default: return 0;
42814be3200SRob Clark 	}
42914be3200SRob Clark }
43014be3200SRob Clark 
mdp_ctl_flush_mask_cursor(int cursor_id)43114be3200SRob Clark u32 mdp_ctl_flush_mask_cursor(int cursor_id)
43214be3200SRob Clark {
43314be3200SRob Clark 	switch (cursor_id) {
43414be3200SRob Clark 	case 0: return MDP5_CTL_FLUSH_CURSOR_0;
43514be3200SRob Clark 	case 1: return MDP5_CTL_FLUSH_CURSOR_1;
43614be3200SRob Clark 	default: return 0;
43714be3200SRob Clark 	}
43814be3200SRob Clark }
43914be3200SRob Clark 
mdp_ctl_flush_mask_pipe(enum mdp5_pipe pipe)44014be3200SRob Clark u32 mdp_ctl_flush_mask_pipe(enum mdp5_pipe pipe)
44114be3200SRob Clark {
44214be3200SRob Clark 	switch (pipe) {
44314be3200SRob Clark 	case SSPP_VIG0: return MDP5_CTL_FLUSH_VIG0;
44414be3200SRob Clark 	case SSPP_VIG1: return MDP5_CTL_FLUSH_VIG1;
44514be3200SRob Clark 	case SSPP_VIG2: return MDP5_CTL_FLUSH_VIG2;
44614be3200SRob Clark 	case SSPP_RGB0: return MDP5_CTL_FLUSH_RGB0;
44714be3200SRob Clark 	case SSPP_RGB1: return MDP5_CTL_FLUSH_RGB1;
44814be3200SRob Clark 	case SSPP_RGB2: return MDP5_CTL_FLUSH_RGB2;
44914be3200SRob Clark 	case SSPP_DMA0: return MDP5_CTL_FLUSH_DMA0;
45014be3200SRob Clark 	case SSPP_DMA1: return MDP5_CTL_FLUSH_DMA1;
45114be3200SRob Clark 	case SSPP_VIG3: return MDP5_CTL_FLUSH_VIG3;
45214be3200SRob Clark 	case SSPP_RGB3: return MDP5_CTL_FLUSH_RGB3;
45314be3200SRob Clark 	case SSPP_CURSOR0: return MDP5_CTL_FLUSH_CURSOR_0;
45414be3200SRob Clark 	case SSPP_CURSOR1: return MDP5_CTL_FLUSH_CURSOR_1;
45514be3200SRob Clark 	default:        return 0;
45614be3200SRob Clark 	}
45714be3200SRob Clark }
45814be3200SRob Clark 
mdp_ctl_flush_mask_lm(int lm)45914be3200SRob Clark u32 mdp_ctl_flush_mask_lm(int lm)
46014be3200SRob Clark {
46114be3200SRob Clark 	switch (lm) {
46214be3200SRob Clark 	case 0:  return MDP5_CTL_FLUSH_LM0;
46314be3200SRob Clark 	case 1:  return MDP5_CTL_FLUSH_LM1;
46414be3200SRob Clark 	case 2:  return MDP5_CTL_FLUSH_LM2;
465583c13fdSRob Clark 	case 3:  return MDP5_CTL_FLUSH_LM3;
466583c13fdSRob Clark 	case 4:  return MDP5_CTL_FLUSH_LM4;
46714be3200SRob Clark 	case 5:  return MDP5_CTL_FLUSH_LM5;
46814be3200SRob Clark 	default: return 0;
46914be3200SRob Clark 	}
47014be3200SRob Clark }
47114be3200SRob Clark 
fix_sw_flush(struct mdp5_ctl * ctl,struct mdp5_pipeline * pipeline,u32 flush_mask)47214be3200SRob Clark static u32 fix_sw_flush(struct mdp5_ctl *ctl, struct mdp5_pipeline *pipeline,
47314be3200SRob Clark 			u32 flush_mask)
47414be3200SRob Clark {
47514be3200SRob Clark 	struct mdp5_ctl_manager *ctl_mgr = ctl->ctlm;
47614be3200SRob Clark 	u32 sw_mask = 0;
47714be3200SRob Clark #define BIT_NEEDS_SW_FIX(bit) \
47814be3200SRob Clark 	(!(ctl_mgr->flush_hw_mask & bit) && (flush_mask & bit))
47914be3200SRob Clark 
48014be3200SRob Clark 	/* for some targets, cursor bit is the same as LM bit */
48114be3200SRob Clark 	if (BIT_NEEDS_SW_FIX(MDP5_CTL_FLUSH_CURSOR_0))
48214be3200SRob Clark 		sw_mask |= mdp_ctl_flush_mask_lm(pipeline->mixer->lm);
48314be3200SRob Clark 
48414be3200SRob Clark 	return sw_mask;
48514be3200SRob Clark }
48614be3200SRob Clark 
fix_for_single_flush(struct mdp5_ctl * ctl,u32 * flush_mask,u32 * flush_id)48714be3200SRob Clark static void fix_for_single_flush(struct mdp5_ctl *ctl, u32 *flush_mask,
48814be3200SRob Clark 		u32 *flush_id)
48914be3200SRob Clark {
49014be3200SRob Clark 	struct mdp5_ctl_manager *ctl_mgr = ctl->ctlm;
49114be3200SRob Clark 
49214be3200SRob Clark 	if (ctl->pair) {
49314be3200SRob Clark 		DBG("CTL %d FLUSH pending mask %x", ctl->id, *flush_mask);
49414be3200SRob Clark 		ctl->flush_pending = true;
49514be3200SRob Clark 		ctl_mgr->single_flush_pending_mask |= (*flush_mask);
49614be3200SRob Clark 		*flush_mask = 0;
49714be3200SRob Clark 
49814be3200SRob Clark 		if (ctl->pair->flush_pending) {
49914be3200SRob Clark 			*flush_id = min_t(u32, ctl->id, ctl->pair->id);
50014be3200SRob Clark 			*flush_mask = ctl_mgr->single_flush_pending_mask;
50114be3200SRob Clark 
50214be3200SRob Clark 			ctl->flush_pending = false;
50314be3200SRob Clark 			ctl->pair->flush_pending = false;
50414be3200SRob Clark 			ctl_mgr->single_flush_pending_mask = 0;
50514be3200SRob Clark 
50614be3200SRob Clark 			DBG("Single FLUSH mask %x,ID %d", *flush_mask,
50714be3200SRob Clark 				*flush_id);
50814be3200SRob Clark 		}
50914be3200SRob Clark 	}
51014be3200SRob Clark }
51114be3200SRob Clark 
51214be3200SRob Clark /**
51314be3200SRob Clark  * mdp5_ctl_commit() - Register Flush
51414be3200SRob Clark  *
51503b6f2d6SRob Clark  * @ctl:        the CTL instance
51603b6f2d6SRob Clark  * @pipeline:   the encoder's INTF + MIXER configuration
51703b6f2d6SRob Clark  * @flush_mask: bitmask of display controller hw blocks to flush
51803b6f2d6SRob Clark  * @start:      if true, immediately update flush registers and set START
51903b6f2d6SRob Clark  *              bit, otherwise accumulate flush_mask bits until we are
52003b6f2d6SRob Clark  *              ready to START
52103b6f2d6SRob Clark  *
52214be3200SRob Clark  * The flush register is used to indicate several registers are all
52314be3200SRob Clark  * programmed, and are safe to update to the back copy of the double
52414be3200SRob Clark  * buffered registers.
52514be3200SRob Clark  *
52614be3200SRob Clark  * Some registers FLUSH bits are shared when the hardware does not have
52714be3200SRob Clark  * dedicated bits for them; handling these is the job of fix_sw_flush().
52814be3200SRob Clark  *
52914be3200SRob Clark  * CTL registers need to be flushed in some circumstances; if that is the
53014be3200SRob Clark  * case, some trigger bits will be present in both flush mask and
53114be3200SRob Clark  * ctl->pending_ctl_trigger.
53214be3200SRob Clark  *
53314be3200SRob Clark  * Return H/W flushed bit mask.
53414be3200SRob Clark  */
mdp5_ctl_commit(struct mdp5_ctl * ctl,struct mdp5_pipeline * pipeline,u32 flush_mask,bool start)53514be3200SRob Clark u32 mdp5_ctl_commit(struct mdp5_ctl *ctl,
53614be3200SRob Clark 		    struct mdp5_pipeline *pipeline,
537f9cb8d8dSRob Clark 		    u32 flush_mask, bool start)
53814be3200SRob Clark {
53914be3200SRob Clark 	struct mdp5_ctl_manager *ctl_mgr = ctl->ctlm;
54014be3200SRob Clark 	unsigned long flags;
54114be3200SRob Clark 	u32 flush_id = ctl->id;
54214be3200SRob Clark 	u32 curr_ctl_flush_mask;
54314be3200SRob Clark 
544f9cb8d8dSRob Clark 	VERB("flush_mask=%x, trigger=%x", flush_mask, ctl->pending_ctl_trigger);
54514be3200SRob Clark 
54614be3200SRob Clark 	if (ctl->pending_ctl_trigger & flush_mask) {
54714be3200SRob Clark 		flush_mask |= MDP5_CTL_FLUSH_CTL;
54814be3200SRob Clark 		ctl->pending_ctl_trigger = 0;
54914be3200SRob Clark 	}
55014be3200SRob Clark 
55114be3200SRob Clark 	flush_mask |= fix_sw_flush(ctl, pipeline, flush_mask);
55214be3200SRob Clark 
55314be3200SRob Clark 	flush_mask &= ctl_mgr->flush_hw_mask;
55414be3200SRob Clark 
55514be3200SRob Clark 	curr_ctl_flush_mask = flush_mask;
55614be3200SRob Clark 
55714be3200SRob Clark 	fix_for_single_flush(ctl, &flush_mask, &flush_id);
55814be3200SRob Clark 
559f9cb8d8dSRob Clark 	if (!start) {
560f9cb8d8dSRob Clark 		ctl->flush_mask |= flush_mask;
561f9cb8d8dSRob Clark 		return curr_ctl_flush_mask;
562f9cb8d8dSRob Clark 	} else {
563f9cb8d8dSRob Clark 		flush_mask |= ctl->flush_mask;
564f9cb8d8dSRob Clark 		ctl->flush_mask = 0;
565f9cb8d8dSRob Clark 	}
566f9cb8d8dSRob Clark 
56714be3200SRob Clark 	if (flush_mask) {
56814be3200SRob Clark 		spin_lock_irqsave(&ctl->hw_lock, flags);
56914be3200SRob Clark 		ctl_write(ctl, REG_MDP5_CTL_FLUSH(flush_id), flush_mask);
57014be3200SRob Clark 		spin_unlock_irqrestore(&ctl->hw_lock, flags);
57114be3200SRob Clark 	}
57214be3200SRob Clark 
57314be3200SRob Clark 	if (start_signal_needed(ctl, pipeline)) {
57414be3200SRob Clark 		send_start_signal(ctl);
57514be3200SRob Clark 	}
57614be3200SRob Clark 
57714be3200SRob Clark 	return curr_ctl_flush_mask;
57814be3200SRob Clark }
57914be3200SRob Clark 
mdp5_ctl_get_commit_status(struct mdp5_ctl * ctl)58014be3200SRob Clark u32 mdp5_ctl_get_commit_status(struct mdp5_ctl *ctl)
58114be3200SRob Clark {
58214be3200SRob Clark 	return ctl_read(ctl, REG_MDP5_CTL_FLUSH(ctl->id));
58314be3200SRob Clark }
58414be3200SRob Clark 
mdp5_ctl_get_ctl_id(struct mdp5_ctl * ctl)58514be3200SRob Clark int mdp5_ctl_get_ctl_id(struct mdp5_ctl *ctl)
58614be3200SRob Clark {
58714be3200SRob Clark 	return WARN_ON(!ctl) ? -EINVAL : ctl->id;
58814be3200SRob Clark }
58914be3200SRob Clark 
59014be3200SRob Clark /*
59114be3200SRob Clark  * mdp5_ctl_pair() - Associate 2 booked CTLs for single FLUSH
59214be3200SRob Clark  */
mdp5_ctl_pair(struct mdp5_ctl * ctlx,struct mdp5_ctl * ctly,bool enable)59314be3200SRob Clark int mdp5_ctl_pair(struct mdp5_ctl *ctlx, struct mdp5_ctl *ctly, bool enable)
59414be3200SRob Clark {
59514be3200SRob Clark 	struct mdp5_ctl_manager *ctl_mgr = ctlx->ctlm;
59614be3200SRob Clark 	struct mdp5_kms *mdp5_kms = get_kms(ctl_mgr);
59714be3200SRob Clark 
59814be3200SRob Clark 	/* do nothing silently if hw doesn't support */
59914be3200SRob Clark 	if (!ctl_mgr->single_flush_supported)
60014be3200SRob Clark 		return 0;
60114be3200SRob Clark 
60214be3200SRob Clark 	if (!enable) {
60314be3200SRob Clark 		ctlx->pair = NULL;
60414be3200SRob Clark 		ctly->pair = NULL;
60514be3200SRob Clark 		mdp5_write(mdp5_kms, REG_MDP5_SPARE_0, 0);
60614be3200SRob Clark 		return 0;
60714be3200SRob Clark 	} else if ((ctlx->pair != NULL) || (ctly->pair != NULL)) {
6086a41da17SMamta Shukla 		DRM_DEV_ERROR(ctl_mgr->dev->dev, "CTLs already paired\n");
60914be3200SRob Clark 		return -EINVAL;
61014be3200SRob Clark 	} else if (!(ctlx->status & ctly->status & CTL_STAT_BOOKED)) {
6116a41da17SMamta Shukla 		DRM_DEV_ERROR(ctl_mgr->dev->dev, "Only pair booked CTLs\n");
61214be3200SRob Clark 		return -EINVAL;
61314be3200SRob Clark 	}
61414be3200SRob Clark 
61514be3200SRob Clark 	ctlx->pair = ctly;
61614be3200SRob Clark 	ctly->pair = ctlx;
61714be3200SRob Clark 
61814be3200SRob Clark 	mdp5_write(mdp5_kms, REG_MDP5_SPARE_0,
61914be3200SRob Clark 		   MDP5_SPARE_0_SPLIT_DPL_SINGLE_FLUSH_EN);
62014be3200SRob Clark 
62114be3200SRob Clark 	return 0;
62214be3200SRob Clark }
62314be3200SRob Clark 
62414be3200SRob Clark /*
62514be3200SRob Clark  * mdp5_ctl_request() - CTL allocation
62614be3200SRob Clark  *
62714be3200SRob Clark  * Try to return booked CTL for @intf_num is 1 or 2, unbooked for other INTFs.
62814be3200SRob Clark  * If no CTL is available in preferred category, allocate from the other one.
62914be3200SRob Clark  *
63014be3200SRob Clark  * @return fail if no CTL is available.
63114be3200SRob Clark  */
mdp5_ctlm_request(struct mdp5_ctl_manager * ctl_mgr,int intf_num)63214be3200SRob Clark struct mdp5_ctl *mdp5_ctlm_request(struct mdp5_ctl_manager *ctl_mgr,
63314be3200SRob Clark 		int intf_num)
63414be3200SRob Clark {
63514be3200SRob Clark 	struct mdp5_ctl *ctl = NULL;
63614be3200SRob Clark 	const u32 checkm = CTL_STAT_BUSY | CTL_STAT_BOOKED;
63714be3200SRob Clark 	u32 match = ((intf_num == 1) || (intf_num == 2)) ? CTL_STAT_BOOKED : 0;
63814be3200SRob Clark 	unsigned long flags;
63914be3200SRob Clark 	int c;
64014be3200SRob Clark 
64114be3200SRob Clark 	spin_lock_irqsave(&ctl_mgr->pool_lock, flags);
64214be3200SRob Clark 
64314be3200SRob Clark 	/* search the preferred */
64414be3200SRob Clark 	for (c = 0; c < ctl_mgr->nctl; c++)
64514be3200SRob Clark 		if ((ctl_mgr->ctls[c].status & checkm) == match)
64614be3200SRob Clark 			goto found;
64714be3200SRob Clark 
64814be3200SRob Clark 	dev_warn(ctl_mgr->dev->dev,
64914be3200SRob Clark 		"fall back to the other CTL category for INTF %d!\n", intf_num);
65014be3200SRob Clark 
65114be3200SRob Clark 	match ^= CTL_STAT_BOOKED;
65214be3200SRob Clark 	for (c = 0; c < ctl_mgr->nctl; c++)
65314be3200SRob Clark 		if ((ctl_mgr->ctls[c].status & checkm) == match)
65414be3200SRob Clark 			goto found;
65514be3200SRob Clark 
6566a41da17SMamta Shukla 	DRM_DEV_ERROR(ctl_mgr->dev->dev, "No more CTL available!");
65714be3200SRob Clark 	goto unlock;
65814be3200SRob Clark 
65914be3200SRob Clark found:
66014be3200SRob Clark 	ctl = &ctl_mgr->ctls[c];
66114be3200SRob Clark 	ctl->status |= CTL_STAT_BUSY;
66214be3200SRob Clark 	ctl->pending_ctl_trigger = 0;
66314be3200SRob Clark 	DBG("CTL %d allocated", ctl->id);
66414be3200SRob Clark 
66514be3200SRob Clark unlock:
66614be3200SRob Clark 	spin_unlock_irqrestore(&ctl_mgr->pool_lock, flags);
66714be3200SRob Clark 	return ctl;
66814be3200SRob Clark }
66914be3200SRob Clark 
mdp5_ctlm_hw_reset(struct mdp5_ctl_manager * ctl_mgr)67014be3200SRob Clark void mdp5_ctlm_hw_reset(struct mdp5_ctl_manager *ctl_mgr)
67114be3200SRob Clark {
67214be3200SRob Clark 	unsigned long flags;
67314be3200SRob Clark 	int c;
67414be3200SRob Clark 
67514be3200SRob Clark 	for (c = 0; c < ctl_mgr->nctl; c++) {
67614be3200SRob Clark 		struct mdp5_ctl *ctl = &ctl_mgr->ctls[c];
67714be3200SRob Clark 
67814be3200SRob Clark 		spin_lock_irqsave(&ctl->hw_lock, flags);
67914be3200SRob Clark 		ctl_write(ctl, REG_MDP5_CTL_OP(ctl->id), 0);
68014be3200SRob Clark 		spin_unlock_irqrestore(&ctl->hw_lock, flags);
68114be3200SRob Clark 	}
68214be3200SRob Clark }
68314be3200SRob Clark 
mdp5_ctlm_init(struct drm_device * dev,void __iomem * mmio_base,struct mdp5_cfg_handler * cfg_hnd)68414be3200SRob Clark struct mdp5_ctl_manager *mdp5_ctlm_init(struct drm_device *dev,
68514be3200SRob Clark 		void __iomem *mmio_base, struct mdp5_cfg_handler *cfg_hnd)
68614be3200SRob Clark {
68714be3200SRob Clark 	struct mdp5_ctl_manager *ctl_mgr;
68814be3200SRob Clark 	const struct mdp5_cfg_hw *hw_cfg = mdp5_cfg_get_hw_config(cfg_hnd);
68914be3200SRob Clark 	int rev = mdp5_cfg_get_hw_rev(cfg_hnd);
69061b734cbSRob Clark 	unsigned dsi_cnt = 0;
69114be3200SRob Clark 	const struct mdp5_ctl_block *ctl_cfg = &hw_cfg->ctl;
69214be3200SRob Clark 	unsigned long flags;
69314be3200SRob Clark 	int c, ret;
69414be3200SRob Clark 
695*4c1f4c1fSDmitry Baryshkov 	ctl_mgr = devm_kzalloc(dev->dev, sizeof(*ctl_mgr), GFP_KERNEL);
69614be3200SRob Clark 	if (!ctl_mgr) {
6976a41da17SMamta Shukla 		DRM_DEV_ERROR(dev->dev, "failed to allocate CTL manager\n");
698*4c1f4c1fSDmitry Baryshkov 		return ERR_PTR(-ENOMEM);
69914be3200SRob Clark 	}
70014be3200SRob Clark 
701c044e86fSDenis Efremov 	if (WARN_ON(ctl_cfg->count > MAX_CTL)) {
7026a41da17SMamta Shukla 		DRM_DEV_ERROR(dev->dev, "Increase static pool size to at least %d\n",
70314be3200SRob Clark 				ctl_cfg->count);
704*4c1f4c1fSDmitry Baryshkov 		return ERR_PTR(-ENOSPC);
70514be3200SRob Clark 	}
70614be3200SRob Clark 
70714be3200SRob Clark 	/* initialize the CTL manager: */
70814be3200SRob Clark 	ctl_mgr->dev = dev;
70914be3200SRob Clark 	ctl_mgr->nlm = hw_cfg->lm.count;
71014be3200SRob Clark 	ctl_mgr->nctl = ctl_cfg->count;
71114be3200SRob Clark 	ctl_mgr->flush_hw_mask = ctl_cfg->flush_hw_mask;
71214be3200SRob Clark 	spin_lock_init(&ctl_mgr->pool_lock);
71314be3200SRob Clark 
71414be3200SRob Clark 	/* initialize each CTL of the pool: */
71514be3200SRob Clark 	spin_lock_irqsave(&ctl_mgr->pool_lock, flags);
71614be3200SRob Clark 	for (c = 0; c < ctl_mgr->nctl; c++) {
71714be3200SRob Clark 		struct mdp5_ctl *ctl = &ctl_mgr->ctls[c];
71814be3200SRob Clark 
71914be3200SRob Clark 		if (WARN_ON(!ctl_cfg->base[c])) {
7206a41da17SMamta Shukla 			DRM_DEV_ERROR(dev->dev, "CTL_%d: base is null!\n", c);
72114be3200SRob Clark 			ret = -EINVAL;
72214be3200SRob Clark 			spin_unlock_irqrestore(&ctl_mgr->pool_lock, flags);
723*4c1f4c1fSDmitry Baryshkov 			return ERR_PTR(ret);
72414be3200SRob Clark 		}
72514be3200SRob Clark 		ctl->ctlm = ctl_mgr;
72614be3200SRob Clark 		ctl->id = c;
72714be3200SRob Clark 		ctl->reg_offset = ctl_cfg->base[c];
72814be3200SRob Clark 		ctl->status = 0;
72914be3200SRob Clark 		spin_lock_init(&ctl->hw_lock);
73014be3200SRob Clark 	}
73114be3200SRob Clark 
73214be3200SRob Clark 	/*
7336183606dSDmitry Baryshkov 	 * In bonded DSI case, CTL0 and CTL1 are always assigned to two DSI
73414be3200SRob Clark 	 * interfaces to support single FLUSH feature (Flush CTL0 and CTL1 when
73514be3200SRob Clark 	 * only write into CTL0's FLUSH register) to keep two DSI pipes in sync.
73614be3200SRob Clark 	 * Single FLUSH is supported from hw rev v3.0.
73714be3200SRob Clark 	 */
73861b734cbSRob Clark 	for (c = 0; c < ARRAY_SIZE(hw_cfg->intf.connect); c++)
73961b734cbSRob Clark 		if (hw_cfg->intf.connect[c] == INTF_DSI)
74061b734cbSRob Clark 			dsi_cnt++;
74161b734cbSRob Clark 	if ((rev >= 3) && (dsi_cnt > 1)) {
74214be3200SRob Clark 		ctl_mgr->single_flush_supported = true;
74314be3200SRob Clark 		/* Reserve CTL0/1 for INTF1/2 */
74414be3200SRob Clark 		ctl_mgr->ctls[0].status |= CTL_STAT_BOOKED;
74514be3200SRob Clark 		ctl_mgr->ctls[1].status |= CTL_STAT_BOOKED;
74614be3200SRob Clark 	}
74714be3200SRob Clark 	spin_unlock_irqrestore(&ctl_mgr->pool_lock, flags);
74814be3200SRob Clark 	DBG("Pool of %d CTLs created.", ctl_mgr->nctl);
74914be3200SRob Clark 
75014be3200SRob Clark 	return ctl_mgr;
75114be3200SRob Clark }
752