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>
14 #include <linux/acpi.h>
15 #include <linux/platform_profile.h>
18 #define APMF_FUNC_VERIFY_INTERFACE 0
19 #define APMF_FUNC_GET_SYS_PARAMS 1
20 #define APMF_FUNC_SBIOS_REQUESTS 2
21 #define APMF_FUNC_SBIOS_HEARTBEAT 4
22 #define APMF_FUNC_AUTO_MODE 5
23 #define APMF_FUNC_SET_FAN_IDX 7
24 #define APMF_FUNC_STATIC_SLIDER_GRANULAR 9
25 #define APMF_FUNC_DYN_SLIDER_AC 11
26 #define APMF_FUNC_DYN_SLIDER_DC 12
28 /* Message Definitions */
29 #define SET_SPL 0x03 /* SPL: Sustained Power Limit */
30 #define SET_SPPT 0x05 /* SPPT: Slow Package Power Tracking */
31 #define SET_FPPT 0x07 /* FPPT: Fast Package Power Tracking */
35 #define SET_DRAM_ADDR_HIGH 0x14
36 #define SET_DRAM_ADDR_LOW 0x15
37 #define SET_TRANSFER_TABLE 0x16
38 #define SET_STT_MIN_LIMIT 0x18 /* STT: Skin Temperature Tracking */
39 #define SET_STT_LIMIT_APU 0x19
40 #define SET_STT_LIMIT_HS2 0x1A
41 #define SET_SPPT_APU_ONLY 0x1D
42 #define GET_SPPT_APU_ONLY 0x1E
43 #define GET_STT_MIN_LIMIT 0x1F
44 #define GET_STT_LIMIT_APU 0x20
45 #define GET_STT_LIMIT_HS2 0x21
47 /* Fan Index for Auto Mode */
48 #define FAN_INDEX_AUTO 0xFFFFFFFF
51 #define AVG_SAMPLE_SIZE 3
53 /* AMD PMF BIOS interfaces */
54 struct apmf_verify_interface {
57 u32 notification_mask;
58 u32 supported_functions;
61 struct apmf_system_params {
69 struct apmf_sbios_req {
90 struct smu_pmf_metrics {
91 u16 gfxclk_freq; /* in MHz */
92 u16 socclk_freq; /* in MHz */
93 u16 vclk_freq; /* in MHz */
94 u16 dclk_freq; /* in MHz */
95 u16 memclk_freq; /* in MHz */
97 u16 gfx_activity; /* in Centi */
98 u16 uvd_activity; /* in Centi */
99 u16 voltage[2]; /* in mV */
100 u16 currents[2]; /* in mA */
101 u16 power[2];/* in mW */
102 u16 core_freq[8]; /* in MHz */
103 u16 core_power[8]; /* in mW */
104 u16 core_temp[8]; /* in centi-Celsius */
105 u16 l3_freq; /* in MHz */
106 u16 l3_temp; /* in centi-Celsius */
107 u16 gfx_temp; /* in centi-Celsius */
108 u16 soc_temp; /* in centi-Celsius */
109 u16 throttler_status;
110 u16 current_socketpower; /* in mW */
111 u16 stapm_orig_limit; /* in W */
112 u16 stapm_cur_limit; /* in W */
113 u32 apu_power; /* in mW */
114 u32 dgpu_power; /* in mW */
115 u16 vdd_tdc_val; /* in mA */
116 u16 soc_tdc_val; /* in mA */
117 u16 vdd_edc_val; /* in mA */
118 u16 soc_edcv_al; /* in mA */
119 u16 infra_cpu_maxfreq; /* in MHz */
120 u16 infra_gfx_maxfreq; /* in MHz */
121 u16 skin_temp; /* in centi-Celsius */
125 enum amd_stt_skin_temp {
143 POWER_MODE_PERFORMANCE,
144 POWER_MODE_BALANCED_POWER,
145 POWER_MODE_POWER_SAVER,
150 void __iomem *regbase;
151 void __iomem *smu_virt_addr;
156 struct mutex lock; /* protects the PMF interface */
158 enum platform_profile_option current_profile;
159 struct platform_profile_handler pprof;
160 struct dentry *dbgfs_dir;
161 int hb_interval; /* SBIOS heartbeat interval */
162 struct delayed_work heart_beat;
163 struct smu_pmf_metrics m_table;
164 struct delayed_work work_buffer;
166 int socket_power_history[AVG_SAMPLE_SIZE];
167 int socket_power_history_idx;
169 struct mutex update_mutex; /* protects race between ACPI handler and metrics thread */
172 struct notifier_block pwr_src_notifier;
175 struct apmf_sps_prop_granular {
181 u8 stt_skin_temp[STT_TEMP_COUNT];
186 struct apmf_static_slider_granular_output {
188 struct apmf_sps_prop_granular prop[POWER_SOURCE_MAX * POWER_MODE_MAX];
191 struct amd_pmf_static_slider_granular {
193 struct apmf_sps_prop_granular prop[POWER_SOURCE_MAX][POWER_MODE_MAX];
196 struct fan_table_control {
198 unsigned long fan_id;
201 struct power_table_control {
207 u32 stt_skin_temp[STT_TEMP_COUNT];
211 /* Auto Mode Layer */
212 enum auto_mode_transition_priority {
213 AUTO_TRANSITION_TO_PERFORMANCE, /* Any other mode to Performance Mode */
214 AUTO_TRANSITION_FROM_QUIET_TO_BALANCE, /* Quiet Mode to Balance Mode */
215 AUTO_TRANSITION_TO_QUIET, /* Any other mode to Quiet Mode */
216 AUTO_TRANSITION_FROM_PERFORMANCE_TO_BALANCE, /* Performance Mode to Balance Mode */
220 enum auto_mode_mode {
223 AUTO_PERFORMANCE_ON_LAP,
228 struct auto_mode_trans_params {
229 u32 time_constant; /* minimum time required to switch to next mode */
230 u32 power_delta; /* delta power to shift mode */
232 u32 timer; /* elapsed time. if timer > TimeThreshold, it will move to next mode */
234 enum auto_mode_mode target_mode;
238 struct auto_mode_mode_settings {
239 struct power_table_control power_control;
240 struct fan_table_control fan_control;
244 struct auto_mode_mode_config {
245 struct auto_mode_trans_params transition[AUTO_TRANSITION_MAX];
246 struct auto_mode_mode_settings mode_set[AUTO_MODE_MAX];
247 enum auto_mode_mode current_mode;
250 struct apmf_auto_mode {
253 u32 balanced_to_perf;
254 u32 perf_to_balanced;
255 u32 quiet_to_balanced;
256 u32 balanced_to_quiet;
261 /* Power delta for mode change */
262 u32 pd_balanced_to_perf;
263 u32 pd_perf_to_balanced;
264 u32 pd_quiet_to_balanced;
265 u32 pd_balanced_to_quiet;
266 /* skin temperature limits */
267 u8 stt_apu_perf_on_lap; /* CQL ON */
268 u8 stt_hs2_perf_on_lap; /* CQL ON */
275 u32 stt_min_limit_perf_on_lap; /* CQL ON */
276 u32 stt_min_limit_perf;
277 u32 stt_min_limit_balanced;
278 u32 stt_min_limit_quiet;
280 u32 fppt_perf_on_lap; /* CQL ON */
281 u32 sppt_perf_on_lap; /* CQL ON */
282 u32 spl_perf_on_lap; /* CQL ON */
283 u32 sppt_apu_only_perf_on_lap; /* CQL ON */
287 u32 sppt_apu_only_perf;
291 u32 sppt_apu_only_balanced;
295 u32 sppt_apu_only_quiet;
303 enum cnqf_trans_priority {
304 CNQF_TRANSITION_TO_TURBO, /* Any other mode to Turbo Mode */
305 CNQF_TRANSITION_FROM_BALANCE_TO_PERFORMANCE, /* quiet/balance to Performance Mode */
306 CNQF_TRANSITION_FROM_QUIET_TO_BALANCE, /* Quiet Mode to Balance Mode */
307 CNQF_TRANSITION_TO_QUIET, /* Any other mode to Quiet Mode */
308 CNQF_TRANSITION_FROM_PERFORMANCE_TO_BALANCE, /* Performance/Turbo to Balance Mode */
309 CNQF_TRANSITION_FROM_TURBO_TO_PERFORMANCE, /* Turbo mode to Performance Mode */
316 CNQF_MODE_PERFORMANCE,
323 APMF_CNQF_PERFORMANCE,
329 struct cnqf_mode_settings {
330 struct power_table_control power_control;
331 struct fan_table_control fan_control;
335 struct cnqf_tran_params {
336 u32 time_constant; /* minimum time required to switch to next mode */
338 u32 timer; /* elapsed time. if timer > timethreshold, it will move to next mode */
343 enum cnqf_mode target_mode;
347 struct cnqf_tran_params trans_param[POWER_SOURCE_MAX][CNQF_TRANSITION_MAX];
348 struct cnqf_mode_settings mode_set[POWER_SOURCE_MAX][CNQF_MODE_MAX];
349 struct power_table_control defaults;
350 enum cnqf_mode current_mode;
355 struct apmf_cnqf_power_set {
362 u8 stt_skintemp[STT_TEMP_COUNT];
366 struct apmf_dyn_slider_output {
370 u32 t_balanced_to_perf;
371 u32 t_quiet_to_balanced;
372 u32 t_balanced_to_quiet;
373 u32 t_perf_to_balanced;
375 struct apmf_cnqf_power_set ps[APMF_CNQF_MAX];
379 int apmf_acpi_init(struct amd_pmf_dev *pmf_dev);
380 void apmf_acpi_deinit(struct amd_pmf_dev *pmf_dev);
381 int is_apmf_func_supported(struct amd_pmf_dev *pdev, unsigned long index);
382 int amd_pmf_send_cmd(struct amd_pmf_dev *dev, u8 message, bool get, u32 arg, u32 *data);
383 int amd_pmf_init_metrics_table(struct amd_pmf_dev *dev);
384 int amd_pmf_get_power_source(void);
385 int apmf_install_handler(struct amd_pmf_dev *pmf_dev);
388 int amd_pmf_get_pprof_modes(struct amd_pmf_dev *pmf);
389 void amd_pmf_update_slider(struct amd_pmf_dev *dev, bool op, int idx,
390 struct amd_pmf_static_slider_granular *table);
391 int amd_pmf_init_sps(struct amd_pmf_dev *dev);
392 void amd_pmf_deinit_sps(struct amd_pmf_dev *dev);
393 int apmf_get_static_slider_granular(struct amd_pmf_dev *pdev,
394 struct apmf_static_slider_granular_output *output);
395 bool is_pprof_balanced(struct amd_pmf_dev *pmf);
398 int apmf_update_fan_idx(struct amd_pmf_dev *pdev, bool manual, u32 idx);
399 int amd_pmf_set_sps_power_limits(struct amd_pmf_dev *pmf);
401 /* Auto Mode Layer */
402 int apmf_get_auto_mode_def(struct amd_pmf_dev *pdev, struct apmf_auto_mode *data);
403 void amd_pmf_init_auto_mode(struct amd_pmf_dev *dev);
404 void amd_pmf_deinit_auto_mode(struct amd_pmf_dev *dev);
405 void amd_pmf_trans_automode(struct amd_pmf_dev *dev, int socket_power, ktime_t time_elapsed_ms);
406 int apmf_get_sbios_requests(struct amd_pmf_dev *pdev, struct apmf_sbios_req *req);
408 void amd_pmf_update_2_cql(struct amd_pmf_dev *dev, bool is_cql_event);
409 int amd_pmf_reset_amt(struct amd_pmf_dev *dev);
410 void amd_pmf_handle_amt(struct amd_pmf_dev *dev);
413 int apmf_get_dyn_slider_def_ac(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data);
414 int apmf_get_dyn_slider_def_dc(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data);
415 int amd_pmf_init_cnqf(struct amd_pmf_dev *dev);
416 void amd_pmf_deinit_cnqf(struct amd_pmf_dev *dev);
417 int amd_pmf_trans_cnqf(struct amd_pmf_dev *dev, int socket_power, ktime_t time_lapsed_ms);
418 extern const struct attribute_group cnqf_feature_attribute_group;