topology.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * arch/arm/kernel/topology.c
  3. *
  4. * Copyright (C) 2011 Linaro Limited.
  5. * Written by: Vincent Guittot
  6. *
  7. * based on 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/cpu.h>
  14. #include <linux/cpumask.h>
  15. #include <linux/export.h>
  16. #include <linux/init.h>
  17. #include <linux/percpu.h>
  18. #include <linux/node.h>
  19. #include <linux/nodemask.h>
  20. #include <linux/of.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/sched_energy.h>
  24. #include <asm/cputype.h>
  25. #include <asm/topology.h>
  26. /*
  27. * cpu capacity scale management
  28. */
  29. /*
  30. * cpu capacity table
  31. * This per cpu data structure describes the relative capacity of each core.
  32. * On a heteregenous system, cores don't have the same computation capacity
  33. * and we reflect that difference in the cpu_capacity field so the scheduler
  34. * can take this difference into account during load balance. A per cpu
  35. * structure is preferred because each CPU updates its own cpu_capacity field
  36. * during the load balance except for idle cores. One idle core is selected
  37. * to run the rebalance_domains for all idle cores and the cpu_capacity can be
  38. * updated during this sequence.
  39. */
  40. static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
  41. #include "topology_dts.c"
  42. unsigned long scale_cpu_capacity(struct sched_domain *sd, int cpu)
  43. {
  44. return per_cpu(cpu_scale, cpu);
  45. }
  46. static void set_capacity_scale(unsigned int cpu, unsigned long capacity)
  47. {
  48. per_cpu(cpu_scale, cpu) = capacity;
  49. }
  50. #ifdef CONFIG_OF
  51. /*
  52. * Look for a customed capacity of a CPU in the cpu_capacity table during the
  53. * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
  54. * function returns directly for SMP system.
  55. */
  56. static void update_cpu_capacity(unsigned int cpu)
  57. {
  58. unsigned long capacity = SCHED_CAPACITY_SCALE;
  59. if (cpu_core_energy(cpu)) {
  60. int max_cap_idx = cpu_core_energy(cpu)->nr_cap_states - 1;
  61. capacity = cpu_core_energy(cpu)->cap_states[max_cap_idx].cap;
  62. }
  63. set_capacity_scale(cpu, capacity);
  64. pr_info("CPU%u: update cpu_capacity %lu\n",
  65. cpu, arch_scale_cpu_capacity(NULL, cpu));
  66. }
  67. #else
  68. static inline void parse_dt_topology(void) {}
  69. static inline void update_cpu_capacity(unsigned int cpuid) {}
  70. #endif
  71. /*
  72. * cpu topology table
  73. */
  74. struct cputopo_arm cpu_topology[NR_CPUS];
  75. EXPORT_SYMBOL_GPL(cpu_topology);
  76. const struct cpumask *cpu_coregroup_mask(int cpu)
  77. {
  78. return &cpu_topology[cpu].core_sibling;
  79. }
  80. /*
  81. * The current assumption is that we can power gate each core independently.
  82. * This will be superseded by DT binding once available.
  83. */
  84. const struct cpumask *cpu_corepower_mask(int cpu)
  85. {
  86. return &cpu_topology[cpu].thread_sibling;
  87. }
  88. static void update_siblings_masks(unsigned int cpuid)
  89. {
  90. struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
  91. int cpu;
  92. /* update core and thread sibling masks */
  93. for_each_possible_cpu(cpu) {
  94. cpu_topo = &cpu_topology[cpu];
  95. if (cpuid_topo->socket_id != cpu_topo->socket_id)
  96. continue;
  97. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  98. if (cpu != cpuid)
  99. cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
  100. if (cpuid_topo->core_id != cpu_topo->core_id)
  101. continue;
  102. cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
  103. if (cpu != cpuid)
  104. cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
  105. }
  106. smp_wmb();
  107. }
  108. /*
  109. * store_cpu_topology is called at boot when only one cpu is running
  110. * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
  111. * which prevents simultaneous write access to cpu_topology array
  112. */
  113. void store_cpu_topology(unsigned int cpuid)
  114. {
  115. struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
  116. unsigned int mpidr;
  117. /* If the cpu topology has been already set, just return */
  118. if (cpuid_topo->core_id != -1)
  119. return;
  120. mpidr = read_cpuid_mpidr();
  121. /* create cpu topology mapping */
  122. if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
  123. /*
  124. * This is a multiprocessor system
  125. * multiprocessor format & multiprocessor mode field are set
  126. */
  127. if (mpidr & MPIDR_MT_BITMASK) {
  128. /* core performance interdependency */
  129. cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  130. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  131. cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
  132. } else {
  133. /* largely independent cores */
  134. cpuid_topo->thread_id = -1;
  135. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  136. cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  137. }
  138. } else {
  139. /*
  140. * This is an uniprocessor system
  141. * we are in multiprocessor format but uniprocessor system
  142. * or in the old uniprocessor format
  143. */
  144. cpuid_topo->thread_id = -1;
  145. cpuid_topo->core_id = 0;
  146. cpuid_topo->socket_id = -1;
  147. }
  148. update_siblings_masks(cpuid);
  149. update_cpu_capacity(cpuid);
  150. pr_info("CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
  151. cpuid, cpu_topology[cpuid].thread_id,
  152. cpu_topology[cpuid].core_id,
  153. cpu_topology[cpuid].socket_id, mpidr);
  154. }
  155. /*
  156. * ARM TC2 specific energy cost model data. There are no unit requirements for
  157. * the data. Data can be normalized to any reference point, but the
  158. * normalization must be consistent. That is, one bogo-joule/watt must be the
  159. * same quantity for all data, but we don't care what it is.
  160. */
  161. #ifndef CONFIG_MTK_UNIFY_POWER
  162. static struct idle_state idle_states_cluster_a7[] = {
  163. { .power = 25 }, /* arch_cpu_idle() (active idle) = WFI */
  164. { .power = 25 }, /* WFI */
  165. { .power = 10 }, /* cluster-sleep-l */
  166. };
  167. static struct idle_state idle_states_cluster_a15[] = {
  168. { .power = 70 }, /* arch_cpu_idle() (active idle) = WFI */
  169. { .power = 70 }, /* WFI */
  170. { .power = 25 }, /* cluster-sleep-b */
  171. };
  172. static struct capacity_state cap_states_cluster_a7[] = {
  173. /* Cluster only power */
  174. { .cap = 150, .power = 2967, }, /* 350 MHz */
  175. { .cap = 172, .power = 2792, }, /* 400 MHz */
  176. { .cap = 215, .power = 2810, }, /* 500 MHz */
  177. { .cap = 258, .power = 2815, }, /* 600 MHz */
  178. { .cap = 301, .power = 2919, }, /* 700 MHz */
  179. { .cap = 344, .power = 2847, }, /* 800 MHz */
  180. { .cap = 387, .power = 3917, }, /* 900 MHz */
  181. { .cap = 430, .power = 4905, }, /* 1000 MHz */
  182. };
  183. static struct capacity_state cap_states_cluster_a15[] = {
  184. /* Cluster only power */
  185. { .cap = 426, .power = 7920, }, /* 500 MHz */
  186. { .cap = 512, .power = 8165, }, /* 600 MHz */
  187. { .cap = 597, .power = 8172, }, /* 700 MHz */
  188. { .cap = 682, .power = 8195, }, /* 800 MHz */
  189. { .cap = 768, .power = 8265, }, /* 900 MHz */
  190. { .cap = 853, .power = 8446, }, /* 1000 MHz */
  191. { .cap = 938, .power = 11426, }, /* 1100 MHz */
  192. { .cap = 1024, .power = 15200, }, /* 1200 MHz */
  193. };
  194. static struct sched_group_energy energy_cluster_a7 = {
  195. .nr_idle_states = ARRAY_SIZE(idle_states_cluster_a7),
  196. .idle_states = idle_states_cluster_a7,
  197. .nr_cap_states = ARRAY_SIZE(cap_states_cluster_a7),
  198. .cap_states = cap_states_cluster_a7,
  199. };
  200. static struct sched_group_energy energy_cluster_a15 = {
  201. .nr_idle_states = ARRAY_SIZE(idle_states_cluster_a15),
  202. .idle_states = idle_states_cluster_a15,
  203. .nr_cap_states = ARRAY_SIZE(cap_states_cluster_a15),
  204. .cap_states = cap_states_cluster_a15,
  205. };
  206. static struct idle_state idle_states_core_a7[] = {
  207. { .power = 0 }, /* arch_cpu_idle (active idle) = WFI */
  208. { .power = 0 }, /* WFI */
  209. { .power = 0 }, /* cluster-sleep-l */
  210. };
  211. static struct idle_state idle_states_core_a15[] = {
  212. { .power = 0 }, /* arch_cpu_idle (active idle) = WFI */
  213. { .power = 0 }, /* WFI */
  214. { .power = 0 }, /* cluster-sleep-b */
  215. };
  216. static struct capacity_state cap_states_core_a7[] = {
  217. /* Power per cpu */
  218. { .cap = 150, .power = 187, }, /* 350 MHz */
  219. { .cap = 172, .power = 275, }, /* 400 MHz */
  220. { .cap = 215, .power = 334, }, /* 500 MHz */
  221. { .cap = 258, .power = 407, }, /* 600 MHz */
  222. { .cap = 301, .power = 447, }, /* 700 MHz */
  223. { .cap = 344, .power = 549, }, /* 800 MHz */
  224. { .cap = 387, .power = 761, }, /* 900 MHz */
  225. { .cap = 430, .power = 1024, }, /* 1000 MHz */
  226. };
  227. static struct capacity_state cap_states_core_a15[] = {
  228. /* Power per cpu */
  229. { .cap = 426, .power = 2021, }, /* 500 MHz */
  230. { .cap = 512, .power = 2312, }, /* 600 MHz */
  231. { .cap = 597, .power = 2756, }, /* 700 MHz */
  232. { .cap = 682, .power = 3125, }, /* 800 MHz */
  233. { .cap = 768, .power = 3524, }, /* 900 MHz */
  234. { .cap = 853, .power = 3846, }, /* 1000 MHz */
  235. { .cap = 938, .power = 5177, }, /* 1100 MHz */
  236. { .cap = 1024, .power = 6997, }, /* 1200 MHz */
  237. };
  238. static struct sched_group_energy energy_core_a7 = {
  239. .nr_idle_states = ARRAY_SIZE(idle_states_core_a7),
  240. .idle_states = idle_states_core_a7,
  241. .nr_cap_states = ARRAY_SIZE(cap_states_core_a7),
  242. .cap_states = cap_states_core_a7,
  243. };
  244. static struct sched_group_energy energy_core_a15 = {
  245. .nr_idle_states = ARRAY_SIZE(idle_states_core_a15),
  246. .idle_states = idle_states_core_a15,
  247. .nr_cap_states = ARRAY_SIZE(cap_states_core_a15),
  248. .cap_states = cap_states_core_a15,
  249. };
  250. #endif
  251. /* sd energy functions */
  252. inline
  253. const struct sched_group_energy * const cpu_cluster_energy(int cpu)
  254. {
  255. #ifndef CONFIG_MTK_UNIFY_POWER
  256. return cpu_topology[cpu].socket_id ? &energy_cluster_a7 :
  257. &energy_cluster_a15;
  258. #else
  259. struct sched_group_energy *sge = sge_array[cpu][SD_LEVEL1];
  260. int cluster_id = cpu_topology[cpu].socket_id;
  261. struct upower_tbl_info **addr_ptr_tbl_info;
  262. struct upower_tbl_info *ptr_tbl_info;
  263. struct upower_tbl *ptr_tbl;
  264. if (!sge) {
  265. pr_warn("Invalid sched_group_energy for Cluster%d\n", cpu);
  266. return NULL;
  267. }
  268. addr_ptr_tbl_info = upower_get_tbl();
  269. ptr_tbl_info = *addr_ptr_tbl_info;
  270. ptr_tbl = ptr_tbl_info[UPOWER_BANK_CLS_BASE+cluster_id].p_upower_tbl;
  271. sge->nr_cap_states = ptr_tbl->row_num;
  272. sge->cap_states = ptr_tbl->row;
  273. sge->lkg_idx = ptr_tbl->lkg_idx;
  274. return sge;
  275. #endif
  276. }
  277. inline
  278. const struct sched_group_energy * const cpu_core_energy(int cpu)
  279. {
  280. #ifndef CONFIG_MTK_UNIFY_POWER
  281. return cpu_topology[cpu].socket_id ? &energy_core_a7 :
  282. &energy_core_a15;
  283. #else
  284. struct sched_group_energy *sge = sge_array[cpu][SD_LEVEL0];
  285. struct upower_tbl *ptr_tbl;
  286. if (!sge) {
  287. pr_warn("Invalid sched_group_energy for CPU%d\n", cpu);
  288. return NULL;
  289. }
  290. ptr_tbl = upower_get_core_tbl(cpu);
  291. sge->nr_cap_states = ptr_tbl->row_num;
  292. sge->cap_states = ptr_tbl->row;
  293. sge->lkg_idx = ptr_tbl->lkg_idx;
  294. return sge;
  295. #endif
  296. }
  297. static inline int cpu_corepower_flags(void)
  298. {
  299. return SD_SHARE_PKG_RESOURCES | SD_SHARE_POWERDOMAIN | \
  300. SD_SHARE_CAP_STATES;
  301. }
  302. static struct sched_domain_topology_level arm_topology[] = {
  303. #ifdef CONFIG_SCHED_MC
  304. { cpu_coregroup_mask, cpu_corepower_flags, cpu_core_energy, SD_INIT_NAME(MC) },
  305. #endif
  306. { cpu_cpu_mask, NULL, cpu_cluster_energy, SD_INIT_NAME(DIE) },
  307. { NULL, },
  308. };
  309. /*
  310. * init_cpu_topology is called at boot when only one cpu is running
  311. * which prevent simultaneous write access to cpu_topology array
  312. */
  313. void __init init_cpu_topology(void)
  314. {
  315. unsigned int cpu;
  316. if (cpu_topology_init)
  317. return;
  318. /* init core mask and capacity */
  319. for_each_possible_cpu(cpu) {
  320. struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
  321. cpu_topo->thread_id = -1;
  322. cpu_topo->core_id = -1;
  323. cpu_topo->socket_id = -1;
  324. cpumask_clear(&cpu_topo->core_sibling);
  325. cpumask_clear(&cpu_topo->thread_sibling);
  326. }
  327. smp_wmb();
  328. parse_dt_topology();
  329. /* Set scheduler topology descriptor */
  330. set_sched_topology(arm_topology);
  331. parse_dt_cpu_capacity();
  332. init_sched_energy_costs();
  333. }
  334. #ifdef CONFIG_MTK_UNIFY_POWER
  335. static int
  336. cpu_capacity_sync(void)
  337. {
  338. int cpu;
  339. for_each_possible_cpu(cpu)
  340. update_cpu_capacity(cpu);
  341. return 0;
  342. }
  343. late_initcall_sync(cpu_capacity_sync)
  344. #endif