intel-uncore-frequency.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel Uncore Frequency Setting
  4. * Copyright (c) 2019, Intel Corporation.
  5. * All rights reserved.
  6. *
  7. * Provide interface to set MSR 620 at a granularity of per die. On CPU online,
  8. * one control CPU is identified per die to read/write limit. This control CPU
  9. * is changed, if the CPU state is changed to offline. When the last CPU is
  10. * offline in a die then remove the sysfs object for that die.
  11. * The majority of actual code is related to sysfs create and read/write
  12. * attributes.
  13. *
  14. * Author: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
  15. */
  16. #include <linux/cpu.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/suspend.h>
  20. #include <asm/cpu_device_id.h>
  21. #include <asm/intel-family.h>
  22. #define MSR_UNCORE_RATIO_LIMIT 0x620
  23. #define UNCORE_FREQ_KHZ_MULTIPLIER 100000
  24. /**
  25. * struct uncore_data - Encapsulate all uncore data
  26. * @stored_uncore_data: Last user changed MSR 620 value, which will be restored
  27. * on system resume.
  28. * @initial_min_freq_khz: Sampled minimum uncore frequency at driver init
  29. * @initial_max_freq_khz: Sampled maximum uncore frequency at driver init
  30. * @control_cpu: Designated CPU for a die to read/write
  31. * @valid: Mark the data valid/invalid
  32. *
  33. * This structure is used to encapsulate all data related to uncore sysfs
  34. * settings for a die/package.
  35. */
  36. struct uncore_data {
  37. struct kobject kobj;
  38. struct completion kobj_unregister;
  39. u64 stored_uncore_data;
  40. u32 initial_min_freq_khz;
  41. u32 initial_max_freq_khz;
  42. int control_cpu;
  43. bool valid;
  44. };
  45. #define to_uncore_data(a) container_of(a, struct uncore_data, kobj)
  46. /* Max instances for uncore data, one for each die */
  47. static int uncore_max_entries __read_mostly;
  48. /* Storage for uncore data for all instances */
  49. static struct uncore_data *uncore_instances;
  50. /* Root of the all uncore sysfs kobjs */
  51. static struct kobject *uncore_root_kobj;
  52. /* Stores the CPU mask of the target CPUs to use during uncore read/write */
  53. static cpumask_t uncore_cpu_mask;
  54. /* CPU online callback register instance */
  55. static enum cpuhp_state uncore_hp_state __read_mostly;
  56. /* Mutex to control all mutual exclusions */
  57. static DEFINE_MUTEX(uncore_lock);
  58. struct uncore_attr {
  59. struct attribute attr;
  60. ssize_t (*show)(struct kobject *kobj,
  61. struct attribute *attr, char *buf);
  62. ssize_t (*store)(struct kobject *kobj,
  63. struct attribute *attr, const char *c, ssize_t count);
  64. };
  65. #define define_one_uncore_ro(_name) \
  66. static struct uncore_attr _name = \
  67. __ATTR(_name, 0444, show_##_name, NULL)
  68. #define define_one_uncore_rw(_name) \
  69. static struct uncore_attr _name = \
  70. __ATTR(_name, 0644, show_##_name, store_##_name)
  71. #define show_uncore_data(member_name) \
  72. static ssize_t show_##member_name(struct kobject *kobj, \
  73. struct attribute *attr, \
  74. char *buf) \
  75. { \
  76. struct uncore_data *data = to_uncore_data(kobj); \
  77. return scnprintf(buf, PAGE_SIZE, "%u\n", \
  78. data->member_name); \
  79. } \
  80. define_one_uncore_ro(member_name)
  81. show_uncore_data(initial_min_freq_khz);
  82. show_uncore_data(initial_max_freq_khz);
  83. /* Common function to read MSR 0x620 and read min/max */
  84. static int uncore_read_ratio(struct uncore_data *data, unsigned int *min,
  85. unsigned int *max)
  86. {
  87. u64 cap;
  88. int ret;
  89. if (data->control_cpu < 0)
  90. return -ENXIO;
  91. ret = rdmsrl_on_cpu(data->control_cpu, MSR_UNCORE_RATIO_LIMIT, &cap);
  92. if (ret)
  93. return ret;
  94. *max = (cap & 0x7F) * UNCORE_FREQ_KHZ_MULTIPLIER;
  95. *min = ((cap & GENMASK(14, 8)) >> 8) * UNCORE_FREQ_KHZ_MULTIPLIER;
  96. return 0;
  97. }
  98. /* Common function to set min/max ratios to be used by sysfs callbacks */
  99. static int uncore_write_ratio(struct uncore_data *data, unsigned int input,
  100. int set_max)
  101. {
  102. int ret;
  103. u64 cap;
  104. mutex_lock(&uncore_lock);
  105. if (data->control_cpu < 0) {
  106. ret = -ENXIO;
  107. goto finish_write;
  108. }
  109. input /= UNCORE_FREQ_KHZ_MULTIPLIER;
  110. if (!input || input > 0x7F) {
  111. ret = -EINVAL;
  112. goto finish_write;
  113. }
  114. ret = rdmsrl_on_cpu(data->control_cpu, MSR_UNCORE_RATIO_LIMIT, &cap);
  115. if (ret)
  116. goto finish_write;
  117. if (set_max) {
  118. cap &= ~0x7F;
  119. cap |= input;
  120. } else {
  121. cap &= ~GENMASK(14, 8);
  122. cap |= (input << 8);
  123. }
  124. ret = wrmsrl_on_cpu(data->control_cpu, MSR_UNCORE_RATIO_LIMIT, cap);
  125. if (ret)
  126. goto finish_write;
  127. data->stored_uncore_data = cap;
  128. finish_write:
  129. mutex_unlock(&uncore_lock);
  130. return ret;
  131. }
  132. static ssize_t store_min_max_freq_khz(struct kobject *kobj,
  133. struct attribute *attr,
  134. const char *buf, ssize_t count,
  135. int min_max)
  136. {
  137. struct uncore_data *data = to_uncore_data(kobj);
  138. unsigned int input;
  139. if (kstrtouint(buf, 10, &input))
  140. return -EINVAL;
  141. uncore_write_ratio(data, input, min_max);
  142. return count;
  143. }
  144. static ssize_t show_min_max_freq_khz(struct kobject *kobj,
  145. struct attribute *attr,
  146. char *buf, int min_max)
  147. {
  148. struct uncore_data *data = to_uncore_data(kobj);
  149. unsigned int min, max;
  150. int ret;
  151. mutex_lock(&uncore_lock);
  152. ret = uncore_read_ratio(data, &min, &max);
  153. mutex_unlock(&uncore_lock);
  154. if (ret)
  155. return ret;
  156. if (min_max)
  157. return sprintf(buf, "%u\n", max);
  158. return sprintf(buf, "%u\n", min);
  159. }
  160. #define store_uncore_min_max(name, min_max) \
  161. static ssize_t store_##name(struct kobject *kobj, \
  162. struct attribute *attr, \
  163. const char *buf, ssize_t count) \
  164. { \
  165. \
  166. return store_min_max_freq_khz(kobj, attr, buf, count, \
  167. min_max); \
  168. }
  169. #define show_uncore_min_max(name, min_max) \
  170. static ssize_t show_##name(struct kobject *kobj, \
  171. struct attribute *attr, char *buf) \
  172. { \
  173. \
  174. return show_min_max_freq_khz(kobj, attr, buf, min_max); \
  175. }
  176. store_uncore_min_max(min_freq_khz, 0);
  177. store_uncore_min_max(max_freq_khz, 1);
  178. show_uncore_min_max(min_freq_khz, 0);
  179. show_uncore_min_max(max_freq_khz, 1);
  180. define_one_uncore_rw(min_freq_khz);
  181. define_one_uncore_rw(max_freq_khz);
  182. static struct attribute *uncore_attrs[] = {
  183. &initial_min_freq_khz.attr,
  184. &initial_max_freq_khz.attr,
  185. &max_freq_khz.attr,
  186. &min_freq_khz.attr,
  187. NULL
  188. };
  189. static void uncore_sysfs_entry_release(struct kobject *kobj)
  190. {
  191. struct uncore_data *data = to_uncore_data(kobj);
  192. complete(&data->kobj_unregister);
  193. }
  194. static struct kobj_type uncore_ktype = {
  195. .release = uncore_sysfs_entry_release,
  196. .sysfs_ops = &kobj_sysfs_ops,
  197. .default_attrs = uncore_attrs,
  198. };
  199. /* Caller provides protection */
  200. static struct uncore_data *uncore_get_instance(unsigned int cpu)
  201. {
  202. int id = topology_logical_die_id(cpu);
  203. if (id >= 0 && id < uncore_max_entries)
  204. return &uncore_instances[id];
  205. return NULL;
  206. }
  207. static void uncore_add_die_entry(int cpu)
  208. {
  209. struct uncore_data *data;
  210. mutex_lock(&uncore_lock);
  211. data = uncore_get_instance(cpu);
  212. if (!data) {
  213. mutex_unlock(&uncore_lock);
  214. return;
  215. }
  216. if (data->valid) {
  217. /* control cpu changed */
  218. data->control_cpu = cpu;
  219. } else {
  220. char str[64];
  221. int ret;
  222. memset(data, 0, sizeof(*data));
  223. sprintf(str, "package_%02d_die_%02d",
  224. topology_physical_package_id(cpu),
  225. topology_die_id(cpu));
  226. uncore_read_ratio(data, &data->initial_min_freq_khz,
  227. &data->initial_max_freq_khz);
  228. init_completion(&data->kobj_unregister);
  229. ret = kobject_init_and_add(&data->kobj, &uncore_ktype,
  230. uncore_root_kobj, str);
  231. if (!ret) {
  232. data->control_cpu = cpu;
  233. data->valid = true;
  234. }
  235. }
  236. mutex_unlock(&uncore_lock);
  237. }
  238. /* Last CPU in this die is offline, make control cpu invalid */
  239. static void uncore_remove_die_entry(int cpu)
  240. {
  241. struct uncore_data *data;
  242. mutex_lock(&uncore_lock);
  243. data = uncore_get_instance(cpu);
  244. if (data)
  245. data->control_cpu = -1;
  246. mutex_unlock(&uncore_lock);
  247. }
  248. static int uncore_event_cpu_online(unsigned int cpu)
  249. {
  250. int target;
  251. /* Check if there is an online cpu in the package for uncore MSR */
  252. target = cpumask_any_and(&uncore_cpu_mask, topology_die_cpumask(cpu));
  253. if (target < nr_cpu_ids)
  254. return 0;
  255. /* Use this CPU on this die as a control CPU */
  256. cpumask_set_cpu(cpu, &uncore_cpu_mask);
  257. uncore_add_die_entry(cpu);
  258. return 0;
  259. }
  260. static int uncore_event_cpu_offline(unsigned int cpu)
  261. {
  262. int target;
  263. /* Check if existing cpu is used for uncore MSRs */
  264. if (!cpumask_test_and_clear_cpu(cpu, &uncore_cpu_mask))
  265. return 0;
  266. /* Find a new cpu to set uncore MSR */
  267. target = cpumask_any_but(topology_die_cpumask(cpu), cpu);
  268. if (target < nr_cpu_ids) {
  269. cpumask_set_cpu(target, &uncore_cpu_mask);
  270. uncore_add_die_entry(target);
  271. } else {
  272. uncore_remove_die_entry(cpu);
  273. }
  274. return 0;
  275. }
  276. static int uncore_pm_notify(struct notifier_block *nb, unsigned long mode,
  277. void *_unused)
  278. {
  279. int cpu;
  280. switch (mode) {
  281. case PM_POST_HIBERNATION:
  282. case PM_POST_RESTORE:
  283. case PM_POST_SUSPEND:
  284. for_each_cpu(cpu, &uncore_cpu_mask) {
  285. struct uncore_data *data;
  286. int ret;
  287. data = uncore_get_instance(cpu);
  288. if (!data || !data->valid || !data->stored_uncore_data)
  289. continue;
  290. ret = wrmsrl_on_cpu(cpu, MSR_UNCORE_RATIO_LIMIT,
  291. data->stored_uncore_data);
  292. if (ret)
  293. return ret;
  294. }
  295. break;
  296. default:
  297. break;
  298. }
  299. return 0;
  300. }
  301. static struct notifier_block uncore_pm_nb = {
  302. .notifier_call = uncore_pm_notify,
  303. };
  304. static const struct x86_cpu_id intel_uncore_cpu_ids[] = {
  305. X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_G, NULL),
  306. X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, NULL),
  307. X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_D, NULL),
  308. X86_MATCH_INTEL_FAM6_MODEL(SKYLAKE_X, NULL),
  309. X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, NULL),
  310. X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, NULL),
  311. {}
  312. };
  313. static int __init intel_uncore_init(void)
  314. {
  315. const struct x86_cpu_id *id;
  316. int ret;
  317. id = x86_match_cpu(intel_uncore_cpu_ids);
  318. if (!id)
  319. return -ENODEV;
  320. uncore_max_entries = topology_max_packages() *
  321. topology_max_die_per_package();
  322. uncore_instances = kcalloc(uncore_max_entries,
  323. sizeof(*uncore_instances), GFP_KERNEL);
  324. if (!uncore_instances)
  325. return -ENOMEM;
  326. uncore_root_kobj = kobject_create_and_add("intel_uncore_frequency",
  327. &cpu_subsys.dev_root->kobj);
  328. if (!uncore_root_kobj) {
  329. ret = -ENOMEM;
  330. goto err_free;
  331. }
  332. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  333. "platform/x86/uncore-freq:online",
  334. uncore_event_cpu_online,
  335. uncore_event_cpu_offline);
  336. if (ret < 0)
  337. goto err_rem_kobj;
  338. uncore_hp_state = ret;
  339. ret = register_pm_notifier(&uncore_pm_nb);
  340. if (ret)
  341. goto err_rem_state;
  342. return 0;
  343. err_rem_state:
  344. cpuhp_remove_state(uncore_hp_state);
  345. err_rem_kobj:
  346. kobject_put(uncore_root_kobj);
  347. err_free:
  348. kfree(uncore_instances);
  349. return ret;
  350. }
  351. module_init(intel_uncore_init)
  352. static void __exit intel_uncore_exit(void)
  353. {
  354. int i;
  355. unregister_pm_notifier(&uncore_pm_nb);
  356. cpuhp_remove_state(uncore_hp_state);
  357. for (i = 0; i < uncore_max_entries; ++i) {
  358. if (uncore_instances[i].valid) {
  359. kobject_put(&uncore_instances[i].kobj);
  360. wait_for_completion(&uncore_instances[i].kobj_unregister);
  361. }
  362. }
  363. kobject_put(uncore_root_kobj);
  364. kfree(uncore_instances);
  365. }
  366. module_exit(intel_uncore_exit)
  367. MODULE_LICENSE("GPL v2");
  368. MODULE_DESCRIPTION("Intel Uncore Frequency Limits Driver");