| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Intel Uncore Frequency Control: Common code implementation |
| 4 |
* Copyright (c) 2022, Intel Corporation. |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
*/ |
| 8 |
#include <linux/cpu.h> |
| 9 |
#include <linux/module.h> |
| 10 |
#include "uncore-frequency-common.h" |
| 11 |
|
| 12 |
/* Mutex to control all mutual exclusions */ |
| 13 |
static DEFINE_MUTEX(uncore_lock); |
| 14 |
/* Root of the all uncore sysfs kobjs */ |
| 15 |
static struct kobject *uncore_root_kobj; |
| 16 |
/* uncore instance count */ |
| 17 |
static int uncore_instance_count; |
| 18 |
|
| 19 |
static DEFINE_IDA(intel_uncore_ida); |
| 20 |
|
| 21 |
/* callbacks for actual HW read/write */ |
| 22 |
static int (*uncore_read)(struct uncore_data *data, unsigned int *value, enum uncore_index index); |
| 23 |
static int (*uncore_write)(struct uncore_data *data, unsigned int input, enum uncore_index index); |
| 24 |
|
| 25 |
static ssize_t show_domain_id(struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 26 |
{ |
| 27 |
struct uncore_data *data = container_of(attr, struct uncore_data, domain_id_kobj_attr); |
| 28 |
|
| 29 |
return sysfs_emit(buf, "%u\n", data->domain_id); |
| 30 |
} |
| 31 |
|
| 32 |
static ssize_t show_fabric_cluster_id(struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 33 |
{ |
| 34 |
struct uncore_data *data = container_of(attr, struct uncore_data, fabric_cluster_id_kobj_attr); |
| 35 |
|
| 36 |
return sysfs_emit(buf, "%u\n", data->cluster_id); |
| 37 |
} |
| 38 |
|
| 39 |
static ssize_t show_package_id(struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 40 |
{ |
| 41 |
struct uncore_data *data = container_of(attr, struct uncore_data, package_id_kobj_attr); |
| 42 |
|
| 43 |
return sysfs_emit(buf, "%u\n", data->package_id); |
| 44 |
} |
| 45 |
|
| 46 |
#define MAX_UNCORE_AGENT_TYPES 4 |
| 47 |
|
| 48 |
/* The order follows AGENT_TYPE_* defines */ |
| 49 |
static const char *agent_name[MAX_UNCORE_AGENT_TYPES] = {"core", "cache", "memory", "io"}; |
| 50 |
|
| 51 |
static ssize_t show_agent_types(struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 52 |
{ |
| 53 |
struct uncore_data *data = container_of(attr, struct uncore_data, agent_types_kobj_attr); |
| 54 |
unsigned long agent_mask = data->agent_type_mask; |
| 55 |
int agent, length = 0; |
| 56 |
|
| 57 |
for_each_set_bit(agent, &agent_mask, MAX_UNCORE_AGENT_TYPES) { |
| 58 |
if (length) |
| 59 |
length += sysfs_emit_at(buf, length, " "); |
| 60 |
|
| 61 |
length += sysfs_emit_at(buf, length, "%s", agent_name[agent]); |
| 62 |
} |
| 63 |
|
| 64 |
length += sysfs_emit_at(buf, length, "\n"); |
| 65 |
|
| 66 |
return length; |
| 67 |
} |
| 68 |
|
| 69 |
static ssize_t show_attr(struct uncore_data *data, char *buf, enum uncore_index index) |
| 70 |
{ |
| 71 |
unsigned int value; |
| 72 |
int ret; |
| 73 |
|
| 74 |
mutex_lock(&uncore_lock); |
| 75 |
ret = uncore_read(data, &value, index); |
| 76 |
mutex_unlock(&uncore_lock); |
| 77 |
if (ret) |
| 78 |
return ret; |
| 79 |
|
| 80 |
return sysfs_emit(buf, "%u\n", value); |
| 81 |
} |
| 82 |
|
| 83 |
static ssize_t store_attr(struct uncore_data *data, const char *buf, ssize_t count, |
| 84 |
enum uncore_index index) |
| 85 |
{ |
| 86 |
unsigned int input = 0; |
| 87 |
int ret; |
| 88 |
|
| 89 |
if (index == UNCORE_INDEX_EFF_LAT_CTRL_HIGH_THRESHOLD_ENABLE) { |
| 90 |
if (kstrtobool(buf, (bool *)&input)) |
| 91 |
return -EINVAL; |
| 92 |
} else { |
| 93 |
if (kstrtouint(buf, 10, &input)) |
| 94 |
return -EINVAL; |
| 95 |
} |
| 96 |
|
| 97 |
mutex_lock(&uncore_lock); |
| 98 |
ret = uncore_write(data, input, index); |
| 99 |
mutex_unlock(&uncore_lock); |
| 100 |
|
| 101 |
if (ret) |
| 102 |
return ret; |
| 103 |
|
| 104 |
return count; |
| 105 |
} |
| 106 |
|
| 107 |
#define store_uncore_attr(name, index) \ |
| 108 |
static ssize_t store_##name(struct kobject *kobj, \ |
| 109 |
struct kobj_attribute *attr, \ |
| 110 |
const char *buf, size_t count) \ |
| 111 |
{ \ |
| 112 |
struct uncore_data *data = container_of(attr, struct uncore_data, name##_kobj_attr);\ |
| 113 |
\ |
| 114 |
return store_attr(data, buf, count, index); \ |
| 115 |
} |
| 116 |
|
| 117 |
#define show_uncore_attr(name, index) \ |
| 118 |
static ssize_t show_##name(struct kobject *kobj, \ |
| 119 |
struct kobj_attribute *attr, char *buf)\ |
| 120 |
{ \ |
| 121 |
struct uncore_data *data = container_of(attr, struct uncore_data, name##_kobj_attr);\ |
| 122 |
\ |
| 123 |
return show_attr(data, buf, index); \ |
| 124 |
} |
| 125 |
|
| 126 |
store_uncore_attr(min_freq_khz, UNCORE_INDEX_MIN_FREQ); |
| 127 |
store_uncore_attr(max_freq_khz, UNCORE_INDEX_MAX_FREQ); |
| 128 |
|
| 129 |
show_uncore_attr(min_freq_khz, UNCORE_INDEX_MIN_FREQ); |
| 130 |
show_uncore_attr(max_freq_khz, UNCORE_INDEX_MAX_FREQ); |
| 131 |
|
| 132 |
show_uncore_attr(current_freq_khz, UNCORE_INDEX_CURRENT_FREQ); |
| 133 |
|
| 134 |
store_uncore_attr(elc_low_threshold_percent, UNCORE_INDEX_EFF_LAT_CTRL_LOW_THRESHOLD); |
| 135 |
store_uncore_attr(elc_high_threshold_percent, UNCORE_INDEX_EFF_LAT_CTRL_HIGH_THRESHOLD); |
| 136 |
store_uncore_attr(elc_high_threshold_enable, |
| 137 |
UNCORE_INDEX_EFF_LAT_CTRL_HIGH_THRESHOLD_ENABLE); |
| 138 |
store_uncore_attr(elc_floor_freq_khz, UNCORE_INDEX_EFF_LAT_CTRL_FREQ); |
| 139 |
|
| 140 |
show_uncore_attr(elc_low_threshold_percent, UNCORE_INDEX_EFF_LAT_CTRL_LOW_THRESHOLD); |
| 141 |
show_uncore_attr(elc_high_threshold_percent, UNCORE_INDEX_EFF_LAT_CTRL_HIGH_THRESHOLD); |
| 142 |
show_uncore_attr(elc_high_threshold_enable, |
| 143 |
UNCORE_INDEX_EFF_LAT_CTRL_HIGH_THRESHOLD_ENABLE); |
| 144 |
show_uncore_attr(elc_floor_freq_khz, UNCORE_INDEX_EFF_LAT_CTRL_FREQ); |
| 145 |
|
| 146 |
show_uncore_attr(die_id, UNCORE_INDEX_DIE_ID); |
| 147 |
|
| 148 |
#define show_uncore_data(member_name) \ |
| 149 |
static ssize_t show_##member_name(struct kobject *kobj, \ |
| 150 |
struct kobj_attribute *attr, char *buf)\ |
| 151 |
{ \ |
| 152 |
struct uncore_data *data = container_of(attr, struct uncore_data,\ |
| 153 |
member_name##_kobj_attr);\ |
| 154 |
\ |
| 155 |
return sysfs_emit(buf, "%u\n", \ |
| 156 |
data->member_name); \ |
| 157 |
} \ |
| 158 |
|
| 159 |
show_uncore_data(initial_min_freq_khz); |
| 160 |
show_uncore_data(initial_max_freq_khz); |
| 161 |
|
| 162 |
#define init_attribute_rw(_name) \ |
| 163 |
do { \ |
| 164 |
sysfs_attr_init(&data->_name##_kobj_attr.attr); \ |
| 165 |
data->_name##_kobj_attr.show = show_##_name; \ |
| 166 |
data->_name##_kobj_attr.store = store_##_name; \ |
| 167 |
data->_name##_kobj_attr.attr.name = #_name; \ |
| 168 |
data->_name##_kobj_attr.attr.mode = 0644; \ |
| 169 |
} while (0) |
| 170 |
|
| 171 |
#define init_attribute_ro(_name) \ |
| 172 |
do { \ |
| 173 |
sysfs_attr_init(&data->_name##_kobj_attr.attr); \ |
| 174 |
data->_name##_kobj_attr.show = show_##_name; \ |
| 175 |
data->_name##_kobj_attr.store = NULL; \ |
| 176 |
data->_name##_kobj_attr.attr.name = #_name; \ |
| 177 |
data->_name##_kobj_attr.attr.mode = 0444; \ |
| 178 |
} while (0) |
| 179 |
|
| 180 |
#define init_attribute_root_ro(_name) \ |
| 181 |
do { \ |
| 182 |
sysfs_attr_init(&data->_name##_kobj_attr.attr); \ |
| 183 |
data->_name##_kobj_attr.show = show_##_name; \ |
| 184 |
data->_name##_kobj_attr.store = NULL; \ |
| 185 |
data->_name##_kobj_attr.attr.name = #_name; \ |
| 186 |
data->_name##_kobj_attr.attr.mode = 0400; \ |
| 187 |
} while (0) |
| 188 |
|
| 189 |
static int create_attr_group(struct uncore_data *data, char *name) |
| 190 |
{ |
| 191 |
int ret, index = 0; |
| 192 |
unsigned int val; |
| 193 |
|
| 194 |
init_attribute_rw(max_freq_khz); |
| 195 |
init_attribute_rw(min_freq_khz); |
| 196 |
init_attribute_ro(initial_min_freq_khz); |
| 197 |
init_attribute_ro(initial_max_freq_khz); |
| 198 |
init_attribute_root_ro(current_freq_khz); |
| 199 |
|
| 200 |
if (data->domain_id != UNCORE_DOMAIN_ID_INVALID) { |
| 201 |
init_attribute_root_ro(domain_id); |
| 202 |
data->uncore_attrs[index++] = &data->domain_id_kobj_attr.attr; |
| 203 |
init_attribute_root_ro(fabric_cluster_id); |
| 204 |
data->uncore_attrs[index++] = &data->fabric_cluster_id_kobj_attr.attr; |
| 205 |
init_attribute_root_ro(package_id); |
| 206 |
data->uncore_attrs[index++] = &data->package_id_kobj_attr.attr; |
| 207 |
if (data->agent_type_mask) { |
| 208 |
init_attribute_ro(agent_types); |
| 209 |
data->uncore_attrs[index++] = &data->agent_types_kobj_attr.attr; |
| 210 |
} |
| 211 |
if (topology_max_dies_per_package() > 1 && |
| 212 |
data->agent_type_mask & AGENT_TYPE_CORE) { |
| 213 |
init_attribute_ro(die_id); |
| 214 |
data->uncore_attrs[index++] = &data->die_id_kobj_attr.attr; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
data->uncore_attrs[index++] = &data->max_freq_khz_kobj_attr.attr; |
| 219 |
data->uncore_attrs[index++] = &data->min_freq_khz_kobj_attr.attr; |
| 220 |
data->uncore_attrs[index++] = &data->initial_min_freq_khz_kobj_attr.attr; |
| 221 |
data->uncore_attrs[index++] = &data->initial_max_freq_khz_kobj_attr.attr; |
| 222 |
|
| 223 |
ret = uncore_read(data, &val, UNCORE_INDEX_CURRENT_FREQ); |
| 224 |
if (!ret) |
| 225 |
data->uncore_attrs[index++] = &data->current_freq_khz_kobj_attr.attr; |
| 226 |
|
| 227 |
ret = uncore_read(data, &val, UNCORE_INDEX_EFF_LAT_CTRL_LOW_THRESHOLD); |
| 228 |
if (!ret) { |
| 229 |
init_attribute_rw(elc_low_threshold_percent); |
| 230 |
init_attribute_rw(elc_high_threshold_percent); |
| 231 |
init_attribute_rw(elc_high_threshold_enable); |
| 232 |
init_attribute_rw(elc_floor_freq_khz); |
| 233 |
|
| 234 |
data->uncore_attrs[index++] = &data->elc_low_threshold_percent_kobj_attr.attr; |
| 235 |
data->uncore_attrs[index++] = &data->elc_high_threshold_percent_kobj_attr.attr; |
| 236 |
data->uncore_attrs[index++] = |
| 237 |
&data->elc_high_threshold_enable_kobj_attr.attr; |
| 238 |
data->uncore_attrs[index++] = &data->elc_floor_freq_khz_kobj_attr.attr; |
| 239 |
} |
| 240 |
|
| 241 |
data->uncore_attrs[index] = NULL; |
| 242 |
|
| 243 |
data->uncore_attr_group.name = name; |
| 244 |
data->uncore_attr_group.attrs = data->uncore_attrs; |
| 245 |
ret = sysfs_create_group(uncore_root_kobj, &data->uncore_attr_group); |
| 246 |
|
| 247 |
return ret; |
| 248 |
} |
| 249 |
|
| 250 |
static void delete_attr_group(struct uncore_data *data, char *name) |
| 251 |
{ |
| 252 |
sysfs_remove_group(uncore_root_kobj, &data->uncore_attr_group); |
| 253 |
} |
| 254 |
|
| 255 |
int uncore_freq_add_entry(struct uncore_data *data, int cpu) |
| 256 |
{ |
| 257 |
int ret = 0; |
| 258 |
|
| 259 |
mutex_lock(&uncore_lock); |
| 260 |
if (data->valid) { |
| 261 |
/* control cpu changed */ |
| 262 |
data->control_cpu = cpu; |
| 263 |
goto uncore_unlock; |
| 264 |
} |
| 265 |
|
| 266 |
if (data->domain_id != UNCORE_DOMAIN_ID_INVALID) { |
| 267 |
ret = ida_alloc(&intel_uncore_ida, GFP_KERNEL); |
| 268 |
if (ret < 0) |
| 269 |
goto uncore_unlock; |
| 270 |
|
| 271 |
data->instance_id = ret; |
| 272 |
scnprintf(data->name, sizeof(data->name), "uncore%02d", ret); |
| 273 |
} else { |
| 274 |
scnprintf(data->name, sizeof(data->name), "package_%02d_die_%02d", |
| 275 |
data->package_id, data->die_id); |
| 276 |
} |
| 277 |
|
| 278 |
uncore_read(data, &data->initial_min_freq_khz, UNCORE_INDEX_MIN_FREQ); |
| 279 |
uncore_read(data, &data->initial_max_freq_khz, UNCORE_INDEX_MAX_FREQ); |
| 280 |
|
| 281 |
ret = create_attr_group(data, data->name); |
| 282 |
if (ret) { |
| 283 |
if (data->domain_id != UNCORE_DOMAIN_ID_INVALID) |
| 284 |
ida_free(&intel_uncore_ida, data->instance_id); |
| 285 |
} else { |
| 286 |
data->control_cpu = cpu; |
| 287 |
data->valid = true; |
| 288 |
} |
| 289 |
|
| 290 |
uncore_unlock: |
| 291 |
mutex_unlock(&uncore_lock); |
| 292 |
|
| 293 |
return ret; |
| 294 |
} |
| 295 |
EXPORT_SYMBOL_NS_GPL(uncore_freq_add_entry, "INTEL_UNCORE_FREQUENCY"); |
| 296 |
|
| 297 |
void uncore_freq_remove_die_entry(struct uncore_data *data) |
| 298 |
{ |
| 299 |
mutex_lock(&uncore_lock); |
| 300 |
delete_attr_group(data, data->name); |
| 301 |
data->control_cpu = -1; |
| 302 |
data->valid = false; |
| 303 |
if (data->domain_id != UNCORE_DOMAIN_ID_INVALID) |
| 304 |
ida_free(&intel_uncore_ida, data->instance_id); |
| 305 |
|
| 306 |
mutex_unlock(&uncore_lock); |
| 307 |
} |
| 308 |
EXPORT_SYMBOL_NS_GPL(uncore_freq_remove_die_entry, "INTEL_UNCORE_FREQUENCY"); |
| 309 |
|
| 310 |
int uncore_freq_common_init(int (*read)(struct uncore_data *data, unsigned int *value, |
| 311 |
enum uncore_index index), |
| 312 |
int (*write)(struct uncore_data *data, unsigned int input, |
| 313 |
enum uncore_index index)) |
| 314 |
{ |
| 315 |
mutex_lock(&uncore_lock); |
| 316 |
|
| 317 |
uncore_read = read; |
| 318 |
uncore_write = write; |
| 319 |
|
| 320 |
if (!uncore_root_kobj) { |
| 321 |
struct device *dev_root = bus_get_dev_root(&cpu_subsys); |
| 322 |
|
| 323 |
if (dev_root) { |
| 324 |
uncore_root_kobj = kobject_create_and_add("intel_uncore_frequency", |
| 325 |
&dev_root->kobj); |
| 326 |
put_device(dev_root); |
| 327 |
} |
| 328 |
} |
| 329 |
if (uncore_root_kobj) |
| 330 |
++uncore_instance_count; |
| 331 |
mutex_unlock(&uncore_lock); |
| 332 |
|
| 333 |
return uncore_root_kobj ? 0 : -ENOMEM; |
| 334 |
} |
| 335 |
EXPORT_SYMBOL_NS_GPL(uncore_freq_common_init, "INTEL_UNCORE_FREQUENCY"); |
| 336 |
|
| 337 |
void uncore_freq_common_exit(void) |
| 338 |
{ |
| 339 |
mutex_lock(&uncore_lock); |
| 340 |
--uncore_instance_count; |
| 341 |
if (!uncore_instance_count) { |
| 342 |
kobject_put(uncore_root_kobj); |
| 343 |
uncore_root_kobj = NULL; |
| 344 |
} |
| 345 |
mutex_unlock(&uncore_lock); |
| 346 |
} |
| 347 |
EXPORT_SYMBOL_NS_GPL(uncore_freq_common_exit, "INTEL_UNCORE_FREQUENCY"); |
| 348 |
|
| 349 |
|
| 350 |
MODULE_LICENSE("GPL v2"); |
| 351 |
MODULE_DESCRIPTION("Intel Uncore Frequency Common Module"); |