1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
5 #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
7 #include <linux/debugfs.h>
8 #include <linux/errno.h>
9 #include <linux/mutex.h>
10 #include <linux/sort.h>
11 #include <linux/clk.h>
12 #include <linux/bitmap.h>
15 #include "dpu_trace.h"
17 #include "dpu_core_perf.h"
20 * enum dpu_perf_mode - performance tuning mode
21 * @DPU_PERF_MODE_NORMAL: performance controlled by user mode client
22 * @DPU_PERF_MODE_MINIMUM: performance bounded by minimum setting
23 * @DPU_PERF_MODE_FIXED: performance bounded by fixed setting
27 DPU_PERF_MODE_MINIMUM,
32 static struct dpu_kms *_dpu_crtc_get_kms(struct drm_crtc *crtc)
34 struct msm_drm_private *priv;
35 priv = crtc->dev->dev_private;
36 return to_dpu_kms(priv->kms);
39 static bool _dpu_core_video_mode_intf_connected(struct drm_crtc *crtc)
41 struct drm_crtc *tmp_crtc;
43 drm_for_each_crtc(tmp_crtc, crtc->dev) {
44 if ((dpu_crtc_get_intf_mode(tmp_crtc) == INTF_MODE_VIDEO) &&
46 DPU_DEBUG("video interface connected crtc:%d\n",
55 static void _dpu_core_perf_calc_crtc(struct dpu_kms *kms,
56 struct drm_crtc *crtc,
57 struct drm_crtc_state *state,
58 struct dpu_core_perf_params *perf)
60 struct dpu_crtc_state *dpu_cstate;
62 if (!kms || !kms->catalog || !crtc || !state || !perf) {
63 DPU_ERROR("invalid parameters\n");
67 dpu_cstate = to_dpu_crtc_state(state);
68 memset(perf, 0, sizeof(struct dpu_core_perf_params));
70 if (!dpu_cstate->bw_control) {
71 perf->bw_ctl = kms->catalog->perf.max_bw_high *
73 perf->max_per_pipe_ib = perf->bw_ctl;
74 perf->core_clk_rate = kms->perf.max_core_clk_rate;
75 } else if (kms->perf.perf_tune.mode == DPU_PERF_MODE_MINIMUM) {
77 perf->max_per_pipe_ib = 0;
78 perf->core_clk_rate = 0;
79 } else if (kms->perf.perf_tune.mode == DPU_PERF_MODE_FIXED) {
80 perf->bw_ctl = kms->perf.fix_core_ab_vote;
81 perf->max_per_pipe_ib = kms->perf.fix_core_ib_vote;
82 perf->core_clk_rate = kms->perf.fix_core_clk_rate;
86 "crtc=%d clk_rate=%llu core_ib=%llu core_ab=%llu\n",
87 crtc->base.id, perf->core_clk_rate,
88 perf->max_per_pipe_ib, perf->bw_ctl);
91 int dpu_core_perf_crtc_check(struct drm_crtc *crtc,
92 struct drm_crtc_state *state)
95 u64 bw_sum_of_intfs = 0;
96 enum dpu_crtc_client_type curr_client_type;
98 struct dpu_crtc_state *dpu_cstate;
99 struct drm_crtc *tmp_crtc;
102 if (!crtc || !state) {
103 DPU_ERROR("invalid crtc\n");
107 kms = _dpu_crtc_get_kms(crtc);
109 DPU_ERROR("invalid parameters\n");
113 /* we only need bandwidth check on real-time clients (interfaces) */
114 if (dpu_crtc_get_client_type(crtc) == NRT_CLIENT)
117 dpu_cstate = to_dpu_crtc_state(state);
119 /* obtain new values */
120 _dpu_core_perf_calc_crtc(kms, crtc, state, &dpu_cstate->new_perf);
122 bw_sum_of_intfs = dpu_cstate->new_perf.bw_ctl;
123 curr_client_type = dpu_crtc_get_client_type(crtc);
125 drm_for_each_crtc(tmp_crtc, crtc->dev) {
126 if (tmp_crtc->enabled &&
127 (dpu_crtc_get_client_type(tmp_crtc) ==
128 curr_client_type) && (tmp_crtc != crtc)) {
129 struct dpu_crtc_state *tmp_cstate =
130 to_dpu_crtc_state(tmp_crtc->state);
132 DPU_DEBUG("crtc:%d bw:%llu ctrl:%d\n",
133 tmp_crtc->base.id, tmp_cstate->new_perf.bw_ctl,
134 tmp_cstate->bw_control);
136 * For bw check only use the bw if the
137 * atomic property has been already set
139 if (tmp_cstate->bw_control)
140 bw_sum_of_intfs += tmp_cstate->new_perf.bw_ctl;
143 /* convert bandwidth to kb */
144 bw = DIV_ROUND_UP_ULL(bw_sum_of_intfs, 1000);
145 DPU_DEBUG("calculated bandwidth=%uk\n", bw);
147 is_video_mode = dpu_crtc_get_intf_mode(crtc) == INTF_MODE_VIDEO;
148 threshold = (is_video_mode ||
149 _dpu_core_video_mode_intf_connected(crtc)) ?
150 kms->catalog->perf.max_bw_low :
151 kms->catalog->perf.max_bw_high;
153 DPU_DEBUG("final threshold bw limit = %d\n", threshold);
155 if (!dpu_cstate->bw_control) {
156 DPU_DEBUG("bypass bandwidth check\n");
157 } else if (!threshold) {
158 DPU_ERROR("no bandwidth limits specified\n");
160 } else if (bw > threshold) {
161 DPU_ERROR("exceeds bandwidth: %ukb > %ukb\n", bw,
170 static int _dpu_core_perf_crtc_update_bus(struct dpu_kms *kms,
171 struct drm_crtc *crtc)
173 struct dpu_core_perf_params perf = { 0 };
174 enum dpu_crtc_client_type curr_client_type
175 = dpu_crtc_get_client_type(crtc);
176 struct drm_crtc *tmp_crtc;
177 struct dpu_crtc_state *dpu_cstate;
180 drm_for_each_crtc(tmp_crtc, crtc->dev) {
181 if (tmp_crtc->enabled &&
183 dpu_crtc_get_client_type(tmp_crtc)) {
184 dpu_cstate = to_dpu_crtc_state(tmp_crtc->state);
186 perf.max_per_pipe_ib = max(perf.max_per_pipe_ib,
187 dpu_cstate->new_perf.max_per_pipe_ib);
189 DPU_DEBUG("crtc=%d bw=%llu\n", tmp_crtc->base.id,
190 dpu_cstate->new_perf.bw_ctl);
197 * @dpu_core_perf_crtc_release_bw() - request zero bandwidth
198 * @crtc - pointer to a crtc
200 * Function checks a state variable for the crtc, if all pending commit
201 * requests are done, meaning no more bandwidth is needed, release
204 void dpu_core_perf_crtc_release_bw(struct drm_crtc *crtc)
206 struct dpu_crtc *dpu_crtc;
210 DPU_ERROR("invalid crtc\n");
214 kms = _dpu_crtc_get_kms(crtc);
216 DPU_ERROR("invalid kms\n");
220 dpu_crtc = to_dpu_crtc(crtc);
222 if (atomic_dec_return(&kms->bandwidth_ref) > 0)
225 /* Release the bandwidth */
226 if (kms->perf.enable_bw_release) {
227 trace_dpu_cmd_release_bw(crtc->base.id);
228 DPU_DEBUG("Release BW crtc=%d\n", crtc->base.id);
229 dpu_crtc->cur_perf.bw_ctl = 0;
230 _dpu_core_perf_crtc_update_bus(kms, crtc);
234 static int _dpu_core_perf_set_core_clk_rate(struct dpu_kms *kms, u64 rate)
236 struct dss_clk *core_clk = kms->perf.core_clk;
238 if (core_clk->max_rate && (rate > core_clk->max_rate))
239 rate = core_clk->max_rate;
241 core_clk->rate = rate;
242 return msm_dss_clk_set_rate(core_clk, 1);
245 static u64 _dpu_core_perf_get_core_clk_rate(struct dpu_kms *kms)
247 u64 clk_rate = kms->perf.perf_tune.min_core_clk;
248 struct drm_crtc *crtc;
249 struct dpu_crtc_state *dpu_cstate;
251 drm_for_each_crtc(crtc, kms->dev) {
253 dpu_cstate = to_dpu_crtc_state(crtc->state);
254 clk_rate = max(dpu_cstate->new_perf.core_clk_rate,
256 clk_rate = clk_round_rate(kms->perf.core_clk->clk,
261 if (kms->perf.perf_tune.mode == DPU_PERF_MODE_FIXED)
262 clk_rate = kms->perf.fix_core_clk_rate;
264 DPU_DEBUG("clk:%llu\n", clk_rate);
269 int dpu_core_perf_crtc_update(struct drm_crtc *crtc,
270 int params_changed, bool stop_req)
272 struct dpu_core_perf_params *new, *old;
273 bool update_bus = false, update_clk = false;
275 struct dpu_crtc *dpu_crtc;
276 struct dpu_crtc_state *dpu_cstate;
281 DPU_ERROR("invalid crtc\n");
285 kms = _dpu_crtc_get_kms(crtc);
287 DPU_ERROR("invalid kms\n");
291 dpu_crtc = to_dpu_crtc(crtc);
292 dpu_cstate = to_dpu_crtc_state(crtc->state);
294 DPU_DEBUG("crtc:%d stop_req:%d core_clk:%llu\n",
295 crtc->base.id, stop_req, kms->perf.core_clk_rate);
297 old = &dpu_crtc->cur_perf;
298 new = &dpu_cstate->new_perf;
300 if (crtc->enabled && !stop_req) {
302 * cases for bus bandwidth update.
303 * 1. new bandwidth vote - "ab or ib vote" is higher
304 * than current vote for update request.
305 * 2. new bandwidth vote - "ab or ib vote" is lower
306 * than current vote at end of commit or stop.
308 if ((params_changed && ((new->bw_ctl > old->bw_ctl) ||
309 (new->max_per_pipe_ib > old->max_per_pipe_ib))) ||
310 (!params_changed && ((new->bw_ctl < old->bw_ctl) ||
311 (new->max_per_pipe_ib < old->max_per_pipe_ib)))) {
312 DPU_DEBUG("crtc=%d p=%d new_bw=%llu,old_bw=%llu\n",
313 crtc->base.id, params_changed,
314 new->bw_ctl, old->bw_ctl);
315 old->bw_ctl = new->bw_ctl;
316 old->max_per_pipe_ib = new->max_per_pipe_ib;
320 if ((params_changed &&
321 (new->core_clk_rate > old->core_clk_rate)) ||
323 (new->core_clk_rate < old->core_clk_rate))) {
324 old->core_clk_rate = new->core_clk_rate;
328 DPU_DEBUG("crtc=%d disable\n", crtc->base.id);
329 memset(old, 0, sizeof(*old));
330 memset(new, 0, sizeof(*new));
335 trace_dpu_perf_crtc_update(crtc->base.id, new->bw_ctl,
336 new->core_clk_rate, stop_req, update_bus, update_clk);
339 ret = _dpu_core_perf_crtc_update_bus(kms, crtc);
341 DPU_ERROR("crtc-%d: failed to update bus bw vote\n",
348 * Update the clock after bandwidth vote to ensure
349 * bandwidth is available before clock rate is increased.
352 clk_rate = _dpu_core_perf_get_core_clk_rate(kms);
354 trace_dpu_core_perf_update_clk(kms->dev, stop_req, clk_rate);
356 ret = _dpu_core_perf_set_core_clk_rate(kms, clk_rate);
358 DPU_ERROR("failed to set %s clock rate %llu\n",
359 kms->perf.core_clk->clk_name, clk_rate);
363 kms->perf.core_clk_rate = clk_rate;
364 DPU_DEBUG("update clk rate = %lld HZ\n", clk_rate);
369 #ifdef CONFIG_DEBUG_FS
371 static ssize_t _dpu_core_perf_mode_write(struct file *file,
372 const char __user *user_buf, size_t count, loff_t *ppos)
374 struct dpu_core_perf *perf = file->private_data;
375 struct dpu_perf_cfg *cfg = &perf->catalog->perf;
379 ret = kstrtouint_from_user(user_buf, count, 0, &perf_mode);
383 if (perf_mode >= DPU_PERF_MODE_MAX)
386 if (perf_mode == DPU_PERF_MODE_FIXED) {
387 DRM_INFO("fix performance mode\n");
388 } else if (perf_mode == DPU_PERF_MODE_MINIMUM) {
389 /* run the driver with max clk and BW vote */
390 perf->perf_tune.min_core_clk = perf->max_core_clk_rate;
391 perf->perf_tune.min_bus_vote =
392 (u64) cfg->max_bw_high * 1000;
393 DRM_INFO("minimum performance mode\n");
394 } else if (perf_mode == DPU_PERF_MODE_NORMAL) {
395 /* reset the perf tune params to 0 */
396 perf->perf_tune.min_core_clk = 0;
397 perf->perf_tune.min_bus_vote = 0;
398 DRM_INFO("normal performance mode\n");
400 perf->perf_tune.mode = perf_mode;
405 static ssize_t _dpu_core_perf_mode_read(struct file *file,
406 char __user *buff, size_t count, loff_t *ppos)
408 struct dpu_core_perf *perf = file->private_data;
412 len = scnprintf(buf, sizeof(buf),
413 "mode %d min_mdp_clk %llu min_bus_vote %llu\n",
414 perf->perf_tune.mode,
415 perf->perf_tune.min_core_clk,
416 perf->perf_tune.min_bus_vote);
418 return simple_read_from_buffer(buff, count, ppos, buf, len);
421 static const struct file_operations dpu_core_perf_mode_fops = {
423 .read = _dpu_core_perf_mode_read,
424 .write = _dpu_core_perf_mode_write,
427 int dpu_core_perf_debugfs_init(struct dpu_kms *dpu_kms, struct dentry *parent)
429 struct dpu_core_perf *perf = &dpu_kms->perf;
430 struct dpu_mdss_cfg *catalog = perf->catalog;
431 struct dentry *entry;
433 entry = debugfs_create_dir("core_perf", parent);
435 debugfs_create_u64("max_core_clk_rate", 0600, entry,
436 &perf->max_core_clk_rate);
437 debugfs_create_u64("core_clk_rate", 0600, entry,
438 &perf->core_clk_rate);
439 debugfs_create_u32("enable_bw_release", 0600, entry,
440 (u32 *)&perf->enable_bw_release);
441 debugfs_create_u32("threshold_low", 0600, entry,
442 (u32 *)&catalog->perf.max_bw_low);
443 debugfs_create_u32("threshold_high", 0600, entry,
444 (u32 *)&catalog->perf.max_bw_high);
445 debugfs_create_u32("min_core_ib", 0600, entry,
446 (u32 *)&catalog->perf.min_core_ib);
447 debugfs_create_u32("min_llcc_ib", 0600, entry,
448 (u32 *)&catalog->perf.min_llcc_ib);
449 debugfs_create_u32("min_dram_ib", 0600, entry,
450 (u32 *)&catalog->perf.min_dram_ib);
451 debugfs_create_file("perf_mode", 0600, entry,
452 (u32 *)perf, &dpu_core_perf_mode_fops);
453 debugfs_create_u64("fix_core_clk_rate", 0600, entry,
454 &perf->fix_core_clk_rate);
455 debugfs_create_u64("fix_core_ib_vote", 0600, entry,
456 &perf->fix_core_ib_vote);
457 debugfs_create_u64("fix_core_ab_vote", 0600, entry,
458 &perf->fix_core_ab_vote);
464 void dpu_core_perf_destroy(struct dpu_core_perf *perf)
467 DPU_ERROR("invalid parameters\n");
471 perf->max_core_clk_rate = 0;
472 perf->core_clk = NULL;
473 perf->catalog = NULL;
477 int dpu_core_perf_init(struct dpu_core_perf *perf,
478 struct drm_device *dev,
479 struct dpu_mdss_cfg *catalog,
480 struct dss_clk *core_clk)
483 perf->catalog = catalog;
484 perf->core_clk = core_clk;
486 perf->max_core_clk_rate = core_clk->max_rate;
487 if (!perf->max_core_clk_rate) {
488 DPU_DEBUG("optional max core clk rate, use default\n");
489 perf->max_core_clk_rate = DPU_PERF_DEFAULT_MAX_CORE_CLK_RATE;