cpumap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* cpumap.c: used for optimizing CPU assignment
  2. *
  3. * Copyright (C) 2009 Hong H. Pham <hong.pham@windriver.com>
  4. */
  5. #include <linux/export.h>
  6. #include <linux/slab.h>
  7. #include <linux/kernel.h>
  8. #include <linux/cpumask.h>
  9. #include <linux/spinlock.h>
  10. #include <asm/cpudata.h>
  11. #include "cpumap.h"
  12. enum {
  13. CPUINFO_LVL_ROOT = 0,
  14. CPUINFO_LVL_NODE,
  15. CPUINFO_LVL_CORE,
  16. CPUINFO_LVL_PROC,
  17. CPUINFO_LVL_MAX,
  18. };
  19. enum {
  20. ROVER_NO_OP = 0,
  21. /* Increment rover every time level is visited */
  22. ROVER_INC_ON_VISIT = 1 << 0,
  23. /* Increment parent's rover every time rover wraps around */
  24. ROVER_INC_PARENT_ON_LOOP = 1 << 1,
  25. };
  26. struct cpuinfo_node {
  27. int id;
  28. int level;
  29. int num_cpus; /* Number of CPUs in this hierarchy */
  30. int parent_index;
  31. int child_start; /* Array index of the first child node */
  32. int child_end; /* Array index of the last child node */
  33. int rover; /* Child node iterator */
  34. };
  35. struct cpuinfo_level {
  36. int start_index; /* Index of first node of a level in a cpuinfo tree */
  37. int end_index; /* Index of last node of a level in a cpuinfo tree */
  38. int num_nodes; /* Number of nodes in a level in a cpuinfo tree */
  39. };
  40. struct cpuinfo_tree {
  41. int total_nodes;
  42. /* Offsets into nodes[] for each level of the tree */
  43. struct cpuinfo_level level[CPUINFO_LVL_MAX];
  44. struct cpuinfo_node nodes[0];
  45. };
  46. static struct cpuinfo_tree *cpuinfo_tree;
  47. static u16 cpu_distribution_map[NR_CPUS];
  48. static DEFINE_SPINLOCK(cpu_map_lock);
  49. /* Niagara optimized cpuinfo tree traversal. */
  50. static const int niagara_iterate_method[] = {
  51. [CPUINFO_LVL_ROOT] = ROVER_NO_OP,
  52. /* Strands (or virtual CPUs) within a core may not run concurrently
  53. * on the Niagara, as instruction pipeline(s) are shared. Distribute
  54. * work to strands in different cores first for better concurrency.
  55. * Go to next NUMA node when all cores are used.
  56. */
  57. [CPUINFO_LVL_NODE] = ROVER_INC_ON_VISIT|ROVER_INC_PARENT_ON_LOOP,
  58. /* Strands are grouped together by proc_id in cpuinfo_sparc, i.e.
  59. * a proc_id represents an instruction pipeline. Distribute work to
  60. * strands in different proc_id groups if the core has multiple
  61. * instruction pipelines (e.g. the Niagara 2/2+ has two).
  62. */
  63. [CPUINFO_LVL_CORE] = ROVER_INC_ON_VISIT,
  64. /* Pick the next strand in the proc_id group. */
  65. [CPUINFO_LVL_PROC] = ROVER_INC_ON_VISIT,
  66. };
  67. /* Generic cpuinfo tree traversal. Distribute work round robin across NUMA
  68. * nodes.
  69. */
  70. static const int generic_iterate_method[] = {
  71. [CPUINFO_LVL_ROOT] = ROVER_INC_ON_VISIT,
  72. [CPUINFO_LVL_NODE] = ROVER_NO_OP,
  73. [CPUINFO_LVL_CORE] = ROVER_INC_PARENT_ON_LOOP,
  74. [CPUINFO_LVL_PROC] = ROVER_INC_ON_VISIT|ROVER_INC_PARENT_ON_LOOP,
  75. };
  76. static int cpuinfo_id(int cpu, int level)
  77. {
  78. int id;
  79. switch (level) {
  80. case CPUINFO_LVL_ROOT:
  81. id = 0;
  82. break;
  83. case CPUINFO_LVL_NODE:
  84. id = cpu_to_node(cpu);
  85. break;
  86. case CPUINFO_LVL_CORE:
  87. id = cpu_data(cpu).core_id;
  88. break;
  89. case CPUINFO_LVL_PROC:
  90. id = cpu_data(cpu).proc_id;
  91. break;
  92. default:
  93. id = -EINVAL;
  94. }
  95. return id;
  96. }
  97. /*
  98. * Enumerate the CPU information in __cpu_data to determine the start index,
  99. * end index, and number of nodes for each level in the cpuinfo tree. The
  100. * total number of cpuinfo nodes required to build the tree is returned.
  101. */
  102. static int enumerate_cpuinfo_nodes(struct cpuinfo_level *tree_level)
  103. {
  104. int prev_id[CPUINFO_LVL_MAX];
  105. int i, n, num_nodes;
  106. for (i = CPUINFO_LVL_ROOT; i < CPUINFO_LVL_MAX; i++) {
  107. struct cpuinfo_level *lv = &tree_level[i];
  108. prev_id[i] = -1;
  109. lv->start_index = lv->end_index = lv->num_nodes = 0;
  110. }
  111. num_nodes = 1; /* Include the root node */
  112. for (i = 0; i < num_possible_cpus(); i++) {
  113. if (!cpu_online(i))
  114. continue;
  115. n = cpuinfo_id(i, CPUINFO_LVL_NODE);
  116. if (n > prev_id[CPUINFO_LVL_NODE]) {
  117. tree_level[CPUINFO_LVL_NODE].num_nodes++;
  118. prev_id[CPUINFO_LVL_NODE] = n;
  119. num_nodes++;
  120. }
  121. n = cpuinfo_id(i, CPUINFO_LVL_CORE);
  122. if (n > prev_id[CPUINFO_LVL_CORE]) {
  123. tree_level[CPUINFO_LVL_CORE].num_nodes++;
  124. prev_id[CPUINFO_LVL_CORE] = n;
  125. num_nodes++;
  126. }
  127. n = cpuinfo_id(i, CPUINFO_LVL_PROC);
  128. if (n > prev_id[CPUINFO_LVL_PROC]) {
  129. tree_level[CPUINFO_LVL_PROC].num_nodes++;
  130. prev_id[CPUINFO_LVL_PROC] = n;
  131. num_nodes++;
  132. }
  133. }
  134. tree_level[CPUINFO_LVL_ROOT].num_nodes = 1;
  135. n = tree_level[CPUINFO_LVL_NODE].num_nodes;
  136. tree_level[CPUINFO_LVL_NODE].start_index = 1;
  137. tree_level[CPUINFO_LVL_NODE].end_index = n;
  138. n++;
  139. tree_level[CPUINFO_LVL_CORE].start_index = n;
  140. n += tree_level[CPUINFO_LVL_CORE].num_nodes;
  141. tree_level[CPUINFO_LVL_CORE].end_index = n - 1;
  142. tree_level[CPUINFO_LVL_PROC].start_index = n;
  143. n += tree_level[CPUINFO_LVL_PROC].num_nodes;
  144. tree_level[CPUINFO_LVL_PROC].end_index = n - 1;
  145. return num_nodes;
  146. }
  147. /* Build a tree representation of the CPU hierarchy using the per CPU
  148. * information in __cpu_data. Entries in __cpu_data[0..NR_CPUS] are
  149. * assumed to be sorted in ascending order based on node, core_id, and
  150. * proc_id (in order of significance).
  151. */
  152. static struct cpuinfo_tree *build_cpuinfo_tree(void)
  153. {
  154. struct cpuinfo_tree *new_tree;
  155. struct cpuinfo_node *node;
  156. struct cpuinfo_level tmp_level[CPUINFO_LVL_MAX];
  157. int num_cpus[CPUINFO_LVL_MAX];
  158. int level_rover[CPUINFO_LVL_MAX];
  159. int prev_id[CPUINFO_LVL_MAX];
  160. int n, id, cpu, prev_cpu, last_cpu, level;
  161. n = enumerate_cpuinfo_nodes(tmp_level);
  162. new_tree = kzalloc(sizeof(struct cpuinfo_tree) +
  163. (sizeof(struct cpuinfo_node) * n), GFP_ATOMIC);
  164. if (!new_tree)
  165. return NULL;
  166. new_tree->total_nodes = n;
  167. memcpy(&new_tree->level, tmp_level, sizeof(tmp_level));
  168. prev_cpu = cpu = cpumask_first(cpu_online_mask);
  169. /* Initialize all levels in the tree with the first CPU */
  170. for (level = CPUINFO_LVL_PROC; level >= CPUINFO_LVL_ROOT; level--) {
  171. n = new_tree->level[level].start_index;
  172. level_rover[level] = n;
  173. node = &new_tree->nodes[n];
  174. id = cpuinfo_id(cpu, level);
  175. if (unlikely(id < 0)) {
  176. kfree(new_tree);
  177. return NULL;
  178. }
  179. node->id = id;
  180. node->level = level;
  181. node->num_cpus = 1;
  182. node->parent_index = (level > CPUINFO_LVL_ROOT)
  183. ? new_tree->level[level - 1].start_index : -1;
  184. node->child_start = node->child_end = node->rover =
  185. (level == CPUINFO_LVL_PROC)
  186. ? cpu : new_tree->level[level + 1].start_index;
  187. prev_id[level] = node->id;
  188. num_cpus[level] = 1;
  189. }
  190. for (last_cpu = (num_possible_cpus() - 1); last_cpu >= 0; last_cpu--) {
  191. if (cpu_online(last_cpu))
  192. break;
  193. }
  194. while (++cpu <= last_cpu) {
  195. if (!cpu_online(cpu))
  196. continue;
  197. for (level = CPUINFO_LVL_PROC; level >= CPUINFO_LVL_ROOT;
  198. level--) {
  199. id = cpuinfo_id(cpu, level);
  200. if (unlikely(id < 0)) {
  201. kfree(new_tree);
  202. return NULL;
  203. }
  204. if ((id != prev_id[level]) || (cpu == last_cpu)) {
  205. prev_id[level] = id;
  206. node = &new_tree->nodes[level_rover[level]];
  207. node->num_cpus = num_cpus[level];
  208. num_cpus[level] = 1;
  209. if (cpu == last_cpu)
  210. node->num_cpus++;
  211. /* Connect tree node to parent */
  212. if (level == CPUINFO_LVL_ROOT)
  213. node->parent_index = -1;
  214. else
  215. node->parent_index =
  216. level_rover[level - 1];
  217. if (level == CPUINFO_LVL_PROC) {
  218. node->child_end =
  219. (cpu == last_cpu) ? cpu : prev_cpu;
  220. } else {
  221. node->child_end =
  222. level_rover[level + 1] - 1;
  223. }
  224. /* Initialize the next node in the same level */
  225. n = ++level_rover[level];
  226. if (n <= new_tree->level[level].end_index) {
  227. node = &new_tree->nodes[n];
  228. node->id = id;
  229. node->level = level;
  230. /* Connect node to child */
  231. node->child_start = node->child_end =
  232. node->rover =
  233. (level == CPUINFO_LVL_PROC)
  234. ? cpu : level_rover[level + 1];
  235. }
  236. } else
  237. num_cpus[level]++;
  238. }
  239. prev_cpu = cpu;
  240. }
  241. return new_tree;
  242. }
  243. static void increment_rover(struct cpuinfo_tree *t, int node_index,
  244. int root_index, const int *rover_inc_table)
  245. {
  246. struct cpuinfo_node *node = &t->nodes[node_index];
  247. int top_level, level;
  248. top_level = t->nodes[root_index].level;
  249. for (level = node->level; level >= top_level; level--) {
  250. node->rover++;
  251. if (node->rover <= node->child_end)
  252. return;
  253. node->rover = node->child_start;
  254. /* If parent's rover does not need to be adjusted, stop here. */
  255. if ((level == top_level) ||
  256. !(rover_inc_table[level] & ROVER_INC_PARENT_ON_LOOP))
  257. return;
  258. node = &t->nodes[node->parent_index];
  259. }
  260. }
  261. static int iterate_cpu(struct cpuinfo_tree *t, unsigned int root_index)
  262. {
  263. const int *rover_inc_table;
  264. int level, new_index, index = root_index;
  265. switch (sun4v_chip_type) {
  266. case SUN4V_CHIP_NIAGARA1:
  267. case SUN4V_CHIP_NIAGARA2:
  268. case SUN4V_CHIP_NIAGARA3:
  269. case SUN4V_CHIP_NIAGARA4:
  270. case SUN4V_CHIP_NIAGARA5:
  271. case SUN4V_CHIP_SPARC_M6:
  272. case SUN4V_CHIP_SPARC_M7:
  273. case SUN4V_CHIP_SPARC_SN:
  274. case SUN4V_CHIP_SPARC64X:
  275. rover_inc_table = niagara_iterate_method;
  276. break;
  277. default:
  278. rover_inc_table = generic_iterate_method;
  279. }
  280. for (level = t->nodes[root_index].level; level < CPUINFO_LVL_MAX;
  281. level++) {
  282. new_index = t->nodes[index].rover;
  283. if (rover_inc_table[level] & ROVER_INC_ON_VISIT)
  284. increment_rover(t, index, root_index, rover_inc_table);
  285. index = new_index;
  286. }
  287. return index;
  288. }
  289. static void _cpu_map_rebuild(void)
  290. {
  291. int i;
  292. if (cpuinfo_tree) {
  293. kfree(cpuinfo_tree);
  294. cpuinfo_tree = NULL;
  295. }
  296. cpuinfo_tree = build_cpuinfo_tree();
  297. if (!cpuinfo_tree)
  298. return;
  299. /* Build CPU distribution map that spans all online CPUs. No need
  300. * to check if the CPU is online, as that is done when the cpuinfo
  301. * tree is being built.
  302. */
  303. for (i = 0; i < cpuinfo_tree->nodes[0].num_cpus; i++)
  304. cpu_distribution_map[i] = iterate_cpu(cpuinfo_tree, 0);
  305. }
  306. /* Fallback if the cpuinfo tree could not be built. CPU mapping is linear
  307. * round robin.
  308. */
  309. static int simple_map_to_cpu(unsigned int index)
  310. {
  311. int i, end, cpu_rover;
  312. cpu_rover = 0;
  313. end = index % num_online_cpus();
  314. for (i = 0; i < num_possible_cpus(); i++) {
  315. if (cpu_online(cpu_rover)) {
  316. if (cpu_rover >= end)
  317. return cpu_rover;
  318. cpu_rover++;
  319. }
  320. }
  321. /* Impossible, since num_online_cpus() <= num_possible_cpus() */
  322. return cpumask_first(cpu_online_mask);
  323. }
  324. static int _map_to_cpu(unsigned int index)
  325. {
  326. struct cpuinfo_node *root_node;
  327. if (unlikely(!cpuinfo_tree)) {
  328. _cpu_map_rebuild();
  329. if (!cpuinfo_tree)
  330. return simple_map_to_cpu(index);
  331. }
  332. root_node = &cpuinfo_tree->nodes[0];
  333. #ifdef CONFIG_HOTPLUG_CPU
  334. if (unlikely(root_node->num_cpus != num_online_cpus())) {
  335. _cpu_map_rebuild();
  336. if (!cpuinfo_tree)
  337. return simple_map_to_cpu(index);
  338. }
  339. #endif
  340. return cpu_distribution_map[index % root_node->num_cpus];
  341. }
  342. int map_to_cpu(unsigned int index)
  343. {
  344. int mapped_cpu;
  345. unsigned long flag;
  346. spin_lock_irqsave(&cpu_map_lock, flag);
  347. mapped_cpu = _map_to_cpu(index);
  348. #ifdef CONFIG_HOTPLUG_CPU
  349. while (unlikely(!cpu_online(mapped_cpu)))
  350. mapped_cpu = _map_to_cpu(index);
  351. #endif
  352. spin_unlock_irqrestore(&cpu_map_lock, flag);
  353. return mapped_cpu;
  354. }
  355. EXPORT_SYMBOL(map_to_cpu);
  356. void cpu_map_rebuild(void)
  357. {
  358. unsigned long flag;
  359. spin_lock_irqsave(&cpu_map_lock, flag);
  360. _cpu_map_rebuild();
  361. spin_unlock_irqrestore(&cpu_map_lock, flag);
  362. }