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