topology.c 9.0 KB

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