1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * AMD Platform Management Framework Driver - Smart PC Capabilities
4 *
5 * Copyright (c) 2023, Advanced Micro Devices, Inc.
6 * All Rights Reserved.
7 *
8 * Authors: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
9 * Patil Rajesh Reddy <Patil.Reddy@amd.com>
10 */
11
12 #include <acpi/button.h>
13 #include <linux/amd-pmf-io.h>
14 #include <linux/power_supply.h>
15 #include <linux/units.h>
16 #include "pmf.h"
17
18 #ifdef CONFIG_AMD_PMF_DEBUG
ta_slider_as_str(unsigned int state)19 static const char *ta_slider_as_str(unsigned int state)
20 {
21 switch (state) {
22 case TA_BEST_PERFORMANCE:
23 return "PERFORMANCE";
24 case TA_BETTER_PERFORMANCE:
25 return "BALANCED";
26 case TA_BEST_BATTERY:
27 return "POWER_SAVER";
28 default:
29 return "Unknown TA Slider State";
30 }
31 }
32
amd_pmf_dump_ta_inputs(struct amd_pmf_dev * dev,struct ta_pmf_enact_table * in)33 void amd_pmf_dump_ta_inputs(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
34 {
35 dev_dbg(dev->dev, "==== TA inputs START ====\n");
36 dev_dbg(dev->dev, "Slider State: %s\n", ta_slider_as_str(in->ev_info.power_slider));
37 dev_dbg(dev->dev, "Power Source: %s\n", amd_pmf_source_as_str(in->ev_info.power_source));
38 dev_dbg(dev->dev, "Battery Percentage: %u\n", in->ev_info.bat_percentage);
39 dev_dbg(dev->dev, "Designed Battery Capacity: %u\n", in->ev_info.bat_design);
40 dev_dbg(dev->dev, "Fully Charged Capacity: %u\n", in->ev_info.full_charge_capacity);
41 dev_dbg(dev->dev, "Drain Rate: %d\n", in->ev_info.drain_rate);
42 dev_dbg(dev->dev, "Socket Power: %u\n", in->ev_info.socket_power);
43 dev_dbg(dev->dev, "Skin Temperature: %u\n", in->ev_info.skin_temperature);
44 dev_dbg(dev->dev, "Avg C0 Residency: %u\n", in->ev_info.avg_c0residency);
45 dev_dbg(dev->dev, "Max C0 Residency: %u\n", in->ev_info.max_c0residency);
46 dev_dbg(dev->dev, "GFX Busy: %u\n", in->ev_info.gfx_busy);
47 dev_dbg(dev->dev, "LID State: %s\n", in->ev_info.lid_state ? "close" : "open");
48 dev_dbg(dev->dev, "User Presence: %s\n", in->ev_info.user_present ? "Present" : "Away");
49 dev_dbg(dev->dev, "Ambient Light: %d\n", in->ev_info.ambient_light);
50 dev_dbg(dev->dev, "==== TA inputs END ====\n");
51 }
52 #else
amd_pmf_dump_ta_inputs(struct amd_pmf_dev * dev,struct ta_pmf_enact_table * in)53 void amd_pmf_dump_ta_inputs(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in) {}
54 #endif
55
amd_pmf_get_c0_residency(u16 * core_res,size_t size,struct ta_pmf_enact_table * in)56 static void amd_pmf_get_c0_residency(u16 *core_res, size_t size, struct ta_pmf_enact_table *in)
57 {
58 u16 max, avg = 0;
59 int i;
60
61 /* Get the avg and max C0 residency of all the cores */
62 max = *core_res;
63 for (i = 0; i < size; i++) {
64 avg += core_res[i];
65 if (core_res[i] > max)
66 max = core_res[i];
67 }
68 avg = DIV_ROUND_CLOSEST(avg, size);
69 in->ev_info.avg_c0residency = avg;
70 in->ev_info.max_c0residency = max;
71 }
72
amd_pmf_get_smu_info(struct amd_pmf_dev * dev,struct ta_pmf_enact_table * in)73 static void amd_pmf_get_smu_info(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
74 {
75 /* Get the updated metrics table data */
76 memset(dev->buf, 0, dev->mtable_size);
77 amd_pmf_send_cmd(dev, SET_TRANSFER_TABLE, 0, 7, NULL);
78
79 switch (dev->cpu_id) {
80 case AMD_CPU_ID_PS:
81 memcpy(&dev->m_table, dev->buf, dev->mtable_size);
82 in->ev_info.socket_power = dev->m_table.apu_power + dev->m_table.dgpu_power;
83 in->ev_info.skin_temperature = dev->m_table.skin_temp;
84 in->ev_info.gfx_busy = dev->m_table.avg_gfx_activity;
85 amd_pmf_get_c0_residency(dev->m_table.avg_core_c0residency,
86 ARRAY_SIZE(dev->m_table.avg_core_c0residency), in);
87 break;
88 case PCI_DEVICE_ID_AMD_1AH_M20H_ROOT:
89 memcpy(&dev->m_table_v2, dev->buf, dev->mtable_size);
90 in->ev_info.socket_power = dev->m_table_v2.apu_power + dev->m_table_v2.dgpu_power;
91 in->ev_info.skin_temperature = dev->m_table_v2.skin_temp;
92 in->ev_info.gfx_busy = dev->m_table_v2.gfx_activity;
93 amd_pmf_get_c0_residency(dev->m_table_v2.core_c0residency,
94 ARRAY_SIZE(dev->m_table_v2.core_c0residency), in);
95 break;
96 default:
97 dev_err(dev->dev, "Unsupported CPU id: 0x%x", dev->cpu_id);
98 }
99 }
100
101 static const char * const pmf_battery_supply_name[] = {
102 "BATT",
103 "BAT0",
104 };
105
amd_pmf_get_battery_prop(enum power_supply_property prop)106 static int amd_pmf_get_battery_prop(enum power_supply_property prop)
107 {
108 union power_supply_propval value;
109 struct power_supply *psy;
110 int i, ret;
111
112 for (i = 0; i < ARRAY_SIZE(pmf_battery_supply_name); i++) {
113 psy = power_supply_get_by_name(pmf_battery_supply_name[i]);
114 if (!psy)
115 continue;
116
117 ret = power_supply_get_property(psy, prop, &value);
118 if (ret) {
119 power_supply_put(psy);
120 return ret;
121 }
122 }
123
124 return value.intval;
125 }
126
amd_pmf_get_battery_info(struct amd_pmf_dev * dev,struct ta_pmf_enact_table * in)127 static int amd_pmf_get_battery_info(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
128 {
129 int val;
130
131 val = amd_pmf_get_battery_prop(POWER_SUPPLY_PROP_PRESENT);
132 if (val < 0)
133 return val;
134 if (val != 1)
135 return -ENODEV;
136
137 in->ev_info.bat_percentage = amd_pmf_get_battery_prop(POWER_SUPPLY_PROP_CAPACITY);
138 /* all values in mWh metrics */
139 in->ev_info.bat_design = amd_pmf_get_battery_prop(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN) /
140 MILLIWATT_PER_WATT;
141 in->ev_info.full_charge_capacity = amd_pmf_get_battery_prop(POWER_SUPPLY_PROP_ENERGY_FULL) /
142 MILLIWATT_PER_WATT;
143 in->ev_info.drain_rate = amd_pmf_get_battery_prop(POWER_SUPPLY_PROP_POWER_NOW) /
144 MILLIWATT_PER_WATT;
145
146 return 0;
147 }
148
amd_pmf_get_slider_info(struct amd_pmf_dev * dev,struct ta_pmf_enact_table * in)149 static int amd_pmf_get_slider_info(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
150 {
151 int val;
152
153 switch (dev->current_profile) {
154 case PLATFORM_PROFILE_PERFORMANCE:
155 val = TA_BEST_PERFORMANCE;
156 break;
157 case PLATFORM_PROFILE_BALANCED:
158 val = TA_BETTER_PERFORMANCE;
159 break;
160 case PLATFORM_PROFILE_LOW_POWER:
161 val = TA_BEST_BATTERY;
162 break;
163 default:
164 dev_err(dev->dev, "Unknown Platform Profile.\n");
165 return -EOPNOTSUPP;
166 }
167 in->ev_info.power_slider = val;
168
169 return 0;
170 }
171
amd_pmf_get_sensor_info(struct amd_pmf_dev * dev,struct ta_pmf_enact_table * in)172 static void amd_pmf_get_sensor_info(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
173 {
174 struct amd_sfh_info sfh_info;
175
176 /* Get the latest information from SFH */
177 in->ev_info.user_present = false;
178
179 /* Get ALS data */
180 if (!amd_get_sfh_info(&sfh_info, MT_ALS))
181 in->ev_info.ambient_light = sfh_info.ambient_light;
182 else
183 dev_dbg(dev->dev, "ALS is not enabled/detected\n");
184
185 /* get HPD data */
186 if (!amd_get_sfh_info(&sfh_info, MT_HPD)) {
187 if (sfh_info.user_present == SFH_USER_PRESENT)
188 in->ev_info.user_present = true;
189 } else {
190 dev_dbg(dev->dev, "HPD is not enabled/detected\n");
191 }
192 }
193
amd_pmf_populate_ta_inputs(struct amd_pmf_dev * dev,struct ta_pmf_enact_table * in)194 void amd_pmf_populate_ta_inputs(struct amd_pmf_dev *dev, struct ta_pmf_enact_table *in)
195 {
196 /* TA side lid open is 1 and close is 0, hence the ! here */
197 in->ev_info.lid_state = !acpi_lid_open();
198 in->ev_info.power_source = amd_pmf_get_power_source();
199 amd_pmf_get_smu_info(dev, in);
200 amd_pmf_get_battery_info(dev, in);
201 amd_pmf_get_slider_info(dev, in);
202 amd_pmf_get_sensor_info(dev, in);
203 }
204