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