xref: /linux/drivers/platform/x86/amd/pmf/sps.c (revision ea522b806162ca947427f8305310d3c8a3d42d7a)
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 
134c71ae41SShyam Sundar S K static struct amd_pmf_static_slider_granular config_store;
144c71ae41SShyam Sundar S K 
154c71ae41SShyam Sundar S K static void amd_pmf_load_defaults_sps(struct amd_pmf_dev *dev)
164c71ae41SShyam Sundar S K {
174c71ae41SShyam Sundar S K 	struct apmf_static_slider_granular_output output;
184c71ae41SShyam Sundar S K 	int i, j, idx = 0;
194c71ae41SShyam Sundar S K 
204c71ae41SShyam Sundar S K 	memset(&config_store, 0, sizeof(config_store));
214c71ae41SShyam Sundar S K 	apmf_get_static_slider_granular(dev, &output);
224c71ae41SShyam Sundar S K 
234c71ae41SShyam Sundar S K 	for (i = 0; i < POWER_SOURCE_MAX; i++) {
244c71ae41SShyam Sundar S K 		for (j = 0; j < POWER_MODE_MAX; j++) {
254c71ae41SShyam Sundar S K 			config_store.prop[i][j].spl = output.prop[idx].spl;
264c71ae41SShyam Sundar S K 			config_store.prop[i][j].sppt = output.prop[idx].sppt;
274c71ae41SShyam Sundar S K 			config_store.prop[i][j].sppt_apu_only =
284c71ae41SShyam Sundar S K 						output.prop[idx].sppt_apu_only;
294c71ae41SShyam Sundar S K 			config_store.prop[i][j].fppt = output.prop[idx].fppt;
304c71ae41SShyam Sundar S K 			config_store.prop[i][j].stt_min = output.prop[idx].stt_min;
314c71ae41SShyam Sundar S K 			config_store.prop[i][j].stt_skin_temp[STT_TEMP_APU] =
324c71ae41SShyam Sundar S K 					output.prop[idx].stt_skin_temp[STT_TEMP_APU];
334c71ae41SShyam Sundar S K 			config_store.prop[i][j].stt_skin_temp[STT_TEMP_HS2] =
344c71ae41SShyam Sundar S K 					output.prop[idx].stt_skin_temp[STT_TEMP_HS2];
354c71ae41SShyam Sundar S K 			config_store.prop[i][j].fan_id = output.prop[idx].fan_id;
364c71ae41SShyam Sundar S K 			idx++;
374c71ae41SShyam Sundar S K 		}
384c71ae41SShyam Sundar S K 	}
394c71ae41SShyam Sundar S K }
404c71ae41SShyam Sundar S K 
414c71ae41SShyam Sundar S K void amd_pmf_update_slider(struct amd_pmf_dev *dev, bool op, int idx,
424c71ae41SShyam Sundar S K 			   struct amd_pmf_static_slider_granular *table)
434c71ae41SShyam Sundar S K {
444c71ae41SShyam Sundar S K 	int src = amd_pmf_get_power_source();
454c71ae41SShyam Sundar S K 
464c71ae41SShyam Sundar S K 	if (op == SLIDER_OP_SET) {
474c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_SPL, false, config_store.prop[src][idx].spl, NULL);
484c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_FPPT, false, config_store.prop[src][idx].fppt, NULL);
494c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_SPPT, false, config_store.prop[src][idx].sppt, NULL);
504c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_SPPT_APU_ONLY, false,
514c71ae41SShyam Sundar S K 				 config_store.prop[src][idx].sppt_apu_only, NULL);
524c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_STT_MIN_LIMIT, false,
534c71ae41SShyam Sundar S K 				 config_store.prop[src][idx].stt_min, NULL);
544c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_STT_LIMIT_APU, false,
554c71ae41SShyam Sundar S K 				 config_store.prop[src][idx].stt_skin_temp[STT_TEMP_APU], NULL);
564c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, SET_STT_LIMIT_HS2, false,
574c71ae41SShyam Sundar S K 				 config_store.prop[src][idx].stt_skin_temp[STT_TEMP_HS2], NULL);
584c71ae41SShyam Sundar S K 	} else if (op == SLIDER_OP_GET) {
594c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_SPL, true, ARG_NONE, &table->prop[src][idx].spl);
604c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_FPPT, true, ARG_NONE, &table->prop[src][idx].fppt);
614c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_SPPT, true, ARG_NONE, &table->prop[src][idx].sppt);
624c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_SPPT_APU_ONLY, true, ARG_NONE,
634c71ae41SShyam Sundar S K 				 &table->prop[src][idx].sppt_apu_only);
644c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_STT_MIN_LIMIT, true, ARG_NONE,
654c71ae41SShyam Sundar S K 				 &table->prop[src][idx].stt_min);
664c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_STT_LIMIT_APU, true, ARG_NONE,
674c71ae41SShyam Sundar S K 				 (u32 *)&table->prop[src][idx].stt_skin_temp[STT_TEMP_APU]);
684c71ae41SShyam Sundar S K 		amd_pmf_send_cmd(dev, GET_STT_LIMIT_HS2, true, ARG_NONE,
694c71ae41SShyam Sundar S K 				 (u32 *)&table->prop[src][idx].stt_skin_temp[STT_TEMP_HS2]);
704c71ae41SShyam Sundar S K 	}
714c71ae41SShyam Sundar S K }
724c71ae41SShyam Sundar S K 
734c71ae41SShyam Sundar S K static int amd_pmf_profile_get(struct platform_profile_handler *pprof,
744c71ae41SShyam Sundar S K 			       enum platform_profile_option *profile)
754c71ae41SShyam Sundar S K {
764c71ae41SShyam Sundar S K 	struct amd_pmf_dev *pmf = container_of(pprof, struct amd_pmf_dev, pprof);
774c71ae41SShyam Sundar S K 
784c71ae41SShyam Sundar S K 	*profile = pmf->current_profile;
794c71ae41SShyam Sundar S K 	return 0;
804c71ae41SShyam Sundar S K }
814c71ae41SShyam Sundar S K 
82*ea522b80SShyam Sundar S K int amd_pmf_get_pprof_modes(struct amd_pmf_dev *pmf)
834c71ae41SShyam Sundar S K {
84*ea522b80SShyam Sundar S K 	int mode;
854c71ae41SShyam Sundar S K 
864c71ae41SShyam Sundar S K 	switch (pmf->current_profile) {
874c71ae41SShyam Sundar S K 	case PLATFORM_PROFILE_PERFORMANCE:
884c71ae41SShyam Sundar S K 		mode = POWER_MODE_PERFORMANCE;
894c71ae41SShyam Sundar S K 		break;
904c71ae41SShyam Sundar S K 	case PLATFORM_PROFILE_BALANCED:
914c71ae41SShyam Sundar S K 		mode = POWER_MODE_BALANCED_POWER;
924c71ae41SShyam Sundar S K 		break;
934c71ae41SShyam Sundar S K 	case PLATFORM_PROFILE_LOW_POWER:
944c71ae41SShyam Sundar S K 		mode = POWER_MODE_POWER_SAVER;
954c71ae41SShyam Sundar S K 		break;
964c71ae41SShyam Sundar S K 	default:
974c71ae41SShyam Sundar S K 		dev_err(pmf->dev, "Unknown Platform Profile.\n");
98*ea522b80SShyam Sundar S K 		return -EOPNOTSUPP;
994c71ae41SShyam Sundar S K 	}
1004c71ae41SShyam Sundar S K 
1014c71ae41SShyam Sundar S K 	return mode;
1024c71ae41SShyam Sundar S K }
1034c71ae41SShyam Sundar S K 
1044c71ae41SShyam Sundar S K static int amd_pmf_profile_set(struct platform_profile_handler *pprof,
1054c71ae41SShyam Sundar S K 			       enum platform_profile_option profile)
1064c71ae41SShyam Sundar S K {
1074c71ae41SShyam Sundar S K 	struct amd_pmf_dev *pmf = container_of(pprof, struct amd_pmf_dev, pprof);
108*ea522b80SShyam Sundar S K 	int mode;
1094c71ae41SShyam Sundar S K 
1104c71ae41SShyam Sundar S K 	pmf->current_profile = profile;
1114c71ae41SShyam Sundar S K 	mode = amd_pmf_get_pprof_modes(pmf);
112*ea522b80SShyam Sundar S K 	if (mode < 0)
113*ea522b80SShyam Sundar S K 		return mode;
114*ea522b80SShyam Sundar S K 
1154c71ae41SShyam Sundar S K 	amd_pmf_update_slider(pmf, SLIDER_OP_SET, mode, NULL);
1164c71ae41SShyam Sundar S K 	return 0;
1174c71ae41SShyam Sundar S K }
1184c71ae41SShyam Sundar S K 
1194c71ae41SShyam Sundar S K int amd_pmf_init_sps(struct amd_pmf_dev *dev)
1204c71ae41SShyam Sundar S K {
1214c71ae41SShyam Sundar S K 	int err;
1224c71ae41SShyam Sundar S K 
1234c71ae41SShyam Sundar S K 	dev->current_profile = PLATFORM_PROFILE_BALANCED;
1244c71ae41SShyam Sundar S K 	amd_pmf_load_defaults_sps(dev);
1254c71ae41SShyam Sundar S K 
1264c71ae41SShyam Sundar S K 	dev->pprof.profile_get = amd_pmf_profile_get;
1274c71ae41SShyam Sundar S K 	dev->pprof.profile_set = amd_pmf_profile_set;
1284c71ae41SShyam Sundar S K 
1294c71ae41SShyam Sundar S K 	/* Setup supported modes */
1304c71ae41SShyam Sundar S K 	set_bit(PLATFORM_PROFILE_LOW_POWER, dev->pprof.choices);
1314c71ae41SShyam Sundar S K 	set_bit(PLATFORM_PROFILE_BALANCED, dev->pprof.choices);
1324c71ae41SShyam Sundar S K 	set_bit(PLATFORM_PROFILE_PERFORMANCE, dev->pprof.choices);
1334c71ae41SShyam Sundar S K 
1344c71ae41SShyam Sundar S K 	/* Create platform_profile structure and register */
1354c71ae41SShyam Sundar S K 	err = platform_profile_register(&dev->pprof);
1364c71ae41SShyam Sundar S K 	if (err)
1374c71ae41SShyam Sundar S K 		dev_err(dev->dev, "Failed to register SPS support, this is most likely an SBIOS bug: %d\n",
1384c71ae41SShyam Sundar S K 			err);
1394c71ae41SShyam Sundar S K 
1404c71ae41SShyam Sundar S K 	return err;
1414c71ae41SShyam Sundar S K }
1424c71ae41SShyam Sundar S K 
1434c71ae41SShyam Sundar S K void amd_pmf_deinit_sps(struct amd_pmf_dev *dev)
1444c71ae41SShyam Sundar S K {
1454c71ae41SShyam Sundar S K 	platform_profile_remove();
1464c71ae41SShyam Sundar S K }
147