1 // SPDX-License-Identifier: GPL-2.0-only
2  /*
3   * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved
4   */
5 
6 #include "dpu_hw_mdss.h"
7 #include "dpu_hwio.h"
8 #include "dpu_hw_catalog.h"
9 #include "dpu_hw_wb.h"
10 #include "dpu_formats.h"
11 #include "dpu_kms.h"
12 
13 #define WB_DST_FORMAT                         0x000
14 #define WB_DST_OP_MODE                        0x004
15 #define WB_DST_PACK_PATTERN                   0x008
16 #define WB_DST0_ADDR                          0x00C
17 #define WB_DST1_ADDR                          0x010
18 #define WB_DST2_ADDR                          0x014
19 #define WB_DST3_ADDR                          0x018
20 #define WB_DST_YSTRIDE0                       0x01C
21 #define WB_DST_YSTRIDE1                       0x020
22 #define WB_DST_YSTRIDE1                       0x020
23 #define WB_DST_DITHER_BITDEPTH                0x024
24 #define WB_DST_MATRIX_ROW0                    0x030
25 #define WB_DST_MATRIX_ROW1                    0x034
26 #define WB_DST_MATRIX_ROW2                    0x038
27 #define WB_DST_MATRIX_ROW3                    0x03C
28 #define WB_DST_WRITE_CONFIG                   0x048
29 #define WB_ROTATION_DNSCALER                  0x050
30 #define WB_ROTATOR_PIPE_DOWNSCALER            0x054
31 #define WB_N16_INIT_PHASE_X_C03               0x060
32 #define WB_N16_INIT_PHASE_X_C12               0x064
33 #define WB_N16_INIT_PHASE_Y_C03               0x068
34 #define WB_N16_INIT_PHASE_Y_C12               0x06C
35 #define WB_OUT_SIZE                           0x074
36 #define WB_ALPHA_X_VALUE                      0x078
37 #define WB_DANGER_LUT                         0x084
38 #define WB_SAFE_LUT                           0x088
39 #define WB_QOS_CTRL                           0x090
40 #define WB_CREQ_LUT_0                         0x098
41 #define WB_CREQ_LUT_1                         0x09C
42 #define WB_UBWC_STATIC_CTRL                   0x144
43 #define WB_MUX                                0x150
44 #define WB_CROP_CTRL                          0x154
45 #define WB_CROP_OFFSET                        0x158
46 #define WB_CSC_BASE                           0x260
47 #define WB_DST_ADDR_SW_STATUS                 0x2B0
48 #define WB_CDP_CNTL                           0x2B4
49 #define WB_OUT_IMAGE_SIZE                     0x2C0
50 #define WB_OUT_XY                             0x2C4
51 
52 /* WB_QOS_CTRL */
53 #define WB_QOS_CTRL_DANGER_SAFE_EN            BIT(0)
54 
55 static const struct dpu_wb_cfg *_wb_offset(enum dpu_wb wb,
56 		const struct dpu_mdss_cfg *m, void __iomem *addr,
57 		struct dpu_hw_blk_reg_map *b)
58 {
59 	int i;
60 
61 	for (i = 0; i < m->wb_count; i++) {
62 		if (wb == m->wb[i].id) {
63 			b->blk_addr = addr + m->wb[i].base;
64 			b->log_mask = DPU_DBG_MASK_WB;
65 			return &m->wb[i];
66 		}
67 	}
68 	return ERR_PTR(-EINVAL);
69 }
70 
71 static void dpu_hw_wb_setup_outaddress(struct dpu_hw_wb *ctx,
72 		struct dpu_hw_wb_cfg *data)
73 {
74 	struct dpu_hw_blk_reg_map *c = &ctx->hw;
75 
76 	DPU_REG_WRITE(c, WB_DST0_ADDR, data->dest.plane_addr[0]);
77 	DPU_REG_WRITE(c, WB_DST1_ADDR, data->dest.plane_addr[1]);
78 	DPU_REG_WRITE(c, WB_DST2_ADDR, data->dest.plane_addr[2]);
79 	DPU_REG_WRITE(c, WB_DST3_ADDR, data->dest.plane_addr[3]);
80 }
81 
82 static void dpu_hw_wb_setup_format(struct dpu_hw_wb *ctx,
83 		struct dpu_hw_wb_cfg *data)
84 {
85 	struct dpu_hw_blk_reg_map *c = &ctx->hw;
86 	const struct dpu_format *fmt = data->dest.format;
87 	u32 dst_format, pattern, ystride0, ystride1, outsize, chroma_samp;
88 	u32 write_config = 0;
89 	u32 opmode = 0;
90 	u32 dst_addr_sw = 0;
91 
92 	chroma_samp = fmt->chroma_sample;
93 
94 	dst_format = (chroma_samp << 23) |
95 		(fmt->fetch_planes << 19) |
96 		(fmt->bits[C3_ALPHA] << 6) |
97 		(fmt->bits[C2_R_Cr] << 4) |
98 		(fmt->bits[C1_B_Cb] << 2) |
99 		(fmt->bits[C0_G_Y] << 0);
100 
101 	if (fmt->bits[C3_ALPHA] || fmt->alpha_enable) {
102 		dst_format |= BIT(8); /* DSTC3_EN */
103 		if (!fmt->alpha_enable ||
104 			!(ctx->caps->features & BIT(DPU_WB_PIPE_ALPHA)))
105 			dst_format |= BIT(14); /* DST_ALPHA_X */
106 	}
107 
108 	pattern = (fmt->element[3] << 24) |
109 		(fmt->element[2] << 16) |
110 		(fmt->element[1] << 8)  |
111 		(fmt->element[0] << 0);
112 
113 	dst_format |= (fmt->unpack_align_msb << 18) |
114 		(fmt->unpack_tight << 17) |
115 		((fmt->unpack_count - 1) << 12) |
116 		((fmt->bpp - 1) << 9);
117 
118 	ystride0 = data->dest.plane_pitch[0] |
119 		(data->dest.plane_pitch[1] << 16);
120 	ystride1 = data->dest.plane_pitch[2] |
121 	(data->dest.plane_pitch[3] << 16);
122 
123 	if (drm_rect_height(&data->roi) && drm_rect_width(&data->roi))
124 		outsize = (drm_rect_height(&data->roi) << 16) | drm_rect_width(&data->roi);
125 	else
126 		outsize = (data->dest.height << 16) | data->dest.width;
127 
128 	DPU_REG_WRITE(c, WB_ALPHA_X_VALUE, 0xFF);
129 	DPU_REG_WRITE(c, WB_DST_FORMAT, dst_format);
130 	DPU_REG_WRITE(c, WB_DST_OP_MODE, opmode);
131 	DPU_REG_WRITE(c, WB_DST_PACK_PATTERN, pattern);
132 	DPU_REG_WRITE(c, WB_DST_YSTRIDE0, ystride0);
133 	DPU_REG_WRITE(c, WB_DST_YSTRIDE1, ystride1);
134 	DPU_REG_WRITE(c, WB_OUT_SIZE, outsize);
135 	DPU_REG_WRITE(c, WB_DST_WRITE_CONFIG, write_config);
136 	DPU_REG_WRITE(c, WB_DST_ADDR_SW_STATUS, dst_addr_sw);
137 }
138 
139 static void dpu_hw_wb_roi(struct dpu_hw_wb *ctx, struct dpu_hw_wb_cfg *wb)
140 {
141 	struct dpu_hw_blk_reg_map *c = &ctx->hw;
142 	u32 image_size, out_size, out_xy;
143 
144 	image_size = (wb->dest.height << 16) | wb->dest.width;
145 	out_xy = 0;
146 	out_size = (drm_rect_height(&wb->roi) << 16) | drm_rect_width(&wb->roi);
147 
148 	DPU_REG_WRITE(c, WB_OUT_IMAGE_SIZE, image_size);
149 	DPU_REG_WRITE(c, WB_OUT_XY, out_xy);
150 	DPU_REG_WRITE(c, WB_OUT_SIZE, out_size);
151 }
152 
153 static void dpu_hw_wb_setup_qos_lut(struct dpu_hw_wb *ctx,
154 		struct dpu_hw_wb_qos_cfg *cfg)
155 {
156 	struct dpu_hw_blk_reg_map *c = &ctx->hw;
157 	u32 qos_ctrl = 0;
158 
159 	if (!ctx || !cfg)
160 		return;
161 
162 	DPU_REG_WRITE(c, WB_DANGER_LUT, cfg->danger_lut);
163 	DPU_REG_WRITE(c, WB_SAFE_LUT, cfg->safe_lut);
164 
165 	/*
166 	 * for chipsets not using DPU_WB_QOS_8LVL but still using DPU
167 	 * driver such as msm8998, the reset value of WB_CREQ_LUT is
168 	 * sufficient for writeback to work. SW doesn't need to explicitly
169 	 * program a value.
170 	 */
171 	if (ctx->caps && test_bit(DPU_WB_QOS_8LVL, &ctx->caps->features)) {
172 		DPU_REG_WRITE(c, WB_CREQ_LUT_0, cfg->creq_lut);
173 		DPU_REG_WRITE(c, WB_CREQ_LUT_1, cfg->creq_lut >> 32);
174 	}
175 
176 	if (cfg->danger_safe_en)
177 		qos_ctrl |= WB_QOS_CTRL_DANGER_SAFE_EN;
178 
179 	DPU_REG_WRITE(c, WB_QOS_CTRL, qos_ctrl);
180 }
181 
182 static void dpu_hw_wb_setup_cdp(struct dpu_hw_wb *ctx,
183 		struct dpu_hw_cdp_cfg *cfg)
184 {
185 	struct dpu_hw_blk_reg_map *c;
186 	u32 cdp_cntl = 0;
187 
188 	if (!ctx || !cfg)
189 		return;
190 
191 	c = &ctx->hw;
192 
193 	if (cfg->enable)
194 		cdp_cntl |= BIT(0);
195 	if (cfg->ubwc_meta_enable)
196 		cdp_cntl |= BIT(1);
197 	if (cfg->preload_ahead == DPU_WB_CDP_PRELOAD_AHEAD_64)
198 		cdp_cntl |= BIT(3);
199 
200 	DPU_REG_WRITE(c, WB_CDP_CNTL, cdp_cntl);
201 }
202 
203 static void dpu_hw_wb_bind_pingpong_blk(
204 		struct dpu_hw_wb *ctx,
205 		bool enable, const enum dpu_pingpong pp)
206 {
207 	struct dpu_hw_blk_reg_map *c;
208 	int mux_cfg;
209 
210 	if (!ctx)
211 		return;
212 
213 	c = &ctx->hw;
214 
215 	mux_cfg = DPU_REG_READ(c, WB_MUX);
216 	mux_cfg &= ~0xf;
217 
218 	if (enable)
219 		mux_cfg |= (pp - PINGPONG_0) & 0x7;
220 	else
221 		mux_cfg |= 0xf;
222 
223 	DPU_REG_WRITE(c, WB_MUX, mux_cfg);
224 }
225 
226 static void _setup_wb_ops(struct dpu_hw_wb_ops *ops,
227 		unsigned long features)
228 {
229 	ops->setup_outaddress = dpu_hw_wb_setup_outaddress;
230 	ops->setup_outformat = dpu_hw_wb_setup_format;
231 
232 	if (test_bit(DPU_WB_XY_ROI_OFFSET, &features))
233 		ops->setup_roi = dpu_hw_wb_roi;
234 
235 	if (test_bit(DPU_WB_QOS, &features))
236 		ops->setup_qos_lut = dpu_hw_wb_setup_qos_lut;
237 
238 	if (test_bit(DPU_WB_CDP, &features))
239 		ops->setup_cdp = dpu_hw_wb_setup_cdp;
240 
241 	if (test_bit(DPU_WB_INPUT_CTRL, &features))
242 		ops->bind_pingpong_blk = dpu_hw_wb_bind_pingpong_blk;
243 }
244 
245 struct dpu_hw_wb *dpu_hw_wb_init(enum dpu_wb idx,
246 		void __iomem *addr, const struct dpu_mdss_cfg *m)
247 {
248 	struct dpu_hw_wb *c;
249 	const struct dpu_wb_cfg *cfg;
250 
251 	if (!addr || !m)
252 		return ERR_PTR(-EINVAL);
253 
254 	c = kzalloc(sizeof(*c), GFP_KERNEL);
255 	if (!c)
256 		return ERR_PTR(-ENOMEM);
257 
258 	cfg = _wb_offset(idx, m, addr, &c->hw);
259 	if (IS_ERR(cfg)) {
260 		WARN(1, "Unable to find wb idx=%d\n", idx);
261 		kfree(c);
262 		return ERR_PTR(-EINVAL);
263 	}
264 
265 	/* Assign ops */
266 	c->mdp = &m->mdp[0];
267 	c->idx = idx;
268 	c->caps = cfg;
269 	_setup_wb_ops(&c->ops, c->caps->features);
270 
271 	return c;
272 }
273 
274 void dpu_hw_wb_destroy(struct dpu_hw_wb *hw_wb)
275 {
276 	kfree(hw_wb);
277 }
278