arch_topology.c 6.5 KB

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