xref: /linux/drivers/platform/x86/amd/pmf/sps.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
14c71ae41SShyam Sundar S K // SPDX-License-Identifier: GPL-2.0
24c71ae41SShyam Sundar S K /*
34c71ae41SShyam Sundar S K  * AMD Platform Management Framework (PMF) Driver
44c71ae41SShyam Sundar S K  *
54c71ae41SShyam Sundar S K  * Copyright (c) 2022, Advanced Micro Devices, Inc.
64c71ae41SShyam Sundar S K  * All Rights Reserved.
74c71ae41SShyam Sundar S K  *
84c71ae41SShyam Sundar S K  * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
94c71ae41SShyam Sundar S K  */
104c71ae41SShyam Sundar S K 
114c71ae41SShyam Sundar S K #include "pmf.h"
124c71ae41SShyam Sundar S K 
1348d38f56SShyam Sundar S K static struct amd_pmf_static_slider_granular_v2 config_store_v2;
144c71ae41SShyam Sundar S K static struct amd_pmf_static_slider_granular config_store;
153eecb434SShyam Sundar S K static struct amd_pmf_apts_granular apts_config_store;
164c71ae41SShyam Sundar S K 
17a82ebb3dSShyam Sundar S K #ifdef CONFIG_AMD_PMF_DEBUG
slider_v2_as_str(unsigned int state)1848d38f56SShyam Sundar S K static const char *slider_v2_as_str(unsigned int state)
1948d38f56SShyam Sundar S K {
2048d38f56SShyam Sundar S K 	switch (state) {
2148d38f56SShyam Sundar S K 	case POWER_MODE_BEST_PERFORMANCE:
2248d38f56SShyam Sundar S K 		return "Best Performance";
2348d38f56SShyam Sundar S K 	case POWER_MODE_BALANCED:
2448d38f56SShyam Sundar S K 		return "Balanced";
2548d38f56SShyam Sundar S K 	case POWER_MODE_BEST_POWER_EFFICIENCY:
2648d38f56SShyam Sundar S K 		return "Best Power Efficiency";
2748d38f56SShyam Sundar S K 	case POWER_MODE_ENERGY_SAVE:
2848d38f56SShyam Sundar S K 		return "Energy Save";
2948d38f56SShyam Sundar S K 	default:
3048d38f56SShyam Sundar S K 		return "Unknown Power Mode";
3148d38f56SShyam Sundar S K 	}
3248d38f56SShyam Sundar S K }
3348d38f56SShyam Sundar S K 
slider_as_str(unsigned int state)34506ed33dSShyam Sundar S K static const char *slider_as_str(unsigned int state)
35a82ebb3dSShyam Sundar S K {
36a82ebb3dSShyam Sundar S K 	switch (state) {
37a82ebb3dSShyam Sundar S K 	case POWER_MODE_PERFORMANCE:
38a82ebb3dSShyam Sundar S K 		return "PERFORMANCE";
39a82ebb3dSShyam Sundar S K 	case POWER_MODE_BALANCED_POWER:
40a82ebb3dSShyam Sundar S K 		return "BALANCED_POWER";
41a82ebb3dSShyam Sundar S K 	case POWER_MODE_POWER_SAVER:
42a82ebb3dSShyam Sundar S K 		return "POWER_SAVER";
43a82ebb3dSShyam Sundar S K 	default:
44a82ebb3dSShyam Sundar S K 		return "Unknown Slider State";
45a82ebb3dSShyam Sundar S K 	}
46a82ebb3dSShyam Sundar S K }
47a82ebb3dSShyam Sundar S K 
amd_pmf_source_as_str(unsigned int state)484984dbb6SShyam Sundar S K const char *amd_pmf_source_as_str(unsigned int state)
49a82ebb3dSShyam Sundar S K {
50a82ebb3dSShyam Sundar S K 	switch (state) {
51a82ebb3dSShyam Sundar S K 	case POWER_SOURCE_AC:
52a82ebb3dSShyam Sundar S K 		return "AC";
53a82ebb3dSShyam Sundar S K 	case POWER_SOURCE_DC:
54a82ebb3dSShyam Sundar S K 		return "DC";
55a82ebb3dSShyam Sundar S K 	default:
56a82ebb3dSShyam Sundar S K 		return "Unknown Power State";
57a82ebb3dSShyam Sundar S K 	}
58a82ebb3dSShyam Sundar S K }
59a82ebb3dSShyam Sundar S K 
amd_pmf_dump_sps_defaults(struct amd_pmf_static_slider_granular * data)60a82ebb3dSShyam Sundar S K static void amd_pmf_dump_sps_defaults(struct amd_pmf_static_slider_granular *data)
61a82ebb3dSShyam Sundar S K {
62a82ebb3dSShyam Sundar S K 	int i, j;
63a82ebb3dSShyam Sundar S K 
64a82ebb3dSShyam Sundar S K 	pr_debug("Static Slider Data - BEGIN\n");
65a82ebb3dSShyam Sundar S K 
66a82ebb3dSShyam Sundar S K 	for (i = 0; i < POWER_SOURCE_MAX; i++) {
67a82ebb3dSShyam Sundar S K 		for (j = 0; j < POWER_MODE_MAX; j++) {
684984dbb6SShyam Sundar S K 			pr_debug("--- Source:%s Mode:%s ---\n", amd_pmf_source_as_str(i),
694984dbb6SShyam Sundar S K 				 slider_as_str(j));
70a82ebb3dSShyam Sundar S K 			pr_debug("SPL: %u mW\n", data->prop[i][j].spl);
71a82ebb3dSShyam Sundar S K 			pr_debug("SPPT: %u mW\n", data->prop[i][j].sppt);
72a82ebb3dSShyam Sundar S K 			pr_debug("SPPT_ApuOnly: %u mW\n", data->prop[i][j].sppt_apu_only);
73a82ebb3dSShyam Sundar S K 			pr_debug("FPPT: %u mW\n", data->prop[i][j].fppt);
74a82ebb3dSShyam Sundar S K 			pr_debug("STTMinLimit: %u mW\n", data->prop[i][j].stt_min);
75a82ebb3dSShyam Sundar S K 			pr_debug("STT_SkinTempLimit_APU: %u C\n",
76a82ebb3dSShyam Sundar S K 				 data->prop[i][j].stt_skin_temp[STT_TEMP_APU]);
77a82ebb3dSShyam Sundar S K 			pr_debug("STT_SkinTempLimit_HS2: %u C\n",
78a82ebb3dSShyam Sundar S K 				 data->prop[i][j].stt_skin_temp[STT_TEMP_HS2]);
79a82ebb3dSShyam Sundar S K 		}
80a82ebb3dSShyam Sundar S K 	}
81a82ebb3dSShyam Sundar S K 
82a82ebb3dSShyam Sundar S K 	pr_debug("Static Slider Data - END\n");
83a82ebb3dSShyam Sundar S K }
8448d38f56SShyam Sundar S K 
amd_pmf_dump_sps_defaults_v2(struct amd_pmf_static_slider_granular_v2 * data)8548d38f56SShyam Sundar S K static void amd_pmf_dump_sps_defaults_v2(struct amd_pmf_static_slider_granular_v2 *data)
8648d38f56SShyam Sundar S K {
8748d38f56SShyam Sundar S K 	unsigned int i, j;
8848d38f56SShyam Sundar S K 
8948d38f56SShyam Sundar S K 	pr_debug("Static Slider APTS state index data - BEGIN");
9048d38f56SShyam Sundar S K 	pr_debug("size: %u\n", data->size);
9148d38f56SShyam Sundar S K 
9248d38f56SShyam Sundar S K 	for (i = 0; i < POWER_SOURCE_MAX; i++)
9348d38f56SShyam Sundar S K 		for (j = 0; j < POWER_MODE_V2_MAX; j++)
9448d38f56SShyam Sundar S K 			pr_debug("%s %s: %u\n", amd_pmf_source_as_str(i), slider_v2_as_str(j),
9548d38f56SShyam Sundar S K 				 data->sps_idx.power_states[i][j]);
9648d38f56SShyam Sundar S K 
9748d38f56SShyam Sundar S K 	pr_debug("Static Slider APTS state index data - END\n");
9848d38f56SShyam Sundar S K }
993eecb434SShyam Sundar S K 
amd_pmf_dump_apts_sps_defaults(struct amd_pmf_apts_granular * info)1003eecb434SShyam Sundar S K static void amd_pmf_dump_apts_sps_defaults(struct amd_pmf_apts_granular *info)
1013eecb434SShyam Sundar S K {
1023eecb434SShyam Sundar S K 	int i;
1033eecb434SShyam Sundar S K 
1043eecb434SShyam Sundar S K 	pr_debug("Static Slider APTS index default values data - BEGIN");
1053eecb434SShyam Sundar S K 
1063eecb434SShyam Sundar S K 	for (i = 0; i < APTS_MAX_STATES; i++) {
1073eecb434SShyam Sundar S K 		pr_debug("Table Version[%d] = %u\n", i, info->val[i].table_version);
1083eecb434SShyam Sundar S K 		pr_debug("Fan Index[%d] = %u\n", i, info->val[i].fan_table_idx);
1093eecb434SShyam Sundar S K 		pr_debug("PPT[%d] = %u\n", i, info->val[i].pmf_ppt);
1103eecb434SShyam Sundar S K 		pr_debug("PPT APU[%d] = %u\n", i, info->val[i].ppt_pmf_apu_only);
1113eecb434SShyam Sundar S K 		pr_debug("STT Min[%d] = %u\n", i, info->val[i].stt_min_limit);
1123eecb434SShyam Sundar S K 		pr_debug("STT APU[%d] = %u\n", i, info->val[i].stt_skin_temp_limit_apu);
1133eecb434SShyam Sundar S K 		pr_debug("STT HS2[%d] = %u\n", i, info->val[i].stt_skin_temp_limit_hs2);
1143eecb434SShyam Sundar S K 	}
1153eecb434SShyam Sundar S K 
1163eecb434SShyam Sundar S K 	pr_debug("Static Slider APTS index default values data - END");
1173eecb434SShyam Sundar S K }
118a82ebb3dSShyam Sundar S K #else
amd_pmf_dump_sps_defaults(struct amd_pmf_static_slider_granular * data)119a82ebb3dSShyam Sundar S K static void amd_pmf_dump_sps_defaults(struct amd_pmf_static_slider_granular *data) {}
amd_pmf_dump_sps_defaults_v2(struct amd_pmf_static_slider_granular_v2 * data)12048d38f56SShyam Sundar S K static void amd_pmf_dump_sps_defaults_v2(struct amd_pmf_static_slider_granular_v2 *data) {}
amd_pmf_dump_apts_sps_defaults(struct amd_pmf_apts_granular * info)1213eecb434SShyam Sundar S K static void amd_pmf_dump_apts_sps_defaults(struct amd_pmf_apts_granular *info) {}
122a82ebb3dSShyam Sundar S K #endif
123a82ebb3dSShyam Sundar S K 
amd_pmf_load_apts_defaults_sps_v2(struct amd_pmf_dev * pdev)1243eecb434SShyam Sundar S K static void amd_pmf_load_apts_defaults_sps_v2(struct amd_pmf_dev *pdev)
1253eecb434SShyam Sundar S K {
1263eecb434SShyam Sundar S K 	struct amd_pmf_apts_granular_output output;
1273eecb434SShyam Sundar S K 	struct amd_pmf_apts_output *ps;
1283eecb434SShyam Sundar S K 	int i;
1293eecb434SShyam Sundar S K 
1303eecb434SShyam Sundar S K 	memset(&apts_config_store, 0, sizeof(apts_config_store));
1313eecb434SShyam Sundar S K 
1323eecb434SShyam Sundar S K 	ps = apts_config_store.val;
1333eecb434SShyam Sundar S K 
1343eecb434SShyam Sundar S K 	for (i = 0; i < APTS_MAX_STATES; i++) {
1353eecb434SShyam Sundar S K 		apts_get_static_slider_granular_v2(pdev, &output, i);
1363eecb434SShyam Sundar S K 		ps[i].table_version = output.val.table_version;
1373eecb434SShyam Sundar S K 		ps[i].fan_table_idx = output.val.fan_table_idx;
1383eecb434SShyam Sundar S K 		ps[i].pmf_ppt = output.val.pmf_ppt;
1393eecb434SShyam Sundar S K 		ps[i].ppt_pmf_apu_only = output.val.ppt_pmf_apu_only;
1403eecb434SShyam Sundar S K 		ps[i].stt_min_limit = output.val.stt_min_limit;
1413eecb434SShyam Sundar S K 		ps[i].stt_skin_temp_limit_apu = output.val.stt_skin_temp_limit_apu;
1423eecb434SShyam Sundar S K 		ps[i].stt_skin_temp_limit_hs2 = output.val.stt_skin_temp_limit_hs2;
1433eecb434SShyam Sundar S K 	}
1443eecb434SShyam Sundar S K 
1453eecb434SShyam Sundar S K 	amd_pmf_dump_apts_sps_defaults(&apts_config_store);
1463eecb434SShyam Sundar S K }
1473eecb434SShyam Sundar S K 
amd_pmf_load_defaults_sps_v2(struct amd_pmf_dev * dev)14848d38f56SShyam Sundar S K static void amd_pmf_load_defaults_sps_v2(struct amd_pmf_dev *dev)
14948d38f56SShyam Sundar S K {
15048d38f56SShyam Sundar S K 	struct apmf_static_slider_granular_output_v2 output;
15148d38f56SShyam Sundar S K 	unsigned int i, j;
15248d38f56SShyam Sundar S K 
15348d38f56SShyam Sundar S K 	memset(&config_store_v2, 0, sizeof(config_store_v2));
15448d38f56SShyam Sundar S K 	apmf_get_static_slider_granular_v2(dev, &output);
15548d38f56SShyam Sundar S K 
15648d38f56SShyam Sundar S K 	config_store_v2.size = output.size;
15748d38f56SShyam Sundar S K 
15848d38f56SShyam Sundar S K 	for (i = 0; i < POWER_SOURCE_MAX; i++)
15948d38f56SShyam Sundar S K 		for (j = 0; j < POWER_MODE_V2_MAX; j++)
16048d38f56SShyam Sundar S K 			config_store_v2.sps_idx.power_states[i][j] =
16148d38f56SShyam Sundar S K 							output.sps_idx.power_states[i][j];
16248d38f56SShyam Sundar S K 
16348d38f56SShyam Sundar S K 	amd_pmf_dump_sps_defaults_v2(&config_store_v2);
16448d38f56SShyam Sundar S K }
16548d38f56SShyam Sundar S K 
amd_pmf_load_defaults_sps(struct amd_pmf_dev * dev)1664c71ae41SShyam Sundar S K static void amd_pmf_load_defaults_sps(struct amd_pmf_dev *dev)
1674c71ae41SShyam Sundar S K {
1684c71ae41SShyam Sundar S K 	struct apmf_static_slider_granular_output output;
1694c71ae41SShyam Sundar S K 	int i, j, idx = 0;
1704c71ae41SShyam Sundar S K 
1714c71ae41SShyam Sundar S K 	memset(&config_store, 0, sizeof(config_store));
1724c71ae41SShyam Sundar S K 	apmf_get_static_slider_granular(dev, &output);
1734c71ae41SShyam Sundar S K 
1744c71ae41SShyam Sundar S K 	for (i = 0; i < POWER_SOURCE_MAX; i++) {
1754c71ae41SShyam Sundar S K 		for (j = 0; j < POWER_MODE_MAX; j++) {
1764c71ae41SShyam Sundar S K 			config_store.prop[i][j].spl = output.prop[idx].spl;
1774c71ae41SShyam Sundar S K 			config_store.prop[i][j].sppt = output.prop[idx].sppt;
1784c71ae41SShyam Sundar S K 			config_store.prop[i][j].sppt_apu_only =
1794c71ae41SShyam Sundar S K 						output.prop[idx].sppt_apu_only;
1804c71ae41SShyam Sundar S K 			config_store.prop[i][j].fppt = output.prop[idx].fppt;
1814c71ae41SShyam Sundar S K 			config_store.prop[i][j].stt_min = output.prop[idx].stt_min;
1824c71ae41SShyam Sundar S K 			config_store.prop[i][j].stt_skin_temp[STT_TEMP_APU] =
1834c71ae41SShyam Sundar S K 					output.prop[idx].stt_skin_temp[STT_TEMP_APU];
1844c71ae41SShyam Sundar S K 			config_store.prop[i][j].stt_skin_temp[STT_TEMP_HS2] =
1854c71ae41SShyam Sundar S K 					output.prop[idx].stt_skin_temp[STT_TEMP_HS2];
1864c71ae41SShyam Sundar S K 			config_store.prop[i][j].fan_id = output.prop[idx].fan_id;
1874c71ae41SShyam Sundar S K 			idx++;
1884c71ae41SShyam Sundar S K 		}
1894c71ae41SShyam Sundar S K 	}
190a82ebb3dSShyam Sundar S K 	amd_pmf_dump_sps_defaults(&config_store);
1914c71ae41SShyam Sundar S K }
1924c71ae41SShyam Sundar S K 
amd_pmf_update_slider_v2(struct amd_pmf_dev * dev,int idx)193*8362e862SShyam Sundar S K static void amd_pmf_update_slider_v2(struct amd_pmf_dev *dev, int idx)
194*8362e862SShyam Sundar S K {
195*8362e862SShyam Sundar S K 	amd_pmf_send_cmd(dev, SET_PMF_PPT, false, apts_config_store.val[idx].pmf_ppt, NULL);
196*8362e862SShyam Sundar S K 	amd_pmf_send_cmd(dev, SET_PMF_PPT_APU_ONLY, false,
197*8362e862SShyam Sundar S K 			 apts_config_store.val[idx].ppt_pmf_apu_only, NULL);
198*8362e862SShyam Sundar S K 	amd_pmf_send_cmd(dev, SET_STT_MIN_LIMIT, false,
199*8362e862SShyam Sundar S K 			 apts_config_store.val[idx].stt_min_limit, NULL);
200*8362e862SShyam Sundar S K 	amd_pmf_send_cmd(dev, SET_STT_LIMIT_APU, false,
201*8362e862SShyam Sundar S K 			 apts_config_store.val[idx].stt_skin_temp_limit_apu, NULL);
202*8362e862SShyam Sundar S K 	amd_pmf_send_cmd(dev, SET_STT_LIMIT_HS2, false,
203*8362e862SShyam Sundar S K 			 apts_config_store.val[idx].stt_skin_temp_limit_hs2, NULL);
204*8362e862SShyam Sundar S K }
205*8362e862SShyam Sundar S K 
amd_pmf_update_slider(struct amd_pmf_dev * dev,bool op,int idx,struct amd_pmf_static_slider_granular * table)2064c71ae41SShyam Sundar S K void amd_pmf_update_slider(struct amd_pmf_dev *dev, bool op, int idx,
2074c71ae41SShyam Sundar S K 			   struct amd_pmf_static_slider_granular *table)
2084c71ae41SShyam Sundar S K {
2094c71ae41SShyam Sundar S K 	int src = amd_pmf_get_power_source();
2104c71ae41SShyam Sundar S K 
2114c71ae41SShyam Sundar S K 	if (op == SLIDER_OP_SET) {
2124c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_SPL, false, config_store.prop[src][idx].spl, NULL);
2134c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_FPPT, false, config_store.prop[src][idx].fppt, NULL);
2144c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_SPPT, false, config_store.prop[src][idx].sppt, NULL);
2154c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_SPPT_APU_ONLY, false,
2164c71ae41SShyam Sundar S K 				 config_store.prop[src][idx].sppt_apu_only, NULL);
2174c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_STT_MIN_LIMIT, false,
2184c71ae41SShyam Sundar S K 				 config_store.prop[src][idx].stt_min, NULL);
2194c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_STT_LIMIT_APU, false,
2204c71ae41SShyam Sundar S K 				 config_store.prop[src][idx].stt_skin_temp[STT_TEMP_APU], NULL);
2214c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_STT_LIMIT_HS2, false,
2224c71ae41SShyam Sundar S K 				 config_store.prop[src][idx].stt_skin_temp[STT_TEMP_HS2], NULL);
2234c71ae41SShyam Sundar S K 	} else if (op == SLIDER_OP_GET) {
2244c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_SPL, true, ARG_NONE, &table->prop[src][idx].spl);
2254c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_FPPT, true, ARG_NONE, &table->prop[src][idx].fppt);
2264c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_SPPT, true, ARG_NONE, &table->prop[src][idx].sppt);
2274c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_SPPT_APU_ONLY, true, ARG_NONE,
2284c71ae41SShyam Sundar S K 				 &table->prop[src][idx].sppt_apu_only);
2294c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_STT_MIN_LIMIT, true, ARG_NONE,
2304c71ae41SShyam Sundar S K 				 &table->prop[src][idx].stt_min);
2314c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_STT_LIMIT_APU, true, ARG_NONE,
2324c71ae41SShyam Sundar S K 				 (u32 *)&table->prop[src][idx].stt_skin_temp[STT_TEMP_APU]);
2334c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_STT_LIMIT_HS2, true, ARG_NONE,
2344c71ae41SShyam Sundar S K 				 (u32 *)&table->prop[src][idx].stt_skin_temp[STT_TEMP_HS2]);
2354c71ae41SShyam Sundar S K 	}
2364c71ae41SShyam Sundar S K }
2374c71ae41SShyam Sundar S K 
amd_pmf_update_sps_power_limits_v2(struct amd_pmf_dev * pdev,int pwr_mode)238*8362e862SShyam Sundar S K static int amd_pmf_update_sps_power_limits_v2(struct amd_pmf_dev *pdev, int pwr_mode)
239*8362e862SShyam Sundar S K {
240*8362e862SShyam Sundar S K 	int src, index;
241*8362e862SShyam Sundar S K 
242*8362e862SShyam Sundar S K 	src = amd_pmf_get_power_source();
243*8362e862SShyam Sundar S K 
244*8362e862SShyam Sundar S K 	switch (pwr_mode) {
245*8362e862SShyam Sundar S K 	case POWER_MODE_PERFORMANCE:
246*8362e862SShyam Sundar S K 		index = config_store_v2.sps_idx.power_states[src][POWER_MODE_BEST_PERFORMANCE];
247*8362e862SShyam Sundar S K 		amd_pmf_update_slider_v2(pdev, index);
248*8362e862SShyam Sundar S K 		break;
249*8362e862SShyam Sundar S K 	case POWER_MODE_BALANCED_POWER:
250*8362e862SShyam Sundar S K 		index = config_store_v2.sps_idx.power_states[src][POWER_MODE_BALANCED];
251*8362e862SShyam Sundar S K 		amd_pmf_update_slider_v2(pdev, index);
252*8362e862SShyam Sundar S K 		break;
253*8362e862SShyam Sundar S K 	case POWER_MODE_POWER_SAVER:
254*8362e862SShyam Sundar S K 		index = config_store_v2.sps_idx.power_states[src][POWER_MODE_BEST_POWER_EFFICIENCY];
255*8362e862SShyam Sundar S K 		amd_pmf_update_slider_v2(pdev, index);
256*8362e862SShyam Sundar S K 		break;
257*8362e862SShyam Sundar S K 	default:
258*8362e862SShyam Sundar S K 		return -EINVAL;
259*8362e862SShyam Sundar S K 	}
260*8362e862SShyam Sundar S K 
261*8362e862SShyam Sundar S K 	return 0;
262*8362e862SShyam Sundar S K }
263*8362e862SShyam Sundar S K 
amd_pmf_set_sps_power_limits(struct amd_pmf_dev * pmf)264c5258d39SShyam Sundar S K int amd_pmf_set_sps_power_limits(struct amd_pmf_dev *pmf)
265c5258d39SShyam Sundar S K {
266c5258d39SShyam Sundar S K 	int mode;
267c5258d39SShyam Sundar S K 
268c5258d39SShyam Sundar S K 	mode = amd_pmf_get_pprof_modes(pmf);
269c5258d39SShyam Sundar S K 	if (mode < 0)
270c5258d39SShyam Sundar S K 		return mode;
271c5258d39SShyam Sundar S K 
272*8362e862SShyam Sundar S K 	if (pmf->pmf_if_version == PMF_IF_V2)
273*8362e862SShyam Sundar S K 		return amd_pmf_update_sps_power_limits_v2(pmf, mode);
274*8362e862SShyam Sundar S K 
275c5258d39SShyam Sundar S K 	amd_pmf_update_slider(pmf, SLIDER_OP_SET, mode, NULL);
276c5258d39SShyam Sundar S K 
277c5258d39SShyam Sundar S K 	return 0;
278c5258d39SShyam Sundar S K }
279c5258d39SShyam Sundar S K 
is_pprof_balanced(struct amd_pmf_dev * pmf)28016909aa8SShyam Sundar S K bool is_pprof_balanced(struct amd_pmf_dev *pmf)
28116909aa8SShyam Sundar S K {
28216909aa8SShyam Sundar S K 	return (pmf->current_profile == PLATFORM_PROFILE_BALANCED) ? true : false;
28316909aa8SShyam Sundar S K }
28416909aa8SShyam Sundar S K 
amd_pmf_profile_get(struct platform_profile_handler * pprof,enum platform_profile_option * profile)2854c71ae41SShyam Sundar S K static int amd_pmf_profile_get(struct platform_profile_handler *pprof,
2864c71ae41SShyam Sundar S K 			       enum platform_profile_option *profile)
2874c71ae41SShyam Sundar S K {
2884c71ae41SShyam Sundar S K 	struct amd_pmf_dev *pmf = container_of(pprof, struct amd_pmf_dev, pprof);
2894c71ae41SShyam Sundar S K 
2904c71ae41SShyam Sundar S K 	*profile = pmf->current_profile;
2914c71ae41SShyam Sundar S K 	return 0;
2924c71ae41SShyam Sundar S K }
2934c71ae41SShyam Sundar S K 
amd_pmf_get_pprof_modes(struct amd_pmf_dev * pmf)294ea522b80SShyam Sundar S K int amd_pmf_get_pprof_modes(struct amd_pmf_dev *pmf)
2954c71ae41SShyam Sundar S K {
296ea522b80SShyam Sundar S K 	int mode;
2974c71ae41SShyam Sundar S K 
2984c71ae41SShyam Sundar S K 	switch (pmf->current_profile) {
2994c71ae41SShyam Sundar S K 	case PLATFORM_PROFILE_PERFORMANCE:
3004c71ae41SShyam Sundar S K 		mode = POWER_MODE_PERFORMANCE;
3014c71ae41SShyam Sundar S K 		break;
3024c71ae41SShyam Sundar S K 	case PLATFORM_PROFILE_BALANCED:
3034c71ae41SShyam Sundar S K 		mode = POWER_MODE_BALANCED_POWER;
3044c71ae41SShyam Sundar S K 		break;
3054c71ae41SShyam Sundar S K 	case PLATFORM_PROFILE_LOW_POWER:
3064c71ae41SShyam Sundar S K 		mode = POWER_MODE_POWER_SAVER;
3074c71ae41SShyam Sundar S K 		break;
3084c71ae41SShyam Sundar S K 	default:
3094c71ae41SShyam Sundar S K 		dev_err(pmf->dev, "Unknown Platform Profile.\n");
310ea522b80SShyam Sundar S K 		return -EOPNOTSUPP;
3114c71ae41SShyam Sundar S K 	}
3124c71ae41SShyam Sundar S K 
3134c71ae41SShyam Sundar S K 	return mode;
3144c71ae41SShyam Sundar S K }
3154c71ae41SShyam Sundar S K 
amd_pmf_power_slider_update_event(struct amd_pmf_dev * dev)31633c9ab5bSShyam Sundar S K int amd_pmf_power_slider_update_event(struct amd_pmf_dev *dev)
31733c9ab5bSShyam Sundar S K {
318785c0099SYang Li 	u8 flag = 0;
319785c0099SYang Li 	int mode;
32033c9ab5bSShyam Sundar S K 	int src;
32133c9ab5bSShyam Sundar S K 
32233c9ab5bSShyam Sundar S K 	mode = amd_pmf_get_pprof_modes(dev);
32333c9ab5bSShyam Sundar S K 	if (mode < 0)
32433c9ab5bSShyam Sundar S K 		return mode;
32533c9ab5bSShyam Sundar S K 
32633c9ab5bSShyam Sundar S K 	src = amd_pmf_get_power_source();
32733c9ab5bSShyam Sundar S K 
32833c9ab5bSShyam Sundar S K 	if (src == POWER_SOURCE_AC) {
32933c9ab5bSShyam Sundar S K 		switch (mode) {
33033c9ab5bSShyam Sundar S K 		case POWER_MODE_PERFORMANCE:
33133c9ab5bSShyam Sundar S K 			flag |= BIT(AC_BEST_PERF);
33233c9ab5bSShyam Sundar S K 			break;
33333c9ab5bSShyam Sundar S K 		case POWER_MODE_BALANCED_POWER:
33433c9ab5bSShyam Sundar S K 			flag |= BIT(AC_BETTER_PERF);
33533c9ab5bSShyam Sundar S K 			break;
33633c9ab5bSShyam Sundar S K 		case POWER_MODE_POWER_SAVER:
33733c9ab5bSShyam Sundar S K 			flag |= BIT(AC_BETTER_BATTERY);
33833c9ab5bSShyam Sundar S K 			break;
33933c9ab5bSShyam Sundar S K 		default:
34033c9ab5bSShyam Sundar S K 			dev_err(dev->dev, "unsupported platform profile\n");
34133c9ab5bSShyam Sundar S K 			return -EOPNOTSUPP;
34233c9ab5bSShyam Sundar S K 		}
34333c9ab5bSShyam Sundar S K 
34433c9ab5bSShyam Sundar S K 	} else if (src == POWER_SOURCE_DC) {
34533c9ab5bSShyam Sundar S K 		switch (mode) {
34633c9ab5bSShyam Sundar S K 		case POWER_MODE_PERFORMANCE:
34733c9ab5bSShyam Sundar S K 			flag |= BIT(DC_BEST_PERF);
34833c9ab5bSShyam Sundar S K 			break;
34933c9ab5bSShyam Sundar S K 		case POWER_MODE_BALANCED_POWER:
35033c9ab5bSShyam Sundar S K 			flag |= BIT(DC_BETTER_PERF);
35133c9ab5bSShyam Sundar S K 			break;
35233c9ab5bSShyam Sundar S K 		case POWER_MODE_POWER_SAVER:
35333c9ab5bSShyam Sundar S K 			flag |= BIT(DC_BATTERY_SAVER);
35433c9ab5bSShyam Sundar S K 			break;
35533c9ab5bSShyam Sundar S K 		default:
35633c9ab5bSShyam Sundar S K 			dev_err(dev->dev, "unsupported platform profile\n");
35733c9ab5bSShyam Sundar S K 			return -EOPNOTSUPP;
35833c9ab5bSShyam Sundar S K 		}
35933c9ab5bSShyam Sundar S K 	}
36033c9ab5bSShyam Sundar S K 
36133c9ab5bSShyam Sundar S K 	apmf_os_power_slider_update(dev, flag);
36233c9ab5bSShyam Sundar S K 
36333c9ab5bSShyam Sundar S K 	return 0;
36433c9ab5bSShyam Sundar S K }
36533c9ab5bSShyam Sundar S K 
amd_pmf_profile_set(struct platform_profile_handler * pprof,enum platform_profile_option profile)3664c71ae41SShyam Sundar S K static int amd_pmf_profile_set(struct platform_profile_handler *pprof,
3674c71ae41SShyam Sundar S K 			       enum platform_profile_option profile)
3684c71ae41SShyam Sundar S K {
3694c71ae41SShyam Sundar S K 	struct amd_pmf_dev *pmf = container_of(pprof, struct amd_pmf_dev, pprof);
37033c9ab5bSShyam Sundar S K 	int ret = 0;
3714c71ae41SShyam Sundar S K 
3724c71ae41SShyam Sundar S K 	pmf->current_profile = profile;
373ea522b80SShyam Sundar S K 
37433c9ab5bSShyam Sundar S K 	/* Notify EC about the slider position change */
37533c9ab5bSShyam Sundar S K 	if (is_apmf_func_supported(pmf, APMF_FUNC_OS_POWER_SLIDER_UPDATE)) {
37633c9ab5bSShyam Sundar S K 		ret = amd_pmf_power_slider_update_event(pmf);
37733c9ab5bSShyam Sundar S K 		if (ret)
37833c9ab5bSShyam Sundar S K 			return ret;
37933c9ab5bSShyam Sundar S K 	}
38033c9ab5bSShyam Sundar S K 
38133c9ab5bSShyam Sundar S K 	if (is_apmf_func_supported(pmf, APMF_FUNC_STATIC_SLIDER_GRANULAR)) {
38233c9ab5bSShyam Sundar S K 		ret = amd_pmf_set_sps_power_limits(pmf);
38333c9ab5bSShyam Sundar S K 		if (ret)
38433c9ab5bSShyam Sundar S K 			return ret;
38533c9ab5bSShyam Sundar S K 	}
38633c9ab5bSShyam Sundar S K 
38733c9ab5bSShyam Sundar S K 	return 0;
3884c71ae41SShyam Sundar S K }
3894c71ae41SShyam Sundar S K 
amd_pmf_init_sps(struct amd_pmf_dev * dev)3904c71ae41SShyam Sundar S K int amd_pmf_init_sps(struct amd_pmf_dev *dev)
3914c71ae41SShyam Sundar S K {
3924c71ae41SShyam Sundar S K 	int err;
3934c71ae41SShyam Sundar S K 
3944c71ae41SShyam Sundar S K 	dev->current_profile = PLATFORM_PROFILE_BALANCED;
39533c9ab5bSShyam Sundar S K 
39633c9ab5bSShyam Sundar S K 	if (is_apmf_func_supported(dev, APMF_FUNC_STATIC_SLIDER_GRANULAR)) {
3973eecb434SShyam Sundar S K 		if (dev->pmf_if_version == PMF_IF_V2) {
39848d38f56SShyam Sundar S K 			amd_pmf_load_defaults_sps_v2(dev);
3993eecb434SShyam Sundar S K 			amd_pmf_load_apts_defaults_sps_v2(dev);
4003eecb434SShyam Sundar S K 		} else {
4014c71ae41SShyam Sundar S K 			amd_pmf_load_defaults_sps(dev);
4023eecb434SShyam Sundar S K 		}
4034c71ae41SShyam Sundar S K 
404635f79bcSShyam Sundar S K 		/* update SPS balanced power mode thermals */
405635f79bcSShyam Sundar S K 		amd_pmf_set_sps_power_limits(dev);
40633c9ab5bSShyam Sundar S K 	}
407635f79bcSShyam Sundar S K 
4084c71ae41SShyam Sundar S K 	dev->pprof.profile_get = amd_pmf_profile_get;
4094c71ae41SShyam Sundar S K 	dev->pprof.profile_set = amd_pmf_profile_set;
4104c71ae41SShyam Sundar S K 
4114c71ae41SShyam Sundar S K 	/* Setup supported modes */
4124c71ae41SShyam Sundar S K 	set_bit(PLATFORM_PROFILE_LOW_POWER, dev->pprof.choices);
4134c71ae41SShyam Sundar S K 	set_bit(PLATFORM_PROFILE_BALANCED, dev->pprof.choices);
4144c71ae41SShyam Sundar S K 	set_bit(PLATFORM_PROFILE_PERFORMANCE, dev->pprof.choices);
4154c71ae41SShyam Sundar S K 
4164c71ae41SShyam Sundar S K 	/* Create platform_profile structure and register */
4174c71ae41SShyam Sundar S K 	err = platform_profile_register(&dev->pprof);
4184c71ae41SShyam Sundar S K 	if (err)
4194c71ae41SShyam Sundar S K 		dev_err(dev->dev, "Failed to register SPS support, this is most likely an SBIOS bug: %d\n",
4204c71ae41SShyam Sundar S K 			err);
4214c71ae41SShyam Sundar S K 
4224c71ae41SShyam Sundar S K 	return err;
4234c71ae41SShyam Sundar S K }
4244c71ae41SShyam Sundar S K 
amd_pmf_deinit_sps(struct amd_pmf_dev * dev)4254c71ae41SShyam Sundar S K void amd_pmf_deinit_sps(struct amd_pmf_dev *dev)
4264c71ae41SShyam Sundar S K {
4274c71ae41SShyam Sundar S K 	platform_profile_remove();
4284c71ae41SShyam Sundar S K }
429