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