topology.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * arch/arm64/kernel/topology.c
  3. *
  4. * Copyright (C) 2011,2013,2014 Linaro Limited.
  5. *
  6. * Based on the arm32 version written by Vincent Guittot in turn based on
  7. * arch/sh/kernel/topology.c
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <linux/acpi.h>
  14. #include <linux/arch_topology.h>
  15. #include <linux/cacheinfo.h>
  16. #include <linux/cpufreq.h>
  17. #include <linux/init.h>
  18. #include <linux/percpu.h>
  19. #include <asm/cpu.h>
  20. #include <asm/cputype.h>
  21. #include <asm/topology.h>
  22. #ifdef CONFIG_ACPI
  23. static bool __init acpi_cpu_is_threaded(int cpu)
  24. {
  25. int is_threaded = acpi_pptt_cpu_is_thread(cpu);
  26. /*
  27. * if the PPTT doesn't have thread information, assume a homogeneous
  28. * machine and return the current CPU's thread state.
  29. */
  30. if (is_threaded < 0)
  31. is_threaded = read_cpuid_mpidr() & MPIDR_MT_BITMASK;
  32. return !!is_threaded;
  33. }
  34. /*
  35. * Propagate the topology information of the processor_topology_node tree to the
  36. * cpu_topology array.
  37. */
  38. int __init parse_acpi_topology(void)
  39. {
  40. int cpu, topology_id;
  41. if (acpi_disabled)
  42. return 0;
  43. for_each_possible_cpu(cpu) {
  44. topology_id = find_acpi_cpu_topology(cpu, 0);
  45. if (topology_id < 0)
  46. return topology_id;
  47. if (acpi_cpu_is_threaded(cpu)) {
  48. cpu_topology[cpu].thread_id = topology_id;
  49. topology_id = find_acpi_cpu_topology(cpu, 1);
  50. cpu_topology[cpu].core_id = topology_id;
  51. } else {
  52. cpu_topology[cpu].thread_id = -1;
  53. cpu_topology[cpu].core_id = topology_id;
  54. }
  55. topology_id = find_acpi_cpu_topology_cluster(cpu);
  56. cpu_topology[cpu].cluster_id = topology_id;
  57. topology_id = find_acpi_cpu_topology_package(cpu);
  58. cpu_topology[cpu].package_id = topology_id;
  59. }
  60. return 0;
  61. }
  62. #endif
  63. #ifdef CONFIG_ARM64_AMU_EXTN
  64. #define read_corecnt() read_sysreg_s(SYS_AMEVCNTR0_CORE_EL0)
  65. #define read_constcnt() read_sysreg_s(SYS_AMEVCNTR0_CONST_EL0)
  66. #else
  67. #define read_corecnt() (0UL)
  68. #define read_constcnt() (0UL)
  69. #endif
  70. #undef pr_fmt
  71. #define pr_fmt(fmt) "AMU: " fmt
  72. /*
  73. * Ensure that amu_scale_freq_tick() will return SCHED_CAPACITY_SCALE until
  74. * the CPU capacity and its associated frequency have been correctly
  75. * initialized.
  76. */
  77. static DEFINE_PER_CPU_READ_MOSTLY(unsigned long, arch_max_freq_scale) = 1UL << (2 * SCHED_CAPACITY_SHIFT);
  78. static DEFINE_PER_CPU(u64, arch_const_cycles_prev);
  79. static DEFINE_PER_CPU(u64, arch_core_cycles_prev);
  80. static cpumask_var_t amu_fie_cpus;
  81. void update_freq_counters_refs(void)
  82. {
  83. this_cpu_write(arch_core_cycles_prev, read_corecnt());
  84. this_cpu_write(arch_const_cycles_prev, read_constcnt());
  85. }
  86. static inline bool freq_counters_valid(int cpu)
  87. {
  88. if ((cpu >= nr_cpu_ids) || !cpumask_test_cpu(cpu, cpu_present_mask))
  89. return false;
  90. if (!cpu_has_amu_feat(cpu)) {
  91. pr_debug("CPU%d: counters are not supported.\n", cpu);
  92. return false;
  93. }
  94. if (unlikely(!per_cpu(arch_const_cycles_prev, cpu) ||
  95. !per_cpu(arch_core_cycles_prev, cpu))) {
  96. pr_debug("CPU%d: cycle counters are not enabled.\n", cpu);
  97. return false;
  98. }
  99. return true;
  100. }
  101. void freq_inv_set_max_ratio(int cpu, u64 max_rate)
  102. {
  103. u64 ratio, ref_rate = arch_timer_get_rate();
  104. if (unlikely(!max_rate || !ref_rate)) {
  105. WARN_ONCE(1, "CPU%d: invalid maximum or reference frequency.\n",
  106. cpu);
  107. return;
  108. }
  109. /*
  110. * Pre-compute the fixed ratio between the frequency of the constant
  111. * reference counter and the maximum frequency of the CPU.
  112. *
  113. * ref_rate
  114. * arch_max_freq_scale = ---------- * SCHED_CAPACITY_SCALE²
  115. * max_rate
  116. *
  117. * We use a factor of 2 * SCHED_CAPACITY_SHIFT -> SCHED_CAPACITY_SCALE²
  118. * in order to ensure a good resolution for arch_max_freq_scale for
  119. * very low reference frequencies (down to the KHz range which should
  120. * be unlikely).
  121. */
  122. ratio = ref_rate << (2 * SCHED_CAPACITY_SHIFT);
  123. ratio = div64_u64(ratio, max_rate);
  124. if (!ratio) {
  125. WARN_ONCE(1, "Reference frequency too low.\n");
  126. return;
  127. }
  128. WRITE_ONCE(per_cpu(arch_max_freq_scale, cpu), (unsigned long)ratio);
  129. }
  130. static void amu_scale_freq_tick(void)
  131. {
  132. u64 prev_core_cnt, prev_const_cnt;
  133. u64 core_cnt, const_cnt, scale;
  134. prev_const_cnt = this_cpu_read(arch_const_cycles_prev);
  135. prev_core_cnt = this_cpu_read(arch_core_cycles_prev);
  136. update_freq_counters_refs();
  137. const_cnt = this_cpu_read(arch_const_cycles_prev);
  138. core_cnt = this_cpu_read(arch_core_cycles_prev);
  139. if (unlikely(core_cnt <= prev_core_cnt ||
  140. const_cnt <= prev_const_cnt))
  141. return;
  142. /*
  143. * /\core arch_max_freq_scale
  144. * scale = ------- * --------------------
  145. * /\const SCHED_CAPACITY_SCALE
  146. *
  147. * See validate_cpu_freq_invariance_counters() for details on
  148. * arch_max_freq_scale and the use of SCHED_CAPACITY_SHIFT.
  149. */
  150. scale = core_cnt - prev_core_cnt;
  151. scale *= this_cpu_read(arch_max_freq_scale);
  152. scale = div64_u64(scale >> SCHED_CAPACITY_SHIFT,
  153. const_cnt - prev_const_cnt);
  154. scale = min_t(unsigned long, scale, SCHED_CAPACITY_SCALE);
  155. this_cpu_write(arch_freq_scale, (unsigned long)scale);
  156. }
  157. static struct scale_freq_data amu_sfd = {
  158. .source = SCALE_FREQ_SOURCE_ARCH,
  159. .set_freq_scale = amu_scale_freq_tick,
  160. };
  161. static void amu_fie_setup(const struct cpumask *cpus)
  162. {
  163. int cpu;
  164. /* We are already set since the last insmod of cpufreq driver */
  165. if (cpumask_available(amu_fie_cpus) &&
  166. unlikely(cpumask_subset(cpus, amu_fie_cpus)))
  167. return;
  168. for_each_cpu(cpu, cpus)
  169. if (!freq_counters_valid(cpu))
  170. return;
  171. if (!cpumask_available(amu_fie_cpus) &&
  172. !zalloc_cpumask_var(&amu_fie_cpus, GFP_KERNEL)) {
  173. WARN_ONCE(1, "Failed to allocate FIE cpumask for CPUs[%*pbl]\n",
  174. cpumask_pr_args(cpus));
  175. return;
  176. }
  177. cpumask_or(amu_fie_cpus, amu_fie_cpus, cpus);
  178. topology_set_scale_freq_source(&amu_sfd, amu_fie_cpus);
  179. pr_debug("CPUs[%*pbl]: counters will be used for FIE.",
  180. cpumask_pr_args(cpus));
  181. }
  182. static int init_amu_fie_callback(struct notifier_block *nb, unsigned long val,
  183. void *data)
  184. {
  185. struct cpufreq_policy *policy = data;
  186. if (val == CPUFREQ_CREATE_POLICY)
  187. amu_fie_setup(policy->related_cpus);
  188. /*
  189. * We don't need to handle CPUFREQ_REMOVE_POLICY event as the AMU
  190. * counters don't have any dependency on cpufreq driver once we have
  191. * initialized AMU support and enabled invariance. The AMU counters will
  192. * keep on working just fine in the absence of the cpufreq driver, and
  193. * for the CPUs for which there are no counters available, the last set
  194. * value of arch_freq_scale will remain valid as that is the frequency
  195. * those CPUs are running at.
  196. */
  197. return 0;
  198. }
  199. static struct notifier_block init_amu_fie_notifier = {
  200. .notifier_call = init_amu_fie_callback,
  201. };
  202. static int __init init_amu_fie(void)
  203. {
  204. return cpufreq_register_notifier(&init_amu_fie_notifier,
  205. CPUFREQ_POLICY_NOTIFIER);
  206. }
  207. core_initcall(init_amu_fie);
  208. #ifdef CONFIG_ACPI_CPPC_LIB
  209. #include <acpi/cppc_acpi.h>
  210. static void cpu_read_corecnt(void *val)
  211. {
  212. /*
  213. * A value of 0 can be returned if the current CPU does not support AMUs
  214. * or if the counter is disabled for this CPU. A return value of 0 at
  215. * counter read is properly handled as an error case by the users of the
  216. * counter.
  217. */
  218. *(u64 *)val = read_corecnt();
  219. }
  220. static void cpu_read_constcnt(void *val)
  221. {
  222. /*
  223. * Return 0 if the current CPU is affected by erratum 2457168. A value
  224. * of 0 is also returned if the current CPU does not support AMUs or if
  225. * the counter is disabled. A return value of 0 at counter read is
  226. * properly handled as an error case by the users of the counter.
  227. */
  228. *(u64 *)val = this_cpu_has_cap(ARM64_WORKAROUND_2457168) ?
  229. 0UL : read_constcnt();
  230. }
  231. static inline
  232. int counters_read_on_cpu(int cpu, smp_call_func_t func, u64 *val)
  233. {
  234. /*
  235. * Abort call on counterless CPU or when interrupts are
  236. * disabled - can lead to deadlock in smp sync call.
  237. */
  238. if (!cpu_has_amu_feat(cpu))
  239. return -EOPNOTSUPP;
  240. if (WARN_ON_ONCE(irqs_disabled()))
  241. return -EPERM;
  242. smp_call_function_single(cpu, func, val, 1);
  243. return 0;
  244. }
  245. /*
  246. * Refer to drivers/acpi/cppc_acpi.c for the description of the functions
  247. * below.
  248. */
  249. bool cpc_ffh_supported(void)
  250. {
  251. int cpu = get_cpu_with_amu_feat();
  252. /*
  253. * FFH is considered supported if there is at least one present CPU that
  254. * supports AMUs. Using FFH to read core and reference counters for CPUs
  255. * that do not support AMUs, have counters disabled or that are affected
  256. * by errata, will result in a return value of 0.
  257. *
  258. * This is done to allow any enabled and valid counters to be read
  259. * through FFH, knowing that potentially returning 0 as counter value is
  260. * properly handled by the users of these counters.
  261. */
  262. if ((cpu >= nr_cpu_ids) || !cpumask_test_cpu(cpu, cpu_present_mask))
  263. return false;
  264. return true;
  265. }
  266. int cpc_read_ffh(int cpu, struct cpc_reg *reg, u64 *val)
  267. {
  268. int ret = -EOPNOTSUPP;
  269. switch ((u64)reg->address) {
  270. case 0x0:
  271. ret = counters_read_on_cpu(cpu, cpu_read_corecnt, val);
  272. break;
  273. case 0x1:
  274. ret = counters_read_on_cpu(cpu, cpu_read_constcnt, val);
  275. break;
  276. }
  277. if (!ret) {
  278. *val &= GENMASK_ULL(reg->bit_offset + reg->bit_width - 1,
  279. reg->bit_offset);
  280. *val >>= reg->bit_offset;
  281. }
  282. return ret;
  283. }
  284. int cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
  285. {
  286. return -EOPNOTSUPP;
  287. }
  288. #endif /* CONFIG_ACPI_CPPC_LIB */