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;
36 if (!crtc->dev || !crtc->dev->dev_private) {
37 DPU_ERROR("invalid device\n");
41 priv = crtc->dev->dev_private;
42 if (!priv || !priv->kms) {
43 DPU_ERROR("invalid kms\n");
47 return to_dpu_kms(priv->kms);
50 static bool _dpu_core_video_mode_intf_connected(struct drm_crtc *crtc)
52 struct drm_crtc *tmp_crtc;
54 drm_for_each_crtc(tmp_crtc, crtc->dev) {
55 if ((dpu_crtc_get_intf_mode(tmp_crtc) == INTF_MODE_VIDEO) &&
57 DPU_DEBUG("video interface connected crtc:%d\n",
66 static void _dpu_core_perf_calc_crtc(struct dpu_kms *kms,
67 struct drm_crtc *crtc,
68 struct drm_crtc_state *state,
69 struct dpu_core_perf_params *perf)
71 struct dpu_crtc_state *dpu_cstate;
73 if (!kms || !kms->catalog || !crtc || !state || !perf) {
74 DPU_ERROR("invalid parameters\n");
78 dpu_cstate = to_dpu_crtc_state(state);
79 memset(perf, 0, sizeof(struct dpu_core_perf_params));
81 if (!dpu_cstate->bw_control) {
82 perf->bw_ctl = kms->catalog->perf.max_bw_high *
84 perf->max_per_pipe_ib = perf->bw_ctl;
85 perf->core_clk_rate = kms->perf.max_core_clk_rate;
86 } else if (kms->perf.perf_tune.mode == DPU_PERF_MODE_MINIMUM) {
88 perf->max_per_pipe_ib = 0;
89 perf->core_clk_rate = 0;
90 } else if (kms->perf.perf_tune.mode == DPU_PERF_MODE_FIXED) {
91 perf->bw_ctl = kms->perf.fix_core_ab_vote;
92 perf->max_per_pipe_ib = kms->perf.fix_core_ib_vote;
93 perf->core_clk_rate = kms->perf.fix_core_clk_rate;
97 "crtc=%d clk_rate=%llu core_ib=%llu core_ab=%llu\n",
98 crtc->base.id, perf->core_clk_rate,
99 perf->max_per_pipe_ib, perf->bw_ctl);
102 int dpu_core_perf_crtc_check(struct drm_crtc *crtc,
103 struct drm_crtc_state *state)
106 u64 bw_sum_of_intfs = 0;
107 enum dpu_crtc_client_type curr_client_type;
109 struct dpu_crtc_state *dpu_cstate;
110 struct drm_crtc *tmp_crtc;
113 if (!crtc || !state) {
114 DPU_ERROR("invalid crtc\n");
118 kms = _dpu_crtc_get_kms(crtc);
119 if (!kms || !kms->catalog) {
120 DPU_ERROR("invalid parameters\n");
124 /* we only need bandwidth check on real-time clients (interfaces) */
125 if (dpu_crtc_get_client_type(crtc) == NRT_CLIENT)
128 dpu_cstate = to_dpu_crtc_state(state);
130 /* obtain new values */
131 _dpu_core_perf_calc_crtc(kms, crtc, state, &dpu_cstate->new_perf);
133 bw_sum_of_intfs = dpu_cstate->new_perf.bw_ctl;
134 curr_client_type = dpu_crtc_get_client_type(crtc);
136 drm_for_each_crtc(tmp_crtc, crtc->dev) {
137 if (tmp_crtc->enabled &&
138 (dpu_crtc_get_client_type(tmp_crtc) ==
139 curr_client_type) && (tmp_crtc != crtc)) {
140 struct dpu_crtc_state *tmp_cstate =
141 to_dpu_crtc_state(tmp_crtc->state);
143 DPU_DEBUG("crtc:%d bw:%llu ctrl:%d\n",
144 tmp_crtc->base.id, tmp_cstate->new_perf.bw_ctl,
145 tmp_cstate->bw_control);
147 * For bw check only use the bw if the
148 * atomic property has been already set
150 if (tmp_cstate->bw_control)
151 bw_sum_of_intfs += tmp_cstate->new_perf.bw_ctl;
154 /* convert bandwidth to kb */
155 bw = DIV_ROUND_UP_ULL(bw_sum_of_intfs, 1000);
156 DPU_DEBUG("calculated bandwidth=%uk\n", bw);
158 is_video_mode = dpu_crtc_get_intf_mode(crtc) == INTF_MODE_VIDEO;
159 threshold = (is_video_mode ||
160 _dpu_core_video_mode_intf_connected(crtc)) ?
161 kms->catalog->perf.max_bw_low :
162 kms->catalog->perf.max_bw_high;
164 DPU_DEBUG("final threshold bw limit = %d\n", threshold);
166 if (!dpu_cstate->bw_control) {
167 DPU_DEBUG("bypass bandwidth check\n");
168 } else if (!threshold) {
169 DPU_ERROR("no bandwidth limits specified\n");
171 } else if (bw > threshold) {
172 DPU_ERROR("exceeds bandwidth: %ukb > %ukb\n", bw,
181 static int _dpu_core_perf_crtc_update_bus(struct dpu_kms *kms,
182 struct drm_crtc *crtc)
184 struct dpu_core_perf_params perf = { 0 };
185 enum dpu_crtc_client_type curr_client_type
186 = dpu_crtc_get_client_type(crtc);
187 struct drm_crtc *tmp_crtc;
188 struct dpu_crtc_state *dpu_cstate;
191 drm_for_each_crtc(tmp_crtc, crtc->dev) {
192 if (tmp_crtc->enabled &&
194 dpu_crtc_get_client_type(tmp_crtc)) {
195 dpu_cstate = to_dpu_crtc_state(tmp_crtc->state);
197 perf.max_per_pipe_ib = max(perf.max_per_pipe_ib,
198 dpu_cstate->new_perf.max_per_pipe_ib);
200 DPU_DEBUG("crtc=%d bw=%llu\n", tmp_crtc->base.id,
201 dpu_cstate->new_perf.bw_ctl);
208 * @dpu_core_perf_crtc_release_bw() - request zero bandwidth
209 * @crtc - pointer to a crtc
211 * Function checks a state variable for the crtc, if all pending commit
212 * requests are done, meaning no more bandwidth is needed, release
215 void dpu_core_perf_crtc_release_bw(struct drm_crtc *crtc)
217 struct drm_crtc *tmp_crtc;
218 struct dpu_crtc *dpu_crtc;
219 struct dpu_crtc_state *dpu_cstate;
223 DPU_ERROR("invalid crtc\n");
227 kms = _dpu_crtc_get_kms(crtc);
228 if (!kms || !kms->catalog) {
229 DPU_ERROR("invalid kms\n");
233 dpu_crtc = to_dpu_crtc(crtc);
234 dpu_cstate = to_dpu_crtc_state(crtc->state);
236 /* only do this for command mode rt client */
237 if (dpu_crtc_get_intf_mode(crtc) != INTF_MODE_CMD)
241 * If video interface present, cmd panel bandwidth cannot be
244 if (dpu_crtc_get_intf_mode(crtc) == INTF_MODE_CMD)
245 drm_for_each_crtc(tmp_crtc, crtc->dev) {
246 if (tmp_crtc->enabled &&
247 dpu_crtc_get_intf_mode(tmp_crtc) ==
252 /* Release the bandwidth */
253 if (kms->perf.enable_bw_release) {
254 trace_dpu_cmd_release_bw(crtc->base.id);
255 DPU_DEBUG("Release BW crtc=%d\n", crtc->base.id);
256 dpu_crtc->cur_perf.bw_ctl = 0;
257 _dpu_core_perf_crtc_update_bus(kms, crtc);
261 static int _dpu_core_perf_set_core_clk_rate(struct dpu_kms *kms, u64 rate)
263 struct dss_clk *core_clk = kms->perf.core_clk;
265 if (core_clk->max_rate && (rate > core_clk->max_rate))
266 rate = core_clk->max_rate;
268 core_clk->rate = rate;
269 return msm_dss_clk_set_rate(core_clk, 1);
272 static u64 _dpu_core_perf_get_core_clk_rate(struct dpu_kms *kms)
274 u64 clk_rate = kms->perf.perf_tune.min_core_clk;
275 struct drm_crtc *crtc;
276 struct dpu_crtc_state *dpu_cstate;
278 drm_for_each_crtc(crtc, kms->dev) {
280 dpu_cstate = to_dpu_crtc_state(crtc->state);
281 clk_rate = max(dpu_cstate->new_perf.core_clk_rate,
283 clk_rate = clk_round_rate(kms->perf.core_clk->clk,
288 if (kms->perf.perf_tune.mode == DPU_PERF_MODE_FIXED)
289 clk_rate = kms->perf.fix_core_clk_rate;
291 DPU_DEBUG("clk:%llu\n", clk_rate);
296 int dpu_core_perf_crtc_update(struct drm_crtc *crtc,
297 int params_changed, bool stop_req)
299 struct dpu_core_perf_params *new, *old;
300 bool update_bus = false, update_clk = false;
302 struct dpu_crtc *dpu_crtc;
303 struct dpu_crtc_state *dpu_cstate;
304 struct msm_drm_private *priv;
309 DPU_ERROR("invalid crtc\n");
313 kms = _dpu_crtc_get_kms(crtc);
314 if (!kms || !kms->catalog) {
315 DPU_ERROR("invalid kms\n");
318 priv = kms->dev->dev_private;
320 dpu_crtc = to_dpu_crtc(crtc);
321 dpu_cstate = to_dpu_crtc_state(crtc->state);
323 DPU_DEBUG("crtc:%d stop_req:%d core_clk:%llu\n",
324 crtc->base.id, stop_req, kms->perf.core_clk_rate);
326 old = &dpu_crtc->cur_perf;
327 new = &dpu_cstate->new_perf;
329 if (crtc->enabled && !stop_req) {
331 * cases for bus bandwidth update.
332 * 1. new bandwidth vote - "ab or ib vote" is higher
333 * than current vote for update request.
334 * 2. new bandwidth vote - "ab or ib vote" is lower
335 * than current vote at end of commit or stop.
337 if ((params_changed && ((new->bw_ctl > old->bw_ctl) ||
338 (new->max_per_pipe_ib > old->max_per_pipe_ib))) ||
339 (!params_changed && ((new->bw_ctl < old->bw_ctl) ||
340 (new->max_per_pipe_ib < old->max_per_pipe_ib)))) {
341 DPU_DEBUG("crtc=%d p=%d new_bw=%llu,old_bw=%llu\n",
342 crtc->base.id, params_changed,
343 new->bw_ctl, old->bw_ctl);
344 old->bw_ctl = new->bw_ctl;
345 old->max_per_pipe_ib = new->max_per_pipe_ib;
349 if ((params_changed &&
350 (new->core_clk_rate > old->core_clk_rate)) ||
352 (new->core_clk_rate < old->core_clk_rate))) {
353 old->core_clk_rate = new->core_clk_rate;
357 DPU_DEBUG("crtc=%d disable\n", crtc->base.id);
358 memset(old, 0, sizeof(*old));
359 memset(new, 0, sizeof(*new));
364 trace_dpu_perf_crtc_update(crtc->base.id, new->bw_ctl,
365 new->core_clk_rate, stop_req, update_bus, update_clk);
368 ret = _dpu_core_perf_crtc_update_bus(kms, crtc);
370 DPU_ERROR("crtc-%d: failed to update bus bw vote\n",
377 * Update the clock after bandwidth vote to ensure
378 * bandwidth is available before clock rate is increased.
381 clk_rate = _dpu_core_perf_get_core_clk_rate(kms);
383 trace_dpu_core_perf_update_clk(kms->dev, stop_req, clk_rate);
385 ret = _dpu_core_perf_set_core_clk_rate(kms, clk_rate);
387 DPU_ERROR("failed to set %s clock rate %llu\n",
388 kms->perf.core_clk->clk_name, clk_rate);
392 kms->perf.core_clk_rate = clk_rate;
393 DPU_DEBUG("update clk rate = %lld HZ\n", clk_rate);
398 #ifdef CONFIG_DEBUG_FS
400 static ssize_t _dpu_core_perf_mode_write(struct file *file,
401 const char __user *user_buf, size_t count, loff_t *ppos)
403 struct dpu_core_perf *perf = file->private_data;
404 struct dpu_perf_cfg *cfg = &perf->catalog->perf;
408 ret = kstrtouint_from_user(user_buf, count, 0, &perf_mode);
412 if (perf_mode >= DPU_PERF_MODE_MAX)
415 if (perf_mode == DPU_PERF_MODE_FIXED) {
416 DRM_INFO("fix performance mode\n");
417 } else if (perf_mode == DPU_PERF_MODE_MINIMUM) {
418 /* run the driver with max clk and BW vote */
419 perf->perf_tune.min_core_clk = perf->max_core_clk_rate;
420 perf->perf_tune.min_bus_vote =
421 (u64) cfg->max_bw_high * 1000;
422 DRM_INFO("minimum performance mode\n");
423 } else if (perf_mode == DPU_PERF_MODE_NORMAL) {
424 /* reset the perf tune params to 0 */
425 perf->perf_tune.min_core_clk = 0;
426 perf->perf_tune.min_bus_vote = 0;
427 DRM_INFO("normal performance mode\n");
429 perf->perf_tune.mode = perf_mode;
434 static ssize_t _dpu_core_perf_mode_read(struct file *file,
435 char __user *buff, size_t count, loff_t *ppos)
437 struct dpu_core_perf *perf = file->private_data;
441 len = scnprintf(buf, sizeof(buf),
442 "mode %d min_mdp_clk %llu min_bus_vote %llu\n",
443 perf->perf_tune.mode,
444 perf->perf_tune.min_core_clk,
445 perf->perf_tune.min_bus_vote);
447 return simple_read_from_buffer(buff, count, ppos, buf, len);
450 static const struct file_operations dpu_core_perf_mode_fops = {
452 .read = _dpu_core_perf_mode_read,
453 .write = _dpu_core_perf_mode_write,
456 int dpu_core_perf_debugfs_init(struct dpu_kms *dpu_kms, struct dentry *parent)
458 struct dpu_core_perf *perf = &dpu_kms->perf;
459 struct dpu_mdss_cfg *catalog = perf->catalog;
460 struct dentry *entry;
462 entry = debugfs_create_dir("core_perf", parent);
464 debugfs_create_u64("max_core_clk_rate", 0600, entry,
465 &perf->max_core_clk_rate);
466 debugfs_create_u64("core_clk_rate", 0600, entry,
467 &perf->core_clk_rate);
468 debugfs_create_u32("enable_bw_release", 0600, entry,
469 (u32 *)&perf->enable_bw_release);
470 debugfs_create_u32("threshold_low", 0600, entry,
471 (u32 *)&catalog->perf.max_bw_low);
472 debugfs_create_u32("threshold_high", 0600, entry,
473 (u32 *)&catalog->perf.max_bw_high);
474 debugfs_create_u32("min_core_ib", 0600, entry,
475 (u32 *)&catalog->perf.min_core_ib);
476 debugfs_create_u32("min_llcc_ib", 0600, entry,
477 (u32 *)&catalog->perf.min_llcc_ib);
478 debugfs_create_u32("min_dram_ib", 0600, entry,
479 (u32 *)&catalog->perf.min_dram_ib);
480 debugfs_create_file("perf_mode", 0600, entry,
481 (u32 *)perf, &dpu_core_perf_mode_fops);
482 debugfs_create_u64("fix_core_clk_rate", 0600, entry,
483 &perf->fix_core_clk_rate);
484 debugfs_create_u64("fix_core_ib_vote", 0600, entry,
485 &perf->fix_core_ib_vote);
486 debugfs_create_u64("fix_core_ab_vote", 0600, entry,
487 &perf->fix_core_ab_vote);
493 void dpu_core_perf_destroy(struct dpu_core_perf *perf)
496 DPU_ERROR("invalid parameters\n");
500 perf->max_core_clk_rate = 0;
501 perf->core_clk = NULL;
502 perf->catalog = NULL;
506 int dpu_core_perf_init(struct dpu_core_perf *perf,
507 struct drm_device *dev,
508 struct dpu_mdss_cfg *catalog,
509 struct dss_clk *core_clk)
512 perf->catalog = catalog;
513 perf->core_clk = core_clk;
515 perf->max_core_clk_rate = core_clk->max_rate;
516 if (!perf->max_core_clk_rate) {
517 DPU_DEBUG("optional max core clk rate, use default\n");
518 perf->max_core_clk_rate = DPU_PERF_DEFAULT_MAX_CORE_CLK_RATE;