1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
3  */
4 
5 #define pr_fmt(fmt)	"[drm:%s:%d] " fmt, __func__, __LINE__
6 
7 #include <linux/debugfs.h>
8 
9 #include "dpu_vbif.h"
10 #include "dpu_hw_vbif.h"
11 #include "dpu_trace.h"
12 
13 /**
14  * _dpu_vbif_wait_for_xin_halt - wait for the xin to halt
15  * @vbif:	Pointer to hardware vbif driver
16  * @xin_id:	Client interface identifier
17  * @return:	0 if success; error code otherwise
18  */
19 static int _dpu_vbif_wait_for_xin_halt(struct dpu_hw_vbif *vbif, u32 xin_id)
20 {
21 	ktime_t timeout;
22 	bool status;
23 	int rc;
24 
25 	if (!vbif || !vbif->cap || !vbif->ops.get_halt_ctrl) {
26 		DPU_ERROR("invalid arguments vbif %d\n", vbif != 0);
27 		return -EINVAL;
28 	}
29 
30 	timeout = ktime_add_us(ktime_get(), vbif->cap->xin_halt_timeout);
31 	for (;;) {
32 		status = vbif->ops.get_halt_ctrl(vbif, xin_id);
33 		if (status)
34 			break;
35 		if (ktime_compare_safe(ktime_get(), timeout) > 0) {
36 			status = vbif->ops.get_halt_ctrl(vbif, xin_id);
37 			break;
38 		}
39 		usleep_range(501, 1000);
40 	}
41 
42 	if (!status) {
43 		rc = -ETIMEDOUT;
44 		DPU_ERROR("VBIF %d client %d not halting. TIMEDOUT.\n",
45 				vbif->idx - VBIF_0, xin_id);
46 	} else {
47 		rc = 0;
48 		DPU_DEBUG("VBIF %d client %d is halted\n",
49 				vbif->idx - VBIF_0, xin_id);
50 	}
51 
52 	return rc;
53 }
54 
55 /**
56  * _dpu_vbif_apply_dynamic_ot_limit - determine OT based on usecase parameters
57  * @vbif:	Pointer to hardware vbif driver
58  * @ot_lim:	Pointer to OT limit to be modified
59  * @params:	Pointer to usecase parameters
60  */
61 static void _dpu_vbif_apply_dynamic_ot_limit(struct dpu_hw_vbif *vbif,
62 		u32 *ot_lim, struct dpu_vbif_set_ot_params *params)
63 {
64 	u64 pps;
65 	const struct dpu_vbif_dynamic_ot_tbl *tbl;
66 	u32 i;
67 
68 	if (!vbif || !(vbif->cap->features & BIT(DPU_VBIF_QOS_OTLIM)))
69 		return;
70 
71 	/* Dynamic OT setting done only for WFD */
72 	if (!params->is_wfd)
73 		return;
74 
75 	pps = params->frame_rate;
76 	pps *= params->width;
77 	pps *= params->height;
78 
79 	tbl = params->rd ? &vbif->cap->dynamic_ot_rd_tbl :
80 			&vbif->cap->dynamic_ot_wr_tbl;
81 
82 	for (i = 0; i < tbl->count; i++) {
83 		if (pps <= tbl->cfg[i].pps) {
84 			*ot_lim = tbl->cfg[i].ot_limit;
85 			break;
86 		}
87 	}
88 
89 	DPU_DEBUG("vbif:%d xin:%d w:%d h:%d fps:%d pps:%llu ot:%u\n",
90 			vbif->idx - VBIF_0, params->xin_id,
91 			params->width, params->height, params->frame_rate,
92 			pps, *ot_lim);
93 }
94 
95 /**
96  * _dpu_vbif_get_ot_limit - get OT based on usecase & configuration parameters
97  * @vbif:	Pointer to hardware vbif driver
98  * @params:	Pointer to usecase parameters
99  * @return:	OT limit
100  */
101 static u32 _dpu_vbif_get_ot_limit(struct dpu_hw_vbif *vbif,
102 	struct dpu_vbif_set_ot_params *params)
103 {
104 	u32 ot_lim = 0;
105 	u32 val;
106 
107 	if (!vbif || !vbif->cap) {
108 		DPU_ERROR("invalid arguments vbif %d\n", vbif != 0);
109 		return -EINVAL;
110 	}
111 
112 	if (vbif->cap->default_ot_wr_limit && !params->rd)
113 		ot_lim = vbif->cap->default_ot_wr_limit;
114 	else if (vbif->cap->default_ot_rd_limit && params->rd)
115 		ot_lim = vbif->cap->default_ot_rd_limit;
116 
117 	/*
118 	 * If default ot is not set from dt/catalog,
119 	 * then do not configure it.
120 	 */
121 	if (ot_lim == 0)
122 		goto exit;
123 
124 	/* Modify the limits if the target and the use case requires it */
125 	_dpu_vbif_apply_dynamic_ot_limit(vbif, &ot_lim, params);
126 
127 	if (vbif && vbif->ops.get_limit_conf) {
128 		val = vbif->ops.get_limit_conf(vbif,
129 				params->xin_id, params->rd);
130 		if (val == ot_lim)
131 			ot_lim = 0;
132 	}
133 
134 exit:
135 	DPU_DEBUG("vbif:%d xin:%d ot_lim:%d\n",
136 			vbif->idx - VBIF_0, params->xin_id, ot_lim);
137 	return ot_lim;
138 }
139 
140 /**
141  * dpu_vbif_set_ot_limit - set OT based on usecase & configuration parameters
142  * @vbif:	Pointer to hardware vbif driver
143  * @params:	Pointer to usecase parameters
144  *
145  * Note this function would block waiting for bus halt.
146  */
147 void dpu_vbif_set_ot_limit(struct dpu_kms *dpu_kms,
148 		struct dpu_vbif_set_ot_params *params)
149 {
150 	struct dpu_hw_vbif *vbif = NULL;
151 	struct dpu_hw_mdp *mdp;
152 	bool forced_on = false;
153 	u32 ot_lim;
154 	int ret, i;
155 
156 	if (!dpu_kms) {
157 		DPU_ERROR("invalid arguments\n");
158 		return;
159 	}
160 	mdp = dpu_kms->hw_mdp;
161 
162 	for (i = 0; i < ARRAY_SIZE(dpu_kms->hw_vbif); i++) {
163 		if (dpu_kms->hw_vbif[i] &&
164 				dpu_kms->hw_vbif[i]->idx == params->vbif_idx)
165 			vbif = dpu_kms->hw_vbif[i];
166 	}
167 
168 	if (!vbif || !mdp) {
169 		DPU_DEBUG("invalid arguments vbif %d mdp %d\n",
170 				vbif != 0, mdp != 0);
171 		return;
172 	}
173 
174 	if (!mdp->ops.setup_clk_force_ctrl ||
175 			!vbif->ops.set_limit_conf ||
176 			!vbif->ops.set_halt_ctrl)
177 		return;
178 
179 	/* set write_gather_en for all write clients */
180 	if (vbif->ops.set_write_gather_en && !params->rd)
181 		vbif->ops.set_write_gather_en(vbif, params->xin_id);
182 
183 	ot_lim = _dpu_vbif_get_ot_limit(vbif, params) & 0xFF;
184 
185 	if (ot_lim == 0)
186 		return;
187 
188 	trace_dpu_perf_set_ot(params->num, params->xin_id, ot_lim,
189 		params->vbif_idx);
190 
191 	forced_on = mdp->ops.setup_clk_force_ctrl(mdp, params->clk_ctrl, true);
192 
193 	vbif->ops.set_limit_conf(vbif, params->xin_id, params->rd, ot_lim);
194 
195 	vbif->ops.set_halt_ctrl(vbif, params->xin_id, true);
196 
197 	ret = _dpu_vbif_wait_for_xin_halt(vbif, params->xin_id);
198 	if (ret)
199 		trace_dpu_vbif_wait_xin_halt_fail(vbif->idx, params->xin_id);
200 
201 	vbif->ops.set_halt_ctrl(vbif, params->xin_id, false);
202 
203 	if (forced_on)
204 		mdp->ops.setup_clk_force_ctrl(mdp, params->clk_ctrl, false);
205 }
206 
207 void dpu_vbif_set_qos_remap(struct dpu_kms *dpu_kms,
208 		struct dpu_vbif_set_qos_params *params)
209 {
210 	struct dpu_hw_vbif *vbif = NULL;
211 	struct dpu_hw_mdp *mdp;
212 	bool forced_on = false;
213 	const struct dpu_vbif_qos_tbl *qos_tbl;
214 	int i;
215 
216 	if (!dpu_kms || !params || !dpu_kms->hw_mdp) {
217 		DPU_ERROR("invalid arguments\n");
218 		return;
219 	}
220 	mdp = dpu_kms->hw_mdp;
221 
222 	for (i = 0; i < ARRAY_SIZE(dpu_kms->hw_vbif); i++) {
223 		if (dpu_kms->hw_vbif[i] &&
224 				dpu_kms->hw_vbif[i]->idx == params->vbif_idx) {
225 			vbif = dpu_kms->hw_vbif[i];
226 			break;
227 		}
228 	}
229 
230 	if (!vbif || !vbif->cap) {
231 		DPU_ERROR("invalid vbif %d\n", params->vbif_idx);
232 		return;
233 	}
234 
235 	if (!vbif->ops.set_qos_remap || !mdp->ops.setup_clk_force_ctrl) {
236 		DPU_DEBUG("qos remap not supported\n");
237 		return;
238 	}
239 
240 	qos_tbl = params->is_rt ? &vbif->cap->qos_rt_tbl :
241 			&vbif->cap->qos_nrt_tbl;
242 
243 	if (!qos_tbl->npriority_lvl || !qos_tbl->priority_lvl) {
244 		DPU_DEBUG("qos tbl not defined\n");
245 		return;
246 	}
247 
248 	forced_on = mdp->ops.setup_clk_force_ctrl(mdp, params->clk_ctrl, true);
249 
250 	for (i = 0; i < qos_tbl->npriority_lvl; i++) {
251 		DPU_DEBUG("vbif:%d xin:%d lvl:%d/%d\n",
252 				params->vbif_idx, params->xin_id, i,
253 				qos_tbl->priority_lvl[i]);
254 		vbif->ops.set_qos_remap(vbif, params->xin_id, i,
255 				qos_tbl->priority_lvl[i]);
256 	}
257 
258 	if (forced_on)
259 		mdp->ops.setup_clk_force_ctrl(mdp, params->clk_ctrl, false);
260 }
261 
262 void dpu_vbif_clear_errors(struct dpu_kms *dpu_kms)
263 {
264 	struct dpu_hw_vbif *vbif;
265 	u32 i, pnd, src;
266 
267 	if (!dpu_kms) {
268 		DPU_ERROR("invalid argument\n");
269 		return;
270 	}
271 
272 	for (i = 0; i < ARRAY_SIZE(dpu_kms->hw_vbif); i++) {
273 		vbif = dpu_kms->hw_vbif[i];
274 		if (vbif && vbif->ops.clear_errors) {
275 			vbif->ops.clear_errors(vbif, &pnd, &src);
276 			if (pnd || src) {
277 				DRM_DEBUG_KMS("VBIF %d: pnd 0x%X, src 0x%X\n",
278 					      vbif->idx - VBIF_0, pnd, src);
279 			}
280 		}
281 	}
282 }
283 
284 void dpu_vbif_init_memtypes(struct dpu_kms *dpu_kms)
285 {
286 	struct dpu_hw_vbif *vbif;
287 	int i, j;
288 
289 	if (!dpu_kms) {
290 		DPU_ERROR("invalid argument\n");
291 		return;
292 	}
293 
294 	for (i = 0; i < ARRAY_SIZE(dpu_kms->hw_vbif); i++) {
295 		vbif = dpu_kms->hw_vbif[i];
296 		if (vbif && vbif->cap && vbif->ops.set_mem_type) {
297 			for (j = 0; j < vbif->cap->memtype_count; j++)
298 				vbif->ops.set_mem_type(
299 						vbif, j, vbif->cap->memtype[j]);
300 		}
301 	}
302 }
303 
304 #ifdef CONFIG_DEBUG_FS
305 
306 void dpu_debugfs_vbif_init(struct dpu_kms *dpu_kms, struct dentry *debugfs_root)
307 {
308 	char vbif_name[32];
309 	struct dentry *entry, *debugfs_vbif;
310 	int i, j;
311 
312 	entry = debugfs_create_dir("vbif", debugfs_root);
313 
314 	for (i = 0; i < dpu_kms->catalog->vbif_count; i++) {
315 		struct dpu_vbif_cfg *vbif = &dpu_kms->catalog->vbif[i];
316 
317 		snprintf(vbif_name, sizeof(vbif_name), "%d", vbif->id);
318 
319 		debugfs_vbif = debugfs_create_dir(vbif_name, entry);
320 
321 		debugfs_create_u32("features", 0600, debugfs_vbif,
322 			(u32 *)&vbif->features);
323 
324 		debugfs_create_u32("xin_halt_timeout", 0400, debugfs_vbif,
325 			(u32 *)&vbif->xin_halt_timeout);
326 
327 		debugfs_create_u32("default_rd_ot_limit", 0400, debugfs_vbif,
328 			(u32 *)&vbif->default_ot_rd_limit);
329 
330 		debugfs_create_u32("default_wr_ot_limit", 0400, debugfs_vbif,
331 			(u32 *)&vbif->default_ot_wr_limit);
332 
333 		for (j = 0; j < vbif->dynamic_ot_rd_tbl.count; j++) {
334 			struct dpu_vbif_dynamic_ot_cfg *cfg =
335 					&vbif->dynamic_ot_rd_tbl.cfg[j];
336 
337 			snprintf(vbif_name, sizeof(vbif_name),
338 					"dynamic_ot_rd_%d_pps", j);
339 			debugfs_create_u64(vbif_name, 0400, debugfs_vbif,
340 					(u64 *)&cfg->pps);
341 			snprintf(vbif_name, sizeof(vbif_name),
342 					"dynamic_ot_rd_%d_ot_limit", j);
343 			debugfs_create_u32(vbif_name, 0400, debugfs_vbif,
344 					(u32 *)&cfg->ot_limit);
345 		}
346 
347 		for (j = 0; j < vbif->dynamic_ot_wr_tbl.count; j++) {
348 			struct dpu_vbif_dynamic_ot_cfg *cfg =
349 					&vbif->dynamic_ot_wr_tbl.cfg[j];
350 
351 			snprintf(vbif_name, sizeof(vbif_name),
352 					"dynamic_ot_wr_%d_pps", j);
353 			debugfs_create_u64(vbif_name, 0400, debugfs_vbif,
354 					(u64 *)&cfg->pps);
355 			snprintf(vbif_name, sizeof(vbif_name),
356 					"dynamic_ot_wr_%d_ot_limit", j);
357 			debugfs_create_u32(vbif_name, 0400, debugfs_vbif,
358 					(u32 *)&cfg->ot_limit);
359 		}
360 	}
361 }
362 #endif
363