arch_topology.c 5.8 KB

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