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