cpumap.c 11 KB

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