topology.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * include/linux/topology.h
  3. *
  4. * Written by: Matthew Dobson, IBM Corporation
  5. *
  6. * Copyright (C) 2002, IBM Corp.
  7. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  18. * NON INFRINGEMENT. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * Send feedback to <[email protected]>
  26. */
  27. #ifndef _LINUX_TOPOLOGY_H
  28. #define _LINUX_TOPOLOGY_H
  29. #include <linux/arch_topology.h>
  30. #include <linux/cpumask.h>
  31. #include <linux/bitops.h>
  32. #include <linux/mmzone.h>
  33. #include <linux/smp.h>
  34. #include <linux/percpu.h>
  35. #include <asm/topology.h>
  36. #ifndef nr_cpus_node
  37. #define nr_cpus_node(node) cpumask_weight(cpumask_of_node(node))
  38. #endif
  39. #define for_each_node_with_cpus(node) \
  40. for_each_online_node(node) \
  41. if (nr_cpus_node(node))
  42. int arch_update_cpu_topology(void);
  43. /* Conform to ACPI 2.0 SLIT distance definitions */
  44. #define LOCAL_DISTANCE 10
  45. #define REMOTE_DISTANCE 20
  46. #define DISTANCE_BITS 8
  47. #ifndef node_distance
  48. #define node_distance(from,to) ((from) == (to) ? LOCAL_DISTANCE : REMOTE_DISTANCE)
  49. #endif
  50. #ifndef RECLAIM_DISTANCE
  51. /*
  52. * If the distance between nodes in a system is larger than RECLAIM_DISTANCE
  53. * (in whatever arch specific measurement units returned by node_distance())
  54. * and node_reclaim_mode is enabled then the VM will only call node_reclaim()
  55. * on nodes within this distance.
  56. */
  57. #define RECLAIM_DISTANCE 30
  58. #endif
  59. /*
  60. * The following tunable allows platforms to override the default node
  61. * reclaim distance (RECLAIM_DISTANCE) if remote memory accesses are
  62. * sufficiently fast that the default value actually hurts
  63. * performance.
  64. *
  65. * AMD EPYC machines use this because even though the 2-hop distance
  66. * is 32 (3.2x slower than a local memory access) performance actually
  67. * *improves* if allowed to reclaim memory and load balance tasks
  68. * between NUMA nodes 2-hops apart.
  69. */
  70. extern int __read_mostly node_reclaim_distance;
  71. #ifndef PENALTY_FOR_NODE_WITH_CPUS
  72. #define PENALTY_FOR_NODE_WITH_CPUS (1)
  73. #endif
  74. #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
  75. DECLARE_PER_CPU(int, numa_node);
  76. #ifndef numa_node_id
  77. /* Returns the number of the current Node. */
  78. static inline int numa_node_id(void)
  79. {
  80. return raw_cpu_read(numa_node);
  81. }
  82. #endif
  83. #ifndef cpu_to_node
  84. static inline int cpu_to_node(int cpu)
  85. {
  86. return per_cpu(numa_node, cpu);
  87. }
  88. #endif
  89. #ifndef set_numa_node
  90. static inline void set_numa_node(int node)
  91. {
  92. this_cpu_write(numa_node, node);
  93. }
  94. #endif
  95. #ifndef set_cpu_numa_node
  96. static inline void set_cpu_numa_node(int cpu, int node)
  97. {
  98. per_cpu(numa_node, cpu) = node;
  99. }
  100. #endif
  101. #else /* !CONFIG_USE_PERCPU_NUMA_NODE_ID */
  102. /* Returns the number of the current Node. */
  103. #ifndef numa_node_id
  104. static inline int numa_node_id(void)
  105. {
  106. return cpu_to_node(raw_smp_processor_id());
  107. }
  108. #endif
  109. #endif /* [!]CONFIG_USE_PERCPU_NUMA_NODE_ID */
  110. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  111. /*
  112. * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
  113. * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
  114. * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem().
  115. */
  116. DECLARE_PER_CPU(int, _numa_mem_);
  117. #ifndef set_numa_mem
  118. static inline void set_numa_mem(int node)
  119. {
  120. this_cpu_write(_numa_mem_, node);
  121. }
  122. #endif
  123. #ifndef numa_mem_id
  124. /* Returns the number of the nearest Node with memory */
  125. static inline int numa_mem_id(void)
  126. {
  127. return raw_cpu_read(_numa_mem_);
  128. }
  129. #endif
  130. #ifndef cpu_to_mem
  131. static inline int cpu_to_mem(int cpu)
  132. {
  133. return per_cpu(_numa_mem_, cpu);
  134. }
  135. #endif
  136. #ifndef set_cpu_numa_mem
  137. static inline void set_cpu_numa_mem(int cpu, int node)
  138. {
  139. per_cpu(_numa_mem_, cpu) = node;
  140. }
  141. #endif
  142. #else /* !CONFIG_HAVE_MEMORYLESS_NODES */
  143. #ifndef numa_mem_id
  144. /* Returns the number of the nearest Node with memory */
  145. static inline int numa_mem_id(void)
  146. {
  147. return numa_node_id();
  148. }
  149. #endif
  150. #ifndef cpu_to_mem
  151. static inline int cpu_to_mem(int cpu)
  152. {
  153. return cpu_to_node(cpu);
  154. }
  155. #endif
  156. #endif /* [!]CONFIG_HAVE_MEMORYLESS_NODES */
  157. #if defined(topology_die_id) && defined(topology_die_cpumask)
  158. #define TOPOLOGY_DIE_SYSFS
  159. #endif
  160. #if defined(topology_cluster_id) && defined(topology_cluster_cpumask)
  161. #define TOPOLOGY_CLUSTER_SYSFS
  162. #endif
  163. #if defined(topology_book_id) && defined(topology_book_cpumask)
  164. #define TOPOLOGY_BOOK_SYSFS
  165. #endif
  166. #if defined(topology_drawer_id) && defined(topology_drawer_cpumask)
  167. #define TOPOLOGY_DRAWER_SYSFS
  168. #endif
  169. #ifndef topology_physical_package_id
  170. #define topology_physical_package_id(cpu) ((void)(cpu), -1)
  171. #endif
  172. #ifndef topology_die_id
  173. #define topology_die_id(cpu) ((void)(cpu), -1)
  174. #endif
  175. #ifndef topology_cluster_id
  176. #define topology_cluster_id(cpu) ((void)(cpu), -1)
  177. #endif
  178. #ifndef topology_core_id
  179. #define topology_core_id(cpu) ((void)(cpu), 0)
  180. #endif
  181. #ifndef topology_book_id
  182. #define topology_book_id(cpu) ((void)(cpu), -1)
  183. #endif
  184. #ifndef topology_drawer_id
  185. #define topology_drawer_id(cpu) ((void)(cpu), -1)
  186. #endif
  187. #ifndef topology_ppin
  188. #define topology_ppin(cpu) ((void)(cpu), 0ull)
  189. #endif
  190. #ifndef topology_sibling_cpumask
  191. #define topology_sibling_cpumask(cpu) cpumask_of(cpu)
  192. #endif
  193. #ifndef topology_core_cpumask
  194. #define topology_core_cpumask(cpu) cpumask_of(cpu)
  195. #endif
  196. #ifndef topology_cluster_cpumask
  197. #define topology_cluster_cpumask(cpu) cpumask_of(cpu)
  198. #endif
  199. #ifndef topology_die_cpumask
  200. #define topology_die_cpumask(cpu) cpumask_of(cpu)
  201. #endif
  202. #ifndef topology_book_cpumask
  203. #define topology_book_cpumask(cpu) cpumask_of(cpu)
  204. #endif
  205. #ifndef topology_drawer_cpumask
  206. #define topology_drawer_cpumask(cpu) cpumask_of(cpu)
  207. #endif
  208. #if defined(CONFIG_SCHED_SMT) && !defined(cpu_smt_mask)
  209. static inline const struct cpumask *cpu_smt_mask(int cpu)
  210. {
  211. return topology_sibling_cpumask(cpu);
  212. }
  213. #endif
  214. static inline const struct cpumask *cpu_cpu_mask(int cpu)
  215. {
  216. return cpumask_of_node(cpu_to_node(cpu));
  217. }
  218. #endif /* _LINUX_TOPOLOGY_H */