xref: /openbmc/linux/include/linux/sched/topology.h (revision 8ec59c0f)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2105ab3d8SIngo Molnar #ifndef _LINUX_SCHED_TOPOLOGY_H
3105ab3d8SIngo Molnar #define _LINUX_SCHED_TOPOLOGY_H
4105ab3d8SIngo Molnar 
5ee6a3d19SIngo Molnar #include <linux/topology.h>
6ee6a3d19SIngo Molnar 
74c822698SIngo Molnar #include <linux/sched/idle.h>
84c822698SIngo Molnar 
9a60b9edaSIngo Molnar /*
107e1a9208SJuri Lelli  * Increase resolution of cpu_capacity calculations
117e1a9208SJuri Lelli  */
127e1a9208SJuri Lelli #define SCHED_CAPACITY_SHIFT	SCHED_FIXEDPOINT_SHIFT
137e1a9208SJuri Lelli #define SCHED_CAPACITY_SCALE	(1L << SCHED_CAPACITY_SHIFT)
147e1a9208SJuri Lelli 
157e1a9208SJuri Lelli /*
16a60b9edaSIngo Molnar  * sched-domains (multiprocessor balancing) declarations:
17a60b9edaSIngo Molnar  */
18a60b9edaSIngo Molnar #ifdef CONFIG_SMP
19a60b9edaSIngo Molnar 
20a60b9edaSIngo Molnar #define SD_LOAD_BALANCE		0x0001	/* Do load balancing on this domain. */
21a60b9edaSIngo Molnar #define SD_BALANCE_NEWIDLE	0x0002	/* Balance when about to become idle */
22a60b9edaSIngo Molnar #define SD_BALANCE_EXEC		0x0004	/* Balance on exec */
23a60b9edaSIngo Molnar #define SD_BALANCE_FORK		0x0008	/* Balance on fork, clone */
24a60b9edaSIngo Molnar #define SD_BALANCE_WAKE		0x0010  /* Balance on wakeup */
25a60b9edaSIngo Molnar #define SD_WAKE_AFFINE		0x0020	/* Wake task to waking CPU */
2605484e09SMorten Rasmussen #define SD_ASYM_CPUCAPACITY	0x0040  /* Domain members have different CPU capacities */
2705484e09SMorten Rasmussen #define SD_SHARE_CPUCAPACITY	0x0080	/* Domain members share CPU capacity */
28a60b9edaSIngo Molnar #define SD_SHARE_POWERDOMAIN	0x0100	/* Domain members share power domain */
2905484e09SMorten Rasmussen #define SD_SHARE_PKG_RESOURCES	0x0200	/* Domain members share CPU pkg resources */
30a60b9edaSIngo Molnar #define SD_SERIALIZE		0x0400	/* Only a single load balancing instance */
31a60b9edaSIngo Molnar #define SD_ASYM_PACKING		0x0800  /* Place busy groups earlier in the domain */
32a60b9edaSIngo Molnar #define SD_PREFER_SIBLING	0x1000	/* Prefer to place tasks in a sibling domain */
33a60b9edaSIngo Molnar #define SD_OVERLAP		0x2000	/* sched_domains of this level overlap */
34a60b9edaSIngo Molnar #define SD_NUMA			0x4000	/* cross-node balancing */
35a60b9edaSIngo Molnar 
36a60b9edaSIngo Molnar #ifdef CONFIG_SCHED_SMT
37a60b9edaSIngo Molnar static inline int cpu_smt_flags(void)
38a60b9edaSIngo Molnar {
39a60b9edaSIngo Molnar 	return SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
40a60b9edaSIngo Molnar }
41a60b9edaSIngo Molnar #endif
42a60b9edaSIngo Molnar 
43a60b9edaSIngo Molnar #ifdef CONFIG_SCHED_MC
44a60b9edaSIngo Molnar static inline int cpu_core_flags(void)
45a60b9edaSIngo Molnar {
46a60b9edaSIngo Molnar 	return SD_SHARE_PKG_RESOURCES;
47a60b9edaSIngo Molnar }
48a60b9edaSIngo Molnar #endif
49a60b9edaSIngo Molnar 
50a60b9edaSIngo Molnar #ifdef CONFIG_NUMA
51a60b9edaSIngo Molnar static inline int cpu_numa_flags(void)
52a60b9edaSIngo Molnar {
53a60b9edaSIngo Molnar 	return SD_NUMA;
54a60b9edaSIngo Molnar }
55a60b9edaSIngo Molnar #endif
56a60b9edaSIngo Molnar 
57a60b9edaSIngo Molnar extern int arch_asym_cpu_priority(int cpu);
58a60b9edaSIngo Molnar 
59a60b9edaSIngo Molnar struct sched_domain_attr {
60a60b9edaSIngo Molnar 	int relax_domain_level;
61a60b9edaSIngo Molnar };
62a60b9edaSIngo Molnar 
63a60b9edaSIngo Molnar #define SD_ATTR_INIT	(struct sched_domain_attr) {	\
64a60b9edaSIngo Molnar 	.relax_domain_level = -1,			\
65a60b9edaSIngo Molnar }
66a60b9edaSIngo Molnar 
67a60b9edaSIngo Molnar extern int sched_domain_level_max;
68a60b9edaSIngo Molnar 
69a60b9edaSIngo Molnar struct sched_group;
70a60b9edaSIngo Molnar 
71a60b9edaSIngo Molnar struct sched_domain_shared {
72a60b9edaSIngo Molnar 	atomic_t	ref;
73a60b9edaSIngo Molnar 	atomic_t	nr_busy_cpus;
74a60b9edaSIngo Molnar 	int		has_idle_cores;
75a60b9edaSIngo Molnar };
76a60b9edaSIngo Molnar 
77a60b9edaSIngo Molnar struct sched_domain {
78a60b9edaSIngo Molnar 	/* These fields must be setup */
79994aeb7aSJoel Fernandes (Google) 	struct sched_domain __rcu *parent;	/* top domain must be null terminated */
80994aeb7aSJoel Fernandes (Google) 	struct sched_domain __rcu *child;	/* bottom domain must be null terminated */
81a60b9edaSIngo Molnar 	struct sched_group *groups;	/* the balancing groups of the domain */
82a60b9edaSIngo Molnar 	unsigned long min_interval;	/* Minimum balance interval ms */
83a60b9edaSIngo Molnar 	unsigned long max_interval;	/* Maximum balance interval ms */
84a60b9edaSIngo Molnar 	unsigned int busy_factor;	/* less balancing by factor if busy */
85a60b9edaSIngo Molnar 	unsigned int imbalance_pct;	/* No balance until over watermark */
86a60b9edaSIngo Molnar 	unsigned int cache_nice_tries;	/* Leave cache hot tasks for # tries */
87a60b9edaSIngo Molnar 
88a60b9edaSIngo Molnar 	int nohz_idle;			/* NOHZ IDLE status */
89a60b9edaSIngo Molnar 	int flags;			/* See SD_* */
90a60b9edaSIngo Molnar 	int level;
91a60b9edaSIngo Molnar 
92a60b9edaSIngo Molnar 	/* Runtime fields. */
93a60b9edaSIngo Molnar 	unsigned long last_balance;	/* init to jiffies. units in jiffies */
94a60b9edaSIngo Molnar 	unsigned int balance_interval;	/* initialise to 1. units in ms. */
95a60b9edaSIngo Molnar 	unsigned int nr_balance_failed; /* initialise to 0 */
96a60b9edaSIngo Molnar 
97a60b9edaSIngo Molnar 	/* idle_balance() stats */
98a60b9edaSIngo Molnar 	u64 max_newidle_lb_cost;
99a60b9edaSIngo Molnar 	unsigned long next_decay_max_lb_cost;
100a60b9edaSIngo Molnar 
101a60b9edaSIngo Molnar 	u64 avg_scan_cost;		/* select_idle_sibling */
102a60b9edaSIngo Molnar 
103a60b9edaSIngo Molnar #ifdef CONFIG_SCHEDSTATS
104a60b9edaSIngo Molnar 	/* load_balance() stats */
105a60b9edaSIngo Molnar 	unsigned int lb_count[CPU_MAX_IDLE_TYPES];
106a60b9edaSIngo Molnar 	unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
107a60b9edaSIngo Molnar 	unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
108a60b9edaSIngo Molnar 	unsigned int lb_imbalance[CPU_MAX_IDLE_TYPES];
109a60b9edaSIngo Molnar 	unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
110a60b9edaSIngo Molnar 	unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
111a60b9edaSIngo Molnar 	unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
112a60b9edaSIngo Molnar 	unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
113a60b9edaSIngo Molnar 
114a60b9edaSIngo Molnar 	/* Active load balancing */
115a60b9edaSIngo Molnar 	unsigned int alb_count;
116a60b9edaSIngo Molnar 	unsigned int alb_failed;
117a60b9edaSIngo Molnar 	unsigned int alb_pushed;
118a60b9edaSIngo Molnar 
119a60b9edaSIngo Molnar 	/* SD_BALANCE_EXEC stats */
120a60b9edaSIngo Molnar 	unsigned int sbe_count;
121a60b9edaSIngo Molnar 	unsigned int sbe_balanced;
122a60b9edaSIngo Molnar 	unsigned int sbe_pushed;
123a60b9edaSIngo Molnar 
124a60b9edaSIngo Molnar 	/* SD_BALANCE_FORK stats */
125a60b9edaSIngo Molnar 	unsigned int sbf_count;
126a60b9edaSIngo Molnar 	unsigned int sbf_balanced;
127a60b9edaSIngo Molnar 	unsigned int sbf_pushed;
128a60b9edaSIngo Molnar 
129a60b9edaSIngo Molnar 	/* try_to_wake_up() stats */
130a60b9edaSIngo Molnar 	unsigned int ttwu_wake_remote;
131a60b9edaSIngo Molnar 	unsigned int ttwu_move_affine;
132a60b9edaSIngo Molnar 	unsigned int ttwu_move_balance;
133a60b9edaSIngo Molnar #endif
134a60b9edaSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
135a60b9edaSIngo Molnar 	char *name;
136a60b9edaSIngo Molnar #endif
137a60b9edaSIngo Molnar 	union {
138a60b9edaSIngo Molnar 		void *private;		/* used during construction */
139a60b9edaSIngo Molnar 		struct rcu_head rcu;	/* used during destruction */
140a60b9edaSIngo Molnar 	};
141a60b9edaSIngo Molnar 	struct sched_domain_shared *shared;
142a60b9edaSIngo Molnar 
143a60b9edaSIngo Molnar 	unsigned int span_weight;
144a60b9edaSIngo Molnar 	/*
145a60b9edaSIngo Molnar 	 * Span of all CPUs in this domain.
146a60b9edaSIngo Molnar 	 *
147a60b9edaSIngo Molnar 	 * NOTE: this field is variable length. (Allocated dynamically
148a60b9edaSIngo Molnar 	 * by attaching extra space to the end of the structure,
149a60b9edaSIngo Molnar 	 * depending on how many CPUs the kernel has booted up with)
150a60b9edaSIngo Molnar 	 */
151a60b9edaSIngo Molnar 	unsigned long span[0];
152a60b9edaSIngo Molnar };
153a60b9edaSIngo Molnar 
154a60b9edaSIngo Molnar static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
155a60b9edaSIngo Molnar {
156a60b9edaSIngo Molnar 	return to_cpumask(sd->span);
157a60b9edaSIngo Molnar }
158a60b9edaSIngo Molnar 
159a60b9edaSIngo Molnar extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
160a60b9edaSIngo Molnar 				    struct sched_domain_attr *dattr_new);
161a60b9edaSIngo Molnar 
162a60b9edaSIngo Molnar /* Allocate an array of sched domains, for partition_sched_domains(). */
163a60b9edaSIngo Molnar cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
164a60b9edaSIngo Molnar void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
165a60b9edaSIngo Molnar 
166a60b9edaSIngo Molnar bool cpus_share_cache(int this_cpu, int that_cpu);
167a60b9edaSIngo Molnar 
168a60b9edaSIngo Molnar typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
169a60b9edaSIngo Molnar typedef int (*sched_domain_flags_f)(void);
170a60b9edaSIngo Molnar 
171a60b9edaSIngo Molnar #define SDTL_OVERLAP	0x01
172a60b9edaSIngo Molnar 
173a60b9edaSIngo Molnar struct sd_data {
17499687cdbSLuc Van Oostenryck 	struct sched_domain *__percpu *sd;
17599687cdbSLuc Van Oostenryck 	struct sched_domain_shared *__percpu *sds;
17699687cdbSLuc Van Oostenryck 	struct sched_group *__percpu *sg;
17799687cdbSLuc Van Oostenryck 	struct sched_group_capacity *__percpu *sgc;
178a60b9edaSIngo Molnar };
179a60b9edaSIngo Molnar 
180a60b9edaSIngo Molnar struct sched_domain_topology_level {
181a60b9edaSIngo Molnar 	sched_domain_mask_f mask;
182a60b9edaSIngo Molnar 	sched_domain_flags_f sd_flags;
183a60b9edaSIngo Molnar 	int		    flags;
184a60b9edaSIngo Molnar 	int		    numa_level;
185a60b9edaSIngo Molnar 	struct sd_data      data;
186a60b9edaSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
187a60b9edaSIngo Molnar 	char                *name;
188a60b9edaSIngo Molnar #endif
189a60b9edaSIngo Molnar };
190a60b9edaSIngo Molnar 
191a60b9edaSIngo Molnar extern void set_sched_topology(struct sched_domain_topology_level *tl);
192a60b9edaSIngo Molnar 
193a60b9edaSIngo Molnar #ifdef CONFIG_SCHED_DEBUG
194a60b9edaSIngo Molnar # define SD_INIT_NAME(type)		.name = #type
195a60b9edaSIngo Molnar #else
196a60b9edaSIngo Molnar # define SD_INIT_NAME(type)
197a60b9edaSIngo Molnar #endif
198a60b9edaSIngo Molnar 
199a60b9edaSIngo Molnar #else /* CONFIG_SMP */
200a60b9edaSIngo Molnar 
201a60b9edaSIngo Molnar struct sched_domain_attr;
202a60b9edaSIngo Molnar 
203a60b9edaSIngo Molnar static inline void
204a60b9edaSIngo Molnar partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
205a60b9edaSIngo Molnar 			struct sched_domain_attr *dattr_new)
206a60b9edaSIngo Molnar {
207a60b9edaSIngo Molnar }
208a60b9edaSIngo Molnar 
209a60b9edaSIngo Molnar static inline bool cpus_share_cache(int this_cpu, int that_cpu)
210a60b9edaSIngo Molnar {
211a60b9edaSIngo Molnar 	return true;
212a60b9edaSIngo Molnar }
213a60b9edaSIngo Molnar 
2148ec59c0fSVincent Guittot #endif	/* !CONFIG_SMP */
2158ec59c0fSVincent Guittot 
2165bd0988bSQuentin Perret #ifndef arch_scale_cpu_capacity
2175bd0988bSQuentin Perret static __always_inline
2188ec59c0fSVincent Guittot unsigned long arch_scale_cpu_capacity(int cpu)
2195bd0988bSQuentin Perret {
2205bd0988bSQuentin Perret 	return SCHED_CAPACITY_SCALE;
2215bd0988bSQuentin Perret }
2225bd0988bSQuentin Perret #endif
2235bd0988bSQuentin Perret 
224ee6a3d19SIngo Molnar static inline int task_node(const struct task_struct *p)
225ee6a3d19SIngo Molnar {
226ee6a3d19SIngo Molnar 	return cpu_to_node(task_cpu(p));
227ee6a3d19SIngo Molnar }
228ee6a3d19SIngo Molnar 
229105ab3d8SIngo Molnar #endif /* _LINUX_SCHED_TOPOLOGY_H */
230