1 // SPDX-License-Identifier: GPL-2.0
3 * AMD Platform Management Framework Driver
5 * Copyright (c) 2022, Advanced Micro Devices, Inc.
8 * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
11 #include <linux/workqueue.h>
14 static struct cnqf_config config_store;
16 static int amd_pmf_set_cnqf(struct amd_pmf_dev *dev, int src, int idx,
17 struct cnqf_config *table)
19 struct power_table_control *pc;
21 pc = &config_store.mode_set[src][idx].power_control;
23 amd_pmf_send_cmd(dev, SET_SPL, false, pc->spl, NULL);
24 amd_pmf_send_cmd(dev, SET_FPPT, false, pc->fppt, NULL);
25 amd_pmf_send_cmd(dev, SET_SPPT, false, pc->sppt, NULL);
26 amd_pmf_send_cmd(dev, SET_SPPT_APU_ONLY, false, pc->sppt_apu_only, NULL);
27 amd_pmf_send_cmd(dev, SET_STT_MIN_LIMIT, false, pc->stt_min, NULL);
28 amd_pmf_send_cmd(dev, SET_STT_LIMIT_APU, false, pc->stt_skin_temp[STT_TEMP_APU],
30 amd_pmf_send_cmd(dev, SET_STT_LIMIT_HS2, false, pc->stt_skin_temp[STT_TEMP_HS2],
33 if (is_apmf_func_supported(dev, APMF_FUNC_SET_FAN_IDX))
34 apmf_update_fan_idx(dev,
35 config_store.mode_set[src][idx].fan_control.manual,
36 config_store.mode_set[src][idx].fan_control.fan_id);
41 static void amd_pmf_update_power_threshold(int src)
43 struct cnqf_mode_settings *ts;
44 struct cnqf_tran_params *tp;
46 tp = &config_store.trans_param[src][CNQF_TRANSITION_TO_QUIET];
47 ts = &config_store.mode_set[src][CNQF_MODE_BALANCE];
48 tp->power_threshold = ts->power_floor;
50 tp = &config_store.trans_param[src][CNQF_TRANSITION_TO_TURBO];
51 ts = &config_store.mode_set[src][CNQF_MODE_PERFORMANCE];
52 tp->power_threshold = ts->power_floor;
54 tp = &config_store.trans_param[src][CNQF_TRANSITION_FROM_BALANCE_TO_PERFORMANCE];
55 ts = &config_store.mode_set[src][CNQF_MODE_BALANCE];
56 tp->power_threshold = ts->power_floor;
58 tp = &config_store.trans_param[src][CNQF_TRANSITION_FROM_PERFORMANCE_TO_BALANCE];
59 ts = &config_store.mode_set[src][CNQF_MODE_PERFORMANCE];
60 tp->power_threshold = ts->power_floor;
62 tp = &config_store.trans_param[src][CNQF_TRANSITION_FROM_QUIET_TO_BALANCE];
63 ts = &config_store.mode_set[src][CNQF_MODE_QUIET];
64 tp->power_threshold = ts->power_floor;
66 tp = &config_store.trans_param[src][CNQF_TRANSITION_FROM_TURBO_TO_PERFORMANCE];
67 ts = &config_store.mode_set[src][CNQF_MODE_TURBO];
68 tp->power_threshold = ts->power_floor;
71 static const char *state_as_str(unsigned int state)
76 case CNQF_MODE_BALANCE:
80 case CNQF_MODE_PERFORMANCE:
83 return "Unknown CnQF mode";
87 static int amd_pmf_cnqf_get_power_source(struct amd_pmf_dev *dev)
89 if (is_apmf_func_supported(dev, APMF_FUNC_DYN_SLIDER_AC) &&
90 is_apmf_func_supported(dev, APMF_FUNC_DYN_SLIDER_DC))
91 return amd_pmf_get_power_source();
92 else if (is_apmf_func_supported(dev, APMF_FUNC_DYN_SLIDER_DC))
93 return POWER_SOURCE_DC;
95 return POWER_SOURCE_AC;
98 int amd_pmf_trans_cnqf(struct amd_pmf_dev *dev, int socket_power, ktime_t time_lapsed_ms)
100 struct cnqf_tran_params *tp;
104 src = amd_pmf_cnqf_get_power_source(dev);
106 if (dev->current_profile == PLATFORM_PROFILE_BALANCED) {
107 amd_pmf_set_cnqf(dev, src, config_store.current_mode, NULL);
110 * Return from here if the platform_profile is not balanced
111 * so that preference is given to user mode selection, rather
112 * than enforcing CnQF to run all the time (if enabled)
117 for (i = 0; i < CNQF_TRANSITION_MAX; i++) {
118 config_store.trans_param[src][i].timer += time_lapsed_ms;
119 config_store.trans_param[src][i].total_power += socket_power;
120 config_store.trans_param[src][i].count++;
122 tp = &config_store.trans_param[src][i];
123 if (tp->timer >= tp->time_constant && tp->count) {
124 avg_power = tp->total_power / tp->count;
126 /* Reset the indices */
131 if ((tp->shifting_up && avg_power >= tp->power_threshold) ||
132 (!tp->shifting_up && avg_power <= tp->power_threshold)) {
135 tp->priority = false;
140 dev_dbg(dev->dev, "[CNQF] Avg power: %u mW socket power: %u mW mode:%s\n",
141 avg_power, socket_power, state_as_str(config_store.current_mode));
143 for (j = 0; j < CNQF_TRANSITION_MAX; j++) {
144 /* apply the highest priority */
145 if (config_store.trans_param[src][j].priority) {
146 if (config_store.current_mode !=
147 config_store.trans_param[src][j].target_mode) {
148 config_store.current_mode =
149 config_store.trans_param[src][j].target_mode;
150 dev_dbg(dev->dev, "Moving to Mode :%s\n",
151 state_as_str(config_store.current_mode));
152 amd_pmf_set_cnqf(dev, src,
153 config_store.current_mode, NULL);
161 static void amd_pmf_update_trans_data(int idx, struct apmf_dyn_slider_output out)
163 struct cnqf_tran_params *tp;
165 tp = &config_store.trans_param[idx][CNQF_TRANSITION_TO_QUIET];
166 tp->time_constant = out.t_balanced_to_quiet;
167 tp->target_mode = CNQF_MODE_QUIET;
168 tp->shifting_up = false;
170 tp = &config_store.trans_param[idx][CNQF_TRANSITION_FROM_BALANCE_TO_PERFORMANCE];
171 tp->time_constant = out.t_balanced_to_perf;
172 tp->target_mode = CNQF_MODE_PERFORMANCE;
173 tp->shifting_up = true;
175 tp = &config_store.trans_param[idx][CNQF_TRANSITION_FROM_QUIET_TO_BALANCE];
176 tp->time_constant = out.t_quiet_to_balanced;
177 tp->target_mode = CNQF_MODE_BALANCE;
178 tp->shifting_up = true;
180 tp = &config_store.trans_param[idx][CNQF_TRANSITION_FROM_PERFORMANCE_TO_BALANCE];
181 tp->time_constant = out.t_perf_to_balanced;
182 tp->target_mode = CNQF_MODE_BALANCE;
183 tp->shifting_up = false;
185 tp = &config_store.trans_param[idx][CNQF_TRANSITION_FROM_TURBO_TO_PERFORMANCE];
186 tp->time_constant = out.t_turbo_to_perf;
187 tp->target_mode = CNQF_MODE_PERFORMANCE;
188 tp->shifting_up = false;
190 tp = &config_store.trans_param[idx][CNQF_TRANSITION_TO_TURBO];
191 tp->time_constant = out.t_perf_to_turbo;
192 tp->target_mode = CNQF_MODE_TURBO;
193 tp->shifting_up = true;
196 static void amd_pmf_update_mode_set(int idx, struct apmf_dyn_slider_output out)
198 struct cnqf_mode_settings *ms;
201 ms = &config_store.mode_set[idx][CNQF_MODE_QUIET];
202 ms->power_floor = out.ps[APMF_CNQF_QUIET].pfloor;
203 ms->power_control.fppt = out.ps[APMF_CNQF_QUIET].fppt;
204 ms->power_control.sppt = out.ps[APMF_CNQF_QUIET].sppt;
205 ms->power_control.sppt_apu_only = out.ps[APMF_CNQF_QUIET].sppt_apu_only;
206 ms->power_control.spl = out.ps[APMF_CNQF_QUIET].spl;
207 ms->power_control.stt_min = out.ps[APMF_CNQF_QUIET].stt_min_limit;
208 ms->power_control.stt_skin_temp[STT_TEMP_APU] =
209 out.ps[APMF_CNQF_QUIET].stt_skintemp[STT_TEMP_APU];
210 ms->power_control.stt_skin_temp[STT_TEMP_HS2] =
211 out.ps[APMF_CNQF_QUIET].stt_skintemp[STT_TEMP_HS2];
212 ms->fan_control.fan_id = out.ps[APMF_CNQF_QUIET].fan_id;
215 ms = &config_store.mode_set[idx][CNQF_MODE_BALANCE];
216 ms->power_floor = out.ps[APMF_CNQF_BALANCE].pfloor;
217 ms->power_control.fppt = out.ps[APMF_CNQF_BALANCE].fppt;
218 ms->power_control.sppt = out.ps[APMF_CNQF_BALANCE].sppt;
219 ms->power_control.sppt_apu_only = out.ps[APMF_CNQF_BALANCE].sppt_apu_only;
220 ms->power_control.spl = out.ps[APMF_CNQF_BALANCE].spl;
221 ms->power_control.stt_min = out.ps[APMF_CNQF_BALANCE].stt_min_limit;
222 ms->power_control.stt_skin_temp[STT_TEMP_APU] =
223 out.ps[APMF_CNQF_BALANCE].stt_skintemp[STT_TEMP_APU];
224 ms->power_control.stt_skin_temp[STT_TEMP_HS2] =
225 out.ps[APMF_CNQF_BALANCE].stt_skintemp[STT_TEMP_HS2];
226 ms->fan_control.fan_id = out.ps[APMF_CNQF_BALANCE].fan_id;
228 /* Performance Mode */
229 ms = &config_store.mode_set[idx][CNQF_MODE_PERFORMANCE];
230 ms->power_floor = out.ps[APMF_CNQF_PERFORMANCE].pfloor;
231 ms->power_control.fppt = out.ps[APMF_CNQF_PERFORMANCE].fppt;
232 ms->power_control.sppt = out.ps[APMF_CNQF_PERFORMANCE].sppt;
233 ms->power_control.sppt_apu_only = out.ps[APMF_CNQF_PERFORMANCE].sppt_apu_only;
234 ms->power_control.spl = out.ps[APMF_CNQF_PERFORMANCE].spl;
235 ms->power_control.stt_min = out.ps[APMF_CNQF_PERFORMANCE].stt_min_limit;
236 ms->power_control.stt_skin_temp[STT_TEMP_APU] =
237 out.ps[APMF_CNQF_PERFORMANCE].stt_skintemp[STT_TEMP_APU];
238 ms->power_control.stt_skin_temp[STT_TEMP_HS2] =
239 out.ps[APMF_CNQF_PERFORMANCE].stt_skintemp[STT_TEMP_HS2];
240 ms->fan_control.fan_id = out.ps[APMF_CNQF_PERFORMANCE].fan_id;
243 ms = &config_store.mode_set[idx][CNQF_MODE_TURBO];
244 ms->power_floor = out.ps[APMF_CNQF_TURBO].pfloor;
245 ms->power_control.fppt = out.ps[APMF_CNQF_TURBO].fppt;
246 ms->power_control.sppt = out.ps[APMF_CNQF_TURBO].sppt;
247 ms->power_control.sppt_apu_only = out.ps[APMF_CNQF_TURBO].sppt_apu_only;
248 ms->power_control.spl = out.ps[APMF_CNQF_TURBO].spl;
249 ms->power_control.stt_min = out.ps[APMF_CNQF_TURBO].stt_min_limit;
250 ms->power_control.stt_skin_temp[STT_TEMP_APU] =
251 out.ps[APMF_CNQF_TURBO].stt_skintemp[STT_TEMP_APU];
252 ms->power_control.stt_skin_temp[STT_TEMP_HS2] =
253 out.ps[APMF_CNQF_TURBO].stt_skintemp[STT_TEMP_HS2];
254 ms->fan_control.fan_id = out.ps[APMF_CNQF_TURBO].fan_id;
257 static int amd_pmf_check_flags(struct amd_pmf_dev *dev)
259 struct apmf_dyn_slider_output out = {};
261 if (is_apmf_func_supported(dev, APMF_FUNC_DYN_SLIDER_AC))
262 apmf_get_dyn_slider_def_ac(dev, &out);
263 else if (is_apmf_func_supported(dev, APMF_FUNC_DYN_SLIDER_DC))
264 apmf_get_dyn_slider_def_dc(dev, &out);
269 static int amd_pmf_load_defaults_cnqf(struct amd_pmf_dev *dev)
271 struct apmf_dyn_slider_output out;
274 for (i = 0; i < POWER_SOURCE_MAX; i++) {
275 if (!is_apmf_func_supported(dev, APMF_FUNC_DYN_SLIDER_AC + i))
278 if (i == POWER_SOURCE_AC)
279 ret = apmf_get_dyn_slider_def_ac(dev, &out);
281 ret = apmf_get_dyn_slider_def_dc(dev, &out);
283 dev_err(dev->dev, "APMF apmf_get_dyn_slider_def_dc failed :%d\n", ret);
287 amd_pmf_update_mode_set(i, out);
288 amd_pmf_update_trans_data(i, out);
289 amd_pmf_update_power_threshold(i);
291 for (j = 0; j < CNQF_MODE_MAX; j++) {
292 if (config_store.mode_set[i][j].fan_control.fan_id == FAN_INDEX_AUTO)
293 config_store.mode_set[i][j].fan_control.manual = false;
295 config_store.mode_set[i][j].fan_control.manual = true;
299 /* set to initial default values */
300 config_store.current_mode = CNQF_MODE_BALANCE;
305 static ssize_t cnqf_enable_store(struct device *dev,
306 struct device_attribute *attr,
307 const char *buf, size_t count)
309 struct amd_pmf_dev *pdev = dev_get_drvdata(dev);
313 result = kstrtobool(buf, &input);
317 src = amd_pmf_cnqf_get_power_source(pdev);
318 pdev->cnqf_enabled = input;
320 if (pdev->cnqf_enabled && pdev->current_profile == PLATFORM_PROFILE_BALANCED) {
321 amd_pmf_set_cnqf(pdev, src, config_store.current_mode, NULL);
323 if (is_apmf_func_supported(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR))
324 amd_pmf_set_sps_power_limits(pdev);
327 dev_dbg(pdev->dev, "Received CnQF %s\n", input ? "on" : "off");
331 static ssize_t cnqf_enable_show(struct device *dev,
332 struct device_attribute *attr,
335 struct amd_pmf_dev *pdev = dev_get_drvdata(dev);
337 return sysfs_emit(buf, "%s\n", pdev->cnqf_enabled ? "on" : "off");
340 static DEVICE_ATTR_RW(cnqf_enable);
342 static umode_t cnqf_feature_is_visible(struct kobject *kobj,
343 struct attribute *attr, int n)
345 struct device *dev = kobj_to_dev(kobj);
346 struct amd_pmf_dev *pdev = dev_get_drvdata(dev);
348 return pdev->cnqf_supported ? attr->mode : 0;
351 static struct attribute *cnqf_feature_attrs[] = {
352 &dev_attr_cnqf_enable.attr,
356 const struct attribute_group cnqf_feature_attribute_group = {
357 .is_visible = cnqf_feature_is_visible,
358 .attrs = cnqf_feature_attrs,
361 void amd_pmf_deinit_cnqf(struct amd_pmf_dev *dev)
363 cancel_delayed_work_sync(&dev->work_buffer);
366 int amd_pmf_init_cnqf(struct amd_pmf_dev *dev)
371 * Note the caller of this function has already checked that both
372 * APMF_FUNC_DYN_SLIDER_AC and APMF_FUNC_DYN_SLIDER_DC are supported.
375 ret = amd_pmf_load_defaults_cnqf(dev);
379 amd_pmf_init_metrics_table(dev);
381 dev->cnqf_supported = true;
382 dev->cnqf_enabled = amd_pmf_check_flags(dev);
384 /* update the thermal for CnQF */
385 if (dev->cnqf_enabled && dev->current_profile == PLATFORM_PROFILE_BALANCED) {
386 src = amd_pmf_cnqf_get_power_source(dev);
387 amd_pmf_set_cnqf(dev, src, config_store.current_mode, NULL);