xref: /linux-6.15/include/linux/resctrl.h (revision cae2bcb6)
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 
61cd84f72bSTony Luck enum resctrl_domain_type {
62cd84f72bSTony Luck 	RESCTRL_CTRL_DOMAIN,
63cd84f72bSTony Luck 	RESCTRL_MON_DOMAIN,
64cd84f72bSTony Luck };
65cd84f72bSTony 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
70cd84f72bSTony 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;
76cd84f72bSTony Luck 	enum resctrl_domain_type	type;
77c103d4d4STony Luck 	struct cpumask			cpu_mask;
78c103d4d4STony Luck };
79c103d4d4STony Luck 
80c103d4d4STony Luck /**
81*cae2bcb6STony Luck  * struct rdt_ctrl_domain - group of CPUs sharing a resctrl control resource
82*cae2bcb6STony Luck  * @hdr:		common header for different domain types
83*cae2bcb6STony Luck  * @plr:		pseudo-locked region (if any) associated with domain
84*cae2bcb6STony Luck  * @staged_config:	parsed configuration to be applied
85*cae2bcb6STony Luck  * @mbps_val:		When mba_sc is enabled, this holds the array of user
86*cae2bcb6STony Luck  *			specified control values for mba_sc in MBps, indexed
87*cae2bcb6STony Luck  *			by closid
88*cae2bcb6STony Luck  */
89*cae2bcb6STony Luck struct rdt_ctrl_domain {
90*cae2bcb6STony Luck 	struct rdt_domain_hdr		hdr;
91*cae2bcb6STony Luck 	struct pseudo_lock_region	*plr;
92*cae2bcb6STony Luck 	struct resctrl_staged_config	staged_config[CDP_NUM_TYPES];
93*cae2bcb6STony Luck 	u32				*mbps_val;
94*cae2bcb6STony Luck };
95*cae2bcb6STony Luck 
96*cae2bcb6STony Luck /**
97*cae2bcb6STony Luck  * struct rdt_mon_domain - group of CPUs sharing a resctrl monitor resource
98c103d4d4STony Luck  * @hdr:		common header for different domain types
99792e0f6fSJames Morse  * @rmid_busy_llc:	bitmap of which limbo RMIDs are above threshold
100792e0f6fSJames Morse  * @mbm_total:		saved state for MBM total bandwidth
101792e0f6fSJames Morse  * @mbm_local:		saved state for MBM local bandwidth
102792e0f6fSJames Morse  * @mbm_over:		worker to periodically read MBM h/w counters
103792e0f6fSJames Morse  * @cqm_limbo:		worker to periodically read CQM h/w counters
104792e0f6fSJames Morse  * @mbm_work_cpu:	worker CPU for MBM h/w counters
105792e0f6fSJames Morse  * @cqm_work_cpu:	worker CPU for CQM h/w counters
106792e0f6fSJames Morse  */
107*cae2bcb6STony Luck struct rdt_mon_domain {
108c103d4d4STony Luck 	struct rdt_domain_hdr		hdr;
109792e0f6fSJames Morse 	unsigned long			*rmid_busy_llc;
110792e0f6fSJames Morse 	struct mbm_state		*mbm_total;
111792e0f6fSJames Morse 	struct mbm_state		*mbm_local;
112792e0f6fSJames Morse 	struct delayed_work		mbm_over;
113792e0f6fSJames Morse 	struct delayed_work		cqm_limbo;
114792e0f6fSJames Morse 	int				mbm_work_cpu;
115792e0f6fSJames Morse 	int				cqm_work_cpu;
116792e0f6fSJames Morse };
11763c8b123SJames Morse 
11863c8b123SJames Morse /**
11963c8b123SJames Morse  * struct resctrl_cache - Cache allocation related data
12063c8b123SJames Morse  * @cbm_len:		Length of the cache bit mask
1212d4daa54SBabu Moger  * @min_cbm_bits:	Minimum number of consecutive bits to be set.
1222d4daa54SBabu Moger  *			The value 0 means the architecture can support
1232d4daa54SBabu Moger  *			zero CBM.
12463c8b123SJames Morse  * @shareable_bits:	Bitmask of shareable resource with other
12563c8b123SJames Morse  *			executing entities
12639c6eed1SMaciej Wieczor-Retman  * @arch_has_sparse_bitmasks:	True if a bitmask like f00f is valid.
12763c8b123SJames Morse  * @arch_has_per_cpu_cfg:	True if QOS_CFG register for this cache
12863c8b123SJames Morse  *				level has CPU scope.
12963c8b123SJames Morse  */
13063c8b123SJames Morse struct resctrl_cache {
13163c8b123SJames Morse 	unsigned int	cbm_len;
13263c8b123SJames Morse 	unsigned int	min_cbm_bits;
13363c8b123SJames Morse 	unsigned int	shareable_bits;
13439c6eed1SMaciej Wieczor-Retman 	bool		arch_has_sparse_bitmasks;
13563c8b123SJames Morse 	bool		arch_has_per_cpu_cfg;
13663c8b123SJames Morse };
13763c8b123SJames Morse 
13863c8b123SJames Morse /**
13963c8b123SJames Morse  * enum membw_throttle_mode - System's memory bandwidth throttling mode
14063c8b123SJames Morse  * @THREAD_THROTTLE_UNDEFINED:	Not relevant to the system
14163c8b123SJames Morse  * @THREAD_THROTTLE_MAX:	Memory bandwidth is throttled at the core
14263c8b123SJames Morse  *				always using smallest bandwidth percentage
14363c8b123SJames Morse  *				assigned to threads, aka "max throttling"
14463c8b123SJames Morse  * @THREAD_THROTTLE_PER_THREAD:	Memory bandwidth is throttled at the thread
14563c8b123SJames Morse  */
14663c8b123SJames Morse enum membw_throttle_mode {
14763c8b123SJames Morse 	THREAD_THROTTLE_UNDEFINED = 0,
14863c8b123SJames Morse 	THREAD_THROTTLE_MAX,
14963c8b123SJames Morse 	THREAD_THROTTLE_PER_THREAD,
15063c8b123SJames Morse };
15163c8b123SJames Morse 
15263c8b123SJames Morse /**
15363c8b123SJames Morse  * struct resctrl_membw - Memory bandwidth allocation related data
15463c8b123SJames Morse  * @min_bw:		Minimum memory bandwidth percentage user can request
15563c8b123SJames Morse  * @bw_gran:		Granularity at which the memory bandwidth is allocated
15663c8b123SJames Morse  * @delay_linear:	True if memory B/W delay is in linear scale
15763c8b123SJames Morse  * @arch_needs_linear:	True if we can't configure non-linear resources
15863c8b123SJames Morse  * @throttle_mode:	Bandwidth throttling mode when threads request
15963c8b123SJames Morse  *			different memory bandwidths
16063c8b123SJames Morse  * @mba_sc:		True if MBA software controller(mba_sc) is enabled
16163c8b123SJames Morse  * @mb_map:		Mapping of memory B/W percentage to memory B/W delay
16263c8b123SJames Morse  */
16363c8b123SJames Morse struct resctrl_membw {
16463c8b123SJames Morse 	u32				min_bw;
16563c8b123SJames Morse 	u32				bw_gran;
16663c8b123SJames Morse 	u32				delay_linear;
16763c8b123SJames Morse 	bool				arch_needs_linear;
16863c8b123SJames Morse 	enum membw_throttle_mode	throttle_mode;
16963c8b123SJames Morse 	bool				mba_sc;
17063c8b123SJames Morse 	u32				*mb_map;
17163c8b123SJames Morse };
17263c8b123SJames Morse 
17363c8b123SJames Morse struct rdt_parse_data;
1741c290682SJames Morse struct resctrl_schema;
17563c8b123SJames Morse 
176f436cb69STony Luck enum resctrl_scope {
177f436cb69STony Luck 	RESCTRL_L2_CACHE = 2,
178f436cb69STony Luck 	RESCTRL_L3_CACHE = 3,
179f436cb69STony Luck };
180f436cb69STony Luck 
18163c8b123SJames Morse /**
18263c8b123SJames Morse  * struct rdt_resource - attributes of a resctrl resource
18363c8b123SJames Morse  * @rid:		The index of the resource
18463c8b123SJames Morse  * @alloc_capable:	Is allocation available on this machine
18563c8b123SJames Morse  * @mon_capable:	Is monitor feature available on this machine
18663c8b123SJames Morse  * @num_rmid:		Number of RMIDs available
187cd84f72bSTony Luck  * @ctrl_scope:		Scope of this resource for control functions
188cd84f72bSTony Luck  * @mon_scope:		Scope of this resource for monitor functions
18963c8b123SJames Morse  * @cache:		Cache allocation related data
19063c8b123SJames Morse  * @membw:		If the component has bandwidth controls, their properties.
191cd84f72bSTony Luck  * @ctrl_domains:	RCU list of all control domains for this resource
192cd84f72bSTony Luck  * @mon_domains:	RCU list of all monitor domains for this resource
19363c8b123SJames Morse  * @name:		Name to use in "schemata" file.
19463c8b123SJames Morse  * @data_width:		Character width of data when displaying
19563c8b123SJames Morse  * @default_ctrl:	Specifies default cache cbm or memory B/W percent.
19663c8b123SJames Morse  * @format_str:		Per resource format string to show domain value
19763c8b123SJames Morse  * @parse_ctrlval:	Per resource function pointer to parse control values
19863c8b123SJames Morse  * @evt_list:		List of monitoring events
19963c8b123SJames Morse  * @fflags:		flags to choose base and info files
200c091e907SJames Morse  * @cdp_capable:	Is the CDP feature available on this resource
20163c8b123SJames Morse  */
20263c8b123SJames Morse struct rdt_resource {
20363c8b123SJames Morse 	int			rid;
20463c8b123SJames Morse 	bool			alloc_capable;
20563c8b123SJames Morse 	bool			mon_capable;
20663c8b123SJames Morse 	int			num_rmid;
207cd84f72bSTony Luck 	enum resctrl_scope	ctrl_scope;
208cd84f72bSTony Luck 	enum resctrl_scope	mon_scope;
20963c8b123SJames Morse 	struct resctrl_cache	cache;
21063c8b123SJames Morse 	struct resctrl_membw	membw;
211cd84f72bSTony Luck 	struct list_head	ctrl_domains;
212cd84f72bSTony Luck 	struct list_head	mon_domains;
21363c8b123SJames Morse 	char			*name;
21463c8b123SJames Morse 	int			data_width;
21563c8b123SJames Morse 	u32			default_ctrl;
21663c8b123SJames Morse 	const char		*format_str;
21763c8b123SJames Morse 	int			(*parse_ctrlval)(struct rdt_parse_data *data,
2181c290682SJames Morse 						 struct resctrl_schema *s,
219*cae2bcb6STony Luck 						 struct rdt_ctrl_domain *d);
22063c8b123SJames Morse 	struct list_head	evt_list;
22163c8b123SJames Morse 	unsigned long		fflags;
222c091e907SJames Morse 	bool			cdp_capable;
22363c8b123SJames Morse };
22463c8b123SJames Morse 
225cdb9ebc9SJames Morse /**
226cdb9ebc9SJames Morse  * struct resctrl_schema - configuration abilities of a resource presented to
227cdb9ebc9SJames Morse  *			   user-space
228cdb9ebc9SJames Morse  * @list:	Member of resctrl_schema_all.
229e198fde3SJames Morse  * @name:	The name to use in the "schemata" file.
230208ab168SJames Morse  * @conf_type:	Whether this schema is specific to code/data.
231cdb9ebc9SJames Morse  * @res:	The resource structure exported by the architecture to describe
232cdb9ebc9SJames Morse  *		the hardware that is configured by this schema.
2333183e87cSJames Morse  * @num_closid:	The number of closid that can be used with this schema. When
2343183e87cSJames Morse  *		features like CDP are enabled, this will be lower than the
2353183e87cSJames Morse  *		hardware supports for the resource.
236cdb9ebc9SJames Morse  */
237cdb9ebc9SJames Morse struct resctrl_schema {
238cdb9ebc9SJames Morse 	struct list_head		list;
239e198fde3SJames Morse 	char				name[8];
240208ab168SJames Morse 	enum resctrl_conf_type		conf_type;
241cdb9ebc9SJames Morse 	struct rdt_resource		*res;
242eb6f3187SJames Morse 	u32				num_closid;
243cdb9ebc9SJames Morse };
244eb6f3187SJames Morse 
245eb6f3187SJames Morse /* The number of closid supported by this resource regardless of CDP */
246eb6f3187SJames Morse u32 resctrl_arch_get_num_closid(struct rdt_resource *r);
2472e667819SJames Morse int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid);
248ff6357bbSJames Morse 
249ff6357bbSJames Morse /*
250ff6357bbSJames Morse  * Update the ctrl_val and apply this config right now.
251ff6357bbSJames Morse  * Must be called on one of the domain's CPUs.
252ff6357bbSJames Morse  */
253*cae2bcb6STony Luck int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d,
254ff6357bbSJames Morse 			    u32 closid, enum resctrl_conf_type t, u32 cfg_val);
255ff6357bbSJames Morse 
256*cae2bcb6STony Luck u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d,
257111136e6SJames Morse 			    u32 closid, enum resctrl_conf_type type);
258*cae2bcb6STony Luck int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d);
259*cae2bcb6STony Luck int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d);
260*cae2bcb6STony Luck void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d);
261*cae2bcb6STony Luck void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d);
2621b3e50ceSJames Morse void resctrl_online_cpu(unsigned int cpu);
263258c91e8SJames Morse void resctrl_offline_cpu(unsigned int cpu);
2648286618aSJames Morse 
2658286618aSJames Morse /**
2668286618aSJames Morse  * resctrl_arch_rmid_read() - Read the eventid counter corresponding to rmid
2678286618aSJames Morse  *			      for this resource and domain.
2688286618aSJames Morse  * @r:			resource that the counter should be read from.
2698286618aSJames Morse  * @d:			domain that the counter should be read from.
27040fc735bSJames Morse  * @closid:		closid that matches the rmid. Depending on the architecture, the
27140fc735bSJames Morse  *			counter may match traffic of both @closid and @rmid, or @rmid
27240fc735bSJames Morse  *			only.
2738286618aSJames Morse  * @rmid:		rmid of the counter to read.
2748286618aSJames Morse  * @eventid:		eventid to read, e.g. L3 occupancy.
275f7b1843eSJames Morse  * @val:		result of the counter read in bytes.
276e557999fSJames Morse  * @arch_mon_ctx:	An architecture specific value from
277e557999fSJames Morse  *			resctrl_arch_mon_ctx_alloc(), for MPAM this identifies
278e557999fSJames Morse  *			the hardware monitor allocated for this read request.
2798286618aSJames Morse  *
2806fde1424SJames Morse  * Some architectures need to sleep when first programming some of the counters.
2816fde1424SJames Morse  * (specifically: arm64's MPAM cache occupancy counters can return 'not ready'
2826fde1424SJames Morse  *  for a short period of time). Call from a non-migrateable process context on
2836fde1424SJames Morse  * a CPU that belongs to domain @d. e.g. use smp_call_on_cpu() or
2846fde1424SJames Morse  * schedule_work_on(). This function can be called with interrupts masked,
2856fde1424SJames Morse  * e.g. using smp_call_function_any(), but may consistently return an error.
2868286618aSJames Morse  *
2878286618aSJames Morse  * Return:
2888286618aSJames Morse  * 0 on success, or -EIO, -EINVAL etc on error.
2898286618aSJames Morse  */
290*cae2bcb6STony Luck int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,
29140fc735bSJames Morse 			   u32 closid, u32 rmid, enum resctrl_event_id eventid,
292e557999fSJames Morse 			   u64 *val, void *arch_mon_ctx);
29340fc735bSJames Morse 
2946fde1424SJames Morse /**
2956fde1424SJames Morse  * resctrl_arch_rmid_read_context_check()  - warn about invalid contexts
2966fde1424SJames Morse  *
2976fde1424SJames Morse  * When built with CONFIG_DEBUG_ATOMIC_SLEEP generate a warning when
2986fde1424SJames Morse  * resctrl_arch_rmid_read() is called with preemption disabled.
2996fde1424SJames Morse  *
3006fde1424SJames Morse  * The contract with resctrl_arch_rmid_read() is that if interrupts
3016fde1424SJames Morse  * are unmasked, it can sleep. This allows NOHZ_FULL systems to use an
3026fde1424SJames Morse  * IPI, (and fail if the call needed to sleep), while most of the time
3036fde1424SJames Morse  * the work is scheduled, allowing the call to sleep.
3046fde1424SJames Morse  */
3056fde1424SJames Morse static inline void resctrl_arch_rmid_read_context_check(void)
3066fde1424SJames Morse {
3076fde1424SJames Morse 	if (!irqs_disabled())
3086fde1424SJames Morse 		might_sleep();
3096fde1424SJames Morse }
310eb6f3187SJames Morse 
311fea62d37SJames Morse /**
312fea62d37SJames Morse  * resctrl_arch_reset_rmid() - Reset any private state associated with rmid
313fea62d37SJames Morse  *			       and eventid.
314fea62d37SJames Morse  * @r:		The domain's resource.
315fea62d37SJames Morse  * @d:		The rmid's domain.
31640fc735bSJames Morse  * @closid:	closid that matches the rmid. Depending on the architecture, the
31740fc735bSJames Morse  *		counter may match traffic of both @closid and @rmid, or @rmid only.
318fea62d37SJames Morse  * @rmid:	The rmid whose counter values should be reset.
319fea62d37SJames Morse  * @eventid:	The eventid whose counter values should be reset.
320fea62d37SJames Morse  *
321fea62d37SJames Morse  * This can be called from any CPU.
322fea62d37SJames Morse  */
323*cae2bcb6STony Luck void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_mon_domain *d,
32440fc735bSJames Morse 			     u32 closid, u32 rmid,
32540fc735bSJames Morse 			     enum resctrl_event_id eventid);
326fea62d37SJames Morse 
32792bd5a13SBabu Moger /**
32892bd5a13SBabu Moger  * resctrl_arch_reset_rmid_all() - Reset all private state associated with
32992bd5a13SBabu Moger  *				   all rmids and eventids.
33092bd5a13SBabu Moger  * @r:		The resctrl resource.
33192bd5a13SBabu Moger  * @d:		The domain for which all architectural counter state will
33292bd5a13SBabu Moger  *		be cleared.
33392bd5a13SBabu Moger  *
33492bd5a13SBabu Moger  * This can be called from any CPU.
33592bd5a13SBabu Moger  */
336*cae2bcb6STony Luck void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *d);
33792bd5a13SBabu Moger 
338ae2328b5SJames Morse extern unsigned int resctrl_rmid_realloc_threshold;
339d80975e2SJames Morse extern unsigned int resctrl_rmid_realloc_limit;
340ae2328b5SJames Morse 
341e79f15a4SChen Yu #endif /* _RESCTRL_H */
342