arch_topology.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Arch specific cpu topology information
  3. *
  4. * Copyright (C) 2016, ARM Ltd.
  5. * Written by: Juri Lelli, ARM Ltd.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. *
  11. * Released under the GPLv2 only.
  12. * SPDX-License-Identifier: GPL-2.0
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/arch_topology.h>
  16. #include <linux/cpu.h>
  17. #include <linux/cpufreq.h>
  18. #include <linux/device.h>
  19. #include <linux/of.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/sched/topology.h>
  23. DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
  24. void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
  25. unsigned long max_freq)
  26. {
  27. unsigned long scale;
  28. int i;
  29. scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
  30. for_each_cpu(i, cpus)
  31. per_cpu(freq_scale, i) = scale;
  32. }
  33. static DEFINE_MUTEX(cpu_scale_mutex);
  34. DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
  35. void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
  36. {
  37. per_cpu(cpu_scale, cpu) = capacity;
  38. }
  39. static ssize_t cpu_capacity_show(struct device *dev,
  40. struct device_attribute *attr,
  41. char *buf)
  42. {
  43. struct cpu *cpu = container_of(dev, struct cpu, dev);
  44. return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
  45. }
  46. static ssize_t cpu_capacity_store(struct device *dev,
  47. struct device_attribute *attr,
  48. const char *buf,
  49. size_t count)
  50. {
  51. struct cpu *cpu = container_of(dev, struct cpu, dev);
  52. int this_cpu = cpu->dev.id;
  53. int i;
  54. unsigned long new_capacity;
  55. ssize_t ret;
  56. if (!count)
  57. return 0;
  58. ret = kstrtoul(buf, 0, &new_capacity);
  59. if (ret)
  60. return ret;
  61. if (new_capacity > SCHED_CAPACITY_SCALE)
  62. return -EINVAL;
  63. mutex_lock(&cpu_scale_mutex);
  64. for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
  65. topology_set_cpu_scale(i, new_capacity);
  66. mutex_unlock(&cpu_scale_mutex);
  67. return count;
  68. }
  69. static DEVICE_ATTR_RW(cpu_capacity);
  70. static int register_cpu_capacity_sysctl(void)
  71. {
  72. int i;
  73. struct device *cpu;
  74. for_each_possible_cpu(i) {
  75. cpu = get_cpu_device(i);
  76. if (!cpu) {
  77. pr_err("%s: too early to get CPU%d device!\n",
  78. __func__, i);
  79. continue;
  80. }
  81. device_create_file(cpu, &dev_attr_cpu_capacity);
  82. }
  83. return 0;
  84. }
  85. subsys_initcall(register_cpu_capacity_sysctl);
  86. static u32 capacity_scale;
  87. static u32 *raw_capacity;
  88. static int free_raw_capacity(void)
  89. {
  90. kfree(raw_capacity);
  91. raw_capacity = NULL;
  92. return 0;
  93. }
  94. void topology_normalize_cpu_scale(void)
  95. {
  96. u64 capacity;
  97. int cpu;
  98. if (!raw_capacity)
  99. return;
  100. pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
  101. mutex_lock(&cpu_scale_mutex);
  102. for_each_possible_cpu(cpu) {
  103. pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
  104. cpu, raw_capacity[cpu]);
  105. capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
  106. / capacity_scale;
  107. topology_set_cpu_scale(cpu, capacity);
  108. pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
  109. cpu, topology_get_cpu_scale(NULL, cpu));
  110. }
  111. mutex_unlock(&cpu_scale_mutex);
  112. }
  113. bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
  114. {
  115. static bool cap_parsing_failed;
  116. int ret;
  117. u32 cpu_capacity;
  118. if (cap_parsing_failed)
  119. return false;
  120. ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
  121. &cpu_capacity);
  122. if (!ret) {
  123. if (!raw_capacity) {
  124. raw_capacity = kcalloc(num_possible_cpus(),
  125. sizeof(*raw_capacity),
  126. GFP_KERNEL);
  127. if (!raw_capacity) {
  128. pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
  129. cap_parsing_failed = true;
  130. return false;
  131. }
  132. }
  133. capacity_scale = max(cpu_capacity, capacity_scale);
  134. raw_capacity[cpu] = cpu_capacity;
  135. pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
  136. cpu_node, raw_capacity[cpu]);
  137. } else {
  138. if (raw_capacity) {
  139. pr_err("cpu_capacity: missing %pOF raw capacity\n",
  140. cpu_node);
  141. pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
  142. }
  143. cap_parsing_failed = true;
  144. free_raw_capacity();
  145. }
  146. return !ret;
  147. }
  148. #ifdef CONFIG_CPU_FREQ
  149. static cpumask_var_t cpus_to_visit __initdata;
  150. static void __init parsing_done_workfn(struct work_struct *work);
  151. static __initdata DECLARE_WORK(parsing_done_work, parsing_done_workfn);
  152. static int __init
  153. init_cpu_capacity_callback(struct notifier_block *nb,
  154. unsigned long val,
  155. void *data)
  156. {
  157. struct cpufreq_policy *policy = data;
  158. int cpu;
  159. if (!raw_capacity)
  160. return 0;
  161. if (val != CPUFREQ_NOTIFY)
  162. return 0;
  163. pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
  164. cpumask_pr_args(policy->related_cpus),
  165. cpumask_pr_args(cpus_to_visit));
  166. cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
  167. for_each_cpu(cpu, policy->related_cpus) {
  168. raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
  169. policy->cpuinfo.max_freq / 1000UL;
  170. capacity_scale = max(raw_capacity[cpu], capacity_scale);
  171. }
  172. if (cpumask_empty(cpus_to_visit)) {
  173. topology_normalize_cpu_scale();
  174. free_raw_capacity();
  175. pr_debug("cpu_capacity: parsing done\n");
  176. schedule_work(&parsing_done_work);
  177. }
  178. return 0;
  179. }
  180. static struct notifier_block init_cpu_capacity_notifier __initdata = {
  181. .notifier_call = init_cpu_capacity_callback,
  182. };
  183. static int __init register_cpufreq_notifier(void)
  184. {
  185. int ret;
  186. /*
  187. * on ACPI-based systems we need to use the default cpu capacity
  188. * until we have the necessary code to parse the cpu capacity, so
  189. * skip registering cpufreq notifier.
  190. */
  191. if (!acpi_disabled || !raw_capacity)
  192. return -EINVAL;
  193. if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
  194. pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
  195. return -ENOMEM;
  196. }
  197. cpumask_copy(cpus_to_visit, cpu_possible_mask);
  198. ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
  199. CPUFREQ_POLICY_NOTIFIER);
  200. if (ret)
  201. free_cpumask_var(cpus_to_visit);
  202. return ret;
  203. }
  204. core_initcall(register_cpufreq_notifier);
  205. static void __init parsing_done_workfn(struct work_struct *work)
  206. {
  207. cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
  208. CPUFREQ_POLICY_NOTIFIER);
  209. free_cpumask_var(cpus_to_visit);
  210. }
  211. #else
  212. core_initcall(free_raw_capacity);
  213. #endif