xref: /linux/arch/x86/kernel/cpu/resctrl/internal.h (revision 67f49869106f78882a8a09b736d4884be85aba18)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_RESCTRL_INTERNAL_H
3 #define _ASM_X86_RESCTRL_INTERNAL_H
4 
5 #include <linux/resctrl.h>
6 #include <linux/sched.h>
7 #include <linux/kernfs.h>
8 #include <linux/fs_context.h>
9 #include <linux/jump_label.h>
10 
11 #define L3_QOS_CDP_ENABLE		0x01ULL
12 
13 #define L2_QOS_CDP_ENABLE		0x01ULL
14 
15 #define CQM_LIMBOCHECK_INTERVAL	1000
16 
17 #define MBM_CNTR_WIDTH_BASE		24
18 #define MBM_OVERFLOW_INTERVAL		1000
19 #define MAX_MBA_BW			100u
20 #define MBA_IS_LINEAR			0x4
21 #define MAX_MBA_BW_AMD			0x800
22 #define MBM_CNTR_WIDTH_OFFSET_AMD	20
23 
24 #define RMID_VAL_ERROR			BIT_ULL(63)
25 #define RMID_VAL_UNAVAIL		BIT_ULL(62)
26 /*
27  * With the above fields in use 62 bits remain in MSR_IA32_QM_CTR for
28  * data to be returned. The counter width is discovered from the hardware
29  * as an offset from MBM_CNTR_WIDTH_BASE.
30  */
31 #define MBM_CNTR_WIDTH_OFFSET_MAX (62 - MBM_CNTR_WIDTH_BASE)
32 
33 
34 struct rdt_fs_context {
35 	struct kernfs_fs_context	kfc;
36 	bool				enable_cdpl2;
37 	bool				enable_cdpl3;
38 	bool				enable_mba_mbps;
39 };
40 
41 static inline struct rdt_fs_context *rdt_fc2context(struct fs_context *fc)
42 {
43 	struct kernfs_fs_context *kfc = fc->fs_private;
44 
45 	return container_of(kfc, struct rdt_fs_context, kfc);
46 }
47 
48 DECLARE_STATIC_KEY_FALSE(rdt_enable_key);
49 DECLARE_STATIC_KEY_FALSE(rdt_mon_enable_key);
50 
51 /**
52  * struct mon_evt - Entry in the event list of a resource
53  * @evtid:		event id
54  * @name:		name of the event
55  * @list:		entry in &rdt_resource->evt_list
56  */
57 struct mon_evt {
58 	enum resctrl_event_id	evtid;
59 	char			*name;
60 	struct list_head	list;
61 };
62 
63 /**
64  * union mon_data_bits - Monitoring details for each event file
65  * @priv:              Used to store monitoring event data in @u
66  *                     as kernfs private data
67  * @rid:               Resource id associated with the event file
68  * @evtid:             Event id associated with the event file
69  * @domid:             The domain to which the event file belongs
70  * @u:                 Name of the bit fields struct
71  */
72 union mon_data_bits {
73 	void *priv;
74 	struct {
75 		unsigned int rid		: 10;
76 		enum resctrl_event_id evtid	: 8;
77 		unsigned int domid		: 14;
78 	} u;
79 };
80 
81 struct rmid_read {
82 	struct rdtgroup		*rgrp;
83 	struct rdt_resource	*r;
84 	struct rdt_domain	*d;
85 	enum resctrl_event_id	evtid;
86 	bool			first;
87 	int			err;
88 	u64			val;
89 };
90 
91 extern bool rdt_alloc_capable;
92 extern bool rdt_mon_capable;
93 extern unsigned int rdt_mon_features;
94 extern struct list_head resctrl_schema_all;
95 
96 enum rdt_group_type {
97 	RDTCTRL_GROUP = 0,
98 	RDTMON_GROUP,
99 	RDT_NUM_GROUP,
100 };
101 
102 /**
103  * enum rdtgrp_mode - Mode of a RDT resource group
104  * @RDT_MODE_SHAREABLE: This resource group allows sharing of its allocations
105  * @RDT_MODE_EXCLUSIVE: No sharing of this resource group's allocations allowed
106  * @RDT_MODE_PSEUDO_LOCKSETUP: Resource group will be used for Pseudo-Locking
107  * @RDT_MODE_PSEUDO_LOCKED: No sharing of this resource group's allocations
108  *                          allowed AND the allocations are Cache Pseudo-Locked
109  * @RDT_NUM_MODES: Total number of modes
110  *
111  * The mode of a resource group enables control over the allowed overlap
112  * between allocations associated with different resource groups (classes
113  * of service). User is able to modify the mode of a resource group by
114  * writing to the "mode" resctrl file associated with the resource group.
115  *
116  * The "shareable", "exclusive", and "pseudo-locksetup" modes are set by
117  * writing the appropriate text to the "mode" file. A resource group enters
118  * "pseudo-locked" mode after the schemata is written while the resource
119  * group is in "pseudo-locksetup" mode.
120  */
121 enum rdtgrp_mode {
122 	RDT_MODE_SHAREABLE = 0,
123 	RDT_MODE_EXCLUSIVE,
124 	RDT_MODE_PSEUDO_LOCKSETUP,
125 	RDT_MODE_PSEUDO_LOCKED,
126 
127 	/* Must be last */
128 	RDT_NUM_MODES,
129 };
130 
131 /**
132  * struct mongroup - store mon group's data in resctrl fs.
133  * @mon_data_kn:		kernfs node for the mon_data directory
134  * @parent:			parent rdtgrp
135  * @crdtgrp_list:		child rdtgroup node list
136  * @rmid:			rmid for this rdtgroup
137  */
138 struct mongroup {
139 	struct kernfs_node	*mon_data_kn;
140 	struct rdtgroup		*parent;
141 	struct list_head	crdtgrp_list;
142 	u32			rmid;
143 };
144 
145 /**
146  * struct pseudo_lock_region - pseudo-lock region information
147  * @s:			Resctrl schema for the resource to which this
148  *			pseudo-locked region belongs
149  * @d:			RDT domain to which this pseudo-locked region
150  *			belongs
151  * @cbm:		bitmask of the pseudo-locked region
152  * @lock_thread_wq:	waitqueue used to wait on the pseudo-locking thread
153  *			completion
154  * @thread_done:	variable used by waitqueue to test if pseudo-locking
155  *			thread completed
156  * @cpu:		core associated with the cache on which the setup code
157  *			will be run
158  * @line_size:		size of the cache lines
159  * @size:		size of pseudo-locked region in bytes
160  * @kmem:		the kernel memory associated with pseudo-locked region
161  * @minor:		minor number of character device associated with this
162  *			region
163  * @debugfs_dir:	pointer to this region's directory in the debugfs
164  *			filesystem
165  * @pm_reqs:		Power management QoS requests related to this region
166  */
167 struct pseudo_lock_region {
168 	struct resctrl_schema	*s;
169 	struct rdt_domain	*d;
170 	u32			cbm;
171 	wait_queue_head_t	lock_thread_wq;
172 	int			thread_done;
173 	int			cpu;
174 	unsigned int		line_size;
175 	unsigned int		size;
176 	void			*kmem;
177 	unsigned int		minor;
178 	struct dentry		*debugfs_dir;
179 	struct list_head	pm_reqs;
180 };
181 
182 /**
183  * struct rdtgroup - store rdtgroup's data in resctrl file system.
184  * @kn:				kernfs node
185  * @rdtgroup_list:		linked list for all rdtgroups
186  * @closid:			closid for this rdtgroup
187  * @cpu_mask:			CPUs assigned to this rdtgroup
188  * @flags:			status bits
189  * @waitcount:			how many cpus expect to find this
190  *				group when they acquire rdtgroup_mutex
191  * @type:			indicates type of this rdtgroup - either
192  *				monitor only or ctrl_mon group
193  * @mon:			mongroup related data
194  * @mode:			mode of resource group
195  * @plr:			pseudo-locked region
196  */
197 struct rdtgroup {
198 	struct kernfs_node		*kn;
199 	struct list_head		rdtgroup_list;
200 	u32				closid;
201 	struct cpumask			cpu_mask;
202 	int				flags;
203 	atomic_t			waitcount;
204 	enum rdt_group_type		type;
205 	struct mongroup			mon;
206 	enum rdtgrp_mode		mode;
207 	struct pseudo_lock_region	*plr;
208 };
209 
210 /* rdtgroup.flags */
211 #define	RDT_DELETED		1
212 
213 /* rftype.flags */
214 #define RFTYPE_FLAGS_CPUS_LIST	1
215 
216 /*
217  * Define the file type flags for base and info directories.
218  */
219 #define RFTYPE_INFO			BIT(0)
220 #define RFTYPE_BASE			BIT(1)
221 #define RF_CTRLSHIFT			4
222 #define RF_MONSHIFT			5
223 #define RF_TOPSHIFT			6
224 #define RFTYPE_CTRL			BIT(RF_CTRLSHIFT)
225 #define RFTYPE_MON			BIT(RF_MONSHIFT)
226 #define RFTYPE_TOP			BIT(RF_TOPSHIFT)
227 #define RFTYPE_RES_CACHE		BIT(8)
228 #define RFTYPE_RES_MB			BIT(9)
229 #define RF_CTRL_INFO			(RFTYPE_INFO | RFTYPE_CTRL)
230 #define RF_MON_INFO			(RFTYPE_INFO | RFTYPE_MON)
231 #define RF_TOP_INFO			(RFTYPE_INFO | RFTYPE_TOP)
232 #define RF_CTRL_BASE			(RFTYPE_BASE | RFTYPE_CTRL)
233 
234 /* List of all resource groups */
235 extern struct list_head rdt_all_groups;
236 
237 extern int max_name_width, max_data_width;
238 
239 int __init rdtgroup_init(void);
240 void __exit rdtgroup_exit(void);
241 
242 /**
243  * struct rftype - describe each file in the resctrl file system
244  * @name:	File name
245  * @mode:	Access mode
246  * @kf_ops:	File operations
247  * @flags:	File specific RFTYPE_FLAGS_* flags
248  * @fflags:	File specific RF_* or RFTYPE_* flags
249  * @seq_show:	Show content of the file
250  * @write:	Write to the file
251  */
252 struct rftype {
253 	char			*name;
254 	umode_t			mode;
255 	const struct kernfs_ops	*kf_ops;
256 	unsigned long		flags;
257 	unsigned long		fflags;
258 
259 	int (*seq_show)(struct kernfs_open_file *of,
260 			struct seq_file *sf, void *v);
261 	/*
262 	 * write() is the generic write callback which maps directly to
263 	 * kernfs write operation and overrides all other operations.
264 	 * Maximum write size is determined by ->max_write_len.
265 	 */
266 	ssize_t (*write)(struct kernfs_open_file *of,
267 			 char *buf, size_t nbytes, loff_t off);
268 };
269 
270 /**
271  * struct mbm_state - status for each MBM counter in each domain
272  * @prev_bw_bytes: Previous bytes value read for bandwidth calculation
273  * @prev_bw:	The most recent bandwidth in MBps
274  * @delta_bw:	Difference between the current and previous bandwidth
275  * @delta_comp:	Indicates whether to compute the delta_bw
276  */
277 struct mbm_state {
278 	u64	prev_bw_bytes;
279 	u32	prev_bw;
280 	u32	delta_bw;
281 	bool	delta_comp;
282 };
283 
284 /**
285  * struct arch_mbm_state - values used to compute resctrl_arch_rmid_read()s
286  *			   return value.
287  * @chunks:	Total data moved (multiply by rdt_group.mon_scale to get bytes)
288  * @prev_msr:	Value of IA32_QM_CTR last time it was read for the RMID used to
289  *		find this struct.
290  */
291 struct arch_mbm_state {
292 	u64	chunks;
293 	u64	prev_msr;
294 };
295 
296 /**
297  * struct rdt_hw_domain - Arch private attributes of a set of CPUs that share
298  *			  a resource
299  * @d_resctrl:	Properties exposed to the resctrl file system
300  * @ctrl_val:	array of cache or mem ctrl values (indexed by CLOSID)
301  * @arch_mbm_total:	arch private state for MBM total bandwidth
302  * @arch_mbm_local:	arch private state for MBM local bandwidth
303  *
304  * Members of this structure are accessed via helpers that provide abstraction.
305  */
306 struct rdt_hw_domain {
307 	struct rdt_domain		d_resctrl;
308 	u32				*ctrl_val;
309 	struct arch_mbm_state		*arch_mbm_total;
310 	struct arch_mbm_state		*arch_mbm_local;
311 };
312 
313 static inline struct rdt_hw_domain *resctrl_to_arch_dom(struct rdt_domain *r)
314 {
315 	return container_of(r, struct rdt_hw_domain, d_resctrl);
316 }
317 
318 /**
319  * struct msr_param - set a range of MSRs from a domain
320  * @res:       The resource to use
321  * @low:       Beginning index from base MSR
322  * @high:      End index
323  */
324 struct msr_param {
325 	struct rdt_resource	*res;
326 	u32			low;
327 	u32			high;
328 };
329 
330 static inline bool is_llc_occupancy_enabled(void)
331 {
332 	return (rdt_mon_features & (1 << QOS_L3_OCCUP_EVENT_ID));
333 }
334 
335 static inline bool is_mbm_total_enabled(void)
336 {
337 	return (rdt_mon_features & (1 << QOS_L3_MBM_TOTAL_EVENT_ID));
338 }
339 
340 static inline bool is_mbm_local_enabled(void)
341 {
342 	return (rdt_mon_features & (1 << QOS_L3_MBM_LOCAL_EVENT_ID));
343 }
344 
345 static inline bool is_mbm_enabled(void)
346 {
347 	return (is_mbm_total_enabled() || is_mbm_local_enabled());
348 }
349 
350 static inline bool is_mbm_event(int e)
351 {
352 	return (e >= QOS_L3_MBM_TOTAL_EVENT_ID &&
353 		e <= QOS_L3_MBM_LOCAL_EVENT_ID);
354 }
355 
356 struct rdt_parse_data {
357 	struct rdtgroup		*rdtgrp;
358 	char			*buf;
359 };
360 
361 /**
362  * struct rdt_hw_resource - arch private attributes of a resctrl resource
363  * @r_resctrl:		Attributes of the resource used directly by resctrl.
364  * @num_closid:		Maximum number of closid this hardware can support,
365  *			regardless of CDP. This is exposed via
366  *			resctrl_arch_get_num_closid() to avoid confusion
367  *			with struct resctrl_schema's property of the same name,
368  *			which has been corrected for features like CDP.
369  * @msr_base:		Base MSR address for CBMs
370  * @msr_update:		Function pointer to update QOS MSRs
371  * @mon_scale:		cqm counter * mon_scale = occupancy in bytes
372  * @mbm_width:		Monitor width, to detect and correct for overflow.
373  * @cdp_enabled:	CDP state of this resource
374  *
375  * Members of this structure are either private to the architecture
376  * e.g. mbm_width, or accessed via helpers that provide abstraction. e.g.
377  * msr_update and msr_base.
378  */
379 struct rdt_hw_resource {
380 	struct rdt_resource	r_resctrl;
381 	u32			num_closid;
382 	unsigned int		msr_base;
383 	void (*msr_update)	(struct rdt_domain *d, struct msr_param *m,
384 				 struct rdt_resource *r);
385 	unsigned int		mon_scale;
386 	unsigned int		mbm_width;
387 	bool			cdp_enabled;
388 };
389 
390 static inline struct rdt_hw_resource *resctrl_to_arch_res(struct rdt_resource *r)
391 {
392 	return container_of(r, struct rdt_hw_resource, r_resctrl);
393 }
394 
395 int parse_cbm(struct rdt_parse_data *data, struct resctrl_schema *s,
396 	      struct rdt_domain *d);
397 int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
398 	     struct rdt_domain *d);
399 
400 extern struct mutex rdtgroup_mutex;
401 
402 extern struct rdt_hw_resource rdt_resources_all[];
403 extern struct rdtgroup rdtgroup_default;
404 DECLARE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
405 
406 extern struct dentry *debugfs_resctrl;
407 
408 enum resctrl_res_level {
409 	RDT_RESOURCE_L3,
410 	RDT_RESOURCE_L2,
411 	RDT_RESOURCE_MBA,
412 
413 	/* Must be the last */
414 	RDT_NUM_RESOURCES,
415 };
416 
417 static inline struct rdt_resource *resctrl_inc(struct rdt_resource *res)
418 {
419 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(res);
420 
421 	hw_res++;
422 	return &hw_res->r_resctrl;
423 }
424 
425 static inline bool resctrl_arch_get_cdp_enabled(enum resctrl_res_level l)
426 {
427 	return rdt_resources_all[l].cdp_enabled;
428 }
429 
430 int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable);
431 
432 /*
433  * To return the common struct rdt_resource, which is contained in struct
434  * rdt_hw_resource, walk the resctrl member of struct rdt_hw_resource.
435  */
436 #define for_each_rdt_resource(r)					      \
437 	for (r = &rdt_resources_all[0].r_resctrl;			      \
438 	     r <= &rdt_resources_all[RDT_NUM_RESOURCES - 1].r_resctrl;	      \
439 	     r = resctrl_inc(r))
440 
441 #define for_each_capable_rdt_resource(r)				      \
442 	for_each_rdt_resource(r)					      \
443 		if (r->alloc_capable || r->mon_capable)
444 
445 #define for_each_alloc_capable_rdt_resource(r)				      \
446 	for_each_rdt_resource(r)					      \
447 		if (r->alloc_capable)
448 
449 #define for_each_mon_capable_rdt_resource(r)				      \
450 	for_each_rdt_resource(r)					      \
451 		if (r->mon_capable)
452 
453 /* CPUID.(EAX=10H, ECX=ResID=1).EAX */
454 union cpuid_0x10_1_eax {
455 	struct {
456 		unsigned int cbm_len:5;
457 	} split;
458 	unsigned int full;
459 };
460 
461 /* CPUID.(EAX=10H, ECX=ResID=3).EAX */
462 union cpuid_0x10_3_eax {
463 	struct {
464 		unsigned int max_delay:12;
465 	} split;
466 	unsigned int full;
467 };
468 
469 /* CPUID.(EAX=10H, ECX=ResID).EDX */
470 union cpuid_0x10_x_edx {
471 	struct {
472 		unsigned int cos_max:16;
473 	} split;
474 	unsigned int full;
475 };
476 
477 void rdt_last_cmd_clear(void);
478 void rdt_last_cmd_puts(const char *s);
479 __printf(1, 2)
480 void rdt_last_cmd_printf(const char *fmt, ...);
481 
482 void rdt_ctrl_update(void *arg);
483 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn);
484 void rdtgroup_kn_unlock(struct kernfs_node *kn);
485 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name);
486 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
487 			     umode_t mask);
488 struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
489 				   struct list_head **pos);
490 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
491 				char *buf, size_t nbytes, loff_t off);
492 int rdtgroup_schemata_show(struct kernfs_open_file *of,
493 			   struct seq_file *s, void *v);
494 bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
495 			   unsigned long cbm, int closid, bool exclusive);
496 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, struct rdt_domain *d,
497 				  unsigned long cbm);
498 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid);
499 int rdtgroup_tasks_assigned(struct rdtgroup *r);
500 int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp);
501 int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp);
502 bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_domain *d, unsigned long cbm);
503 bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_domain *d);
504 int rdt_pseudo_lock_init(void);
505 void rdt_pseudo_lock_release(void);
506 int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp);
507 void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp);
508 struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r);
509 int closids_supported(void);
510 void closid_free(int closid);
511 int alloc_rmid(void);
512 void free_rmid(u32 rmid);
513 int rdt_get_mon_l3_config(struct rdt_resource *r);
514 void mon_event_count(void *info);
515 int rdtgroup_mondata_show(struct seq_file *m, void *arg);
516 void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
517 		    struct rdt_domain *d, struct rdtgroup *rdtgrp,
518 		    int evtid, int first);
519 void mbm_setup_overflow_handler(struct rdt_domain *dom,
520 				unsigned long delay_ms);
521 void mbm_handle_overflow(struct work_struct *work);
522 void __init intel_rdt_mbm_apply_quirk(void);
523 bool is_mba_sc(struct rdt_resource *r);
524 void cqm_setup_limbo_handler(struct rdt_domain *dom, unsigned long delay_ms);
525 void cqm_handle_limbo(struct work_struct *work);
526 bool has_busy_rmid(struct rdt_resource *r, struct rdt_domain *d);
527 void __check_limbo(struct rdt_domain *d, bool force_free);
528 void rdt_domain_reconfigure_cdp(struct rdt_resource *r);
529 void __init thread_throttle_mode_init(void);
530 
531 #endif /* _ASM_X86_RESCTRL_INTERNAL_H */
532