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