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