• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PKG_RESOURCES;
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_SHARE_PKG_RESOURCES;
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_PKG_RESOURCES;
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 	u64 avg_scan_cost;		/* select_idle_sibling */
114 
115 #ifdef CONFIG_SCHEDSTATS
116 	/* load_balance() stats */
117 	unsigned int lb_count[CPU_MAX_IDLE_TYPES];
118 	unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
119 	unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
120 	unsigned int lb_imbalance[CPU_MAX_IDLE_TYPES];
121 	unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
122 	unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
123 	unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
124 	unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
125 
126 	/* Active load balancing */
127 	unsigned int alb_count;
128 	unsigned int alb_failed;
129 	unsigned int alb_pushed;
130 
131 	/* SD_BALANCE_EXEC stats */
132 	unsigned int sbe_count;
133 	unsigned int sbe_balanced;
134 	unsigned int sbe_pushed;
135 
136 	/* SD_BALANCE_FORK stats */
137 	unsigned int sbf_count;
138 	unsigned int sbf_balanced;
139 	unsigned int sbf_pushed;
140 
141 	/* try_to_wake_up() stats */
142 	unsigned int ttwu_wake_remote;
143 	unsigned int ttwu_move_affine;
144 	unsigned int ttwu_move_balance;
145 #endif
146 #ifdef CONFIG_SCHED_DEBUG
147 	char *name;
148 #endif
149 	union {
150 		void *private;		/* used during construction */
151 		struct rcu_head rcu;	/* used during destruction */
152 	};
153 	struct sched_domain_shared *shared;
154 
155 	unsigned int span_weight;
156 
157 	ANDROID_KABI_RESERVE(1);
158 	ANDROID_KABI_RESERVE(2);
159 
160 	/*
161 	 * Span of all CPUs in this domain.
162 	 *
163 	 * NOTE: this field is variable length. (Allocated dynamically
164 	 * by attaching extra space to the end of the structure,
165 	 * depending on how many CPUs the kernel has booted up with)
166 	 */
167 	unsigned long span[];
168 };
169 
sched_domain_span(struct sched_domain * sd)170 static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
171 {
172 	return to_cpumask(sd->span);
173 }
174 
175 extern void partition_sched_domains_locked(int ndoms_new,
176 					   cpumask_var_t doms_new[],
177 					   struct sched_domain_attr *dattr_new);
178 
179 extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
180 				    struct sched_domain_attr *dattr_new);
181 
182 /* Allocate an array of sched domains, for partition_sched_domains(). */
183 cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
184 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
185 
186 bool cpus_equal_capacity(int this_cpu, int that_cpu);
187 bool cpus_share_cache(int this_cpu, int that_cpu);
188 
189 typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
190 typedef int (*sched_domain_flags_f)(void);
191 
192 #define SDTL_OVERLAP	0x01
193 
194 struct sd_data {
195 	struct sched_domain *__percpu *sd;
196 	struct sched_domain_shared *__percpu *sds;
197 	struct sched_group *__percpu *sg;
198 	struct sched_group_capacity *__percpu *sgc;
199 };
200 
201 struct sched_domain_topology_level {
202 	sched_domain_mask_f mask;
203 	sched_domain_flags_f sd_flags;
204 	int		    flags;
205 	int		    numa_level;
206 	struct sd_data      data;
207 #ifdef CONFIG_SCHED_DEBUG
208 	char                *name;
209 #endif
210 };
211 
212 extern void __init set_sched_topology(struct sched_domain_topology_level *tl);
213 
214 #ifdef CONFIG_SCHED_DEBUG
215 # define SD_INIT_NAME(type)		.name = #type
216 #else
217 # define SD_INIT_NAME(type)
218 #endif
219 
220 #else /* CONFIG_SMP */
221 
222 struct sched_domain_attr;
223 
224 static inline void
partition_sched_domains_locked(int ndoms_new,cpumask_var_t doms_new[],struct sched_domain_attr * dattr_new)225 partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new[],
226 			       struct sched_domain_attr *dattr_new)
227 {
228 }
229 
230 static inline void
partition_sched_domains(int ndoms_new,cpumask_var_t doms_new[],struct sched_domain_attr * dattr_new)231 partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
232 			struct sched_domain_attr *dattr_new)
233 {
234 }
235 
cpus_equal_capacity(int this_cpu,int that_cpu)236 static inline bool cpus_equal_capacity(int this_cpu, int that_cpu)
237 {
238 	return true;
239 }
240 
cpus_share_cache(int this_cpu,int that_cpu)241 static inline bool cpus_share_cache(int this_cpu, int that_cpu)
242 {
243 	return true;
244 }
245 
246 #endif	/* !CONFIG_SMP */
247 
248 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
249 extern void rebuild_sched_domains_energy(void);
250 #else
rebuild_sched_domains_energy(void)251 static inline void rebuild_sched_domains_energy(void)
252 {
253 }
254 #endif
255 
256 #ifndef arch_scale_cpu_capacity
257 /**
258  * arch_scale_cpu_capacity - get the capacity scale factor of a given CPU.
259  * @cpu: the CPU in question.
260  *
261  * Return: the CPU scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
262  *
263  *             max_perf(cpu)
264  *      ----------------------------- * SCHED_CAPACITY_SCALE
265  *      max(max_perf(c) : c \in CPUs)
266  */
267 static __always_inline
arch_scale_cpu_capacity(int cpu)268 unsigned long arch_scale_cpu_capacity(int cpu)
269 {
270 	return SCHED_CAPACITY_SCALE;
271 }
272 #endif
273 
274 #ifndef arch_scale_thermal_pressure
275 static __always_inline
arch_scale_thermal_pressure(int cpu)276 unsigned long arch_scale_thermal_pressure(int cpu)
277 {
278 	return 0;
279 }
280 #endif
281 
282 #ifndef arch_update_thermal_pressure
283 static __always_inline
arch_update_thermal_pressure(const struct cpumask * cpus,unsigned long capped_frequency)284 void arch_update_thermal_pressure(const struct cpumask *cpus,
285 				  unsigned long capped_frequency)
286 { }
287 #endif
288 
task_node(const struct task_struct * p)289 static inline int task_node(const struct task_struct *p)
290 {
291 	return cpu_to_node(task_cpu(p));
292 }
293 
294 #endif /* _LINUX_SCHED_TOPOLOGY_H */
295