2 * Copyright 2018 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "dcn10_clk_mgr.h"
28 #include "reg_helper.h"
29 #include "core_types.h"
31 #define TO_DCE_CLK_MGR(clocks)\
32 container_of(clocks, struct dce_clk_mgr, base)
35 (clk_mgr_dce->regs->reg)
38 #define FN(reg_name, field_name) \
39 clk_mgr_dce->clk_mgr_shift->field_name, clk_mgr_dce->clk_mgr_mask->field_name
46 static int dcn1_determine_dppclk_threshold(struct clk_mgr *clk_mgr, struct dc_clocks *new_clocks)
48 bool request_dpp_div = new_clocks->dispclk_khz > new_clocks->dppclk_khz;
49 bool dispclk_increase = new_clocks->dispclk_khz > clk_mgr->clks.dispclk_khz;
50 int disp_clk_threshold = new_clocks->max_supported_dppclk_khz;
51 bool cur_dpp_div = clk_mgr->clks.dispclk_khz > clk_mgr->clks.dppclk_khz;
53 /* increase clock, looking for div is 0 for current, request div is 1*/
54 if (dispclk_increase) {
55 /* already divided by 2, no need to reach target clk with 2 steps*/
57 return new_clocks->dispclk_khz;
59 /* request disp clk is lower than maximum supported dpp clk,
60 * no need to reach target clk with two steps.
62 if (new_clocks->dispclk_khz <= disp_clk_threshold)
63 return new_clocks->dispclk_khz;
65 /* target dpp clk not request divided by 2, still within threshold */
67 return new_clocks->dispclk_khz;
70 /* decrease clock, looking for current dppclk divided by 2,
71 * request dppclk not divided by 2.
74 /* current dpp clk not divided by 2, no need to ramp*/
76 return new_clocks->dispclk_khz;
78 /* current disp clk is lower than current maximum dpp clk,
81 if (clk_mgr->clks.dispclk_khz <= disp_clk_threshold)
82 return new_clocks->dispclk_khz;
84 /* request dpp clk need to be divided by 2 */
86 return new_clocks->dispclk_khz;
89 return disp_clk_threshold;
92 static void dcn1_ramp_up_dispclk_with_dpp(struct clk_mgr *clk_mgr, struct dc_clocks *new_clocks)
94 struct dc *dc = clk_mgr->ctx->dc;
95 int dispclk_to_dpp_threshold = dcn1_determine_dppclk_threshold(clk_mgr, new_clocks);
96 bool request_dpp_div = new_clocks->dispclk_khz > new_clocks->dppclk_khz;
99 /* set disp clk to dpp clk threshold */
100 dce112_set_clock(clk_mgr, dispclk_to_dpp_threshold);
102 /* update request dpp clk division option */
103 for (i = 0; i < dc->res_pool->pipe_count; i++) {
104 struct pipe_ctx *pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
106 if (!pipe_ctx->plane_state)
109 pipe_ctx->plane_res.dpp->funcs->dpp_dppclk_control(
110 pipe_ctx->plane_res.dpp,
115 /* If target clk not same as dppclk threshold, set to target clock */
116 if (dispclk_to_dpp_threshold != new_clocks->dispclk_khz)
117 dce112_set_clock(clk_mgr, new_clocks->dispclk_khz);
119 clk_mgr->clks.dispclk_khz = new_clocks->dispclk_khz;
120 clk_mgr->clks.dppclk_khz = new_clocks->dppclk_khz;
121 clk_mgr->clks.max_supported_dppclk_khz = new_clocks->max_supported_dppclk_khz;
124 static int get_active_display_cnt(
126 struct dc_state *context)
128 int i, display_count;
131 for (i = 0; i < context->stream_count; i++) {
132 const struct dc_stream_state *stream = context->streams[i];
135 * Only notify active stream or virtual stream.
136 * Need to notify virtual stream to work around
137 * headless case. HPD does not fire when system is in
140 if (!stream->dpms_off || stream->signal == SIGNAL_TYPE_VIRTUAL)
144 return display_count;
147 static void dcn1_update_clocks(struct clk_mgr *clk_mgr,
148 struct dc_state *context,
151 struct dc *dc = clk_mgr->ctx->dc;
152 struct dc_debug_options *debug = &dc->debug;
153 struct dc_clocks *new_clocks = &context->bw.dcn.clk;
154 struct pp_smu_display_requirement_rv *smu_req_cur =
155 &dc->res_pool->pp_smu_req;
156 struct pp_smu_display_requirement_rv smu_req = *smu_req_cur;
157 struct pp_smu_funcs_rv *pp_smu = NULL;
158 bool send_request_to_increase = false;
159 bool send_request_to_lower = false;
162 bool enter_display_off = false;
164 display_count = get_active_display_cnt(dc, context);
165 if (dc->res_pool->pp_smu)
166 pp_smu = &dc->res_pool->pp_smu->rv_funcs;
167 if (display_count == 0)
168 enter_display_off = true;
170 if (enter_display_off == safe_to_lower) {
172 * Notify SMU active displays
173 * if function pointer not set up, this message is
174 * sent as part of pplib_apply_display_requirements.
176 if (pp_smu && pp_smu->set_display_count)
177 pp_smu->set_display_count(&pp_smu->pp_smu, display_count);
179 smu_req.display_count = display_count;
182 if (new_clocks->dispclk_khz > clk_mgr->clks.dispclk_khz
183 || new_clocks->phyclk_khz > clk_mgr->clks.phyclk_khz
184 || new_clocks->fclk_khz > clk_mgr->clks.fclk_khz
185 || new_clocks->dcfclk_khz > clk_mgr->clks.dcfclk_khz)
186 send_request_to_increase = true;
188 if (should_set_clock(safe_to_lower, new_clocks->phyclk_khz, clk_mgr->clks.phyclk_khz)) {
189 clk_mgr->clks.phyclk_khz = new_clocks->phyclk_khz;
191 send_request_to_lower = true;
195 if (debug->force_fclk_khz != 0)
196 new_clocks->fclk_khz = debug->force_fclk_khz;
198 if (should_set_clock(safe_to_lower, new_clocks->fclk_khz, clk_mgr->clks.fclk_khz)) {
199 clk_mgr->clks.fclk_khz = new_clocks->fclk_khz;
200 smu_req.hard_min_fclk_mhz = new_clocks->fclk_khz / 1000;
202 send_request_to_lower = true;
206 if (should_set_clock(safe_to_lower, new_clocks->dcfclk_khz, clk_mgr->clks.dcfclk_khz)) {
207 clk_mgr->clks.dcfclk_khz = new_clocks->dcfclk_khz;
208 smu_req.hard_min_dcefclk_mhz = new_clocks->dcfclk_khz / 1000;
210 send_request_to_lower = true;
213 if (should_set_clock(safe_to_lower,
214 new_clocks->dcfclk_deep_sleep_khz, clk_mgr->clks.dcfclk_deep_sleep_khz)) {
215 clk_mgr->clks.dcfclk_deep_sleep_khz = new_clocks->dcfclk_deep_sleep_khz;
216 smu_req.min_deep_sleep_dcefclk_mhz = (new_clocks->dcfclk_deep_sleep_khz + 999) / 1000;
218 send_request_to_lower = true;
221 /* make sure dcf clk is before dpp clk to
222 * make sure we have enough voltage to run dpp clk
224 if (send_request_to_increase) {
225 /*use dcfclk to request voltage*/
226 if (pp_smu && pp_smu->set_hard_min_fclk_by_freq &&
227 pp_smu->set_hard_min_dcfclk_by_freq &&
228 pp_smu->set_min_deep_sleep_dcfclk) {
230 pp_smu->set_hard_min_fclk_by_freq(&pp_smu->pp_smu, smu_req.hard_min_fclk_mhz);
231 pp_smu->set_hard_min_dcfclk_by_freq(&pp_smu->pp_smu, smu_req.hard_min_dcefclk_mhz);
232 pp_smu->set_min_deep_sleep_dcfclk(&pp_smu->pp_smu, smu_req.min_deep_sleep_dcefclk_mhz);
236 /* dcn1 dppclk is tied to dispclk */
237 /* program dispclk on = as a w/a for sleep resume clock ramping issues */
238 if (should_set_clock(safe_to_lower, new_clocks->dispclk_khz, clk_mgr->clks.dispclk_khz)
239 || new_clocks->dispclk_khz == clk_mgr->clks.dispclk_khz) {
240 dcn1_ramp_up_dispclk_with_dpp(clk_mgr, new_clocks);
241 clk_mgr->clks.dispclk_khz = new_clocks->dispclk_khz;
243 send_request_to_lower = true;
246 if (!send_request_to_increase && send_request_to_lower) {
247 /*use dcfclk to request voltage*/
248 if (pp_smu && pp_smu->set_hard_min_fclk_by_freq &&
249 pp_smu->set_hard_min_dcfclk_by_freq &&
250 pp_smu->set_min_deep_sleep_dcfclk) {
252 pp_smu->set_hard_min_fclk_by_freq(&pp_smu->pp_smu, smu_req.hard_min_fclk_mhz);
253 pp_smu->set_hard_min_dcfclk_by_freq(&pp_smu->pp_smu, smu_req.hard_min_dcefclk_mhz);
254 pp_smu->set_min_deep_sleep_dcfclk(&pp_smu->pp_smu, smu_req.min_deep_sleep_dcefclk_mhz);
258 *smu_req_cur = smu_req;
260 static const struct clk_mgr_funcs dcn1_funcs = {
261 .get_dp_ref_clk_frequency = dce12_get_dp_ref_freq_khz,
262 .update_clocks = dcn1_update_clocks
264 struct clk_mgr *dcn1_clk_mgr_create(struct dc_context *ctx)
266 struct dc_debug_options *debug = &ctx->dc->debug;
267 struct dc_bios *bp = ctx->dc_bios;
268 struct dc_firmware_info fw_info = { { 0 } };
269 struct dce_clk_mgr *clk_mgr_dce = kzalloc(sizeof(*clk_mgr_dce), GFP_KERNEL);
271 if (clk_mgr_dce == NULL) {
276 clk_mgr_dce->base.ctx = ctx;
277 clk_mgr_dce->base.funcs = &dcn1_funcs;
279 clk_mgr_dce->dfs_bypass_disp_clk = 0;
281 clk_mgr_dce->dprefclk_ss_percentage = 0;
282 clk_mgr_dce->dprefclk_ss_divider = 1000;
283 clk_mgr_dce->ss_on_dprefclk = false;
285 clk_mgr_dce->dprefclk_khz = 600000;
286 if (bp->integrated_info)
287 clk_mgr_dce->dentist_vco_freq_khz = bp->integrated_info->dentist_vco_freq;
288 if (clk_mgr_dce->dentist_vco_freq_khz == 0) {
289 bp->funcs->get_firmware_info(bp, &fw_info);
290 clk_mgr_dce->dentist_vco_freq_khz = fw_info.smu_gpu_pll_output_freq;
291 if (clk_mgr_dce->dentist_vco_freq_khz == 0)
292 clk_mgr_dce->dentist_vco_freq_khz = 3600000;
295 if (!debug->disable_dfs_bypass && bp->integrated_info)
296 if (bp->integrated_info->gpu_cap_info & DFS_BYPASS_ENABLE)
297 clk_mgr_dce->dfs_bypass_enabled = true;
299 dce_clock_read_ss_info(clk_mgr_dce);
301 return &clk_mgr_dce->base;