Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * include/linux/topology.h
0003  *
0004  * Written by: Matthew Dobson, IBM Corporation
0005  *
0006  * Copyright (C) 2002, IBM Corp.
0007  *
0008  * All rights reserved.
0009  *
0010  * This program is free software; you can redistribute it and/or modify
0011  * it under the terms of the GNU General Public License as published by
0012  * the Free Software Foundation; either version 2 of the License, or
0013  * (at your option) any later version.
0014  *
0015  * This program is distributed in the hope that it will be useful, but
0016  * WITHOUT ANY WARRANTY; without even the implied warranty of
0017  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
0018  * NON INFRINGEMENT.  See the GNU General Public License for more
0019  * details.
0020  *
0021  * You should have received a copy of the GNU General Public License
0022  * along with this program; if not, write to the Free Software
0023  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
0024  *
0025  * Send feedback to <colpatch@us.ibm.com>
0026  */
0027 #ifndef _LINUX_TOPOLOGY_H
0028 #define _LINUX_TOPOLOGY_H
0029 
0030 #include <linux/arch_topology.h>
0031 #include <linux/cpumask.h>
0032 #include <linux/bitops.h>
0033 #include <linux/mmzone.h>
0034 #include <linux/smp.h>
0035 #include <linux/percpu.h>
0036 #include <asm/topology.h>
0037 
0038 #ifndef nr_cpus_node
0039 #define nr_cpus_node(node) cpumask_weight(cpumask_of_node(node))
0040 #endif
0041 
0042 #define for_each_node_with_cpus(node)           \
0043     for_each_online_node(node)          \
0044         if (nr_cpus_node(node))
0045 
0046 int arch_update_cpu_topology(void);
0047 
0048 /* Conform to ACPI 2.0 SLIT distance definitions */
0049 #define LOCAL_DISTANCE      10
0050 #define REMOTE_DISTANCE     20
0051 #define DISTANCE_BITS           8
0052 #ifndef node_distance
0053 #define node_distance(from,to)  ((from) == (to) ? LOCAL_DISTANCE : REMOTE_DISTANCE)
0054 #endif
0055 #ifndef RECLAIM_DISTANCE
0056 /*
0057  * If the distance between nodes in a system is larger than RECLAIM_DISTANCE
0058  * (in whatever arch specific measurement units returned by node_distance())
0059  * and node_reclaim_mode is enabled then the VM will only call node_reclaim()
0060  * on nodes within this distance.
0061  */
0062 #define RECLAIM_DISTANCE 30
0063 #endif
0064 
0065 /*
0066  * The following tunable allows platforms to override the default node
0067  * reclaim distance (RECLAIM_DISTANCE) if remote memory accesses are
0068  * sufficiently fast that the default value actually hurts
0069  * performance.
0070  *
0071  * AMD EPYC machines use this because even though the 2-hop distance
0072  * is 32 (3.2x slower than a local memory access) performance actually
0073  * *improves* if allowed to reclaim memory and load balance tasks
0074  * between NUMA nodes 2-hops apart.
0075  */
0076 extern int __read_mostly node_reclaim_distance;
0077 
0078 #ifndef PENALTY_FOR_NODE_WITH_CPUS
0079 #define PENALTY_FOR_NODE_WITH_CPUS  (1)
0080 #endif
0081 
0082 #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
0083 DECLARE_PER_CPU(int, numa_node);
0084 
0085 #ifndef numa_node_id
0086 /* Returns the number of the current Node. */
0087 static inline int numa_node_id(void)
0088 {
0089     return raw_cpu_read(numa_node);
0090 }
0091 #endif
0092 
0093 #ifndef cpu_to_node
0094 static inline int cpu_to_node(int cpu)
0095 {
0096     return per_cpu(numa_node, cpu);
0097 }
0098 #endif
0099 
0100 #ifndef set_numa_node
0101 static inline void set_numa_node(int node)
0102 {
0103     this_cpu_write(numa_node, node);
0104 }
0105 #endif
0106 
0107 #ifndef set_cpu_numa_node
0108 static inline void set_cpu_numa_node(int cpu, int node)
0109 {
0110     per_cpu(numa_node, cpu) = node;
0111 }
0112 #endif
0113 
0114 #else   /* !CONFIG_USE_PERCPU_NUMA_NODE_ID */
0115 
0116 /* Returns the number of the current Node. */
0117 #ifndef numa_node_id
0118 static inline int numa_node_id(void)
0119 {
0120     return cpu_to_node(raw_smp_processor_id());
0121 }
0122 #endif
0123 
0124 #endif  /* [!]CONFIG_USE_PERCPU_NUMA_NODE_ID */
0125 
0126 #ifdef CONFIG_HAVE_MEMORYLESS_NODES
0127 
0128 /*
0129  * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
0130  * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
0131  * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem().
0132  */
0133 DECLARE_PER_CPU(int, _numa_mem_);
0134 
0135 #ifndef set_numa_mem
0136 static inline void set_numa_mem(int node)
0137 {
0138     this_cpu_write(_numa_mem_, node);
0139 }
0140 #endif
0141 
0142 #ifndef numa_mem_id
0143 /* Returns the number of the nearest Node with memory */
0144 static inline int numa_mem_id(void)
0145 {
0146     return raw_cpu_read(_numa_mem_);
0147 }
0148 #endif
0149 
0150 #ifndef cpu_to_mem
0151 static inline int cpu_to_mem(int cpu)
0152 {
0153     return per_cpu(_numa_mem_, cpu);
0154 }
0155 #endif
0156 
0157 #ifndef set_cpu_numa_mem
0158 static inline void set_cpu_numa_mem(int cpu, int node)
0159 {
0160     per_cpu(_numa_mem_, cpu) = node;
0161 }
0162 #endif
0163 
0164 #else   /* !CONFIG_HAVE_MEMORYLESS_NODES */
0165 
0166 #ifndef numa_mem_id
0167 /* Returns the number of the nearest Node with memory */
0168 static inline int numa_mem_id(void)
0169 {
0170     return numa_node_id();
0171 }
0172 #endif
0173 
0174 #ifndef cpu_to_mem
0175 static inline int cpu_to_mem(int cpu)
0176 {
0177     return cpu_to_node(cpu);
0178 }
0179 #endif
0180 
0181 #endif  /* [!]CONFIG_HAVE_MEMORYLESS_NODES */
0182 
0183 #if defined(topology_die_id) && defined(topology_die_cpumask)
0184 #define TOPOLOGY_DIE_SYSFS
0185 #endif
0186 #if defined(topology_cluster_id) && defined(topology_cluster_cpumask)
0187 #define TOPOLOGY_CLUSTER_SYSFS
0188 #endif
0189 #if defined(topology_book_id) && defined(topology_book_cpumask)
0190 #define TOPOLOGY_BOOK_SYSFS
0191 #endif
0192 #if defined(topology_drawer_id) && defined(topology_drawer_cpumask)
0193 #define TOPOLOGY_DRAWER_SYSFS
0194 #endif
0195 
0196 #ifndef topology_physical_package_id
0197 #define topology_physical_package_id(cpu)   ((void)(cpu), -1)
0198 #endif
0199 #ifndef topology_die_id
0200 #define topology_die_id(cpu)            ((void)(cpu), -1)
0201 #endif
0202 #ifndef topology_cluster_id
0203 #define topology_cluster_id(cpu)        ((void)(cpu), -1)
0204 #endif
0205 #ifndef topology_core_id
0206 #define topology_core_id(cpu)           ((void)(cpu), 0)
0207 #endif
0208 #ifndef topology_book_id
0209 #define topology_book_id(cpu)           ((void)(cpu), -1)
0210 #endif
0211 #ifndef topology_drawer_id
0212 #define topology_drawer_id(cpu)         ((void)(cpu), -1)
0213 #endif
0214 #ifndef topology_ppin
0215 #define topology_ppin(cpu)          ((void)(cpu), 0ull)
0216 #endif
0217 #ifndef topology_sibling_cpumask
0218 #define topology_sibling_cpumask(cpu)       cpumask_of(cpu)
0219 #endif
0220 #ifndef topology_core_cpumask
0221 #define topology_core_cpumask(cpu)      cpumask_of(cpu)
0222 #endif
0223 #ifndef topology_cluster_cpumask
0224 #define topology_cluster_cpumask(cpu)       cpumask_of(cpu)
0225 #endif
0226 #ifndef topology_die_cpumask
0227 #define topology_die_cpumask(cpu)       cpumask_of(cpu)
0228 #endif
0229 #ifndef topology_book_cpumask
0230 #define topology_book_cpumask(cpu)      cpumask_of(cpu)
0231 #endif
0232 #ifndef topology_drawer_cpumask
0233 #define topology_drawer_cpumask(cpu)        cpumask_of(cpu)
0234 #endif
0235 
0236 #if defined(CONFIG_SCHED_SMT) && !defined(cpu_smt_mask)
0237 static inline const struct cpumask *cpu_smt_mask(int cpu)
0238 {
0239     return topology_sibling_cpumask(cpu);
0240 }
0241 #endif
0242 
0243 static inline const struct cpumask *cpu_cpu_mask(int cpu)
0244 {
0245     return cpumask_of_node(cpu_to_node(cpu));
0246 }
0247 
0248 
0249 #endif /* _LINUX_TOPOLOGY_H */