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/pm_opp.h>
11 #include <linux/sort.h>
12 #include <linux/clk.h>
13 #include <linux/bitmap.h>
16 #include "dpu_trace.h"
18 #include "dpu_core_perf.h"
21 * enum dpu_perf_mode - performance tuning mode
22 * @DPU_PERF_MODE_NORMAL: performance controlled by user mode client
23 * @DPU_PERF_MODE_MINIMUM: performance bounded by minimum setting
24 * @DPU_PERF_MODE_FIXED: performance bounded by fixed setting
25 * @DPU_PERF_MODE_MAX: maximum value, used for error checking
29 DPU_PERF_MODE_MINIMUM,
35 * _dpu_core_perf_calc_bw() - to calculate BW per crtc
36 * @perf_cfg: performance configuration
37 * @crtc: pointer to a crtc
38 * Return: returns aggregated BW for all planes in crtc.
40 static u64 _dpu_core_perf_calc_bw(const struct dpu_perf_cfg *perf_cfg,
41 struct drm_crtc *crtc)
43 struct drm_plane *plane;
44 struct dpu_plane_state *pstate;
45 u64 crtc_plane_bw = 0;
48 drm_atomic_crtc_for_each_plane(plane, crtc) {
49 pstate = to_dpu_plane_state(plane->state);
53 crtc_plane_bw += pstate->plane_fetch_bw;
56 bw_factor = perf_cfg->bw_inefficiency_factor;
58 crtc_plane_bw *= bw_factor;
59 do_div(crtc_plane_bw, 100);
66 * _dpu_core_perf_calc_clk() - to calculate clock per crtc
67 * @perf_cfg: performance configuration
68 * @crtc: pointer to a crtc
69 * @state: pointer to a crtc state
70 * Return: returns max clk for all planes in crtc.
72 static u64 _dpu_core_perf_calc_clk(const struct dpu_perf_cfg *perf_cfg,
73 struct drm_crtc *crtc, struct drm_crtc_state *state)
75 struct drm_plane *plane;
76 struct dpu_plane_state *pstate;
77 struct drm_display_mode *mode;
81 mode = &state->adjusted_mode;
83 crtc_clk = mode->vtotal * mode->hdisplay * drm_mode_vrefresh(mode);
85 drm_atomic_crtc_for_each_plane(plane, crtc) {
86 pstate = to_dpu_plane_state(plane->state);
90 crtc_clk = max(pstate->plane_clk, crtc_clk);
93 clk_factor = perf_cfg->clk_inefficiency_factor;
95 crtc_clk *= clk_factor;
96 do_div(crtc_clk, 100);
102 static struct dpu_kms *_dpu_crtc_get_kms(struct drm_crtc *crtc)
104 struct msm_drm_private *priv;
105 priv = crtc->dev->dev_private;
106 return to_dpu_kms(priv->kms);
109 static void _dpu_core_perf_calc_crtc(const struct dpu_core_perf *core_perf,
110 struct drm_crtc *crtc,
111 struct drm_crtc_state *state,
112 struct dpu_core_perf_params *perf)
114 const struct dpu_perf_cfg *perf_cfg = core_perf->perf_cfg;
116 if (!perf_cfg || !crtc || !state || !perf) {
117 DPU_ERROR("invalid parameters\n");
121 memset(perf, 0, sizeof(struct dpu_core_perf_params));
123 if (core_perf->perf_tune.mode == DPU_PERF_MODE_MINIMUM) {
125 perf->max_per_pipe_ib = 0;
126 perf->core_clk_rate = 0;
127 } else if (core_perf->perf_tune.mode == DPU_PERF_MODE_FIXED) {
128 perf->bw_ctl = core_perf->fix_core_ab_vote;
129 perf->max_per_pipe_ib = core_perf->fix_core_ib_vote;
130 perf->core_clk_rate = core_perf->fix_core_clk_rate;
132 perf->bw_ctl = _dpu_core_perf_calc_bw(perf_cfg, crtc);
133 perf->max_per_pipe_ib = perf_cfg->min_dram_ib;
134 perf->core_clk_rate = _dpu_core_perf_calc_clk(perf_cfg, crtc, state);
138 "crtc=%d clk_rate=%llu core_ib=%llu core_ab=%llu\n",
139 crtc->base.id, perf->core_clk_rate,
140 perf->max_per_pipe_ib, perf->bw_ctl);
143 int dpu_core_perf_crtc_check(struct drm_crtc *crtc,
144 struct drm_crtc_state *state)
147 u64 bw_sum_of_intfs = 0;
148 enum dpu_crtc_client_type curr_client_type;
149 struct dpu_crtc_state *dpu_cstate;
150 struct drm_crtc *tmp_crtc;
153 if (!crtc || !state) {
154 DPU_ERROR("invalid crtc\n");
158 kms = _dpu_crtc_get_kms(crtc);
160 /* we only need bandwidth check on real-time clients (interfaces) */
161 if (dpu_crtc_get_client_type(crtc) == NRT_CLIENT)
164 dpu_cstate = to_dpu_crtc_state(state);
166 /* obtain new values */
167 _dpu_core_perf_calc_crtc(&kms->perf, crtc, state, &dpu_cstate->new_perf);
169 bw_sum_of_intfs = dpu_cstate->new_perf.bw_ctl;
170 curr_client_type = dpu_crtc_get_client_type(crtc);
172 drm_for_each_crtc(tmp_crtc, crtc->dev) {
173 if (tmp_crtc->enabled &&
174 dpu_crtc_get_client_type(tmp_crtc) == curr_client_type &&
176 struct dpu_crtc_state *tmp_cstate =
177 to_dpu_crtc_state(tmp_crtc->state);
179 DRM_DEBUG_ATOMIC("crtc:%d bw:%llu ctrl:%d\n",
180 tmp_crtc->base.id, tmp_cstate->new_perf.bw_ctl,
181 tmp_cstate->bw_control);
183 bw_sum_of_intfs += tmp_cstate->new_perf.bw_ctl;
186 /* convert bandwidth to kb */
187 bw = DIV_ROUND_UP_ULL(bw_sum_of_intfs, 1000);
188 DRM_DEBUG_ATOMIC("calculated bandwidth=%uk\n", bw);
190 threshold = kms->perf.perf_cfg->max_bw_high;
192 DRM_DEBUG_ATOMIC("final threshold bw limit = %d\n", threshold);
195 DPU_ERROR("no bandwidth limits specified\n");
197 } else if (bw > threshold) {
198 DPU_ERROR("exceeds bandwidth: %ukb > %ukb\n", bw,
207 static int _dpu_core_perf_crtc_update_bus(struct dpu_kms *kms,
208 struct drm_crtc *crtc)
210 struct dpu_core_perf_params perf = { 0 };
211 enum dpu_crtc_client_type curr_client_type
212 = dpu_crtc_get_client_type(crtc);
213 struct drm_crtc *tmp_crtc;
214 struct dpu_crtc_state *dpu_cstate;
221 drm_for_each_crtc(tmp_crtc, crtc->dev) {
222 if (tmp_crtc->enabled &&
224 dpu_crtc_get_client_type(tmp_crtc)) {
225 dpu_cstate = to_dpu_crtc_state(tmp_crtc->state);
227 perf.max_per_pipe_ib = max(perf.max_per_pipe_ib,
228 dpu_cstate->new_perf.max_per_pipe_ib);
230 perf.bw_ctl += dpu_cstate->new_perf.bw_ctl;
232 DRM_DEBUG_ATOMIC("crtc=%d bw=%llu paths:%d\n",
234 dpu_cstate->new_perf.bw_ctl, kms->num_paths);
238 avg_bw = perf.bw_ctl;
239 do_div(avg_bw, (kms->num_paths * 1000)); /*Bps_to_icc*/
241 for (i = 0; i < kms->num_paths; i++)
242 icc_set_bw(kms->path[i], avg_bw, perf.max_per_pipe_ib);
248 * dpu_core_perf_crtc_release_bw() - request zero bandwidth
249 * @crtc: pointer to a crtc
251 * Function checks a state variable for the crtc, if all pending commit
252 * requests are done, meaning no more bandwidth is needed, release
255 void dpu_core_perf_crtc_release_bw(struct drm_crtc *crtc)
257 struct dpu_crtc *dpu_crtc;
261 DPU_ERROR("invalid crtc\n");
265 kms = _dpu_crtc_get_kms(crtc);
266 dpu_crtc = to_dpu_crtc(crtc);
268 if (atomic_dec_return(&kms->bandwidth_ref) > 0)
271 /* Release the bandwidth */
272 if (kms->perf.enable_bw_release) {
273 trace_dpu_cmd_release_bw(crtc->base.id);
274 DRM_DEBUG_ATOMIC("Release BW crtc=%d\n", crtc->base.id);
275 dpu_crtc->cur_perf.bw_ctl = 0;
276 _dpu_core_perf_crtc_update_bus(kms, crtc);
280 static u64 _dpu_core_perf_get_core_clk_rate(struct dpu_kms *kms)
283 struct drm_crtc *crtc;
284 struct dpu_crtc_state *dpu_cstate;
286 if (kms->perf.perf_tune.mode == DPU_PERF_MODE_FIXED)
287 return kms->perf.fix_core_clk_rate;
289 if (kms->perf.perf_tune.mode == DPU_PERF_MODE_MINIMUM)
290 return kms->perf.max_core_clk_rate;
293 drm_for_each_crtc(crtc, kms->dev) {
295 dpu_cstate = to_dpu_crtc_state(crtc->state);
296 clk_rate = max(dpu_cstate->new_perf.core_clk_rate,
304 int dpu_core_perf_crtc_update(struct drm_crtc *crtc,
307 struct dpu_core_perf_params *new, *old;
308 bool update_bus = false, update_clk = false;
310 struct dpu_crtc *dpu_crtc;
311 struct dpu_crtc_state *dpu_cstate;
316 DPU_ERROR("invalid crtc\n");
320 kms = _dpu_crtc_get_kms(crtc);
322 dpu_crtc = to_dpu_crtc(crtc);
323 dpu_cstate = to_dpu_crtc_state(crtc->state);
325 DRM_DEBUG_ATOMIC("crtc:%d enabled:%d core_clk:%llu\n",
326 crtc->base.id, crtc->enabled, kms->perf.core_clk_rate);
328 old = &dpu_crtc->cur_perf;
329 new = &dpu_cstate->new_perf;
333 * cases for bus bandwidth update.
334 * 1. new bandwidth vote - "ab or ib vote" is higher
335 * than current vote for update request.
336 * 2. new bandwidth vote - "ab or ib vote" is lower
337 * than current vote at end of commit or stop.
339 if ((params_changed && ((new->bw_ctl > old->bw_ctl) ||
340 (new->max_per_pipe_ib > old->max_per_pipe_ib))) ||
341 (!params_changed && ((new->bw_ctl < old->bw_ctl) ||
342 (new->max_per_pipe_ib < old->max_per_pipe_ib)))) {
343 DRM_DEBUG_ATOMIC("crtc=%d p=%d new_bw=%llu,old_bw=%llu\n",
344 crtc->base.id, params_changed,
345 new->bw_ctl, old->bw_ctl);
346 old->bw_ctl = new->bw_ctl;
347 old->max_per_pipe_ib = new->max_per_pipe_ib;
351 if ((params_changed && new->core_clk_rate > old->core_clk_rate) ||
352 (!params_changed && new->core_clk_rate < old->core_clk_rate)) {
353 old->core_clk_rate = new->core_clk_rate;
357 DRM_DEBUG_ATOMIC("crtc=%d disable\n", crtc->base.id);
358 memset(old, 0, sizeof(*old));
363 trace_dpu_perf_crtc_update(crtc->base.id, new->bw_ctl,
364 new->core_clk_rate, !crtc->enabled, update_bus, update_clk);
367 ret = _dpu_core_perf_crtc_update_bus(kms, crtc);
369 DPU_ERROR("crtc-%d: failed to update bus bw vote\n",
376 * Update the clock after bandwidth vote to ensure
377 * bandwidth is available before clock rate is increased.
380 clk_rate = _dpu_core_perf_get_core_clk_rate(kms);
382 DRM_DEBUG_ATOMIC("clk:%llu\n", clk_rate);
384 trace_dpu_core_perf_update_clk(kms->dev, !crtc->enabled, clk_rate);
386 clk_rate = min(clk_rate, kms->perf.max_core_clk_rate);
387 ret = dev_pm_opp_set_rate(&kms->pdev->dev, clk_rate);
389 DPU_ERROR("failed to set core clock rate %llu\n", clk_rate);
393 kms->perf.core_clk_rate = clk_rate;
394 DRM_DEBUG_ATOMIC("update clk rate = %lld HZ\n", clk_rate);
399 #ifdef CONFIG_DEBUG_FS
401 static ssize_t _dpu_core_perf_mode_write(struct file *file,
402 const char __user *user_buf, size_t count, loff_t *ppos)
404 struct dpu_core_perf *perf = file->private_data;
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 DRM_INFO("minimum performance mode\n");
420 } else if (perf_mode == DPU_PERF_MODE_NORMAL) {
421 /* reset the perf tune params to 0 */
422 DRM_INFO("normal performance mode\n");
424 perf->perf_tune.mode = perf_mode;
429 static ssize_t _dpu_core_perf_mode_read(struct file *file,
430 char __user *buff, size_t count, loff_t *ppos)
432 struct dpu_core_perf *perf = file->private_data;
436 len = scnprintf(buf, sizeof(buf),
438 perf->perf_tune.mode);
440 return simple_read_from_buffer(buff, count, ppos, buf, len);
443 static const struct file_operations dpu_core_perf_mode_fops = {
445 .read = _dpu_core_perf_mode_read,
446 .write = _dpu_core_perf_mode_write,
449 int dpu_core_perf_debugfs_init(struct dpu_kms *dpu_kms, struct dentry *parent)
451 struct dpu_core_perf *perf = &dpu_kms->perf;
452 struct dentry *entry;
454 entry = debugfs_create_dir("core_perf", parent);
456 debugfs_create_u64("max_core_clk_rate", 0600, entry,
457 &perf->max_core_clk_rate);
458 debugfs_create_u64("core_clk_rate", 0600, entry,
459 &perf->core_clk_rate);
460 debugfs_create_u32("enable_bw_release", 0600, entry,
461 (u32 *)&perf->enable_bw_release);
462 debugfs_create_u32("threshold_low", 0400, entry,
463 (u32 *)&perf->perf_cfg->max_bw_low);
464 debugfs_create_u32("threshold_high", 0400, entry,
465 (u32 *)&perf->perf_cfg->max_bw_high);
466 debugfs_create_u32("min_core_ib", 0400, entry,
467 (u32 *)&perf->perf_cfg->min_core_ib);
468 debugfs_create_u32("min_llcc_ib", 0400, entry,
469 (u32 *)&perf->perf_cfg->min_llcc_ib);
470 debugfs_create_u32("min_dram_ib", 0400, entry,
471 (u32 *)&perf->perf_cfg->min_dram_ib);
472 debugfs_create_file("perf_mode", 0600, entry,
473 (u32 *)perf, &dpu_core_perf_mode_fops);
474 debugfs_create_u64("fix_core_clk_rate", 0600, entry,
475 &perf->fix_core_clk_rate);
476 debugfs_create_u64("fix_core_ib_vote", 0600, entry,
477 &perf->fix_core_ib_vote);
478 debugfs_create_u64("fix_core_ab_vote", 0600, entry,
479 &perf->fix_core_ab_vote);
485 int dpu_core_perf_init(struct dpu_core_perf *perf,
486 const struct dpu_perf_cfg *perf_cfg,
487 unsigned long max_core_clk_rate)
489 perf->perf_cfg = perf_cfg;
490 perf->max_core_clk_rate = max_core_clk_rate;