xref: /linux-6.15/include/linux/resctrl.h (revision cd84f72b)
1e79f15a4SChen Yu /* SPDX-License-Identifier: GPL-2.0 */
2e79f15a4SChen Yu #ifndef _RESCTRL_H
3e79f15a4SChen Yu #define _RESCTRL_H
4e79f15a4SChen Yu 
563c8b123SJames Morse #include <linux/kernel.h>
663c8b123SJames Morse #include <linux/list.h>
7a21a4391SJames Morse #include <linux/pid.h>
8a21a4391SJames Morse 
940fc735bSJames Morse /* CLOSID, RMID value used by the default control group */
1040fc735bSJames Morse #define RESCTRL_RESERVED_CLOSID		0
1140fc735bSJames Morse #define RESCTRL_RESERVED_RMID		0
1240fc735bSJames Morse 
13978fcca9SJames Morse #define RESCTRL_PICK_ANY_CPU		-1
14978fcca9SJames Morse 
15e79f15a4SChen Yu #ifdef CONFIG_PROC_CPU_RESCTRL
16e79f15a4SChen Yu 
17e79f15a4SChen Yu int proc_resctrl_show(struct seq_file *m,
18e79f15a4SChen Yu 		      struct pid_namespace *ns,
19e79f15a4SChen Yu 		      struct pid *pid,
20e79f15a4SChen Yu 		      struct task_struct *tsk);
21e79f15a4SChen Yu 
22e79f15a4SChen Yu #endif
23e79f15a4SChen Yu 
24781096d9SJames Morse /* max value for struct rdt_domain's mbps_val */
25781096d9SJames Morse #define MBA_MAX_MBPS   U32_MAX
26781096d9SJames Morse 
27792e0f6fSJames Morse /**
28208ab168SJames Morse  * enum resctrl_conf_type - The type of configuration.
29208ab168SJames Morse  * @CDP_NONE:	No prioritisation, both code and data are controlled or monitored.
30208ab168SJames Morse  * @CDP_CODE:	Configuration applies to instruction fetches.
31208ab168SJames Morse  * @CDP_DATA:	Configuration applies to reads and writes.
32208ab168SJames Morse  */
33208ab168SJames Morse enum resctrl_conf_type {
34208ab168SJames Morse 	CDP_NONE,
35208ab168SJames Morse 	CDP_CODE,
36208ab168SJames Morse 	CDP_DATA,
37208ab168SJames Morse };
38208ab168SJames Morse 
3975408e43SJames Morse #define CDP_NUM_TYPES	(CDP_DATA + 1)
4075408e43SJames Morse 
41fea62d37SJames Morse /*
42fea62d37SJames Morse  * Event IDs, the values match those used to program IA32_QM_EVTSEL before
43fea62d37SJames Morse  * reading IA32_QM_CTR on RDT systems.
44fea62d37SJames Morse  */
45fea62d37SJames Morse enum resctrl_event_id {
46fea62d37SJames Morse 	QOS_L3_OCCUP_EVENT_ID		= 0x01,
47fea62d37SJames Morse 	QOS_L3_MBM_TOTAL_EVENT_ID	= 0x02,
48fea62d37SJames Morse 	QOS_L3_MBM_LOCAL_EVENT_ID	= 0x03,
49fea62d37SJames Morse };
50fea62d37SJames Morse 
51208ab168SJames Morse /**
52e8f72825SJames Morse  * struct resctrl_staged_config - parsed configuration to be applied
53e8f72825SJames Morse  * @new_ctrl:		new ctrl value to be loaded
54e8f72825SJames Morse  * @have_new_ctrl:	whether the user provided new_ctrl is valid
55e8f72825SJames Morse  */
56e8f72825SJames Morse struct resctrl_staged_config {
57e8f72825SJames Morse 	u32			new_ctrl;
58e8f72825SJames Morse 	bool			have_new_ctrl;
59e8f72825SJames Morse };
60e8f72825SJames Morse 
61*cd84f72bSTony Luck enum resctrl_domain_type {
62*cd84f72bSTony Luck 	RESCTRL_CTRL_DOMAIN,
63*cd84f72bSTony Luck 	RESCTRL_MON_DOMAIN,
64*cd84f72bSTony Luck };
65*cd84f72bSTony Luck 
66e8f72825SJames Morse /**
67c103d4d4STony Luck  * struct rdt_domain_hdr - common header for different domain types
68792e0f6fSJames Morse  * @list:		all instances of this resource
69792e0f6fSJames Morse  * @id:			unique id for this instance
70*cd84f72bSTony Luck  * @type:		type of this instance
71792e0f6fSJames Morse  * @cpu_mask:		which CPUs share this resource
72c103d4d4STony Luck  */
73c103d4d4STony Luck struct rdt_domain_hdr {
74c103d4d4STony Luck 	struct list_head		list;
75c103d4d4STony Luck 	int				id;
76*cd84f72bSTony Luck 	enum resctrl_domain_type	type;
77c103d4d4STony Luck 	struct cpumask			cpu_mask;
78c103d4d4STony Luck };
79c103d4d4STony Luck 
80c103d4d4STony Luck /**
81c103d4d4STony Luck  * struct rdt_domain - group of CPUs sharing a resctrl resource
82c103d4d4STony Luck  * @hdr:		common header for different domain types
83792e0f6fSJames Morse  * @rmid_busy_llc:	bitmap of which limbo RMIDs are above threshold
84792e0f6fSJames Morse  * @mbm_total:		saved state for MBM total bandwidth
85792e0f6fSJames Morse  * @mbm_local:		saved state for MBM local bandwidth
86792e0f6fSJames Morse  * @mbm_over:		worker to periodically read MBM h/w counters
87792e0f6fSJames Morse  * @cqm_limbo:		worker to periodically read CQM h/w counters
88792e0f6fSJames Morse  * @mbm_work_cpu:	worker CPU for MBM h/w counters
89792e0f6fSJames Morse  * @cqm_work_cpu:	worker CPU for CQM h/w counters
90792e0f6fSJames Morse  * @plr:		pseudo-locked region (if any) associated with domain
91e8f72825SJames Morse  * @staged_config:	parsed configuration to be applied
92781096d9SJames Morse  * @mbps_val:		When mba_sc is enabled, this holds the array of user
93781096d9SJames Morse  *			specified control values for mba_sc in MBps, indexed
94781096d9SJames Morse  *			by closid
95792e0f6fSJames Morse  */
96792e0f6fSJames Morse struct rdt_domain {
97c103d4d4STony Luck 	struct rdt_domain_hdr		hdr;
98792e0f6fSJames Morse 	unsigned long			*rmid_busy_llc;
99792e0f6fSJames Morse 	struct mbm_state		*mbm_total;
100792e0f6fSJames Morse 	struct mbm_state		*mbm_local;
101792e0f6fSJames Morse 	struct delayed_work		mbm_over;
102792e0f6fSJames Morse 	struct delayed_work		cqm_limbo;
103792e0f6fSJames Morse 	int				mbm_work_cpu;
104792e0f6fSJames Morse 	int				cqm_work_cpu;
105792e0f6fSJames Morse 	struct pseudo_lock_region	*plr;
10675408e43SJames Morse 	struct resctrl_staged_config	staged_config[CDP_NUM_TYPES];
107781096d9SJames Morse 	u32				*mbps_val;
108792e0f6fSJames Morse };
10963c8b123SJames Morse 
11063c8b123SJames Morse /**
11163c8b123SJames Morse  * struct resctrl_cache - Cache allocation related data
11263c8b123SJames Morse  * @cbm_len:		Length of the cache bit mask
1132d4daa54SBabu Moger  * @min_cbm_bits:	Minimum number of consecutive bits to be set.
1142d4daa54SBabu Moger  *			The value 0 means the architecture can support
1152d4daa54SBabu Moger  *			zero CBM.
11663c8b123SJames Morse  * @shareable_bits:	Bitmask of shareable resource with other
11763c8b123SJames Morse  *			executing entities
11839c6eed1SMaciej Wieczor-Retman  * @arch_has_sparse_bitmasks:	True if a bitmask like f00f is valid.
11963c8b123SJames Morse  * @arch_has_per_cpu_cfg:	True if QOS_CFG register for this cache
12063c8b123SJames Morse  *				level has CPU scope.
12163c8b123SJames Morse  */
12263c8b123SJames Morse struct resctrl_cache {
12363c8b123SJames Morse 	unsigned int	cbm_len;
12463c8b123SJames Morse 	unsigned int	min_cbm_bits;
12563c8b123SJames Morse 	unsigned int	shareable_bits;
12639c6eed1SMaciej Wieczor-Retman 	bool		arch_has_sparse_bitmasks;
12763c8b123SJames Morse 	bool		arch_has_per_cpu_cfg;
12863c8b123SJames Morse };
12963c8b123SJames Morse 
13063c8b123SJames Morse /**
13163c8b123SJames Morse  * enum membw_throttle_mode - System's memory bandwidth throttling mode
13263c8b123SJames Morse  * @THREAD_THROTTLE_UNDEFINED:	Not relevant to the system
13363c8b123SJames Morse  * @THREAD_THROTTLE_MAX:	Memory bandwidth is throttled at the core
13463c8b123SJames Morse  *				always using smallest bandwidth percentage
13563c8b123SJames Morse  *				assigned to threads, aka "max throttling"
13663c8b123SJames Morse  * @THREAD_THROTTLE_PER_THREAD:	Memory bandwidth is throttled at the thread
13763c8b123SJames Morse  */
13863c8b123SJames Morse enum membw_throttle_mode {
13963c8b123SJames Morse 	THREAD_THROTTLE_UNDEFINED = 0,
14063c8b123SJames Morse 	THREAD_THROTTLE_MAX,
14163c8b123SJames Morse 	THREAD_THROTTLE_PER_THREAD,
14263c8b123SJames Morse };
14363c8b123SJames Morse 
14463c8b123SJames Morse /**
14563c8b123SJames Morse  * struct resctrl_membw - Memory bandwidth allocation related data
14663c8b123SJames Morse  * @min_bw:		Minimum memory bandwidth percentage user can request
14763c8b123SJames Morse  * @bw_gran:		Granularity at which the memory bandwidth is allocated
14863c8b123SJames Morse  * @delay_linear:	True if memory B/W delay is in linear scale
14963c8b123SJames Morse  * @arch_needs_linear:	True if we can't configure non-linear resources
15063c8b123SJames Morse  * @throttle_mode:	Bandwidth throttling mode when threads request
15163c8b123SJames Morse  *			different memory bandwidths
15263c8b123SJames Morse  * @mba_sc:		True if MBA software controller(mba_sc) is enabled
15363c8b123SJames Morse  * @mb_map:		Mapping of memory B/W percentage to memory B/W delay
15463c8b123SJames Morse  */
15563c8b123SJames Morse struct resctrl_membw {
15663c8b123SJames Morse 	u32				min_bw;
15763c8b123SJames Morse 	u32				bw_gran;
15863c8b123SJames Morse 	u32				delay_linear;
15963c8b123SJames Morse 	bool				arch_needs_linear;
16063c8b123SJames Morse 	enum membw_throttle_mode	throttle_mode;
16163c8b123SJames Morse 	bool				mba_sc;
16263c8b123SJames Morse 	u32				*mb_map;
16363c8b123SJames Morse };
16463c8b123SJames Morse 
16563c8b123SJames Morse struct rdt_parse_data;
1661c290682SJames Morse struct resctrl_schema;
16763c8b123SJames Morse 
168f436cb69STony Luck enum resctrl_scope {
169f436cb69STony Luck 	RESCTRL_L2_CACHE = 2,
170f436cb69STony Luck 	RESCTRL_L3_CACHE = 3,
171f436cb69STony Luck };
172f436cb69STony Luck 
17363c8b123SJames Morse /**
17463c8b123SJames Morse  * struct rdt_resource - attributes of a resctrl resource
17563c8b123SJames Morse  * @rid:		The index of the resource
17663c8b123SJames Morse  * @alloc_capable:	Is allocation available on this machine
17763c8b123SJames Morse  * @mon_capable:	Is monitor feature available on this machine
17863c8b123SJames Morse  * @num_rmid:		Number of RMIDs available
179*cd84f72bSTony Luck  * @ctrl_scope:		Scope of this resource for control functions
180*cd84f72bSTony Luck  * @mon_scope:		Scope of this resource for monitor functions
18163c8b123SJames Morse  * @cache:		Cache allocation related data
18263c8b123SJames Morse  * @membw:		If the component has bandwidth controls, their properties.
183*cd84f72bSTony Luck  * @ctrl_domains:	RCU list of all control domains for this resource
184*cd84f72bSTony Luck  * @mon_domains:	RCU list of all monitor domains for this resource
18563c8b123SJames Morse  * @name:		Name to use in "schemata" file.
18663c8b123SJames Morse  * @data_width:		Character width of data when displaying
18763c8b123SJames Morse  * @default_ctrl:	Specifies default cache cbm or memory B/W percent.
18863c8b123SJames Morse  * @format_str:		Per resource format string to show domain value
18963c8b123SJames Morse  * @parse_ctrlval:	Per resource function pointer to parse control values
19063c8b123SJames Morse  * @evt_list:		List of monitoring events
19163c8b123SJames Morse  * @fflags:		flags to choose base and info files
192c091e907SJames Morse  * @cdp_capable:	Is the CDP feature available on this resource
19363c8b123SJames Morse  */
19463c8b123SJames Morse struct rdt_resource {
19563c8b123SJames Morse 	int			rid;
19663c8b123SJames Morse 	bool			alloc_capable;
19763c8b123SJames Morse 	bool			mon_capable;
19863c8b123SJames Morse 	int			num_rmid;
199*cd84f72bSTony Luck 	enum resctrl_scope	ctrl_scope;
200*cd84f72bSTony Luck 	enum resctrl_scope	mon_scope;
20163c8b123SJames Morse 	struct resctrl_cache	cache;
20263c8b123SJames Morse 	struct resctrl_membw	membw;
203*cd84f72bSTony Luck 	struct list_head	ctrl_domains;
204*cd84f72bSTony Luck 	struct list_head	mon_domains;
20563c8b123SJames Morse 	char			*name;
20663c8b123SJames Morse 	int			data_width;
20763c8b123SJames Morse 	u32			default_ctrl;
20863c8b123SJames Morse 	const char		*format_str;
20963c8b123SJames Morse 	int			(*parse_ctrlval)(struct rdt_parse_data *data,
2101c290682SJames Morse 						 struct resctrl_schema *s,
21163c8b123SJames Morse 						 struct rdt_domain *d);
21263c8b123SJames Morse 	struct list_head	evt_list;
21363c8b123SJames Morse 	unsigned long		fflags;
214c091e907SJames Morse 	bool			cdp_capable;
21563c8b123SJames Morse };
21663c8b123SJames Morse 
217cdb9ebc9SJames Morse /**
218cdb9ebc9SJames Morse  * struct resctrl_schema - configuration abilities of a resource presented to
219cdb9ebc9SJames Morse  *			   user-space
220cdb9ebc9SJames Morse  * @list:	Member of resctrl_schema_all.
221e198fde3SJames Morse  * @name:	The name to use in the "schemata" file.
222208ab168SJames Morse  * @conf_type:	Whether this schema is specific to code/data.
223cdb9ebc9SJames Morse  * @res:	The resource structure exported by the architecture to describe
224cdb9ebc9SJames Morse  *		the hardware that is configured by this schema.
2253183e87cSJames Morse  * @num_closid:	The number of closid that can be used with this schema. When
2263183e87cSJames Morse  *		features like CDP are enabled, this will be lower than the
2273183e87cSJames Morse  *		hardware supports for the resource.
228cdb9ebc9SJames Morse  */
229cdb9ebc9SJames Morse struct resctrl_schema {
230cdb9ebc9SJames Morse 	struct list_head		list;
231e198fde3SJames Morse 	char				name[8];
232208ab168SJames Morse 	enum resctrl_conf_type		conf_type;
233cdb9ebc9SJames Morse 	struct rdt_resource		*res;
234eb6f3187SJames Morse 	u32				num_closid;
235cdb9ebc9SJames Morse };
236eb6f3187SJames Morse 
237eb6f3187SJames Morse /* The number of closid supported by this resource regardless of CDP */
238eb6f3187SJames Morse u32 resctrl_arch_get_num_closid(struct rdt_resource *r);
2392e667819SJames Morse int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid);
240ff6357bbSJames Morse 
241ff6357bbSJames Morse /*
242ff6357bbSJames Morse  * Update the ctrl_val and apply this config right now.
243ff6357bbSJames Morse  * Must be called on one of the domain's CPUs.
244ff6357bbSJames Morse  */
245ff6357bbSJames Morse int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_domain *d,
246ff6357bbSJames Morse 			    u32 closid, enum resctrl_conf_type t, u32 cfg_val);
247ff6357bbSJames Morse 
248111136e6SJames Morse u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_domain *d,
249111136e6SJames Morse 			    u32 closid, enum resctrl_conf_type type);
250*cd84f72bSTony Luck int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_domain *d);
251*cd84f72bSTony Luck int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_domain *d);
252*cd84f72bSTony Luck void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_domain *d);
253*cd84f72bSTony Luck void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain *d);
2541b3e50ceSJames Morse void resctrl_online_cpu(unsigned int cpu);
255258c91e8SJames Morse void resctrl_offline_cpu(unsigned int cpu);
2568286618aSJames Morse 
2578286618aSJames Morse /**
2588286618aSJames Morse  * resctrl_arch_rmid_read() - Read the eventid counter corresponding to rmid
2598286618aSJames Morse  *			      for this resource and domain.
2608286618aSJames Morse  * @r:			resource that the counter should be read from.
2618286618aSJames Morse  * @d:			domain that the counter should be read from.
26240fc735bSJames Morse  * @closid:		closid that matches the rmid. Depending on the architecture, the
26340fc735bSJames Morse  *			counter may match traffic of both @closid and @rmid, or @rmid
26440fc735bSJames Morse  *			only.
2658286618aSJames Morse  * @rmid:		rmid of the counter to read.
2668286618aSJames Morse  * @eventid:		eventid to read, e.g. L3 occupancy.
267f7b1843eSJames Morse  * @val:		result of the counter read in bytes.
268e557999fSJames Morse  * @arch_mon_ctx:	An architecture specific value from
269e557999fSJames Morse  *			resctrl_arch_mon_ctx_alloc(), for MPAM this identifies
270e557999fSJames Morse  *			the hardware monitor allocated for this read request.
2718286618aSJames Morse  *
2726fde1424SJames Morse  * Some architectures need to sleep when first programming some of the counters.
2736fde1424SJames Morse  * (specifically: arm64's MPAM cache occupancy counters can return 'not ready'
2746fde1424SJames Morse  *  for a short period of time). Call from a non-migrateable process context on
2756fde1424SJames Morse  * a CPU that belongs to domain @d. e.g. use smp_call_on_cpu() or
2766fde1424SJames Morse  * schedule_work_on(). This function can be called with interrupts masked,
2776fde1424SJames Morse  * e.g. using smp_call_function_any(), but may consistently return an error.
2788286618aSJames Morse  *
2798286618aSJames Morse  * Return:
2808286618aSJames Morse  * 0 on success, or -EIO, -EINVAL etc on error.
2818286618aSJames Morse  */
2828286618aSJames Morse int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_domain *d,
28340fc735bSJames Morse 			   u32 closid, u32 rmid, enum resctrl_event_id eventid,
284e557999fSJames Morse 			   u64 *val, void *arch_mon_ctx);
28540fc735bSJames Morse 
2866fde1424SJames Morse /**
2876fde1424SJames Morse  * resctrl_arch_rmid_read_context_check()  - warn about invalid contexts
2886fde1424SJames Morse  *
2896fde1424SJames Morse  * When built with CONFIG_DEBUG_ATOMIC_SLEEP generate a warning when
2906fde1424SJames Morse  * resctrl_arch_rmid_read() is called with preemption disabled.
2916fde1424SJames Morse  *
2926fde1424SJames Morse  * The contract with resctrl_arch_rmid_read() is that if interrupts
2936fde1424SJames Morse  * are unmasked, it can sleep. This allows NOHZ_FULL systems to use an
2946fde1424SJames Morse  * IPI, (and fail if the call needed to sleep), while most of the time
2956fde1424SJames Morse  * the work is scheduled, allowing the call to sleep.
2966fde1424SJames Morse  */
2976fde1424SJames Morse static inline void resctrl_arch_rmid_read_context_check(void)
2986fde1424SJames Morse {
2996fde1424SJames Morse 	if (!irqs_disabled())
3006fde1424SJames Morse 		might_sleep();
3016fde1424SJames Morse }
302eb6f3187SJames Morse 
303fea62d37SJames Morse /**
304fea62d37SJames Morse  * resctrl_arch_reset_rmid() - Reset any private state associated with rmid
305fea62d37SJames Morse  *			       and eventid.
306fea62d37SJames Morse  * @r:		The domain's resource.
307fea62d37SJames Morse  * @d:		The rmid's domain.
30840fc735bSJames Morse  * @closid:	closid that matches the rmid. Depending on the architecture, the
30940fc735bSJames Morse  *		counter may match traffic of both @closid and @rmid, or @rmid only.
310fea62d37SJames Morse  * @rmid:	The rmid whose counter values should be reset.
311fea62d37SJames Morse  * @eventid:	The eventid whose counter values should be reset.
312fea62d37SJames Morse  *
313fea62d37SJames Morse  * This can be called from any CPU.
314fea62d37SJames Morse  */
315fea62d37SJames Morse void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_domain *d,
31640fc735bSJames Morse 			     u32 closid, u32 rmid,
31740fc735bSJames Morse 			     enum resctrl_event_id eventid);
318fea62d37SJames Morse 
31992bd5a13SBabu Moger /**
32092bd5a13SBabu Moger  * resctrl_arch_reset_rmid_all() - Reset all private state associated with
32192bd5a13SBabu Moger  *				   all rmids and eventids.
32292bd5a13SBabu Moger  * @r:		The resctrl resource.
32392bd5a13SBabu Moger  * @d:		The domain for which all architectural counter state will
32492bd5a13SBabu Moger  *		be cleared.
32592bd5a13SBabu Moger  *
32692bd5a13SBabu Moger  * This can be called from any CPU.
32792bd5a13SBabu Moger  */
32892bd5a13SBabu Moger void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_domain *d);
32992bd5a13SBabu Moger 
330ae2328b5SJames Morse extern unsigned int resctrl_rmid_realloc_threshold;
331d80975e2SJames Morse extern unsigned int resctrl_rmid_realloc_limit;
332ae2328b5SJames Morse 
333e79f15a4SChen Yu #endif /* _RESCTRL_H */
334