1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SCHED_TOPOLOGY_H
3 #define _LINUX_SCHED_TOPOLOGY_H
4
5 #include <linux/topology.h>
6 #include <linux/android_kabi.h>
7
8 #include <linux/sched/idle.h>
9
10 /*
11 * sched-domains (multiprocessor balancing) declarations:
12 */
13 #ifdef CONFIG_SMP
14
15 /* Generate SD flag indexes */
16 #define SD_FLAG(name, mflags) __##name,
17 enum {
18 #include <linux/sched/sd_flags.h>
19 __SD_FLAG_CNT,
20 };
21 #undef SD_FLAG
22 /* Generate SD flag bits */
23 #define SD_FLAG(name, mflags) name = 1 << __##name,
24 enum {
25 #include <linux/sched/sd_flags.h>
26 };
27 #undef SD_FLAG
28
29 #ifdef CONFIG_SCHED_DEBUG
30
31 struct sd_flag_debug {
32 unsigned int meta_flags;
33 char *name;
34 };
35 extern const struct sd_flag_debug sd_flag_debug[];
36
37 #endif
38
39 #ifdef CONFIG_SCHED_SMT
cpu_smt_flags(void)40 static inline int cpu_smt_flags(void)
41 {
42 return SD_SHARE_CPUCAPACITY | SD_SHARE_LLC;
43 }
44 #endif
45
46 #ifdef CONFIG_SCHED_CLUSTER
cpu_cluster_flags(void)47 static inline int cpu_cluster_flags(void)
48 {
49 return SD_CLUSTER | SD_SHARE_LLC;
50 }
51 #endif
52
53 #ifdef CONFIG_SCHED_MC
cpu_core_flags(void)54 static inline int cpu_core_flags(void)
55 {
56 return SD_SHARE_LLC;
57 }
58 #endif
59
60 #ifdef CONFIG_NUMA
cpu_numa_flags(void)61 static inline int cpu_numa_flags(void)
62 {
63 return SD_NUMA;
64 }
65 #endif
66
67 extern int arch_asym_cpu_priority(int cpu);
68
69 struct sched_domain_attr {
70 int relax_domain_level;
71 };
72
73 #define SD_ATTR_INIT (struct sched_domain_attr) { \
74 .relax_domain_level = -1, \
75 }
76
77 extern int sched_domain_level_max;
78
79 struct sched_group;
80
81 struct sched_domain_shared {
82 atomic_t ref;
83 atomic_t nr_busy_cpus;
84 int has_idle_cores;
85 int nr_idle_scan;
86 };
87
88 struct sched_domain {
89 /* These fields must be setup */
90 struct sched_domain __rcu *parent; /* top domain must be null terminated */
91 struct sched_domain __rcu *child; /* bottom domain must be null terminated */
92 struct sched_group *groups; /* the balancing groups of the domain */
93 unsigned long min_interval; /* Minimum balance interval ms */
94 unsigned long max_interval; /* Maximum balance interval ms */
95 unsigned int busy_factor; /* less balancing by factor if busy */
96 unsigned int imbalance_pct; /* No balance until over watermark */
97 unsigned int cache_nice_tries; /* Leave cache hot tasks for # tries */
98 unsigned int imb_numa_nr; /* Nr running tasks that allows a NUMA imbalance */
99
100 int nohz_idle; /* NOHZ IDLE status */
101 int flags; /* See SD_* */
102 int level;
103
104 /* Runtime fields. */
105 unsigned long last_balance; /* init to jiffies. units in jiffies */
106 unsigned int balance_interval; /* initialise to 1. units in ms. */
107 unsigned int nr_balance_failed; /* initialise to 0 */
108
109 /* idle_balance() stats */
110 u64 max_newidle_lb_cost;
111 unsigned long last_decay_max_lb_cost;
112
113 #ifdef CONFIG_SCHEDSTATS
114 /* sched_balance_rq() stats */
115 unsigned int lb_count[CPU_MAX_IDLE_TYPES];
116 unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
117 unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
118 unsigned int lb_imbalance[CPU_MAX_IDLE_TYPES];
119 unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
120 unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
121 unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
122 unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
123
124 /* Active load balancing */
125 unsigned int alb_count;
126 unsigned int alb_failed;
127 unsigned int alb_pushed;
128
129 /* SD_BALANCE_EXEC stats */
130 unsigned int sbe_count;
131 unsigned int sbe_balanced;
132 unsigned int sbe_pushed;
133
134 /* SD_BALANCE_FORK stats */
135 unsigned int sbf_count;
136 unsigned int sbf_balanced;
137 unsigned int sbf_pushed;
138
139 /* try_to_wake_up() stats */
140 unsigned int ttwu_wake_remote;
141 unsigned int ttwu_move_affine;
142 unsigned int ttwu_move_balance;
143 #endif
144 #ifdef CONFIG_SCHED_DEBUG
145 char *name;
146 #endif
147 union {
148 void *private; /* used during construction */
149 struct rcu_head rcu; /* used during destruction */
150 };
151 struct sched_domain_shared *shared;
152
153 unsigned int span_weight;
154
155 ANDROID_KABI_RESERVE(1);
156 ANDROID_KABI_RESERVE(2);
157
158 /*
159 * Span of all CPUs in this domain.
160 *
161 * NOTE: this field is variable length. (Allocated dynamically
162 * by attaching extra space to the end of the structure,
163 * depending on how many CPUs the kernel has booted up with)
164 */
165 unsigned long span[];
166 };
167
sched_domain_span(struct sched_domain * sd)168 static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
169 {
170 return to_cpumask(sd->span);
171 }
172
173 extern void partition_sched_domains_locked(int ndoms_new,
174 cpumask_var_t doms_new[],
175 struct sched_domain_attr *dattr_new);
176
177 extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
178 struct sched_domain_attr *dattr_new);
179
180 /* Allocate an array of sched domains, for partition_sched_domains(). */
181 cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
182 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
183
184 bool cpus_equal_capacity(int this_cpu, int that_cpu);
185 bool cpus_share_cache(int this_cpu, int that_cpu);
186 bool cpus_share_resources(int this_cpu, int that_cpu);
187
188 typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
189 typedef int (*sched_domain_flags_f)(void);
190
191 #define SDTL_OVERLAP 0x01
192
193 struct sd_data {
194 struct sched_domain *__percpu *sd;
195 struct sched_domain_shared *__percpu *sds;
196 struct sched_group *__percpu *sg;
197 struct sched_group_capacity *__percpu *sgc;
198 };
199
200 struct sched_domain_topology_level {
201 sched_domain_mask_f mask;
202 sched_domain_flags_f sd_flags;
203 int flags;
204 int numa_level;
205 struct sd_data data;
206 #ifdef CONFIG_SCHED_DEBUG
207 char *name;
208 #endif
209 };
210
211 extern void __init set_sched_topology(struct sched_domain_topology_level *tl);
212
213 #ifdef CONFIG_SCHED_DEBUG
214 # define SD_INIT_NAME(type) .name = #type
215 #else
216 # define SD_INIT_NAME(type)
217 #endif
218
219 #else /* CONFIG_SMP */
220
221 struct sched_domain_attr;
222
223 static inline void
partition_sched_domains_locked(int ndoms_new,cpumask_var_t doms_new[],struct sched_domain_attr * dattr_new)224 partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[],
225 struct sched_domain_attr *dattr_new)
226 {
227 }
228
229 static inline void
partition_sched_domains(int ndoms_new,cpumask_var_t doms_new[],struct sched_domain_attr * dattr_new)230 partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
231 struct sched_domain_attr *dattr_new)
232 {
233 }
234
cpus_equal_capacity(int this_cpu,int that_cpu)235 static inline bool cpus_equal_capacity(int this_cpu, int that_cpu)
236 {
237 return true;
238 }
239
cpus_share_cache(int this_cpu,int that_cpu)240 static inline bool cpus_share_cache(int this_cpu, int that_cpu)
241 {
242 return true;
243 }
244
cpus_share_resources(int this_cpu,int that_cpu)245 static inline bool cpus_share_resources(int this_cpu, int that_cpu)
246 {
247 return true;
248 }
249
250 #endif /* !CONFIG_SMP */
251
252 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
253 extern void rebuild_sched_domains_energy(void);
254 #else
rebuild_sched_domains_energy(void)255 static inline void rebuild_sched_domains_energy(void)
256 {
257 }
258 #endif
259
260 #ifndef arch_scale_cpu_capacity
261 /**
262 * arch_scale_cpu_capacity - get the capacity scale factor of a given CPU.
263 * @cpu: the CPU in question.
264 *
265 * Return: the CPU scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
266 *
267 * max_perf(cpu)
268 * ----------------------------- * SCHED_CAPACITY_SCALE
269 * max(max_perf(c) : c \in CPUs)
270 */
271 static __always_inline
arch_scale_cpu_capacity(int cpu)272 unsigned long arch_scale_cpu_capacity(int cpu)
273 {
274 return SCHED_CAPACITY_SCALE;
275 }
276 #endif
277
278 #ifndef arch_scale_hw_pressure
279 static __always_inline
arch_scale_hw_pressure(int cpu)280 unsigned long arch_scale_hw_pressure(int cpu)
281 {
282 return 0;
283 }
284 #endif
285
286 #ifndef arch_update_hw_pressure
287 static __always_inline
arch_update_hw_pressure(const struct cpumask * cpus,unsigned long capped_frequency)288 void arch_update_hw_pressure(const struct cpumask *cpus,
289 unsigned long capped_frequency)
290 { }
291 #endif
292
293 #ifndef arch_scale_freq_ref
294 static __always_inline
arch_scale_freq_ref(int cpu)295 unsigned int arch_scale_freq_ref(int cpu)
296 {
297 return 0;
298 }
299 #endif
300
task_node(const struct task_struct * p)301 static inline int task_node(const struct task_struct *p)
302 {
303 return cpu_to_node(task_cpu(p));
304 }
305
306 #endif /* _LINUX_SCHED_TOPOLOGY_H */
307