|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Processor Thermal Device Interface for Reading and Writing |
| 4 | + * SoC Power Slider Values from User Space. |
| 5 | + * |
| 6 | + * Operation: |
| 7 | + * The SOC_EFFICIENCY_SLIDER_0_0_0_MCHBAR register is accessed |
| 8 | + * using the MMIO (Memory-Mapped I/O) interface with an MMIO offset of 0x5B38. |
| 9 | + * Although this register is 64 bits wide, only bits 7:0 are used, |
| 10 | + * and the other bits remain unchanged. |
| 11 | + * |
| 12 | + * Bit definitions |
| 13 | + * |
| 14 | + * Bits 2:0 (Slider value): |
| 15 | + * The SoC optimizer slider value indicates the system wide energy performance |
| 16 | + * hint. The slider has no specific units and ranges from 0 (highest |
| 17 | + * performance) to 6 (highest energy efficiency). Value of 7 is reserved. |
| 18 | + * Bits 3 : Reserved |
| 19 | + * Bits 6:4 (Offset) |
| 20 | + * Offset allows the SoC to automatically switch slider position in range |
| 21 | + * [slider value (bits 2:0) + offset] to improve power efficiency based on |
| 22 | + * internal SoC algorithms. |
| 23 | + * Bit 7 (Enable): |
| 24 | + * If this bit is set, the SoC Optimization sliders will be processed by the |
| 25 | + * SoC firmware. |
| 26 | + * |
| 27 | + * Copyright (c) 2025, Intel Corporation. |
| 28 | + */ |
| 29 | + |
| 30 | +#include <linux/bitfield.h> |
| 31 | +#include <linux/pci.h> |
| 32 | +#include <linux/platform_profile.h> |
| 33 | +#include "processor_thermal_device.h" |
| 34 | + |
| 35 | +#define SOC_POWER_SLIDER_OFFSET 0x5B38 |
| 36 | + |
| 37 | +enum power_slider_preference { |
| 38 | + SOC_POWER_SLIDER_PERFORMANCE, |
| 39 | + SOC_POWER_SLIDER_BALANCE, |
| 40 | + SOC_POWER_SLIDER_POWERSAVE, |
| 41 | +}; |
| 42 | + |
| 43 | +#define SOC_SLIDER_VALUE_MINIMUM 0x00 |
| 44 | +#define SOC_SLIDER_VALUE_BALANCE 0x03 |
| 45 | +#define SOC_SLIDER_VALUE_MAXIMUM 0x06 |
| 46 | + |
| 47 | +#define SLIDER_MASK GENMASK_ULL(2, 0) |
| 48 | +#define SLIDER_ENABLE_BIT 7 |
| 49 | + |
| 50 | +static u8 slider_values[] = { |
| 51 | + [SOC_POWER_SLIDER_PERFORMANCE] = SOC_SLIDER_VALUE_MINIMUM, |
| 52 | + [SOC_POWER_SLIDER_BALANCE] = SOC_SLIDER_VALUE_BALANCE, |
| 53 | + [SOC_POWER_SLIDER_POWERSAVE] = SOC_SLIDER_VALUE_MAXIMUM, |
| 54 | +}; |
| 55 | + |
| 56 | +/* Lock to protect module param updates */ |
| 57 | +static DEFINE_MUTEX(slider_param_lock); |
| 58 | + |
| 59 | +static int slider_balanced_param = SOC_SLIDER_VALUE_BALANCE; |
| 60 | + |
| 61 | +static int slider_def_balance_set(const char *arg, const struct kernel_param *kp) |
| 62 | +{ |
| 63 | + u8 slider_val; |
| 64 | + int ret; |
| 65 | + |
| 66 | + guard(mutex)(&slider_param_lock); |
| 67 | + |
| 68 | + ret = kstrtou8(arg, 16, &slider_val); |
| 69 | + if (!ret) { |
| 70 | + if (slider_val > SOC_SLIDER_VALUE_MAXIMUM) |
| 71 | + return -EINVAL; |
| 72 | + |
| 73 | + slider_balanced_param = slider_val; |
| 74 | + } |
| 75 | + |
| 76 | + return ret; |
| 77 | +} |
| 78 | + |
| 79 | +static int slider_def_balance_get(char *buf, const struct kernel_param *kp) |
| 80 | +{ |
| 81 | + guard(mutex)(&slider_param_lock); |
| 82 | + return sysfs_emit(buf, "%02x\n", slider_values[SOC_POWER_SLIDER_BALANCE]); |
| 83 | +} |
| 84 | + |
| 85 | +static const struct kernel_param_ops slider_def_balance_ops = { |
| 86 | + .set = slider_def_balance_set, |
| 87 | + .get = slider_def_balance_get, |
| 88 | +}; |
| 89 | + |
| 90 | +module_param_cb(slider_balance, &slider_def_balance_ops, NULL, 0644); |
| 91 | +MODULE_PARM_DESC(slider_balance, "Set slider default value for balance"); |
| 92 | + |
| 93 | +static u8 slider_offset; |
| 94 | + |
| 95 | +static int slider_def_offset_set(const char *arg, const struct kernel_param *kp) |
| 96 | +{ |
| 97 | + u8 offset; |
| 98 | + int ret; |
| 99 | + |
| 100 | + guard(mutex)(&slider_param_lock); |
| 101 | + |
| 102 | + ret = kstrtou8(arg, 16, &offset); |
| 103 | + if (!ret) { |
| 104 | + if (offset > SOC_SLIDER_VALUE_MAXIMUM) |
| 105 | + return -EINVAL; |
| 106 | + |
| 107 | + slider_offset = offset; |
| 108 | + } |
| 109 | + |
| 110 | + return ret; |
| 111 | +} |
| 112 | + |
| 113 | +static int slider_def_offset_get(char *buf, const struct kernel_param *kp) |
| 114 | +{ |
| 115 | + guard(mutex)(&slider_param_lock); |
| 116 | + return sysfs_emit(buf, "%02x\n", slider_offset); |
| 117 | +} |
| 118 | + |
| 119 | +static const struct kernel_param_ops slider_offset_ops = { |
| 120 | + .set = slider_def_offset_set, |
| 121 | + .get = slider_def_offset_get, |
| 122 | +}; |
| 123 | + |
| 124 | +/* |
| 125 | + * To enhance power efficiency dynamically, the firmware can optionally |
| 126 | + * auto-adjust the slider value based on the current workload. This |
| 127 | + * adjustment is controlled by the "slider_offset" module parameter. |
| 128 | + * This offset permits the firmware to increase the slider value |
| 129 | + * up to and including "SoC slider + slider offset,". |
| 130 | + */ |
| 131 | +module_param_cb(slider_offset, &slider_offset_ops, NULL, 0644); |
| 132 | +MODULE_PARM_DESC(slider_offset, "Set slider offset"); |
| 133 | + |
| 134 | +/* Convert from platform power profile option to SoC slider value */ |
| 135 | +static int convert_profile_to_power_slider(enum platform_profile_option profile) |
| 136 | +{ |
| 137 | + switch (profile) { |
| 138 | + case PLATFORM_PROFILE_LOW_POWER: |
| 139 | + return slider_values[SOC_POWER_SLIDER_POWERSAVE]; |
| 140 | + case PLATFORM_PROFILE_BALANCED: |
| 141 | + return slider_values[SOC_POWER_SLIDER_BALANCE]; |
| 142 | + case PLATFORM_PROFILE_PERFORMANCE: |
| 143 | + return slider_values[SOC_POWER_SLIDER_PERFORMANCE]; |
| 144 | + default: |
| 145 | + break; |
| 146 | + } |
| 147 | + |
| 148 | + return -EOPNOTSUPP; |
| 149 | +} |
| 150 | + |
| 151 | +/* Convert to platform power profile option from SoC slider values */ |
| 152 | +static int convert_power_slider_to_profile(u8 slider) |
| 153 | +{ |
| 154 | + if (slider == slider_values[SOC_POWER_SLIDER_PERFORMANCE]) |
| 155 | + return PLATFORM_PROFILE_PERFORMANCE; |
| 156 | + if (slider == slider_values[SOC_POWER_SLIDER_BALANCE]) |
| 157 | + return PLATFORM_PROFILE_BALANCED; |
| 158 | + if (slider == slider_values[SOC_POWER_SLIDER_POWERSAVE]) |
| 159 | + return PLATFORM_PROFILE_LOW_POWER; |
| 160 | + |
| 161 | + return -EOPNOTSUPP; |
| 162 | +} |
| 163 | + |
| 164 | +static inline u64 read_soc_slider(struct proc_thermal_device *proc_priv) |
| 165 | +{ |
| 166 | + return readq(proc_priv->mmio_base + SOC_POWER_SLIDER_OFFSET); |
| 167 | +} |
| 168 | + |
| 169 | +static inline void write_soc_slider(struct proc_thermal_device *proc_priv, u64 val) |
| 170 | +{ |
| 171 | + writeq(val, proc_priv->mmio_base + SOC_POWER_SLIDER_OFFSET); |
| 172 | +} |
| 173 | + |
| 174 | +#define SLIDER_OFFSET_MASK GENMASK_ULL(6, 4) |
| 175 | + |
| 176 | +static void set_soc_power_profile(struct proc_thermal_device *proc_priv, int slider) |
| 177 | +{ |
| 178 | + u64 val; |
| 179 | + |
| 180 | + val = read_soc_slider(proc_priv); |
| 181 | + val &= ~SLIDER_MASK; |
| 182 | + val |= FIELD_PREP(SLIDER_MASK, slider) | BIT(SLIDER_ENABLE_BIT); |
| 183 | + |
| 184 | + /* Set the slider offset from module params */ |
| 185 | + val &= ~SLIDER_OFFSET_MASK; |
| 186 | + val |= FIELD_PREP(SLIDER_OFFSET_MASK, slider_offset); |
| 187 | + |
| 188 | + write_soc_slider(proc_priv, val); |
| 189 | +} |
| 190 | + |
| 191 | +/* profile get/set callbacks are called with a profile lock, so no need for local locks */ |
| 192 | + |
| 193 | +static int power_slider_platform_profile_set(struct device *dev, |
| 194 | + enum platform_profile_option profile) |
| 195 | +{ |
| 196 | + struct proc_thermal_device *proc_priv; |
| 197 | + int slider; |
| 198 | + |
| 199 | + proc_priv = dev_get_drvdata(dev); |
| 200 | + if (!proc_priv) |
| 201 | + return -EOPNOTSUPP; |
| 202 | + |
| 203 | + guard(mutex)(&slider_param_lock); |
| 204 | + |
| 205 | + slider_values[SOC_POWER_SLIDER_BALANCE] = slider_balanced_param; |
| 206 | + |
| 207 | + slider = convert_profile_to_power_slider(profile); |
| 208 | + if (slider < 0) |
| 209 | + return slider; |
| 210 | + |
| 211 | + set_soc_power_profile(proc_priv, slider); |
| 212 | + |
| 213 | + return 0; |
| 214 | +} |
| 215 | + |
| 216 | +static int power_slider_platform_profile_get(struct device *dev, |
| 217 | + enum platform_profile_option *profile) |
| 218 | +{ |
| 219 | + struct proc_thermal_device *proc_priv; |
| 220 | + int slider, ret; |
| 221 | + u64 val; |
| 222 | + |
| 223 | + proc_priv = dev_get_drvdata(dev); |
| 224 | + if (!proc_priv) |
| 225 | + return -EOPNOTSUPP; |
| 226 | + |
| 227 | + val = read_soc_slider(proc_priv); |
| 228 | + slider = FIELD_GET(SLIDER_MASK, val); |
| 229 | + |
| 230 | + ret = convert_power_slider_to_profile(slider); |
| 231 | + if (ret < 0) |
| 232 | + return ret; |
| 233 | + |
| 234 | + *profile = ret; |
| 235 | + |
| 236 | + return 0; |
| 237 | +} |
| 238 | + |
| 239 | +static int power_slider_platform_profile_probe(void *drvdata, unsigned long *choices) |
| 240 | +{ |
| 241 | + set_bit(PLATFORM_PROFILE_LOW_POWER, choices); |
| 242 | + set_bit(PLATFORM_PROFILE_BALANCED, choices); |
| 243 | + set_bit(PLATFORM_PROFILE_PERFORMANCE, choices); |
| 244 | + |
| 245 | + return 0; |
| 246 | +} |
| 247 | + |
| 248 | +static const struct platform_profile_ops power_slider_platform_profile_ops = { |
| 249 | + .probe = power_slider_platform_profile_probe, |
| 250 | + .profile_get = power_slider_platform_profile_get, |
| 251 | + .profile_set = power_slider_platform_profile_set, |
| 252 | +}; |
| 253 | + |
| 254 | +int proc_thermal_soc_power_slider_add(struct pci_dev *pdev, struct proc_thermal_device *proc_priv) |
| 255 | +{ |
| 256 | + struct device *ppdev; |
| 257 | + |
| 258 | + set_soc_power_profile(proc_priv, slider_values[SOC_POWER_SLIDER_BALANCE]); |
| 259 | + |
| 260 | + ppdev = devm_platform_profile_register(&pdev->dev, "SoC Power Slider", proc_priv, |
| 261 | + &power_slider_platform_profile_ops); |
| 262 | + |
| 263 | + return PTR_ERR_OR_ZERO(ppdev); |
| 264 | +} |
| 265 | +EXPORT_SYMBOL_NS_GPL(proc_thermal_soc_power_slider_add, "INT340X_THERMAL"); |
| 266 | + |
| 267 | +static u64 soc_slider_save; |
| 268 | + |
| 269 | +void proc_thermal_soc_power_slider_suspend(struct proc_thermal_device *proc_priv) |
| 270 | +{ |
| 271 | + soc_slider_save = read_soc_slider(proc_priv); |
| 272 | +} |
| 273 | +EXPORT_SYMBOL_NS_GPL(proc_thermal_soc_power_slider_suspend, "INT340X_THERMAL"); |
| 274 | + |
| 275 | +void proc_thermal_soc_power_slider_resume(struct proc_thermal_device *proc_priv) |
| 276 | +{ |
| 277 | + write_soc_slider(proc_priv, soc_slider_save); |
| 278 | +} |
| 279 | +EXPORT_SYMBOL_NS_GPL(proc_thermal_soc_power_slider_resume, "INT340X_THERMAL"); |
| 280 | + |
| 281 | +MODULE_IMPORT_NS("INT340X_THERMAL"); |
| 282 | +MODULE_LICENSE("GPL"); |
| 283 | +MODULE_DESCRIPTION("Processor Thermal Power Slider Interface"); |
0 commit comments