|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * uncore-frquency-tpmi: Uncore frequency scaling using TPMI |
| 4 | + * |
| 5 | + * Copyright (c) 2023, Intel Corporation. |
| 6 | + * All Rights Reserved. |
| 7 | + * |
| 8 | + * The hardware interface to read/write is basically substitution of |
| 9 | + * MSR 0x620 and 0x621. |
| 10 | + * There are specific MMIO offset and bits to get/set minimum and |
| 11 | + * maximum uncore ratio, similar to MSRs. |
| 12 | + * The scope of the uncore MSRs was package scope. But TPMI allows |
| 13 | + * new gen CPUs to have multiple uncore controls at uncore-cluster |
| 14 | + * level. Each package can have multiple power domains which further |
| 15 | + * can have multiple clusters. |
| 16 | + * Here number of power domains = number of resources in this aux |
| 17 | + * device. There are offsets and bits to discover number of clusters |
| 18 | + * and offset for each cluster level controls. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +#include <linux/auxiliary_bus.h> |
| 23 | +#include <linux/bitfield.h> |
| 24 | +#include <linux/bits.h> |
| 25 | +#include <linux/io.h> |
| 26 | +#include <linux/module.h> |
| 27 | +#include <linux/intel_tpmi.h> |
| 28 | + |
| 29 | +#include "uncore-frequency-common.h" |
| 30 | + |
| 31 | +#define UNCORE_HEADER_VERSION 1 |
| 32 | +#define UNCORE_HEADER_INDEX 0 |
| 33 | +#define UNCORE_FABRIC_CLUSTER_OFFSET 8 |
| 34 | + |
| 35 | +/* status + control + adv_ctl1 + adv_ctl2 */ |
| 36 | +#define UNCORE_FABRIC_CLUSTER_SIZE (4 * 8) |
| 37 | + |
| 38 | +#define UNCORE_STATUS_INDEX 0 |
| 39 | +#define UNCORE_CONTROL_INDEX 8 |
| 40 | + |
| 41 | +#define UNCORE_FREQ_KHZ_MULTIPLIER 100000 |
| 42 | + |
| 43 | +struct tpmi_uncore_struct; |
| 44 | + |
| 45 | +/* Information for each cluster */ |
| 46 | +struct tpmi_uncore_cluster_info { |
| 47 | + u8 __iomem *cluster_base; |
| 48 | + struct uncore_data uncore_data; |
| 49 | + struct tpmi_uncore_struct *uncore_root; |
| 50 | +}; |
| 51 | + |
| 52 | +/* Information for each power domain */ |
| 53 | +struct tpmi_uncore_power_domain_info { |
| 54 | + u8 __iomem *uncore_base; |
| 55 | + int ufs_header_ver; |
| 56 | + int cluster_count; |
| 57 | + struct tpmi_uncore_cluster_info *cluster_infos; |
| 58 | +}; |
| 59 | + |
| 60 | +/* Information for all power domains in a package */ |
| 61 | +struct tpmi_uncore_struct { |
| 62 | + int power_domain_count; |
| 63 | + struct tpmi_uncore_power_domain_info *pd_info; |
| 64 | + struct tpmi_uncore_cluster_info root_cluster; |
| 65 | +}; |
| 66 | + |
| 67 | +#define UNCORE_GENMASK_MIN_RATIO GENMASK_ULL(21, 15) |
| 68 | +#define UNCORE_GENMASK_MAX_RATIO GENMASK_ULL(14, 8) |
| 69 | + |
| 70 | +/* Helper function to read MMIO offset for max/min control frequency */ |
| 71 | +static void read_control_freq(struct tpmi_uncore_cluster_info *cluster_info, |
| 72 | + unsigned int *min, unsigned int *max) |
| 73 | +{ |
| 74 | + u64 control; |
| 75 | + |
| 76 | + control = readq(cluster_info->cluster_base + UNCORE_CONTROL_INDEX); |
| 77 | + *max = FIELD_GET(UNCORE_GENMASK_MAX_RATIO, control) * UNCORE_FREQ_KHZ_MULTIPLIER; |
| 78 | + *min = FIELD_GET(UNCORE_GENMASK_MIN_RATIO, control) * UNCORE_FREQ_KHZ_MULTIPLIER; |
| 79 | +} |
| 80 | + |
| 81 | +#define UNCORE_MAX_RATIO FIELD_MAX(UNCORE_GENMASK_MAX_RATIO) |
| 82 | + |
| 83 | +/* Callback for sysfs read for max/min frequencies. Called under mutex locks */ |
| 84 | +static int uncore_read_control_freq(struct uncore_data *data, unsigned int *min, |
| 85 | + unsigned int *max) |
| 86 | +{ |
| 87 | + struct tpmi_uncore_cluster_info *cluster_info; |
| 88 | + struct tpmi_uncore_struct *uncore_root; |
| 89 | + int i, _min = 0, _max = 0; |
| 90 | + |
| 91 | + cluster_info = container_of(data, struct tpmi_uncore_cluster_info, uncore_data); |
| 92 | + uncore_root = cluster_info->uncore_root; |
| 93 | + |
| 94 | + *min = UNCORE_MAX_RATIO * UNCORE_FREQ_KHZ_MULTIPLIER; |
| 95 | + *max = 0; |
| 96 | + |
| 97 | + /* |
| 98 | + * Get the max/min by looking at each cluster. Get the lowest |
| 99 | + * min and highest max. |
| 100 | + */ |
| 101 | + for (i = 0; i < uncore_root->power_domain_count; ++i) { |
| 102 | + int j; |
| 103 | + |
| 104 | + for (j = 0; j < uncore_root->pd_info[i].cluster_count; ++j) { |
| 105 | + read_control_freq(&uncore_root->pd_info[i].cluster_infos[j], |
| 106 | + &_min, &_max); |
| 107 | + if (*min > _min) |
| 108 | + *min = _min; |
| 109 | + if (*max < _max) |
| 110 | + *max = _max; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +/* Helper function to write MMIO offset for max/min control frequency */ |
| 118 | +static void write_control_freq(struct tpmi_uncore_cluster_info *cluster_info, unsigned int input, |
| 119 | + unsigned int min_max) |
| 120 | +{ |
| 121 | + u64 control; |
| 122 | + |
| 123 | + control = readq(cluster_info->cluster_base + UNCORE_CONTROL_INDEX); |
| 124 | + |
| 125 | + if (min_max) { |
| 126 | + control &= ~UNCORE_GENMASK_MAX_RATIO; |
| 127 | + control |= FIELD_PREP(UNCORE_GENMASK_MAX_RATIO, input); |
| 128 | + } else { |
| 129 | + control &= ~UNCORE_GENMASK_MIN_RATIO; |
| 130 | + control |= FIELD_PREP(UNCORE_GENMASK_MIN_RATIO, input); |
| 131 | + } |
| 132 | + |
| 133 | + writeq(control, (cluster_info->cluster_base + UNCORE_CONTROL_INDEX)); |
| 134 | +} |
| 135 | + |
| 136 | +/* Callback for sysfs write for max/min frequencies. Called under mutex locks */ |
| 137 | +static int uncore_write_control_freq(struct uncore_data *data, unsigned int input, |
| 138 | + unsigned int min_max) |
| 139 | +{ |
| 140 | + struct tpmi_uncore_cluster_info *cluster_info; |
| 141 | + struct tpmi_uncore_struct *uncore_root; |
| 142 | + int i; |
| 143 | + |
| 144 | + input /= UNCORE_FREQ_KHZ_MULTIPLIER; |
| 145 | + if (!input || input > UNCORE_MAX_RATIO) |
| 146 | + return -EINVAL; |
| 147 | + |
| 148 | + cluster_info = container_of(data, struct tpmi_uncore_cluster_info, uncore_data); |
| 149 | + uncore_root = cluster_info->uncore_root; |
| 150 | + |
| 151 | + /* Update each cluster in a package */ |
| 152 | + for (i = 0; i < uncore_root->power_domain_count; ++i) { |
| 153 | + int j; |
| 154 | + |
| 155 | + for (j = 0; j < uncore_root->pd_info[i].cluster_count; ++j) |
| 156 | + write_control_freq(&uncore_root->pd_info[i].cluster_infos[j], |
| 157 | + input, min_max); |
| 158 | + } |
| 159 | + |
| 160 | + return 0; |
| 161 | +} |
| 162 | + |
| 163 | +/* Callback for sysfs read for the current uncore frequency. Called under mutex locks */ |
| 164 | +static int uncore_read_freq(struct uncore_data *data, unsigned int *freq) |
| 165 | +{ |
| 166 | + return -ENODATA; |
| 167 | +} |
| 168 | + |
| 169 | +#define UNCORE_VERSION_MASK GENMASK_ULL(7, 0) |
| 170 | +#define UNCORE_LOCAL_FABRIC_CLUSTER_ID_MASK GENMASK_ULL(15, 8) |
| 171 | +#define UNCORE_CLUSTER_OFF_MASK GENMASK_ULL(7, 0) |
| 172 | +#define UNCORE_MAX_CLUSTER_PER_DOMAIN 8 |
| 173 | + |
| 174 | +static int uncore_probe(struct auxiliary_device *auxdev, const struct auxiliary_device_id *id) |
| 175 | +{ |
| 176 | + struct intel_tpmi_plat_info *plat_info; |
| 177 | + struct tpmi_uncore_struct *tpmi_uncore; |
| 178 | + int ret, i, pkg = 0; |
| 179 | + int num_resources; |
| 180 | + |
| 181 | + /* Get number of power domains, which is equal to number of resources */ |
| 182 | + num_resources = tpmi_get_resource_count(auxdev); |
| 183 | + if (!num_resources) |
| 184 | + return -EINVAL; |
| 185 | + |
| 186 | + /* Register callbacks to uncore core */ |
| 187 | + ret = uncore_freq_common_init(uncore_read_control_freq, uncore_write_control_freq, |
| 188 | + uncore_read_freq); |
| 189 | + if (ret) |
| 190 | + return ret; |
| 191 | + |
| 192 | + /* Allocate uncore instance per package */ |
| 193 | + tpmi_uncore = devm_kzalloc(&auxdev->dev, sizeof(*tpmi_uncore), GFP_KERNEL); |
| 194 | + if (!tpmi_uncore) { |
| 195 | + ret = -ENOMEM; |
| 196 | + goto err_rem_common; |
| 197 | + } |
| 198 | + |
| 199 | + /* Allocate memory for all power domains in a package */ |
| 200 | + tpmi_uncore->pd_info = devm_kcalloc(&auxdev->dev, num_resources, |
| 201 | + sizeof(*tpmi_uncore->pd_info), |
| 202 | + GFP_KERNEL); |
| 203 | + if (!tpmi_uncore->pd_info) { |
| 204 | + ret = -ENOMEM; |
| 205 | + goto err_rem_common; |
| 206 | + } |
| 207 | + |
| 208 | + tpmi_uncore->power_domain_count = num_resources; |
| 209 | + |
| 210 | + /* Get the package ID from the TPMI core */ |
| 211 | + plat_info = tpmi_get_platform_data(auxdev); |
| 212 | + if (plat_info) |
| 213 | + pkg = plat_info->package_id; |
| 214 | + else |
| 215 | + dev_info(&auxdev->dev, "Platform information is NULL\n"); |
| 216 | + |
| 217 | + for (i = 0; i < num_resources; ++i) { |
| 218 | + struct tpmi_uncore_power_domain_info *pd_info; |
| 219 | + struct resource *res; |
| 220 | + u64 cluster_offset; |
| 221 | + u8 cluster_mask; |
| 222 | + int mask, j; |
| 223 | + u64 header; |
| 224 | + |
| 225 | + res = tpmi_get_resource_at_index(auxdev, i); |
| 226 | + if (!res) |
| 227 | + continue; |
| 228 | + |
| 229 | + pd_info = &tpmi_uncore->pd_info[i]; |
| 230 | + |
| 231 | + pd_info->uncore_base = devm_ioremap_resource(&auxdev->dev, res); |
| 232 | + if (IS_ERR(pd_info->uncore_base)) { |
| 233 | + ret = PTR_ERR(pd_info->uncore_base); |
| 234 | + goto err_rem_common; |
| 235 | + } |
| 236 | + |
| 237 | + /* Check for version and skip this resource if there is mismatch */ |
| 238 | + header = readq(pd_info->uncore_base); |
| 239 | + pd_info->ufs_header_ver = header & UNCORE_VERSION_MASK; |
| 240 | + if (pd_info->ufs_header_ver != UNCORE_HEADER_VERSION) { |
| 241 | + dev_info(&auxdev->dev, "Uncore: Unsupported version:%d\n", |
| 242 | + pd_info->ufs_header_ver); |
| 243 | + continue; |
| 244 | + } |
| 245 | + |
| 246 | + /* Get Cluster ID Mask */ |
| 247 | + cluster_mask = FIELD_GET(UNCORE_LOCAL_FABRIC_CLUSTER_ID_MASK, header); |
| 248 | + if (!cluster_mask) { |
| 249 | + dev_info(&auxdev->dev, "Uncore: Invalid cluster mask:%x\n", cluster_mask); |
| 250 | + continue; |
| 251 | + } |
| 252 | + |
| 253 | + /* Find out number of clusters in this resource */ |
| 254 | + pd_info->cluster_count = hweight8(cluster_mask); |
| 255 | + |
| 256 | + pd_info->cluster_infos = devm_kcalloc(&auxdev->dev, pd_info->cluster_count, |
| 257 | + sizeof(struct tpmi_uncore_cluster_info), |
| 258 | + GFP_KERNEL); |
| 259 | + if (!pd_info->cluster_infos) { |
| 260 | + ret = -ENOMEM; |
| 261 | + goto err_rem_common; |
| 262 | + } |
| 263 | + /* |
| 264 | + * Each byte in the register point to status and control |
| 265 | + * registers belonging to cluster id 0-8. |
| 266 | + */ |
| 267 | + cluster_offset = readq(pd_info->uncore_base + |
| 268 | + UNCORE_FABRIC_CLUSTER_OFFSET); |
| 269 | + |
| 270 | + for (j = 0; j < pd_info->cluster_count; ++j) { |
| 271 | + struct tpmi_uncore_cluster_info *cluster_info; |
| 272 | + |
| 273 | + /* Get the offset for this cluster */ |
| 274 | + mask = (cluster_offset & UNCORE_CLUSTER_OFF_MASK); |
| 275 | + /* Offset in QWORD, so change to bytes */ |
| 276 | + mask <<= 3; |
| 277 | + |
| 278 | + cluster_info = &pd_info->cluster_infos[j]; |
| 279 | + |
| 280 | + cluster_info->cluster_base = pd_info->uncore_base + mask; |
| 281 | + |
| 282 | + cluster_info->uncore_data.package_id = pkg; |
| 283 | + /* There are no dies like Cascade Lake */ |
| 284 | + cluster_info->uncore_data.die_id = 0; |
| 285 | + |
| 286 | + /* Point to next cluster offset */ |
| 287 | + cluster_offset >>= UNCORE_MAX_CLUSTER_PER_DOMAIN; |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + auxiliary_set_drvdata(auxdev, tpmi_uncore); |
| 292 | + |
| 293 | + tpmi_uncore->root_cluster.uncore_root = tpmi_uncore; |
| 294 | + tpmi_uncore->root_cluster.uncore_data.package_id = pkg; |
| 295 | + ret = uncore_freq_add_entry(&tpmi_uncore->root_cluster.uncore_data, 0); |
| 296 | + if (ret) |
| 297 | + goto err_rem_common; |
| 298 | + |
| 299 | + return 0; |
| 300 | + |
| 301 | +err_rem_common: |
| 302 | + uncore_freq_common_exit(); |
| 303 | + |
| 304 | + return ret; |
| 305 | +} |
| 306 | + |
| 307 | +static void uncore_remove(struct auxiliary_device *auxdev) |
| 308 | +{ |
| 309 | + struct tpmi_uncore_struct *tpmi_uncore = auxiliary_get_drvdata(auxdev); |
| 310 | + |
| 311 | + uncore_freq_remove_die_entry(&tpmi_uncore->root_cluster.uncore_data); |
| 312 | + |
| 313 | + uncore_freq_common_exit(); |
| 314 | +} |
| 315 | + |
| 316 | +static const struct auxiliary_device_id intel_uncore_id_table[] = { |
| 317 | + { .name = "intel_vsec.tpmi-uncore" }, |
| 318 | + {} |
| 319 | +}; |
| 320 | +MODULE_DEVICE_TABLE(auxiliary, intel_uncore_id_table); |
| 321 | + |
| 322 | +static struct auxiliary_driver intel_uncore_aux_driver = { |
| 323 | + .id_table = intel_uncore_id_table, |
| 324 | + .remove = uncore_remove, |
| 325 | + .probe = uncore_probe, |
| 326 | +}; |
| 327 | + |
| 328 | +module_auxiliary_driver(intel_uncore_aux_driver); |
| 329 | + |
| 330 | +MODULE_IMPORT_NS(INTEL_TPMI); |
| 331 | +MODULE_IMPORT_NS(INTEL_UNCORE_FREQUENCY); |
| 332 | +MODULE_DESCRIPTION("Intel TPMI UFS Driver"); |
| 333 | +MODULE_LICENSE("GPL"); |
0 commit comments