arch_topology.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. static DEFINE_MUTEX(cpu_scale_mutex);
  24. static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
  25. unsigned long topology_get_cpu_scale(struct sched_domain *sd, int cpu)
  26. {
  27. return per_cpu(cpu_scale, cpu);
  28. }
  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 __init 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 __initdata;
  144. static void __init parsing_done_workfn(struct work_struct *work);
  145. static __initdata DECLARE_WORK(parsing_done_work, parsing_done_workfn);
  146. static int __init
  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 __initdata = {
  175. .notifier_call = init_cpu_capacity_callback,
  176. };
  177. static int __init register_cpufreq_notifier(void)
  178. {
  179. /*
  180. * on ACPI-based systems we need to use the default cpu capacity
  181. * until we have the necessary code to parse the cpu capacity, so
  182. * skip registering cpufreq notifier.
  183. */
  184. if (!acpi_disabled || !raw_capacity)
  185. return -EINVAL;
  186. if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
  187. pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
  188. return -ENOMEM;
  189. }
  190. cpumask_copy(cpus_to_visit, cpu_possible_mask);
  191. return cpufreq_register_notifier(&init_cpu_capacity_notifier,
  192. CPUFREQ_POLICY_NOTIFIER);
  193. }
  194. core_initcall(register_cpufreq_notifier);
  195. static void __init parsing_done_workfn(struct work_struct *work)
  196. {
  197. cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
  198. CPUFREQ_POLICY_NOTIFIER);
  199. }
  200. #else
  201. core_initcall(free_raw_capacity);
  202. #endif